Downloads · 30 days
4
9% of all-time downloads
Yellowforesty/qwen2.5-json-extractor-p0
qwen2.5-json-extractor-p0 is a text generation model from Yellowforesty. Use it when you need the model to write or continue text. It is set up for peft. The card lists the license as mit.
A LoRA adapter for Qwen2.5-1.5B that performs schema-guided structured information extraction.
Downloads · 30 days
4
9% of all-time downloads
All-time downloads
44
Public
Repo size
48.4 MB
Likes
1
Public
Click a slice to open those files.
.safetensors37 MB · 76%
From the Hugging Face model README
A LoRA adapter for Qwen2.5-1.5B that performs schema-guided structured information extraction.
Given a JSON schema and an unstructured text, the model extracts the required information and returns a valid JSON object.
The model extracts only explicitly mentioned information and never infers missing values.
null)The model expects a chat conversation with the following format.
You are an information extraction system.
Return ONLY a valid JSON object.
Rules:
- Extract only explicitly mentioned information.
- Never infer or assume values.
- Preserve the schema exactly.
- Use null for missing values.
- Return only JSON.
Schema:
{
"company": "string",
"ceo": "string",
"founded": "int",
"employees": "int"
}
Text:
OpenAI was founded in 2015. Sam Altman is the CEO. The company employs approximately 4500 people.
{
"company": "OpenAI",
"ceo": "Sam Altman",
"founded": 2015,
"employees": 4500
}
Install the required packages.
pip install transformers peft accelerate torch
This adapter is trained on Qwen/Qwen2.5-1.5B.
The base model will be downloaded automatically the first time you run the code.
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch
BASE_MODEL = "Qwen/Qwen2.5-1.5B"
LORA_MODEL = "Yellowforesty/qwen2.5-json-extractor-p0"
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
base = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map="auto"
)
model = PeftModel.from_pretrained(
base,
LORA_MODEL
)
SYSTEM_PROMPT = """
You are an information extraction system.
Return ONLY a valid JSON object.
Rules:
- Extract only explicitly mentioned information.
- Never infer or assume values.
- Preserve the schema exactly.
- Use null for missing values.
- Return only JSON.
""".strip()
prompt = """
Schema:
{
"company": "string",
"ceo": "string",
"founded": "int",
"employees": "int"
}
Text:
OpenAI was founded in 2015. Sam Altman is the CEO. The company employs approximately 4500 people.
"""
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
output = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.0,
do_sample=False,
)
response = tokenizer.decode(
output[0][inputs["input_ids"].shape[1]:],
skip_special_tokens=True,
)
print(response)
{
"company": "OpenAI",
"ceo": "Sam Altman",
"founded": 2015,
"employees": 4500
}
import os
import torch
from huggingface_hub import login
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
# ==========================================
# CONFIG
# ==========================================
BASE_MODEL = "Qwen/Qwen2.5-1.5B"
LORA_MODEL = "Yellowforesty/qwen2.5-json-extractor-p0"
HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN:
login(token=HF_TOKEN)
SYSTEM_PROMPT = """
You are an information extraction system.
Return ONLY a valid JSON object.
Rules:
- Extract only explicitly mentioned information.
- Never infer or assume values.
- Preserve the schema exactly.
- Use null for missing values.
- Return only JSON.
""".strip()
PROMPT = """
Schema:
{
"company": "string",
"ceo": "string",
"founded": "int",
"employees": "int"
}
Text:
OpenAI was founded in 2015. Sam Altman is the CEO. The company employs approximately 4500 people.
""".strip()
# ==========================================
# LOAD TOKENIZER
# ==========================================
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
# ==========================================
# LOAD BASE MODEL
# ==========================================
base = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map="auto",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
)
# ==========================================
# LOAD LORA
# ==========================================
model = PeftModel.from_pretrained(
base,
LORA_MODEL,
)
model.eval()
print("Device:", next(model.parameters()).device)
# ==========================================
# INFERENCE
# ==========================================
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": PROMPT},
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
output = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.0,
do_sample=False,
)
response = tokenizer.decode(
output[0][inputs["input_ids"].shape[1]:],
skip_special_tokens=True,
)
print(response.strip())
Schema:
{
"name": "string",
"age": "int",
"city": "string"
}
Text:
Aman, aged 31, recently moved to Chandigarh.
{
"name": "Aman",
"age": 31,
"city": "Chandigarh"
}
Current Version (P0)
Evaluation
Current version supports
Not yet supported
The training dataset used to train this adapter is not included in this repository.
MIT