Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (52 loc) · 1.88 KB
/
Copy pathapp.py
File metadata and controls
62 lines (52 loc) · 1.88 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
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import json
import pandas as pd
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import uvicorn
from graph.workflow import ask
app = FastAPI()
class Query(BaseModel):
question: str
@app.post("/ask")
async def ask_question(query: Query):
try:
result = ask(query.question)
answer = result.get("answer", "")
sql = result.get("sql", "")
viz = result.get("viz", {})
rows = result.get("rows", [])
trace = []
if sql:
trace.append("✅ SQL Agent — queried election database")
if "Wikipedia" in answer:
trace.append("✅ Wikipedia Agent — fetched background")
if "news" in answer.lower():
trace.append("✅ News Agent — fetched recent news")
if "Budget" in answer or "President" in answer:
trace.append("✅ RAG Agent — searched speeches")
if viz.get("should_visualize"):
trace.append(f"✅ Visualization Agent — {viz.get('chart_type', '')} chart")
table = ""
if rows:
df = pd.DataFrame(rows)
table = df.to_html(index=False, classes="data-table", border=0)
chart_json = viz.get("chart_json", "") if viz.get("should_visualize") else ""
return JSONResponse({
"answer": answer,
"sql": sql,
"trace": "\n".join(trace),
"table": table,
"chart_json": chart_json
})
except Exception as e:
return JSONResponse({"error": str(e)}, status_code=500)
@app.get("/", response_class=HTMLResponse)
async def index():
return HTMLResponse(open("ui/index.html").read())
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)