Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,28 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
text = extract_text_from_pdf(pdf_file.name)
|
| 6 |
-
result = extract_key_values_with_gemini(text, form_type=form_type)
|
| 7 |
-
return result
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
title="🧾 AutoFill Form Extractor",
|
| 17 |
-
description="Upload a document and extract structured fields using OCR + Gemini LLM"
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, Form
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import uvicorn, os, tempfile
|
| 4 |
+
from pipeline import extract_text_from_pdf, extract_key_values_with_gemini
|
| 5 |
|
| 6 |
+
app = FastAPI(title="AutoFill Form Extractor API")
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
@app.post("/extract")
|
| 9 |
+
async def extract_api(file: UploadFile, form_type: str = Form("pancard_form")):
|
| 10 |
+
try:
|
| 11 |
+
# Save uploaded file temporarily
|
| 12 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 13 |
+
tmp.write(await file.read())
|
| 14 |
+
tmp_path = tmp.name
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
text = extract_text_from_pdf(tmp_path)
|
| 17 |
+
result = extract_key_values_with_gemini(text, form_type=form_type)
|
| 18 |
+
|
| 19 |
+
return JSONResponse(content={"success": True, "data": result})
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return JSONResponse(content={"success": False, "error": str(e)})
|
| 22 |
+
|
| 23 |
+
@app.get("/")
|
| 24 |
+
def home():
|
| 25 |
+
return {"message": "AutoFill Form API is running! Use POST /extract with a PDF."}
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|