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

Improve JSON extraction with fallback methods

Browse files
Files changed (1) hide show
  1. tinyllama_inference.py +12 -26
tinyllama_inference.py CHANGED
@@ -15,39 +15,25 @@ def load_model():
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):
@@ -73,13 +59,13 @@ Your response:"""
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
 
15
  return tokenizer, model
16
 
17
  def extract_json(response_text):
18
+ # First attempt: Use regex (non-greedy, with DOTALL) to find JSON blocks
19
  matches = re.findall(r'\{.*?\}', response_text, re.DOTALL)
20
+ # Check the matches in reverse order (last one might be the evaluation output)
21
+ for m in reversed(matches):
22
  try:
23
+ temp = json.loads(m)
24
  if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
25
  return temp
26
  except Exception:
27
  continue
28
+ # Fallback: try extracting JSON from each line that looks like a JSON object
29
+ json_lines = [line.strip() for line in response_text.splitlines() if line.strip().startswith('{') and line.strip().endswith('}')]
30
+ for line in reversed(json_lines):
 
 
 
31
  try:
32
+ temp = json.loads(line)
33
  if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
34
  return temp
35
  except Exception:
36
+ continue
 
 
 
 
 
 
 
 
 
 
 
37
  return {"stars": 0, "feedback": "Evaluation failed. Unable to extract valid JSON from AI response."}
38
 
39
  def evaluate_code(question, code):
 
59
  inputs = tokenizer(prompt, return_tensors="pt")
60
  outputs = model.generate(
61
  **inputs,
62
+ max_new_tokens=120, # Increase token allowance for a complete response
63
+ temperature=0.2, # Low randomness for deterministic output
64
  pad_token_id=tokenizer.eos_token_id,
65
  do_sample=True
66
  )
67
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
68
+ print("Raw model response:", response_text) # Debug: Inspect raw output
69
 
70
  result = extract_json(response_text)
71
  return result