Update app.py
Browse files
app.py
CHANGED
|
@@ -94,36 +94,55 @@ def clean_data(df):
|
|
| 94 |
|
| 95 |
# Add a extraction of JSON if CodeAgent Output is not in format
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def extract_json_from_codeagent_output(raw_output):
|
| 98 |
try:
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
print(f"[extract_json] Error: {e}")
|
| 123 |
-
|
| 124 |
-
#
|
| 125 |
return {"error": "Failed to extract structured JSON"}
|
| 126 |
|
|
|
|
| 127 |
import pandas as pd
|
| 128 |
import tempfile
|
| 129 |
|
|
|
|
| 94 |
|
| 95 |
# Add a extraction of JSON if CodeAgent Output is not in format
|
| 96 |
|
| 97 |
+
import json
|
| 98 |
+
import re
|
| 99 |
+
import ast
|
| 100 |
+
|
| 101 |
def extract_json_from_codeagent_output(raw_output):
|
| 102 |
try:
|
| 103 |
+
# Case 1: Already a dictionary
|
| 104 |
+
if isinstance(raw_output, dict):
|
| 105 |
+
return raw_output
|
| 106 |
+
|
| 107 |
+
# Case 2: Attempt direct JSON parsing
|
| 108 |
+
if isinstance(raw_output, str):
|
| 109 |
+
try:
|
| 110 |
+
return json.loads(raw_output)
|
| 111 |
+
except json.JSONDecodeError:
|
| 112 |
+
pass # fallback to code block extraction
|
| 113 |
+
|
| 114 |
+
# Case 3: Extract code blocks and search for JSON-like structures
|
| 115 |
+
code_blocks = re.findall(r"```(?:json|py|python)?\n(.*?)```", raw_output, re.DOTALL)
|
| 116 |
+
|
| 117 |
+
for block in code_blocks:
|
| 118 |
+
for pattern in [
|
| 119 |
+
r"print\(\s*json\.dumps\(\s*(\{[\s\S]*?\})\s*\)\s*\)",
|
| 120 |
+
r"json\.dumps\(\s*(\{[\s\S]*?\})\s*\)",
|
| 121 |
+
r"result\s*=\s*(\{[\s\S]*?\})",
|
| 122 |
+
r"final_answer\s*\(\s*(\{[\s\S]*?\})\s*\)"
|
| 123 |
+
]:
|
| 124 |
+
match = re.search(pattern, block, re.DOTALL)
|
| 125 |
+
if match:
|
| 126 |
+
try:
|
| 127 |
+
return json.loads(match.group(1))
|
| 128 |
+
except json.JSONDecodeError:
|
| 129 |
+
return ast.literal_eval(match.group(1))
|
| 130 |
+
|
| 131 |
+
# Case 4: Fallback — match the first top-level dict-like structure in raw_output
|
| 132 |
+
fallback = re.search(r"\{[\s\S]+?\}", raw_output)
|
| 133 |
+
if fallback:
|
| 134 |
+
try:
|
| 135 |
+
return json.loads(fallback.group(0))
|
| 136 |
+
except json.JSONDecodeError:
|
| 137 |
+
return ast.literal_eval(fallback.group(0))
|
| 138 |
+
|
| 139 |
except Exception as e:
|
| 140 |
print(f"[extract_json] Error: {e}")
|
| 141 |
+
|
| 142 |
+
# Final fallback — structured error response
|
| 143 |
return {"error": "Failed to extract structured JSON"}
|
| 144 |
|
| 145 |
+
|
| 146 |
import pandas as pd
|
| 147 |
import tempfile
|
| 148 |
|