File size: 763 Bytes
cbc325b
66c529e
cbc325b
b7a20f0
 
cbc325b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"]}