Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
FIELDS = [
|
| 9 |
+
"Name", "DOB", "Gender", "FatherName", "MotherName",
|
| 10 |
+
"Address", "City", "State", "Pincode", "Mobile", "Email",
|
| 11 |
+
"DocumentType", "DocumentNumber", "IssueAuthority",
|
| 12 |
+
"IssueDate", "ExpiryDate"
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
@app.post("/extract")
|
| 16 |
+
async def extract_api(file: UploadFile):
|
| 17 |
+
try:
|
| 18 |
+
# Save uploaded file temporarily
|
| 19 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 20 |
+
tmp.write(await file.read())
|
| 21 |
+
tmp_path = tmp.name
|
| 22 |
+
|
| 23 |
+
text = extract_text_from_pdf(tmp_path)
|
| 24 |
+
result = extract_key_values_with_gemini(text, fields=FIELDS)
|
| 25 |
+
|
| 26 |
+
return JSONResponse(content={"success": True, "data": result})
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return JSONResponse(content={"success": False, "error": str(e)})
|
| 29 |
+
|
| 30 |
+
@app.get("/")
|
| 31 |
+
def home():
|
| 32 |
+
return {"message": "AutoFill Form API is running! Use POST /extract with a PDF."}
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|