Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 111 |
-
|
| 112 |
-
|
| 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 |
-
|
| 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 |
|