Create api.py
Browse files
api.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# api.py
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 3 |
+
from model import load_vectorstore, ask_question
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@app.post("/upload")
|
| 9 |
+
async def upload(file: UploadFile = File(...)):
|
| 10 |
+
with open("document.pdf", "wb") as buffer:
|
| 11 |
+
shutil.copyfileobj(file.file, buffer)
|
| 12 |
+
load_vectorstore("document.pdf")
|
| 13 |
+
return {"message": "File indexed successfully."}
|
| 14 |
+
|
| 15 |
+
@app.post("/ask")
|
| 16 |
+
async def ask(question: str = Form(...)):
|
| 17 |
+
answer = ask_question(question)
|
| 18 |
+
return {"question": question, "answer": answer}
|
| 19 |
+
|