DEADLOCK007X commited on
Commit
b13d31f
·
1 Parent(s): 0471bb9

Improve JSON extraction with fallback methods

Browse files
Files changed (1) hide show
  1. tinyllama_inference.py +39 -18
tinyllama_inference.py CHANGED
@@ -14,8 +14,43 @@ def load_model():
14
  model = AutoModelForCausalLM.from_pretrained(model_name)
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,29 +73,15 @@ Your response:"""
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
 
66
  # For direct command-line testing.
 
14
  model = AutoModelForCausalLM.from_pretrained(model_name)
15
  return tokenizer, model
16
 
17
+ def extract_json(response_text):
18
+ # First, try to extract all JSON blocks using regex with DOTALL.
19
+ matches = re.findall(r'\{.*?\}', response_text, re.DOTALL)
20
+ for m in matches:
21
+ json_text = m.strip()
22
+ try:
23
+ temp = json.loads(json_text)
24
+ if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
25
+ return temp
26
+ except Exception:
27
+ continue
28
+
29
+ # Fallback: try splitting the text on "Your response:" and then extract JSON.
30
+ parts = response_text.split("Your response:")
31
+ if len(parts) > 1:
32
+ possible = parts[-1].strip()
33
+ # Try to extract JSON from this part.
34
+ try:
35
+ temp = json.loads(possible)
36
+ if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
37
+ return temp
38
+ except Exception:
39
+ # If it fails, try regex on this part.
40
+ matches = re.findall(r'\{.*?\}', possible, re.DOTALL)
41
+ for m in matches:
42
+ json_text = m.strip()
43
+ try:
44
+ temp = json.loads(json_text)
45
+ if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
46
+ return temp
47
+ except Exception:
48
+ continue
49
+
50
+ # If all methods fail, return a fallback result.
51
+ return {"stars": 0, "feedback": "Evaluation failed. Unable to extract valid JSON from AI response."}
52
+
53
  def evaluate_code(question, code):
 
54
  prompt = f"""You are an expert code evaluator.
55
  Evaluate the following solution for the given problem.
56
  Rate the solution as follows:
 
73
  inputs = tokenizer(prompt, return_tensors="pt")
74
  outputs = model.generate(
75
  **inputs,
76
+ max_new_tokens=120, # Allow enough tokens for a complete response
77
+ temperature=0.2, # Small randomness for creativity
78
  pad_token_id=tokenizer.eos_token_id,
79
  do_sample=True
80
  )
81
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
82
  print("Raw model response:", response_text) # Debug output
83
 
84
+ result = extract_json(response_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  return result
86
 
87
  # For direct command-line testing.