from fastapi import FastAPI, UploadFile, File import tempfile from script.ingest import ingest_pdf from script.graph import build_graph app = FastAPI() graph = build_graph() vectorstore = None @app.post("/upload") async def upload_pdf(file: UploadFile = File(...)): global vectorstore with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(await file.read()) tmp_path = tmp.name vectorstore = ingest_pdf(tmp_path) return {"status": "PDF ingested"} @app.post("/ask") async def ask(question: str): if vectorstore is None: return {"error": "Upload a PDF first"} result = graph.invoke({ "question": question, "vectorstore": vectorstore }) return {"answer": result["answer"]}