File size: 1,000 Bytes
636abee
5e17616
 
e10985e
5e17616
e10985e
636abee
 
 
 
 
5e17616
636abee
5e17616
9e514c3
 
5e17616
 
636abee
 
cb1d653
636abee
 
 
 
 
 
 
 
9e514c3
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
from fastapi import FastAPI, UploadFile, File, Form, Request
from fastapi.responses import JSONResponse
import tempfile, shutil, os

from llm import load_and_process_pdf, create_vectorstore, create_rag_chain, get_response

app = FastAPI(
    title="PDF Q&A Chatbot",
    description="Ask questions about a PDF file using RAG.",
    version="1.0"
)

# Global in-memory state
global_state = {
    "vectorstore": create_vectorstore(),
    "rag_chain": create_rag_chain()
}

@app.get("/")
def home():
    return {"message": "Visit link https://userlele-21thang4.hf.space/docs"}

@app.get("/ask")
def ask(prompt: str):
    """Ask a question using the uploaded and processed PDF."""
    if not global_state["vectorstore"] or not global_state["rag_chain"]:
        return JSONResponse(status_code=400, content={"error": "Please upload and process a PDF first."})
    
    answer = get_response(global_state["rag_chain"], global_state["vectorstore"], prompt)
    return {"question": prompt, "answer": answer}