Update app.py
Browse files67 unmodified lines686970717273747576779 unmodified lines8788899091922 unmodified lines95969798991001011021033 unmodified lines10710810911011111234 unmodified lines14714814915015115267 unmodified lines# Add a extraction of JSON if CodeAgent Output is not in formatimport os, json, shutil, time, psutil, tempfile, re, astimport pandas as pdimport wandbdef extract_json_from_codeagent_output(raw_output):try:code_blocks = re.findall(r"```(?:py|python)?\n(.*?)```", raw_output, re.DOTALL)9 unmodified linesreturn json.loads(match.group(1))except json.JSONDecodeError:return ast.literal_eval(match.group(1))fallback = re.search(r"\{[\s\S]+?\}", raw_output)if fallback:return json.loads(fallback.group(0))2 unmodified linesreturn {"error": "Failed to extract structured JSON"}def analyze_data(csv_file, additional_notes=""):import time, os, shutil, psutil, jsonfrom pathlib import Pathstart_time = time.time()process = psutil.Process(os.getpid())initial_memory = process.memory_info().rss / 1024 ** 23 unmodified linesshutil.rmtree('./figures')os.makedirs('./figures', exist_ok=True)wandb.login(key=os.environ.get('WANDB_API_KEY'))run = wandb.init(project="huggingface-data-analysis", config={"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",34 unmodified lines"error": "Failed to extract structured JSON"}execution_time = time.time() - start_timefinal_memory = process.memory_info().rss / 1024 ** 2memory_usage = final_memory - initial_memory67 unmodified lines68697071727374757677787980818283848586878889909192939495969798991009 unmodified lines1101111121131141151162 unmodified lines1191201211221231243 unmodified lines12812913013113213313434 unmodified lines16917017117217317417567 unmodified lines# Add a extraction of JSON if CodeAgent Output is not in formatdef extract_json_from_codeagent_output(raw_output):try:code_blocks = re.findall(r"```(?:py|python)?\n(.*?)```", raw_output, re.DOTALL)for block in code_blocks:for pattern in [r"print\(\s*json\.dumps\(\s*(\{[\s\S]*?\})\s*\)\s*\)",r"json\.dumps\(\s*(\{[\s\S]*?\})\s*\)",r"result\s*=\s*(\{[\s\S]*?\})"]:match = re.search(pattern, block, re.DOTALL)if match:try:return json.loads(match.group(1))except json.JSONDecodeError:return ast.literal_eval(match.group(1))fallback = re.search(r"\{[\s\S]+?\}", raw_output)if fallback:return json.loads(fallback.group(0))except Exception as e:print(f"[extract_json] Error: {e}")return {"error": "Failed to extract structured JSON"}import time, os, shutil, psutil, jsonfrom pathlib import Pathimport wandb# Add a robust JSON extraction functiondef extract_json_from_codeagent_output(raw_output):try:code_blocks = re.findall(r"```(?:py|python)?\n(.*?)```", raw_output, re.DOTALL)9 unmodified linesreturn json.loads(match.group(1))except json.JSONDecodeError:return ast.literal_eval(match.group(1))# Fallback when no structured JSON pattern is foundfallback = re.search(r"\{[\s\S]+?\}", raw_output)if fallback:return json.loads(fallback.group(0))2 unmodified linesreturn {"error": "Failed to extract structured JSON"}def analyze_data(csv_file, additional_notes=""):start_time = time.time()process = psutil.Process(os.getpid())initial_memory = process.memory_info().rss / 1024 ** 23 unmodified linesshutil.rmtree('./figures')os.makedirs('./figures', exist_ok=True)# Initialize WandBwandb.login(key=os.environ.get('WANDB_API_KEY'))run = wandb.init(project="huggingface-data-analysis", config={"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",34 unmodified lines"error": "Failed to extract structured JSON"}# Record execution time and memory usageexecution_time = time.time() - start_timefinal_memory = process.memory_info().rss / 1024 ** 2memory_usage = final_memory - initial_memory
app.py+28-5