pavanmutha commited on
Commit
3841272
·
verified ·
1 Parent(s): fcdbf51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -24
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
- # Correct regex pattern to extract code blocks that contain Python code
100
- code_blocks = re.findall(r"```(?:py|python)?\n(.*?)```", raw_output, re.DOTALL)
101
-
102
- # Loop through each code block and look for patterns related to JSON or result definitions
103
- for block in code_blocks:
104
- for pattern in [
105
- r"print\(\s*json\.dumps\(\s*(\{[\s\S]*?\})\s*\)\s*\)",
106
- r"json\.dumps\(\s*(\{[\s\S]*?\})\s*\)",
107
- r"result\s*=\s*(\{[\s\S]*?\})"
108
- ]:
109
- match = re.search(pattern, block, re.DOTALL)
110
- if match:
111
- try:
112
- return json.loads(match.group(1)) # Try to parse the JSON directly
113
- except json.JSONDecodeError:
114
- return ast.literal_eval(match.group(1)) # Fallback to evaluating the dictionary
115
-
116
- # Final fallback: Look for any dictionary-like structure in the entire output
117
- fallback = re.search(r"\{[\s\S]+?\}", raw_output)
118
- if fallback:
119
- return json.loads(fallback.group(0))
120
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  except Exception as e:
122
  print(f"[extract_json] Error: {e}")
123
-
124
- # Return an error if JSON extraction fails
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