Update app.py
Browse files140 unmodified lines14114214314414514614714814915015115215315415515615715815916016116216316416516616731 unmodified lines199200201202203204205206207208209210211212213214215216217218219220221222140 unmodified linesdef extract_json_from_codeagent_output(raw_output):import json, ast, retry:# First look for json.dumps inside a code blockcode_blocks = re.findall(r"```(?:py|python)?\n(.*?)```", raw_output, re.DOTALL)for block in code_blocks:json_match = re.search(r"json\.dumps\s*\(\s*(\{.*?\})\s*\)", block, re.DOTALL)if json_match:cleaned = json_match.group(1)return json.loads(cleaned)# Final fallback - extract any JSON-like blockfallback_match = re.search(r"(\{[\s\S]+\})", raw_output)if fallback_match:return json.loads(fallback_match.group(1))except Exception as e:print(f"JSON parsing failed: {e}")return Nonedef analyze_data(csv_file, additional_notes=""):31 unmodified lines3. THEN generate visualizations using EXISTING columns with least 5 visualizations and 5 insights.4. Save all plots to `./figures/` as PNGs using matplotlib or seaborn.5. DO NOT use open() or print() statements.6. FINALLY return ONLY this exact format:```pyimport jsonresult = {"observations": {"numeric_columns": ["list", "of", "columns"],"categorical_columns": ["list", "of", "columns"],"data_issues": "Any problems found"},"insights": {"key_insight_1": "Your first insight","key_insight_2": "Your second insight"}}json.dumps(result)```<end_code>Be concise and avoid any narrative outside this final dictionary.Never use unauthorized imports (only pandas, numpy, matplotlib, seaborn are allowed)140 unmodified lines14114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817931 unmodified lines211212213214215216217218219220221222223224225226227228229230231232233234235236140 unmodified linesThis eliminates the chance of `Thought:` responses and ensures the regex matches.---### ✅ 2. Add a pre-check fallback for non-compliant agent outputsBefore applying the regex, check if the output contains backticks at all:def extract_json_from_codeagent_output(raw_output):import re, jsontry:# If output does not contain code block, exit earlyif "```" not in raw_output:print("Agent output did not contain a code block.")return None# Extract code inside triple backtickscode_blocks = re.findall(r"```(?:py|python)?\n(.*?)```", raw_output, re.DOTALL)for block in code_blocks:json_match = re.search(r"json\.dumps\((\{.*?\})\)", block, re.DOTALL)if json_match:return json.loads(json_match.group(1))# Last fallback: grab full JSON-looking blockfallback_match = re.search(r"\{[\s\S]+\}", raw_output)if fallback_match:return json.loads(fallback_match.group(0))except Exception as e:print(f"extract_json_from_codeagent_output() failed: {e}")return Nonedef analyze_data(csv_file, additional_notes=""):31 unmodified lines3. THEN generate visualizations using EXISTING columns with least 5 visualizations and 5 insights.4. Save all plots to `./figures/` as PNGs using matplotlib or seaborn.5. DO NOT use open() or print() statements.6. Use only authorized imports: `pandas`, `numpy`, `matplotlib.pyplot`, `seaborn`, `json`.7. DO NOT return any explanations, thoughts, or narration outside the final output block.8. FINALLY return ONLY this exact format:```pythonimport jsonresult = {"observations": {"numeric_columns": [...],"categorical_columns": [...],"data_issues": "..."},"insights": [{"category": "Insight A", "insight": "Description of insight A"},{"category": "Insight B", "insight": "Description of insight B"}]}print(json.dumps(result))```<end_code>Be concise and avoid any narrative outside this final dictionary.Never use unauthorized imports (only pandas, numpy, matplotlib, seaborn are allowed)
app.py+35-21