Aadityaramrame commited on
Commit
6835e4a
·
verified ·
1 Parent(s): 7ad7c0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -1,20 +1,28 @@
1
- import gradio as gr
2
- from pipeline import extract_text_from_pdf, extract_key_values_with_gemini, FORMS
 
 
3
 
4
- def process(pdf_file, form_type):
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
- iface = gr.Interface(
10
- fn=process,
11
- inputs=[
12
- gr.File(label="Upload Document (PDF only)"),
13
- gr.Dropdown(choices=list(FORMS.keys()), value="pancard_form", label="Form Type")
14
- ],
15
- outputs="json",
16
- title="🧾 AutoFill Form Extractor",
17
- description="Upload a document and extract structured fields using OCR + Gemini LLM"
18
- )
19
 
20
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
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)))