DEADLOCK007X commited on
Commit
b72b033
·
1 Parent(s): 3c44ee8

Update JSON extraction in tinyllama_inference.py to select last JSON block

Browse files
Files changed (1) hide show
  1. tinyllama_inference.py +17 -13
tinyllama_inference.py CHANGED
@@ -15,39 +15,43 @@ def load_model():
15
  return tokenizer, model
16
 
17
  def evaluate_code(question, code):
18
- # Refined prompt: instruct the model to output exactly one JSON object.
19
  prompt = f"""You are an expert code evaluator.
20
  Evaluate the following solution for the given problem.
21
  Respond with exactly one JSON object (with no extra text) that has exactly two keys:
22
  "stars": an integer between 0 and 5 (0 means completely incorrect, 5 means excellent),
23
  "feedback": a concise string message.
24
  The JSON must start with '{{' and end with '}}'.
25
- Do not output any text besides the JSON.
26
  Question: "{question}"
27
  Solution: "{code}"
28
  Your response:"""
 
29
  tokenizer, model = load_model()
30
  inputs = tokenizer(prompt, return_tensors="pt")
31
  outputs = model.generate(
32
  **inputs,
33
  max_new_tokens=100, # Allow enough tokens for a complete response
34
- temperature=0.2, # Small randomness for a bit of creativity
35
  pad_token_id=tokenizer.eos_token_id,
36
- do_sample=True # Enable sampling to encourage model generation
37
  )
38
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
39
  print("Raw model response:", response_text) # Debug output
40
-
41
- # Use findall to get all JSON objects and take the last one
42
  matches = re.findall(r'\{.*?\}', response_text)
43
- if matches:
44
- json_text = matches[-1]
45
  try:
46
- result = json.loads(json_text)
47
- except Exception as e:
48
- result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
49
- else:
50
- result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract JSON from AI response."}
 
 
 
 
51
 
52
  return result
53
 
 
15
  return tokenizer, model
16
 
17
  def evaluate_code(question, code):
 
18
  prompt = f"""You are an expert code evaluator.
19
  Evaluate the following solution for the given problem.
20
  Respond with exactly one JSON object (with no extra text) that has exactly two keys:
21
  "stars": an integer between 0 and 5 (0 means completely incorrect, 5 means excellent),
22
  "feedback": a concise string message.
23
  The JSON must start with '{{' and end with '}}'.
24
+ Do not output anything else.
25
  Question: "{question}"
26
  Solution: "{code}"
27
  Your response:"""
28
+
29
  tokenizer, model = load_model()
30
  inputs = tokenizer(prompt, return_tensors="pt")
31
  outputs = model.generate(
32
  **inputs,
33
  max_new_tokens=100, # Allow enough tokens for a complete response
34
+ temperature=0.2, # Small randomness for creativity
35
  pad_token_id=tokenizer.eos_token_id,
36
+ do_sample=True # Enable sampling to encourage generation
37
  )
38
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
39
  print("Raw model response:", response_text) # Debug output
40
+
41
+ # Use regex to extract all JSON objects (non-greedy)
42
  matches = re.findall(r'\{.*?\}', response_text)
43
+ result = None
44
+ for m in matches:
45
  try:
46
+ temp = json.loads(m)
47
+ # Check that the parsed JSON contains both expected keys
48
+ if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
49
+ result = temp
50
+ break
51
+ except Exception:
52
+ continue
53
+ if result is None:
54
+ result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract valid JSON from AI response."}
55
 
56
  return result
57