Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from uuid import uuid4
|
| 4 |
+
from vector_handler import create_vector_store, delete_vector_store, query_vector_store
|
| 5 |
+
from pdf_utils import extract_text_from_pdfs, split_text
|
| 6 |
+
from chat_utils import get_chain
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.post("/upload/")
|
| 16 |
+
async def upload_pdfs(background_tasks: BackgroundTasks, files: list[UploadFile] = File(...)):
|
| 17 |
+
session_id = str(uuid4())
|
| 18 |
+
raw_text = await extract_text_from_pdfs(files)
|
| 19 |
+
chunks = split_text(raw_text)
|
| 20 |
+
|
| 21 |
+
create_vector_store(session_id, chunks)
|
| 22 |
+
|
| 23 |
+
# Schedule deletion after 15 minutes
|
| 24 |
+
background_tasks.add_task(delete_vector_store, session_id, delay=900)
|
| 25 |
+
|
| 26 |
+
return {"session_id": session_id, "message": "PDF uploaded and processed."}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@app.post("/query/")
|
| 30 |
+
async def query_pdf(session_id: str = Form(...), question: str = Form(...)):
|
| 31 |
+
response = query_vector_store(session_id, question)
|
| 32 |
+
return JSONResponse(content={"answer": response})
|