Spaces:
Running
Running
Commit
·
b72b033
1
Parent(s):
3c44ee8
Update JSON extraction in tinyllama_inference.py to select last JSON block
Browse files- 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
|
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
|
35 |
pad_token_id=tokenizer.eos_token_id,
|
36 |
-
do_sample=True # Enable sampling to encourage
|
37 |
)
|
38 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
print("Raw model response:", response_text) # Debug output
|
40 |
-
|
41 |
-
# Use
|
42 |
matches = re.findall(r'\{.*?\}', response_text)
|
43 |
-
|
44 |
-
|
45 |
try:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
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 |
|