Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -234,6 +234,24 @@ def construct_analysis_prompt(code_files_dict, requested_analyses):
|
|
| 234 |
full_prompt = "".join(prompt_parts)
|
| 235 |
return full_prompt, included_files
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
def call_gemini_api(prompt):
|
| 238 |
"""
|
| 239 |
Calls the Gemini API using the provided prompt.
|
|
@@ -277,16 +295,20 @@ def call_gemini_api(prompt):
|
|
| 277 |
time.sleep(1)
|
| 278 |
api_status.empty()
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
json_response_text = response.text.strip()
|
| 282 |
-
|
| 283 |
match = re.search(r'({.*})', json_response_text, re.DOTALL)
|
| 284 |
if match:
|
| 285 |
final_json_text = match.group(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
insights = json.loads(final_json_text)
|
| 287 |
return insights, None
|
| 288 |
else:
|
| 289 |
-
st.warning("⚠️ Could not
|
| 290 |
return {"raw_response": response.text}, "AI response did not contain clear JSON object."
|
| 291 |
except json.JSONDecodeError as json_err:
|
| 292 |
st.error(f"🚨 Error parsing JSON: {json_err}")
|
|
|
|
| 234 |
full_prompt = "".join(prompt_parts)
|
| 235 |
return full_prompt, included_files
|
| 236 |
|
| 237 |
+
def extract_json_from_text(text):
|
| 238 |
+
"""
|
| 239 |
+
Attempts to extract a balanced JSON object from the given text.
|
| 240 |
+
It looks for the first '{' and returns the substring until the braces are balanced.
|
| 241 |
+
"""
|
| 242 |
+
start = text.find('{')
|
| 243 |
+
if start == -1:
|
| 244 |
+
return None
|
| 245 |
+
count = 0
|
| 246 |
+
for i in range(start, len(text)):
|
| 247 |
+
if text[i] == '{':
|
| 248 |
+
count += 1
|
| 249 |
+
elif text[i] == '}':
|
| 250 |
+
count -= 1
|
| 251 |
+
if count == 0:
|
| 252 |
+
return text[start:i+1]
|
| 253 |
+
return None
|
| 254 |
+
|
| 255 |
def call_gemini_api(prompt):
|
| 256 |
"""
|
| 257 |
Calls the Gemini API using the provided prompt.
|
|
|
|
| 295 |
time.sleep(1)
|
| 296 |
api_status.empty()
|
| 297 |
|
| 298 |
+
# Remove markdown formatting if present
|
| 299 |
+
json_response_text = response.text.strip().replace("```json", "").replace("```", "")
|
| 300 |
+
# First try regex extraction
|
| 301 |
match = re.search(r'({.*})', json_response_text, re.DOTALL)
|
| 302 |
if match:
|
| 303 |
final_json_text = match.group(1)
|
| 304 |
+
else:
|
| 305 |
+
# Fallback: extract using our balanced-braces function
|
| 306 |
+
final_json_text = extract_json_from_text(json_response_text)
|
| 307 |
+
if final_json_text:
|
| 308 |
insights = json.loads(final_json_text)
|
| 309 |
return insights, None
|
| 310 |
else:
|
| 311 |
+
st.warning("⚠️ Could not extract a valid JSON object.")
|
| 312 |
return {"raw_response": response.text}, "AI response did not contain clear JSON object."
|
| 313 |
except json.JSONDecodeError as json_err:
|
| 314 |
st.error(f"🚨 Error parsing JSON: {json_err}")
|