|
|
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_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} |