Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -257,10 +257,10 @@ def fix_json(text):
|
|
257 |
Attempt to fix common JSON formatting issues.
|
258 |
This function removes trailing commas and inserts commas between adjacent objects.
|
259 |
"""
|
260 |
-
# Remove trailing commas before closing
|
261 |
-
text = re.sub(r',\s*}',
|
262 |
-
# Insert
|
263 |
-
text = re.sub(r'}\s*{',
|
264 |
return text
|
265 |
|
266 |
def call_gemini_api(prompt):
|
@@ -306,14 +306,14 @@ def call_gemini_api(prompt):
|
|
306 |
time.sleep(1)
|
307 |
api_status.empty()
|
308 |
|
309 |
-
# Remove markdown formatting if present
|
310 |
json_response_text = response.text.strip().replace("```json", "").replace("```", "")
|
311 |
-
# First
|
312 |
match = re.search(r'({.*})', json_response_text, re.DOTALL)
|
313 |
if match:
|
314 |
final_json_text = match.group(1)
|
315 |
else:
|
316 |
-
# Fallback: extract using balanced braces
|
317 |
final_json_text = extract_json_from_text(json_response_text)
|
318 |
if not final_json_text:
|
319 |
st.warning("⚠️ Could not extract a valid JSON object.")
|
@@ -322,7 +322,7 @@ def call_gemini_api(prompt):
|
|
322 |
insights = json.loads(final_json_text)
|
323 |
return insights, None
|
324 |
except json.JSONDecodeError as json_err:
|
325 |
-
# Attempt to fix the JSON string
|
326 |
fixed_text = fix_json(final_json_text)
|
327 |
try:
|
328 |
insights = json.loads(fixed_text)
|
|
|
257 |
Attempt to fix common JSON formatting issues.
|
258 |
This function removes trailing commas and inserts commas between adjacent objects.
|
259 |
"""
|
260 |
+
# Remove trailing commas before a closing brace or bracket.
|
261 |
+
text = re.sub(r',\s*([}\]])', r'\1', text)
|
262 |
+
# Insert commas between adjacent objects if missing.
|
263 |
+
text = re.sub(r'}\s*{', '},{', text)
|
264 |
return text
|
265 |
|
266 |
def call_gemini_api(prompt):
|
|
|
306 |
time.sleep(1)
|
307 |
api_status.empty()
|
308 |
|
309 |
+
# Remove markdown formatting if present.
|
310 |
json_response_text = response.text.strip().replace("```json", "").replace("```", "")
|
311 |
+
# First, attempt regex extraction.
|
312 |
match = re.search(r'({.*})', json_response_text, re.DOTALL)
|
313 |
if match:
|
314 |
final_json_text = match.group(1)
|
315 |
else:
|
316 |
+
# Fallback: extract using balanced braces.
|
317 |
final_json_text = extract_json_from_text(json_response_text)
|
318 |
if not final_json_text:
|
319 |
st.warning("⚠️ Could not extract a valid JSON object.")
|
|
|
322 |
insights = json.loads(final_json_text)
|
323 |
return insights, None
|
324 |
except json.JSONDecodeError as json_err:
|
325 |
+
# Attempt to fix the JSON string.
|
326 |
fixed_text = fix_json(final_json_text)
|
327 |
try:
|
328 |
insights = json.loads(fixed_text)
|