Spaces:
Running
Running
| # api.py | |
| from rag import get_answer_rag, add_pdf_to_vectorstore | |
| from pathlib import Path | |
| import shutil | |
| from fastapi import FastAPI, Form, UploadFile, File | |
| api = FastAPI() | |
| UPLOAD_DIR = Path("uploads") | |
| UPLOAD_DIR.mkdir(exist_ok=True) | |
| async def ask(question: str = Form(...)) -> dict[str, str]: | |
| answer, context = get_answer_rag(question) | |
| return { | |
| "answer": answer, | |
| "context": context, | |
| } | |
| async def upload_pdf(file: UploadFile = File(...)) -> dict[str, str]: | |
| pdf_path = UPLOAD_DIR / file.filename | |
| with pdf_path.open("wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| add_pdf_to_vectorstore(str(pdf_path)) | |
| return { | |
| "message": "PDF μ λ‘λ λ° λ²‘ν° DB μμ±μ΄ μλ£λμμ΅λλ€.", | |
| "filename": file.filename, | |
| } |