Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,133 +1,96 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from google.genai import types
|
| 12 |
-
|
| 13 |
-
# -----------------------------
|
| 14 |
-
# FastAPI setup
|
| 15 |
-
# -----------------------------
|
| 16 |
-
app = FastAPI()
|
| 17 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 18 |
-
templates = Jinja2Templates(directory="templates")
|
| 19 |
-
|
| 20 |
-
# -----------------------------
|
| 21 |
-
# Gemini client setup
|
| 22 |
-
# -----------------------------
|
| 23 |
-
client = genai.Client(api_key="AIzaSyB1jgGCuzg7ELPwNEEwaluQZoZhxhgLmAs")
|
| 24 |
-
|
| 25 |
-
UPLOAD_DIR = "tmp/uploads"
|
| 26 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def
|
| 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 |
-
file_path = os.path.join(UPLOAD_DIR, f"{file_id}{file_ext}")
|
| 113 |
-
|
| 114 |
-
with open(file_path, "wb") as f:
|
| 115 |
-
f.write(await file.read())
|
| 116 |
-
|
| 117 |
-
df = pd.read_excel(file_path)
|
| 118 |
-
metadata = get_metadata(df)
|
| 119 |
-
analysis = generate_metadata_analysis(metadata)
|
| 120 |
-
|
| 121 |
-
# Store session info temporarily
|
| 122 |
-
session_data = {
|
| 123 |
-
"file_path": file_path,
|
| 124 |
-
"metadata": metadata,
|
| 125 |
-
"analysis": analysis
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
return JSONResponse(session_data)
|
| 129 |
-
|
| 130 |
-
@app.post("/generate_plot", response_class=JSONResponse)
|
| 131 |
-
async def generate_plot(command: str = Form(...), file_path: str = Form(...)):
|
| 132 |
-
visualization_json = generate_visualization(command, file_path)
|
| 133 |
-
return JSONResponse(visualization_json)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import uuid, os, asyncio, time
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
import pathlib
|
| 6 |
+
|
| 7 |
+
# import functions from pipeline file (unchanged)
|
| 8 |
+
from pipeline_with_agents import process_file
|
| 9 |
+
|
| 10 |
+
UPLOAD_DIR = "/tmp/uploads"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 12 |
|
| 13 |
+
app = FastAPI(title="AI Pipeline Service")
|
| 14 |
+
|
| 15 |
+
# in-memory job store (simple). Replace with DB for production.
|
| 16 |
+
JOBS: Dict[str, Dict[str, Any]] = {}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@app.get("/health")
|
| 20 |
+
async def health():
|
| 21 |
+
return {"status": "ok", "time": int(time.time())}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@app.post("/api/files/upload")
|
| 25 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 26 |
+
filename = pathlib.Path(file.filename).name
|
| 27 |
+
ext = pathlib.Path(filename).suffix.lower()
|
| 28 |
+
if ext not in [".csv", ".xls", ".xlsx", ".txt"]:
|
| 29 |
+
raise HTTPException(status_code=400, detail="Unsupported file type")
|
| 30 |
+
file_id = f"{uuid.uuid4()}{ext}"
|
| 31 |
+
path = os.path.join(UPLOAD_DIR, file_id)
|
| 32 |
+
contents = await file.read()
|
| 33 |
+
with open(path, "wb") as f:
|
| 34 |
+
f.write(contents)
|
| 35 |
+
return {"file_id": path, "filename": filename, "size": len(contents)}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@app.post("/api/jobs")
|
| 39 |
+
async def create_job(file_id: str = Form(...), sheet: str = Form(None), model: str = Form("gemini-2.5-flash-lite"), wait: bool = Form(False)):
|
| 40 |
+
if not os.path.exists(file_id):
|
| 41 |
+
raise HTTPException(status_code=400, detail="file_id not found on server")
|
| 42 |
+
job_id = str(uuid.uuid4())
|
| 43 |
+
JOBS[job_id] = {"status": "queued", "file_id": file_id, "result": None, "created_at": int(time.time())}
|
| 44 |
+
|
| 45 |
+
if wait:
|
| 46 |
+
# synchronous run, return result
|
| 47 |
+
result = process_file(file_id, sheet, model)
|
| 48 |
+
JOBS[job_id]["status"] = "finished"
|
| 49 |
+
JOBS[job_id]["result"] = result
|
| 50 |
+
JOBS[job_id]["finished_at"] = int(time.time())
|
| 51 |
+
return {"job_id": job_id, "status": "finished", "result": result}
|
| 52 |
+
|
| 53 |
+
# asynchronous run
|
| 54 |
+
loop = asyncio.get_running_loop()
|
| 55 |
+
|
| 56 |
+
async def run_and_store():
|
| 57 |
+
JOBS[job_id]["status"] = "running"
|
| 58 |
+
try:
|
| 59 |
+
result = await loop.run_in_executor(None, process_file, file_id, sheet, model)
|
| 60 |
+
JOBS[job_id]["result"] = result
|
| 61 |
+
JOBS[job_id]["status"] = "finished"
|
| 62 |
+
JOBS[job_id]["finished_at"] = int(time.time())
|
| 63 |
+
except Exception as e:
|
| 64 |
+
JOBS[job_id]["status"] = "failed"
|
| 65 |
+
JOBS[job_id]["error"] = str(e)
|
| 66 |
+
|
| 67 |
+
asyncio.create_task(run_and_store())
|
| 68 |
+
return {"job_id": job_id, "status": "queued"}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
@app.get("/api/jobs/{job_id}")
|
| 72 |
+
async def get_job(job_id: str):
|
| 73 |
+
job = JOBS.get(job_id)
|
| 74 |
+
if not job:
|
| 75 |
+
raise HTTPException(status_code=404, detail="job_id not found")
|
| 76 |
+
# return snapshot (may include result if ready)
|
| 77 |
+
return JSONResponse(content=job)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@app.get("/api/jobs/{job_id}/charts")
|
| 81 |
+
async def get_job_charts(job_id: str):
|
| 82 |
+
job = JOBS.get(job_id)
|
| 83 |
+
if not job:
|
| 84 |
+
raise HTTPException(status_code=404, detail="job_id not found")
|
| 85 |
+
result = job.get("result")
|
| 86 |
+
if not result:
|
| 87 |
+
return {"status": job["status"], "charts": None}
|
| 88 |
+
return {"status": job["status"], "charts": result.get("charts")}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
@app.get("/api/jobs")
|
| 92 |
+
async def list_jobs():
|
| 93 |
+
out = []
|
| 94 |
+
for jid, info in JOBS.items():
|
| 95 |
+
out.append({"job_id": jid, "status": info.get("status"), "file_id": info.get("file_id"), "created_at": info.get("created_at")})
|
| 96 |
+
return {"jobs": out}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|