Update app.py
Browse files139 unmodified lines14014114214314414514614714814915015115215315415515615715815916016116216327 unmodified lines19119219319419519619719819920020142 unmodified lines244245246247248249250139 unmodified lines])def extract_json_from_codeagent_output(raw_output):try:# First try direct JSON parseif isinstance(raw_output, str):return json.loads(raw_output)# If that fails, look for the json.dumps patternmatch = re.search(r'json\.dumps\(({.*?})\)', raw_output, re.DOTALL)if match:return ast.literal_eval(match.group(1))# Final fallback - try to find any JSON in the stringjson_match = re.search(r'\{.*\}', raw_output, re.DOTALL)if json_match:return json.loads(json_match.group(0))except Exception as e:print(f"JSON parsing error: {e}")return Nonedef analyze_data(csv_file, additional_notes=""):27 unmodified linesYou are a helpful data analysis agent. Please follow these very strict instructions and formatting:1. Load the data from the provided `source_file`.2. Detect numeric and categorical columns.3. Generate at 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. Always wrap code in markdown code blocks like this:```pyimport json42 unmodified lineswandb.log({os.path.basename(viz): wandb.Image(viz)})run.finish()# Parse the agent outputparsed_result = extract_json_from_codeagent_output(analysis_result)print(f"DEBUG - Parsed result: {parsed_result}") # Debug output139 unmodified lines14014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317427 unmodified lines20220320420520620720820921021121242 unmodified lines255256257258259260261262263264265266139 unmodified lines])2. **Improve the JSON Parsing Function**:```pythondef extract_json_from_codeagent_output(raw_output):try:# First try direct JSON parse if output is cleanif isinstance(raw_output, str) and raw_output.strip().startswith('{'):return json.loads(raw_output)# Look for complete code block patterncode_block_pattern = r'```(?:py|python)?\n(.*?)\n```'matches = re.findall(code_block_pattern, raw_output, re.DOTALL)if matches:for match in matches:# Find the json.dumps patternif 'json.dumps' in match:dict_part = re.search(r'result\s*=\s*({.*?})\s*\n', match, re.DOTALL)if dict_part:return ast.literal_eval(dict_part.group(1))# Final fallback - find any JSON structurejson_match = re.search(r'\{.*\}', raw_output, re.DOTALL)if json_match:return json.loads(json_match.group(0))except Exception as e:print(f"JSON parsing failed: {str(e)}")return Nonedef analyze_data(csv_file, additional_notes=""):27 unmodified linesYou are a helpful data analysis agent. Please follow these very strict instructions and formatting:1. Load the data from the provided `source_file`.2. FIRST analyze the data structure (column names and types)3. 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 json42 unmodified lineswandb.log({os.path.basename(viz): wandb.Image(viz)})run.finish()print("DEBUG - Raw agent output:", analysis_result[:500] + "...")print("Columns in data:", df_global.columns.tolist())print("Data types:", df_global.dtypes)with open("agent_output.txt", "w") as f:f.write(str(analysis_result))# Parse the agent outputparsed_result = extract_json_from_codeagent_output(analysis_result)print(f"DEBUG - Parsed result: {parsed_result}") # Debug output
app.py+29-13