File size: 1,204 Bytes
a77318b
 
a3f9e7d
a77318b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3f9e7d
 
 
 
a77318b
 
a3f9e7d
 
a77318b
 
 
 
a3f9e7d
a77318b
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
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=["*"],
)

@app.post("/parse-rfq")
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))

@app.get("/health")
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)