Update app.py
Browse files141 unmodified lines142143144145146147148149150151152153154155156157158159160141 unmodified linesdef extract_json_from_codeagent_output(raw_output):try:# Try to extract just the json.dumps(...) output if the model followed instructionsmatch = re.search(r'json\.dumps\((.*?)\)\s*$', raw_output, re.DOTALL)if match:json_string = match.group(1)# Attempt to load the inner Python dictparsed_dict = eval(json_string) # Safe-ish since it’s model-generatedreturn parsed_dictelse:# As fallback, try loading the whole string as JSONreturn json.loads(raw_output)except Exception as e:print(f"[Error] Failed to parse agent output as JSON: {e}")return Nonedef analyze_data(csv_file, additional_notes=""):141 unmodified lines142143144145146147148149150151152153154155156157158159160161162163164165166167168141 unmodified linesdef extract_json_from_codeagent_output(raw_output):try:# Strip everything before the first `{`json_start = raw_output.find('{')if json_start != -1:json_str = raw_output[json_start:]return json.loads(json_str)except Exception as e:print(f"[Fallback 1] Failed to parse stripped JSON: {e}")try:# Try the json.dumps(...) regex extraction (only if that pattern exists)match = re.search(r'json\.dumps\((.*?)\)\s*$', raw_output, re.DOTALL)if match:json_string = match.group(1)parsed_dict = eval(json_string)return parsed_dictexcept Exception as e:print(f"[Fallback 2] Failed to parse json.dumps output: {e}")print("[Error] Final fallback — unable to parse agent output.")return Nonedef analyze_data(csv_file, additional_notes=""):
app.py+16-8