Seth0330 commited on
Commit
73e2df7
·
verified ·
1 Parent(s): dd68ed4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -107,15 +107,17 @@ def clean_json_response(text, model_choice):
107
 
108
  # Handle Mistral's markdown response
109
  if model_choice == "Mistral Small":
110
- json_match = re.search(r'```(?:json)?\n({.*?})\n```', text, re.DOTALL)
111
- if json_match:
112
- text = json_match.group(1)
113
-
114
  # Handle Llama's truncated response
115
  if model_choice == "Llama 4 Mavericks":
116
  if '"line_items":' in text and not text.strip().endswith('}}'):
117
- text = text.split('"line_items":')[0] + '"line_items": []}}'
118
-
 
 
 
119
  # Try parsing the cleaned JSON
120
  try:
121
  data = json.loads(text)
@@ -129,6 +131,16 @@ def clean_json_response(text, model_choice):
129
 
130
  return data
131
  except json.JSONDecodeError as e:
 
 
 
 
 
 
 
 
 
 
132
  st.warning(f"JSON parsing failed: {str(e)}")
133
  return None
134
 
 
107
 
108
  # Handle Mistral's markdown response
109
  if model_choice == "Mistral Small":
110
+ # Remove all markdown formatting
111
+ text = text.replace('```json', '').replace('```', '').strip()
112
+
 
113
  # Handle Llama's truncated response
114
  if model_choice == "Llama 4 Mavericks":
115
  if '"line_items":' in text and not text.strip().endswith('}}'):
116
+ # Check if we have at least a complete header
117
+ if '"invoice_header":' in text:
118
+ # Return with empty line items
119
+ text = text.split('"line_items":')[0] + '"line_items": []}'
120
+
121
  # Try parsing the cleaned JSON
122
  try:
123
  data = json.loads(text)
 
131
 
132
  return data
133
  except json.JSONDecodeError as e:
134
+ # Try one more time with strict=False for Llama
135
+ if model_choice == "Llama 4 Mavericks":
136
+ try:
137
+ # Find the last complete JSON object
138
+ end_pos = text.rfind('}')
139
+ if end_pos != -1:
140
+ return json.loads(text[:end_pos+1])
141
+ except:
142
+ pass
143
+
144
  st.warning(f"JSON parsing failed: {str(e)}")
145
  return None
146