Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
from chain import extract_pdf_with_user, get_final_aswer
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
UPLOAD_DIR = "uploads"
|
10 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
11 |
+
|
12 |
+
@app.post("/extract/")
|
13 |
+
async def extract_pdf(
|
14 |
+
user_id: str = Form(...),
|
15 |
+
name: str = Form(...),
|
16 |
+
uploaded_file: UploadFile = File(...)
|
17 |
+
):
|
18 |
+
if not uploaded_file.filename.endswith(".pdf"):
|
19 |
+
raise HTTPException(status_code=400, detail="Only PDF files are allowed.")
|
20 |
+
|
21 |
+
upload_path = os.path.join(UPLOAD_DIR, uploaded_file.filename)
|
22 |
+
with open(upload_path, "wb") as buffer:
|
23 |
+
shutil.copyfileobj(uploaded_file.file, buffer)
|
24 |
+
|
25 |
+
documents = extract_pdf_with_user(user_id, upload_path, name)
|
26 |
+
return {"message": "PDF processed!", "documents": documents}
|
27 |
+
|
28 |
+
|
29 |
+
@app.post("/answer/")
|
30 |
+
async def get_answer(
|
31 |
+
user_id: str = Form(...),
|
32 |
+
name: str = Form(...),
|
33 |
+
query: str = Form(...)
|
34 |
+
):
|
35 |
+
pdf_path = os.path.join(UPLOAD_DIR, f"{name}.pdf")
|
36 |
+
if not os.path.exists(pdf_path):
|
37 |
+
raise HTTPException(status_code=404, detail=f"No PDF found with name '{name}.pdf'. Please upload and extract first.")
|
38 |
+
|
39 |
+
answer = get_final_aswer(query, user_id, pdf_path)
|
40 |
+
return {"message": "Answer generated!", "answer": answer}
|