Download main.py from userlele/21thang4: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/userlele/21thang4/resolve/main/main.py
- Command line
-
hf download hf://spaces/userlele/21thang4/main.py
-
curl -L -o main.py https://huggingface.co/spaces/userlele/21thang4/resolve/main/main.py
1 kB
| 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() | |
| } | |
| def home(): | |
| return {"message": "Visit link https://userlele-21thang4.hf.space/docs"} | |
| 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} |