Spaces:
Building
Building
| import os | |
| from dotenv import load_dotenv | |
| from fastapi import FastAPI, UploadFile, File, HTTPException, Form | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import uvicorn | |
| from rfq_parser import parse_rfq_pdf | |
| load_dotenv() | |
| app = FastAPI(title="Dynamic RFQ Parser") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def parse_rfq( | |
| file: UploadFile = File(...), | |
| use_gemini: bool = Form(True), | |
| ): | |
| if not file.filename.endswith(".pdf"): | |
| raise HTTPException(status_code=400, detail="Only PDF files are supported") | |
| if use_gemini and not os.getenv("GOOGLE_API_KEY"): | |
| raise HTTPException(status_code=500, detail="GOOGLE_API_KEY not configured") | |
| contents = await file.read() | |
| try: | |
| result = parse_rfq_pdf(contents, use_gemini=use_gemini) | |
| return result | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def health(): | |
| return {"status": "ok"} | |
| if __name__ == "__main__": | |
| port = int(os.getenv("PORT", "7860")) | |
| uvicorn.run("main:app", host="0.0.0.0", port=port) |