File size: 842 Bytes
cfb9fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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)

@api.post("/ask")
async def ask(question: str = Form(...)) -> dict[str, str]:
    answer, context = get_answer_rag(question)
    return {
        "answer": answer,
        "context": context,
    }

@api.post("/upload")
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,
    }