from transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModelimport json base_model = "Qwen/Qwen2.5-1.5B-Instruct"adapter_id = "adityapande403/universal-json-extractor" tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(base_model, device_map="auto", torch_dtype="auto")model = PeftModel.from_pretrained(model, adapter_id) schema = { "type": "object", "properties": { "device_id": {"type": "string"}, "temperature_c": {"type": ["number", "null"]}, "error_code": {"type": ["string", "null"]} }, "required": ["device_id", "temperature_c"], "additionalProperties": False} source_text = "dev=EC200U-07; temp 72.4C; modem ERROR 304 at 2026/10/21 14:30:11" prompt = f"""<|im_start|>systemYou are a precise, deterministic information extraction engine. Return ONLY the raw JSON object conforming to the schema.<|im_end|><|im_start|>user<TARGET_JSON_SCHEMA>{json.dumps(schema, indent=2)}</TARGET_JSON_SCHEMA><UNTRUSTED_SOURCE_TEXT>{source_text}</UNTRUSTED_SOURCE_TEXT>Output strictly the conforming JSON object now:<|im_end|><|im_start|>assistant""" inputs = tokenizer(prompt, return_tensors="pt").to(model.device)outputs = model.generate(**inputs, max_new_tokens=512, do_sample=False)output_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)print(output_text)