-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1119 lines (978 loc) · 40.4 KB
/
app.py
File metadata and controls
1119 lines (978 loc) · 40.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import networkx as nx
import re
import json
import base64
import tempfile
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import io
import os
import re
import json
import base64
import tempfile
import subprocess
import logging
from io import BytesIO
from typing import Dict, Any, List
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi import FastAPI
from dotenv import load_dotenv
import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Optional image conversion
try:
from PIL import Image
PIL_AVAILABLE = True
except Exception:
PIL_AVAILABLE = False
# LangChain / LLM imports (keep as you used)
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(title="TDS Data Analyst Agent")
# -------------------- Robust Gemini LLM with fallback --------------------
from collections import defaultdict
import time
from langchain_google_genai import ChatGoogleGenerativeAI
# Config
GEMINI_KEYS = [os.getenv(f"gemini_api_{i}") for i in range(1, 11)]
GEMINI_KEYS = [k for k in GEMINI_KEYS if k]
MODEL_HIERARCHY = [
"gemini-2.5-pro",
"gemini-2.5-flash",
"gemini-2.5-flash-lite",
"gemini-2.0-flash",
"gemini-2.0-flash-lite"
]
MAX_RETRIES_PER_KEY = 2
TIMEOUT = 30
QUOTA_KEYWORDS = ["quota", "exceeded", "rate limit", "403", "too many requests"]
if not GEMINI_KEYS:
raise RuntimeError("No Gemini API keys found. Please set them in your environment.")
# -------------------- LLM wrapper --------------------
class LLMWithFallback:
def __init__(self, keys=None, models=None, temperature=0):
self.keys = keys or GEMINI_KEYS
self.models = models or MODEL_HIERARCHY
self.temperature = temperature
self.slow_keys_log = defaultdict(list)
self.failing_keys_log = defaultdict(int)
self.current_llm = None # placeholder for actual ChatGoogleGenerativeAI instance
def _get_llm_instance(self):
last_error = None
for model in self.models:
for key in self.keys:
try:
llm_instance = ChatGoogleGenerativeAI(
model=model,
temperature=self.temperature,
google_api_key=key
)
self.current_llm = llm_instance
return llm_instance
except Exception as e:
last_error = e
msg = str(e).lower()
if any(qk in msg for qk in QUOTA_KEYWORDS):
self.slow_keys_log[key].append(model)
self.failing_keys_log[key] += 1
time.sleep(0.5)
raise RuntimeError(f"All models/keys failed. Last error: {last_error}")
# Required by LangChain agent
def bind_tools(self, tools):
llm_instance = self._get_llm_instance()
return llm_instance.bind_tools(tools)
# Keep .invoke interface
def invoke(self, prompt):
llm_instance = self._get_llm_instance()
return llm_instance.invoke(prompt)
LLM_TIMEOUT_SECONDS = int(os.getenv("LLM_TIMEOUT_SECONDS", 240))
@app.get("/", response_class=HTMLResponse)
async def serve_frontend():
"""Serve the main HTML interface"""
try:
with open("index.html", "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read())
except FileNotFoundError:
return HTMLResponse(content="<h1>Frontend not found</h1><p>Please ensure index.html is in the same directory as app.py</p>", status_code=404)
def parse_keys_and_types(raw_questions: str):
"""
Parses the key/type section from the questions file.
Returns:
keys_list: list of keys in order
type_map: dict key -> casting function
"""
import re
pattern = r"-\s*`([^`]+)`\s*:\s*(\w+)"
matches = re.findall(pattern, raw_questions)
type_map_def = {
"number": float,
"string": str,
"integer": int,
"int": int,
"float": float
}
type_map = {key: type_map_def.get(t.lower(), str) for key, t in matches}
keys_list = [k for k, _ in matches]
return keys_list, type_map
# -----------------------------
# Tools
# -----------------------------
@tool
def scrape_url_to_dataframe(url: str) -> Dict[str, Any]:
"""
Fetch a URL and return data as a DataFrame (supports HTML tables, CSV, Excel, Parquet, JSON, and plain text).
Always returns {"status": "success", "data": [...], "columns": [...]} if fetch works.
"""
print(f"Scraping URL: {url}")
try:
from io import BytesIO, StringIO
from bs4 import BeautifulSoup
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/138.0.0.0 Safari/537.36"
),
"Referer": "https://www.google.com/",
}
resp = requests.get(url, headers=headers, timeout=20)
resp.raise_for_status()
ctype = resp.headers.get("Content-Type", "").lower()
df = None
# --- CSV ---
if "text/csv" in ctype or url.lower().endswith(".csv"):
df = pd.read_csv(BytesIO(resp.content))
# --- Excel ---
elif any(url.lower().endswith(ext) for ext in (".xls", ".xlsx")) or "spreadsheetml" in ctype:
df = pd.read_excel(BytesIO(resp.content))
# --- Parquet ---
elif url.lower().endswith(".parquet"):
df = pd.read_parquet(BytesIO(resp.content))
# --- JSON ---
elif "application/json" in ctype or url.lower().endswith(".json"):
try:
data = resp.json()
df = pd.json_normalize(data)
except Exception:
df = pd.DataFrame([{"text": resp.text}])
# --- HTML / Fallback ---
elif "text/html" in ctype or re.search(r'/wiki/|\.org|\.com', url, re.IGNORECASE):
html_content = resp.text
# Try HTML tables first
try:
tables = pd.read_html(StringIO(html_content), flavor="bs4")
if tables:
df = tables[0]
except ValueError:
pass
# If no table found, fallback to plain text
if df is None:
soup = BeautifulSoup(html_content, "html.parser")
text = soup.get_text(separator="\n", strip=True)
df = pd.DataFrame({"text": [text]})
# --- Unknown type fallback ---
else:
df = pd.DataFrame({"text": [resp.text]})
# --- Normalize columns ---
df.columns = df.columns.map(str).str.replace(r'\[.*\]', '', regex=True).str.strip()
return {
"status": "success",
"data": df.to_dict(orient="records"),
"columns": df.columns.tolist()
}
except Exception as e:
return {"status": "error", "message": str(e)}
# -----------------------------
# Utilities for executing code safely
# -----------------------------
def clean_llm_output(output: str) -> Dict:
"""
Extract JSON object from LLM output robustly.
Returns dict or {"error": "..."}
"""
try:
if not output:
return {"error": "Empty LLM output"}
# remove triple-fence markers if present
s = re.sub(r"^```(?:json)?\s*", "", output.strip())
s = re.sub(r"\s*```$", "", s)
# find outermost JSON object by scanning for balanced braces
first = s.find("{")
last = s.rfind("}")
if first == -1 or last == -1 or last <= first:
return {"error": "No JSON object found in LLM output", "raw": s}
candidate = s[first:last+1]
try:
return json.loads(candidate)
except Exception as e:
# fallback: try last balanced pair scanning backwards
for i in range(last, first, -1):
cand = s[first:i+1]
try:
return json.loads(cand)
except Exception:
continue
return {"error": f"JSON parsing failed: {str(e)}", "raw": candidate}
except Exception as e:
return {"error": str(e)}
SCRAPE_FUNC = r'''
from typing import Dict, Any
import requests
from bs4 import BeautifulSoup
import pandas as pd
import re
def scrape_url_to_dataframe(url: str) -> Dict[str, Any]:
try:
response = requests.get(
url,
headers={"User-Agent": "Mozilla/5.0"},
timeout=5
)
response.raise_for_status()
except Exception as e:
return {
"status": "error",
"error": str(e),
"data": [],
"columns": []
}
soup = BeautifulSoup(response.text, "html.parser")
tables = pd.read_html(response.text)
if tables:
df = tables[0] # Take first table
df.columns = [str(c).strip() for c in df.columns]
# Ensure all columns are unique and string
df.columns = [str(col) for col in df.columns]
return {
"status": "success",
"data": df.to_dict(orient="records"),
"columns": list(df.columns)
}
else:
# Fallback to plain text
text_data = soup.get_text(separator="\n", strip=True)
# Try to detect possible "keys" from text like Runtime, Genre, etc.
detected_cols = set(re.findall(r"\b[A-Z][a-zA-Z ]{2,15}\b", text_data))
df = pd.DataFrame([{}]) # start empty
for col in detected_cols:
df[col] = None
if df.empty:
df["text"] = [text_data]
return {
"status": "success",
"data": df.to_dict(orient="records"),
"columns": list(df.columns)
}
'''
def write_and_run_temp_python(code: str, injected_pickle: str = None, timeout: int = 60) -> Dict[str, Any]:
"""
Write a temp python file which:
- provides a safe environment (imports)
- loads df/from pickle if provided into df and data variables
- defines a robust plot_to_base64() helper that ensures < 100kB (attempts resizing/conversion)
- executes the user code (which should populate `results` dict)
- prints json.dumps({"status":"success","result":results})
Returns dict with parsed JSON or error details.
"""
# create file content
preamble = [
"import json, sys, gc",
"import pandas as pd, numpy as np",
"import matplotlib",
"matplotlib.use('Agg')",
"import matplotlib.pyplot as plt",
"from io import BytesIO",
"import base64",
]
if PIL_AVAILABLE:
preamble.append("from PIL import Image")
# inject df if a pickle path provided
if injected_pickle:
preamble.append(f"df = pd.read_pickle(r'''{injected_pickle}''')\n")
preamble.append("data = df.to_dict(orient='records')\n")
else:
# ensure data exists so user code that references data won't break
preamble.append("data = globals().get('data', {})\n")
# plot_to_base64 helper that tries to reduce size under 100_000 bytes
helper = r'''
def plot_to_base64(max_bytes=100000):
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=100)
buf.seek(0)
img_bytes = buf.getvalue()
if len(img_bytes) <= max_bytes:
return base64.b64encode(img_bytes).decode('ascii')
# try decreasing dpi/figure size iteratively
for dpi in [80, 60, 50, 40, 30]:
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=dpi)
buf.seek(0)
b = buf.getvalue()
if len(b) <= max_bytes:
return base64.b64encode(b).decode('ascii')
# if Pillow available, try convert to WEBP which is typically smaller
try:
from PIL import Image
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=40)
buf.seek(0)
im = Image.open(buf)
out_buf = BytesIO()
im.save(out_buf, format='WEBP', quality=80, method=6)
out_buf.seek(0)
ob = out_buf.getvalue()
if len(ob) <= max_bytes:
return base64.b64encode(ob).decode('ascii')
# try lower quality
out_buf = BytesIO()
im.save(out_buf, format='WEBP', quality=60, method=6)
out_buf.seek(0)
ob = out_buf.getvalue()
if len(ob) <= max_bytes:
return base64.b64encode(ob).decode('ascii')
except Exception:
pass
# as last resort return downsized PNG even if > max_bytes
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=20)
buf.seek(0)
return base64.b64encode(buf.getvalue()).decode('ascii')
'''
# Build the code to write
script_lines = []
script_lines.extend(preamble)
script_lines.append(helper)
script_lines.append(SCRAPE_FUNC)
script_lines.append("\nresults = {}\n")
script_lines.append(code)
# ensure results printed as json
script_lines.append("\nprint(json.dumps({'status':'success','result':results}, default=str), flush=True)\n")
tmp = tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8')
tmp.write("\n".join(script_lines))
tmp.flush()
tmp_path = tmp.name
tmp.close()
try:
completed = subprocess.run([sys.executable, tmp_path],
capture_output=True, text=True, timeout=timeout)
if completed.returncode != 0:
# collect stderr and stdout for debugging
return {"status": "error", "message": completed.stderr.strip() or completed.stdout.strip()}
# parse stdout as json
out = completed.stdout.strip()
try:
parsed = json.loads(out)
return parsed
except Exception as e:
return {"status": "error", "message": f"Could not parse JSON output: {str(e)}", "raw": out}
except subprocess.TimeoutExpired:
return {"status": "error", "message": "Execution timed out"}
finally:
try:
os.unlink(tmp_path)
if injected_pickle and os.path.exists(injected_pickle):
os.unlink(injected_pickle)
except Exception:
pass
# -----------------------------
# LLM agent setup
# -----------------------------
# llm = ChatGoogleGenerativeAI(
# model=os.getenv("GOOGLE_MODEL", "gemini-2.5-pro"),
# temperature=0,
# google_api_key=os.getenv("GOOGLE_API_KEY")
# )
# -------------------- Initialize LLM --------------------
llm = LLMWithFallback(temperature=0)
# -----------------------------
# Tools list for agent (LangChain tool decorator returns metadata for the LLM)
tools = [scrape_url_to_dataframe] # we only expose scraping as a tool; agent will still produce code
# Prompt: instruct agent to call the tool and output JSON only
prompt = ChatPromptTemplate.from_messages([
("system", """You are a full-stack autonomous data analyst agent.
You will receive:
- A set of **rules** for this request (these rules may differ depending on whether a dataset is uploaded or not)
- One or more **questions**
- An optional **dataset preview**
You must:
1. Follow the provided rules exactly.
2. Return only a valid JSON object — no extra commentary or formatting.
3. The JSON must contain:
- "questions": [ list of original question strings exactly as provided ]
- "code": "..." (Python code that creates a dict called `results` with each question string as a key and its computed answer as the value)
4. Your Python code will run in a sandbox with:
- pandas, numpy, matplotlib available
- A helper function `plot_to_base64(max_bytes=100000)` for generating base64-encoded images under 100KB.
5. When returning plots, always use `plot_to_base64()` to keep image sizes small.
6. Make sure all variables are defined before use, and the code can run without any undefined references.
"""),
("human", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
])
agent = create_tool_calling_agent(
llm=llm,
tools=[scrape_url_to_dataframe], # let the agent call tools if it wants; we will also pre-process scrapes
prompt=prompt
)
agent_executor = AgentExecutor(
agent=agent,
tools=[scrape_url_to_dataframe],
verbose=True,
max_iterations=3,
early_stopping_method="generate",
handle_parsing_errors=True,
return_intermediate_steps=False
)
# -----------------------------
# Runner: orchestrates agent -> pre-scrape inject -> execute
# -----------------------------
def run_agent_safely(llm_input: str) -> Dict:
"""
1. Run the agent_executor.invoke to get LLM output
2. Extract JSON, get 'code' and 'questions'
3. Detect scrape_url_to_dataframe("...") calls in code, run them here, pickle df and inject before exec
4. Execute the code in a temp file and return results mapping questions -> answers
"""
try:
response = agent_executor.invoke({"input": llm_input}, {"timeout": LLM_TIMEOUT_SECONDS})
raw_out = response.get("output") or response.get("final_output") or response.get("text") or ""
if not raw_out:
return {"error": f"Agent returned no output. Full response: {response}"}
parsed = clean_llm_output(raw_out)
if "error" in parsed:
return parsed
if not isinstance(parsed, dict) or "code" not in parsed or "questions" not in parsed:
return {"error": f"Invalid agent response format: {parsed}"}
code = parsed["code"]
questions: List[str] = parsed["questions"]
# Detect scrape calls; find all URLs used in scrape_url_to_dataframe("URL")
urls = re.findall(r"scrape_url_to_dataframe\(\s*['\"](.*?)['\"]\s*\)", code)
pickle_path = None
if urls:
# For now support only the first URL (agent may code multiple scrapes; you can extend this)
url = urls[0]
tool_resp = scrape_url_to_dataframe(url)
if tool_resp.get("status") != "success":
return {"error": f"Scrape tool failed: {tool_resp.get('message')}"}
# create df and pickle it
df = pd.DataFrame(tool_resp["data"])
temp_pkl = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False)
temp_pkl.close()
df.to_pickle(temp_pkl.name)
pickle_path = temp_pkl.name
# Make sure agent's code can reference df/data: we will inject the pickle loader in the temp script
# Execute code in temp python script
exec_result = write_and_run_temp_python(code, injected_pickle=pickle_path, timeout=LLM_TIMEOUT_SECONDS)
if exec_result.get("status") != "success":
return {"error": f"Execution failed: {exec_result.get('message', exec_result)}", "raw": exec_result.get("raw")}
# exec_result['result'] should be results dict
results_dict = exec_result.get("result", {})
# Map to original questions (they asked to use exact question strings)
output = {}
for q in questions:
output[q] = results_dict.get(q, "Answer not found")
return output
except Exception as e:
logger.exception("run_agent_safely failed")
return {"error": str(e)}
from fastapi import Request
@app.post("/api")
async def analyze_data(request: Request):
try:
form = await request.form()
questions_file = None
data_file = None
for key, val in form.items():
if hasattr(val, "filename") and val.filename: # it's a file
fname = val.filename.lower()
if fname.endswith(".txt") and questions_file is None:
questions_file = val
else:
data_file = val
if not questions_file:
raise HTTPException(400, "Missing questions file (.txt)")
raw_questions = (await questions_file.read()).decode("utf-8")
keys_list, type_map = parse_keys_and_types(raw_questions)
pickle_path = None
df_preview = ""
dataset_uploaded = False
if data_file:
dataset_uploaded = True
filename = data_file.filename.lower()
content = await data_file.read()
from io import BytesIO
if filename.endswith(".csv"):
df = pd.read_csv(BytesIO(content))
elif filename.endswith((".xlsx", ".xls")):
df = pd.read_excel(BytesIO(content))
elif filename.endswith(".parquet"):
df = pd.read_parquet(BytesIO(content))
elif filename.endswith(".json"):
try:
df = pd.read_json(BytesIO(content))
except ValueError:
df = pd.DataFrame(json.loads(content.decode("utf-8")))
elif filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".jpeg"):
try:
if PIL_AVAILABLE:
image = Image.open(BytesIO(content))
image = image.convert("RGB") # ensure RGB format
df = pd.DataFrame({"image": [image]})
else:
raise HTTPException(400, "PIL not available for image processing")
except Exception as e:
raise HTTPException(400, f"Image processing failed: {str(e)}")
else:
raise HTTPException(400, f"Unsupported data file type: {filename}")
# Pickle for injection
temp_pkl = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False)
temp_pkl.close()
df.to_pickle(temp_pkl.name)
pickle_path = temp_pkl.name
df_preview = (
f"\n\nThe uploaded dataset has {len(df)} rows and {len(df.columns)} columns.\n"
f"Columns: {', '.join(df.columns.astype(str))}\n"
f"First rows:\n{df.head(5).to_markdown(index=False)}\n"
)
# Build rules based on data presence
if dataset_uploaded:
llm_rules = (
"Rules:\n"
"1) You have access to a pandas DataFrame called `df` and its dictionary form `data`.\n"
"2) DO NOT call scrape_url_to_dataframe() or fetch any external data.\n"
"3) Use only the uploaded dataset for answering questions.\n"
"4) Produce a final JSON object with keys:\n"
' - "questions": [ ... original question strings ... ]\n'
' - "code": "..." (Python code that fills `results` with exact question strings as keys)\n'
"5) For plots: use plot_to_base64() helper to return base64 image data under 100kB.\n"
)
else:
llm_rules = (
"Rules:\n"
"1) If you need web data, CALL scrape_url_to_dataframe(url).\n"
"2) Produce a final JSON object with keys:\n"
' - "questions": [ ... original question strings ... ]\n'
' - "code": "..." (Python code that fills `results` with exact question strings as keys)\n'
"3) For plots: use plot_to_base64() helper to return base64 image data under 100kB.\n"
)
llm_input = (
f"{llm_rules}\nQuestions:\n{raw_questions}\n"
f"{df_preview if df_preview else ''}"
"Respond with the JSON object only."
)
# Run agent
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as ex:
fut = ex.submit(run_agent_safely_unified, llm_input, pickle_path)
try:
result = fut.result(timeout=LLM_TIMEOUT_SECONDS)
except concurrent.futures.TimeoutError:
raise HTTPException(408, "Processing timeout")
if "error" in result:
raise HTTPException(500, detail=result["error"])
# Post-process key mapping & type casting
if keys_list and type_map:
mapped = {}
for idx, q in enumerate(result.keys()):
if idx < len(keys_list):
key = keys_list[idx]
caster = type_map.get(key, str)
try:
val = result[q]
if isinstance(val, str) and val.startswith("data:image/"):
# Remove data URI prefix
val = val.split(",", 1)[1] if "," in val else val
mapped[key] = caster(val) if val not in (None, "") else val
except Exception:
mapped[key] = result[q]
result = mapped
return JSONResponse(content=result)
except HTTPException as he:
raise he
except Exception as e:
logger.exception("analyze_data failed")
raise HTTPException(500, detail=str(e))
def run_agent_safely_unified(llm_input: str, pickle_path: str = None) -> Dict:
"""
Runs the LLM agent and executes code.
- Retries up to 3 times if agent returns no output.
- If pickle_path is provided, injects that DataFrame directly.
- If no pickle_path, falls back to scraping when needed.
"""
try:
max_retries = 3
raw_out = ""
for attempt in range(1, max_retries + 1):
response = agent_executor.invoke({"input": llm_input}, {"timeout": LLM_TIMEOUT_SECONDS})
raw_out = response.get("output") or response.get("final_output") or response.get("text") or ""
if raw_out:
break
if not raw_out:
return {"error": f"Agent returned no output after {max_retries} attempts"}
parsed = clean_llm_output(raw_out)
if "error" in parsed:
return parsed
if "code" not in parsed or "questions" not in parsed:
return {"error": f"Invalid agent response: {parsed}"}
code = parsed["code"]
questions = parsed["questions"]
if pickle_path is None:
urls = re.findall(r"scrape_url_to_dataframe\(\s*['\"](.*?)['\"]\s*\)", code)
if urls:
url = urls[0]
tool_resp = scrape_url_to_dataframe(url)
if tool_resp.get("status") != "success":
return {"error": f"Scrape tool failed: {tool_resp.get('message')}"}
df = pd.DataFrame(tool_resp["data"])
temp_pkl = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False)
temp_pkl.close()
df.to_pickle(temp_pkl.name)
pickle_path = temp_pkl.name
exec_result = write_and_run_temp_python(code, injected_pickle=pickle_path, timeout=LLM_TIMEOUT_SECONDS)
if exec_result.get("status") != "success":
return {"error": f"Execution failed: {exec_result.get('message')}", "raw": exec_result.get("raw")}
results_dict = exec_result.get("result", {})
return {q: results_dict.get(q, "Answer not found") for q in questions}
except Exception as e:
logger.exception("run_agent_safely_unified failed")
return {"error": str(e)}
from fastapi.responses import FileResponse, Response
import base64, os
# 1×1 transparent PNG fallback (if favicon.ico file not present)
_FAVICON_FALLBACK_PNG = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO3n+9QAAAAASUVORK5CYII="
)
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
"""
Serve favicon.ico if present in the working directory.
Otherwise return a tiny transparent PNG to avoid 404s.
"""
path = "favicon.ico"
if os.path.exists(path):
return FileResponse(path, media_type="image/x-icon")
return Response(content=_FAVICON_FALLBACK_PNG, media_type="image/png")
@app.get("/api", include_in_schema=False)
async def analyze_get_info():
"""Health/info endpoint. Use POST /api for actual analysis."""
return JSONResponse({
"ok": True,
"message": "Server is running. Use POST /api with 'questions_file' and optional 'data_file'.",
})
# -----------------------------
# System Diagnostics
# -----------------------------
# ---- Add these imports near other imports at top of app.py ----
import asyncio
import httpx
import importlib.metadata
import traceback
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from datetime import datetime, timedelta
import socket
import platform
import psutil
import shutil
import tempfile
import os
import time
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse
# ---- Configuration for diagnostics (tweak as needed) ----
DIAG_NETWORK_TARGETS = {
"Google AI": "https://generativelanguage.googleapis.com",
"AISTUDIO": "https://aistudio.google.com/",
"OpenAI": "https://api.openai.com",
"GitHub": "https://api.github.com",
}
DIAG_LLM_KEY_TIMEOUT = 30 # seconds per key/model simple ping test (sync tests run in threadpool)
DIAG_PARALLELISM = 6 # how many thread workers for sync checks
RUN_LONGER_CHECKS = False # Playwright/duckdb tests run only if true (they can be slow)
# Use existing GEMINI_KEYS / MODEL_HIERARCHY from your app. If not defined, create empty lists.
try:
_GEMINI_KEYS = GEMINI_KEYS
_MODEL_HIERARCHY = MODEL_HIERARCHY
except NameError:
_GEMINI_KEYS = []
_MODEL_HIERARCHY = []
# helper: iso timestamp
def _now_iso():
return datetime.utcnow().isoformat() + "Z"
# helper: run sync func in threadpool and return result / exception info
_executor = ThreadPoolExecutor(max_workers=DIAG_PARALLELISM)
async def run_in_thread(fn, *a, timeout=30, **kw):
loop = asyncio.get_running_loop()
try:
task = loop.run_in_executor(_executor, partial(fn, *a, **kw))
return await asyncio.wait_for(task, timeout=timeout)
except asyncio.TimeoutError:
raise TimeoutError("timeout")
except Exception as e:
# re-raise for caller to capture stacktrace easily
raise
# ---- Diagnostic check functions (safely return dicts) ----
def _env_check(required=None):
required = required or []
out = {}
for k in required:
out[k] = {"present": bool(os.getenv(k)), "masked": (os.getenv(k)[:4] + "..." + os.getenv(k)[-4:]) if os.getenv(k) else None}
# Also include simple helpful values
out["GOOGLE_MODEL"] = os.getenv("GOOGLE_MODEL")
out["LLM_TIMEOUT_SECONDS"] = os.getenv("LLM_TIMEOUT_SECONDS")
return out
def _system_info():
info = {
"host": socket.gethostname(),
"platform": platform.system(),
"platform_release": platform.release(),
"python_version": platform.python_version(),
"cpu_logical_cores": psutil.cpu_count(logical=True),
"memory_total_gb": round(psutil.virtual_memory().total / 1024**3, 2),
}
# disk free for app dir and tmp
try:
_cwd = os.getcwd()
info["cwd_free_gb"] = round(shutil.disk_usage(_cwd).free / 1024**3, 2)
except Exception:
info["cwd_free_gb"] = None
try:
info["tmp_free_gb"] = round(shutil.disk_usage(tempfile.gettempdir()).free / 1024**3, 2)
except Exception:
info["tmp_free_gb"] = None
# GPU quick probe (if torch installed)
try:
import torch
info["torch_installed"] = True
info["cuda_available"] = torch.cuda.is_available()
if torch.cuda.is_available():
info["cuda_device_name"] = torch.cuda.get_device_name(0)
except Exception:
info["torch_installed"] = False
info["cuda_available"] = False
return info
def _temp_write_test():
tmp = tempfile.gettempdir()
path = os.path.join(tmp, f"diag_test_{int(time.time())}.tmp")
with open(path, "w") as f:
f.write("ok")
ok = os.path.exists(path)
os.remove(path)
return {"tmp_dir": tmp, "write_ok": ok}
def _app_write_test():
# try writing into current working directory
cwd = os.getcwd()
path = os.path.join(cwd, f"diag_test_{int(time.time())}.tmp")
with open(path, "w") as f:
f.write("ok")
ok = os.path.exists(path)
os.remove(path)
return {"cwd": cwd, "write_ok": ok}
def _pandas_pipeline_test():
import pandas as _pd
df = _pd.DataFrame({"x":[1,2,3], "y":[4,5,6]})
df["z"] = df["x"] * df["y"]
agg = df["z"].sum()
return {"rows": df.shape[0], "cols": df.shape[1], "z_sum": int(agg)}
def _installed_packages_sample():
# return top 20 installed package names + versions
try:
out = []
for dist in importlib.metadata.distributions():
try:
out.append(f"{dist.metadata['Name']}=={dist.version}")
except Exception:
try:
out.append(f"{dist.metadata['Name']}")
except Exception:
continue
return {"sample_packages": sorted(out)[:20]}
except Exception as e:
return {"error": str(e)}
def _network_probe_sync(url, timeout=30):
# synchronous network probe for threadpool use
try:
r = requests.head(url, timeout=timeout)
return {"ok": True, "status_code": r.status_code, "latency_ms": int(r.elapsed.total_seconds()*1000)}
except Exception as e:
return {"ok": False, "error": str(e)}
# ---- LLM key+model light test (sync) ----
# tries each key for each model with a short per-call timeout (run in threadpool)
def _test_gemini_key_model(key, model, ping_text="ping"):
"""
Test a Gemini API key by sending a minimal request.
Always returns a pure dict with only primitive types.
"""
try:
from langchain_google_genai import ChatGoogleGenerativeAI
except Exception as e:
return {"ok": False, "error": f"langchain_google_genai import error: {e}"}
try:
obj = ChatGoogleGenerativeAI(
model=model,
temperature=0,
google_api_key=key
)
def extract_text(resp):
"""Normalize any type of LLM response into a clean string."""
try:
if resp is None:
return None
if isinstance(resp, str):
return resp
if hasattr(resp, "content") and isinstance(resp.content, str):
return resp.content
if hasattr(resp, "text") and isinstance(resp.text, str):
return resp.text
# For objects with .dict() method
if hasattr(resp, "dict"):
try:
return str(resp.dict())
except Exception:
pass
return str(resp)
except Exception as e:
return f"[unreadable response: {e}]"
# First try invoke()
try:
resp = obj.invoke(ping_text)
text = extract_text(resp)
return {"ok": True, "model": model, "summary": text[:200] if text else None}
except Exception as e_invoke:
# Try __call__()
try:
resp = obj.__call__(ping_text)
text = extract_text(resp)
return {"ok": True, "model": model, "summary": text[:200] if text else None}
except Exception as e_call:
return {"ok": False, "error": f"invoke failed: {e_invoke}; call failed: {e_call}"}
except Exception as e_outer:
return {"ok": False, "error": str(e_outer)}
# ---- Async wrappers that call the sync checks in threadpool ----
async def check_network():
coros = []
for name, url in DIAG_NETWORK_TARGETS.items():
coros.append(run_in_thread(_network_probe_sync, url, timeout=30))
results = await asyncio.gather(*[asyncio.create_task(c) for c in coros], return_exceptions=True)
out = {}
for (name, _), res in zip(DIAG_NETWORK_TARGETS.items(), results):
if isinstance(res, Exception):
out[name] = {"ok": False, "error": str(res)}