Kailashalgo's picture
Update backend/main.py
29e87bf verified
Raw
History Blame Contribute Delete
1.31 kB
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
import shutil
import os
import traceback
from ingest import ingest_pdf
from retriever import get_answer
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
os.makedirs("../docs", exist_ok=True)
os.makedirs("chroma_db", exist_ok=True)
app.mount("/static", StaticFiles(directory="../frontend"), name="static")
class Question(BaseModel):
question: str
@app.get("/")
def root():
return FileResponse("../frontend/index.html")
@app.post("/upload")
async def upload_pdf(file: UploadFile = File(...)):
file_path = f"../docs/{file.filename}"
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
ingest_pdf(file_path)
return {"message": f"✅ {file.filename} ingested successfully"}
@app.post("/ask")
def ask_question(body: Question):
try:
answer = get_answer(body.question)
return {"answer": answer}
except Exception as e:
print("ERROR:", traceback.format_exc())
return {"error": str(e)}