Update README.md
Browse files
README.md
CHANGED
|
@@ -72,3 +72,67 @@ base_model = AutoModelForCausalLM.from_pretrained(
|
|
| 72 |
model = PeftModel.from_pretrained(base_model, lora_model_id, device_map="auto")
|
| 73 |
model = model.merge_and_unload()
|
| 74 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
model = PeftModel.from_pretrained(base_model, lora_model_id, device_map="auto")
|
| 73 |
model = model.merge_and_unload()
|
| 74 |
model.eval()
|
| 75 |
+
|
| 76 |
+
def extract_and_clean_json(text):
|
| 77 |
+
"""Extract JSON from LLM output, even if extra text is present."""
|
| 78 |
+
match = re.search(r"\{[\s\S]*\}", text)
|
| 79 |
+
if not match:
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
json_str = match.group(0)
|
| 83 |
+
json_str = json_str.replace("None", "null")
|
| 84 |
+
json_str = json_str.replace("True", "true").replace("False", "false")
|
| 85 |
+
json_str = re.sub(r",(\s*[}\]])", r"\1", json_str)
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
return json5.loads(json_str)
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"JSON parse error: {e}")
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def infer_from_text(jd_text: str):
|
| 95 |
+
"""Runs inference on a job description."""
|
| 96 |
+
start_time = time.time()
|
| 97 |
+
|
| 98 |
+
system_prompt = """Extract structured information from the following job description and return it as JSON.
|
| 99 |
+
""".strip()
|
| 100 |
+
|
| 101 |
+
user_prompt = f"""
|
| 102 |
+
Job Description:
|
| 103 |
+
{jd_text}
|
| 104 |
+
""".strip()
|
| 105 |
+
|
| 106 |
+
messages = [
|
| 107 |
+
{"role": "system", "content": system_prompt},
|
| 108 |
+
{"role": "user", "content": user_prompt}
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
prompt = tokenizer.apply_chat_template(
|
| 112 |
+
messages,
|
| 113 |
+
tokenize=False,
|
| 114 |
+
add_generation_prompt=True
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
raw_inputs = tokenizer(prompt, return_tensors="pt")
|
| 118 |
+
device = model.device
|
| 119 |
+
inputs = {k: v.to(device) for k, v in raw_inputs.items()}
|
| 120 |
+
|
| 121 |
+
with torch.no_grad():
|
| 122 |
+
out = model.generate(
|
| 123 |
+
**inputs,
|
| 124 |
+
max_new_tokens=1000,
|
| 125 |
+
do_sample=False,
|
| 126 |
+
temperature=0,
|
| 127 |
+
pad_token_id=tokenizer.eos_token_id
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
gen_tokens = out[0][inputs["input_ids"].shape[1]:]
|
| 131 |
+
response_text = tokenizer.decode(gen_tokens, skip_special_tokens=True)
|
| 132 |
+
duration = round(time.time() - start_time, 2)
|
| 133 |
+
|
| 134 |
+
parsed = extract_and_clean_json(response_text)
|
| 135 |
+
if parsed is not None:
|
| 136 |
+
return json.dumps(parsed, indent=2), duration
|
| 137 |
+
|
| 138 |
+
return response_text, duration
|