-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
55 lines (32 loc) · 1.08 KB
/
Copy pathapi.py
File metadata and controls
55 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import HTMLResponse
from typing import List
import shutil
import os
from rag_pipeline import load_pdf, create_vector_store, ask_question
app = FastAPI()
vector_db = None
@app.get("/", response_class=HTMLResponse)
def home():
with open("index.html") as f:
return f.read()
@app.post("/upload")
async def upload_pdf(files: List[UploadFile] = File(...)):
global vector_db
os.makedirs("uploads", exist_ok=True)
all_docs = []
for file in files:
file_path = f"uploads/{file.filename}"
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
docs = load_pdf(file_path)
all_docs.extend(docs)
vector_db = create_vector_store(all_docs)
return {"message": f"{len(files)} PDF(s) uploaded successfully"}
@app.get("/ask")
def ask(question: str):
global vector_db
if vector_db is None:
return {"answer": "Please upload a PDF first."}
answer = ask_question(vector_db, question)
return {"answer": answer}