Spaces:
Sleeping
Sleeping
Commit
·
4980b54
1
Parent(s):
35d31e1
Refine evaluation prompt and extraction for DeepSeek instruct model
Browse files- tinyllama_inference.py +13 -16
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
|
| 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,27 +38,24 @@ Your response:"""
|
|
| 38 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 39 |
outputs = model.generate(
|
| 40 |
**inputs,
|
| 41 |
-
max_new_tokens=
|
| 42 |
-
temperature=0.
|
| 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
|
| 48 |
|
| 49 |
-
# Extract all JSON objects
|
| 50 |
matches = re.findall(r'\{.*?\}', response_text)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
continue
|
| 60 |
-
if result is None:
|
| 61 |
-
result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract valid JSON from AI response."}
|
| 62 |
|
| 63 |
return result
|
| 64 |
|
|
|
|
| 15 |
return tokenizer, model
|
| 16 |
|
| 17 |
def evaluate_code(question, code):
|
| 18 |
+
# Refined prompt with explicit instructions and a "Your response:" line.
|
| 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=100, # Increase token allowance if needed
|
| 42 |
+
temperature=0.2, # Allow some creativity, but mostly deterministic
|
| 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 |
+
# Extract all JSON objects (non-greedy) and use the last one
|
| 50 |
matches = re.findall(r'\{.*?\}', response_text)
|
| 51 |
+
if matches:
|
| 52 |
+
json_text = matches[-1] # Pick the last JSON block
|
| 53 |
try:
|
| 54 |
+
result = json.loads(json_text)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
|
| 57 |
+
else:
|
| 58 |
+
result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract JSON from AI response."}
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
return result
|
| 61 |
|