import osimport torch from huggingface_hub import loginfrom transformers import AutoTokenizer, AutoModelForCausalLMfrom 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())