Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 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
|
|
@@ -6,6 +6,7 @@ from pdf_utils import extract_text_from_pdfs, split_text
|
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from datetime import datetime, timedelta
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
|
@@ -36,5 +37,21 @@ async def upload_pdfs(background_tasks: BackgroundTasks, files: list[UploadFile]
|
|
| 36 |
|
| 37 |
@app.post("/query/")
|
| 38 |
async def query_pdf(session_id: str = Form(...), question: str = Form(...)):
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks, HTTPException
|
| 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
|
|
|
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
+
from pinecone.openapi_support.exceptions import NotFoundException
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
| 37 |
|
| 38 |
@app.post("/query/")
|
| 39 |
async def query_pdf(session_id: str = Form(...), question: str = Form(...)):
|
| 40 |
+
try:
|
| 41 |
+
response = query_vector_store(session_id, question)
|
| 42 |
+
return JSONResponse(content={"answer": response})
|
| 43 |
+
except NotFoundException:
|
| 44 |
+
raise HTTPException(status_code=404, detail=f"Session '{session_id}' does not exist or has expired.")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.post("/clear/")
|
| 50 |
+
async def clear_session(session_id: str = Form(...)):
|
| 51 |
+
try:
|
| 52 |
+
delete_vector_store(session_id)
|
| 53 |
+
return {"message": f"Session '{session_id}' has been cleared."}
|
| 54 |
+
except NotFoundException:
|
| 55 |
+
raise HTTPException(status_code=404, detail=f"Session '{session_id}' does not exist.")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise HTTPException(status_code=500, detail=str(e))
|