Spaces:
Sleeping
Sleeping
| 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 | |
| def root(): | |
| return FileResponse("../frontend/index.html") | |
| 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"} | |
| 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)} |