File size: 1,062 Bytes
6835e4a
 
 
 
90e84d2
6835e4a
90e84d2
6835e4a
 
 
 
 
 
 
90e84d2
6835e4a
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, Form
from fastapi.responses import JSONResponse
import uvicorn, os, tempfile
from pipeline import extract_text_from_pdf, extract_key_values_with_gemini

app = FastAPI(title="AutoFill Form Extractor API")

@app.post("/extract")
async def extract_api(file: UploadFile, form_type: str = Form("pancard_form")):
    try:
        # Save uploaded file temporarily
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
            tmp.write(await file.read())
            tmp_path = tmp.name

        text = extract_text_from_pdf(tmp_path)
        result = extract_key_values_with_gemini(text, form_type=form_type)

        return JSONResponse(content={"success": True, "data": result})
    except Exception as e:
        return JSONResponse(content={"success": False, "error": str(e)})

@app.get("/")
def home():
    return {"message": "AutoFill Form API is running! Use POST /extract with a PDF."}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))