Spaces:
Running
Running
Commit
·
0471bb9
1
Parent(s):
4980b54
Improve JSON extraction to select a valid evaluation output
Browse files- tinyllama_inference.py +17 -13
tinyllama_inference.py
CHANGED
@@ -15,7 +15,7 @@ def load_model():
|
|
15 |
return tokenizer, model
|
16 |
|
17 |
def evaluate_code(question, code):
|
18 |
-
# Refined prompt with explicit instructions and a "Your response:"
|
19 |
prompt = f"""You are an expert code evaluator.
|
20 |
Evaluate the following solution for the given problem.
|
21 |
Rate the solution as follows:
|
@@ -38,24 +38,28 @@ Your response:"""
|
|
38 |
inputs = tokenizer(prompt, return_tensors="pt")
|
39 |
outputs = model.generate(
|
40 |
**inputs,
|
41 |
-
max_new_tokens=
|
42 |
-
temperature=0.2, # Allow some creativity
|
43 |
pad_token_id=tokenizer.eos_token_id,
|
44 |
do_sample=True
|
45 |
)
|
46 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
47 |
print("Raw model response:", response_text) # Debug output
|
48 |
|
49 |
-
#
|
50 |
-
matches = re.findall(r'\{.*?\}', response_text)
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
return result
|
61 |
|
|
|
15 |
return tokenizer, model
|
16 |
|
17 |
def evaluate_code(question, code):
|
18 |
+
# Refined prompt with explicit instructions and a "Your response:" marker.
|
19 |
prompt = f"""You are an expert code evaluator.
|
20 |
Evaluate the following solution for the given problem.
|
21 |
Rate the solution as follows:
|
|
|
38 |
inputs = tokenizer(prompt, return_tensors="pt")
|
39 |
outputs = model.generate(
|
40 |
**inputs,
|
41 |
+
max_new_tokens=120, # Increase token allowance if needed
|
42 |
+
temperature=0.2, # Allow some creativity
|
43 |
pad_token_id=tokenizer.eos_token_id,
|
44 |
do_sample=True
|
45 |
)
|
46 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
47 |
print("Raw model response:", response_text) # Debug output
|
48 |
|
49 |
+
# Use regex to find all JSON blocks (including across multiple lines)
|
50 |
+
matches = re.findall(r'\{.*?\}', response_text, re.DOTALL)
|
51 |
+
result = None
|
52 |
+
# Iterate over matches and select the first one that contains both "stars" and "feedback"
|
53 |
+
for m in matches:
|
54 |
+
if '"stars"' in m and '"feedback"' in m:
|
55 |
+
try:
|
56 |
+
temp = json.loads(m)
|
57 |
+
result = temp
|
58 |
+
break
|
59 |
+
except Exception:
|
60 |
+
continue
|
61 |
+
if result is None:
|
62 |
+
result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract valid JSON from AI response."}
|
63 |
|
64 |
return result
|
65 |
|