Aadityaramrame commited on
Commit
9e23cf3
·
verified ·
1 Parent(s): 575d5aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -24
app.py CHANGED
@@ -1,10 +1,6 @@
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",
@@ -12,24 +8,16 @@ FIELDS = [
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)))
 
1
+ import gradio as gr
 
 
2
  from pipeline import extract_text_from_pdf, extract_key_values_with_gemini
3
 
 
 
4
  FIELDS = [
5
  "Name", "DOB", "Gender", "FatherName", "MotherName",
6
  "Address", "City", "State", "Pincode", "Mobile", "Email",
 
8
  "IssueDate", "ExpiryDate"
9
  ]
10
 
11
+ def process_pdf(file):
12
+ if not file:
13
+ return {"error": "No file uploaded."}
14
+ text = extract_text_from_pdf(file.name)
15
+ return extract_key_values_with_gemini(text, FIELDS)
 
 
 
 
 
 
 
 
 
16
 
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("## AutoForm Extractor")
19
+ file_input = gr.File(label="Upload PDF")
20
+ output = gr.JSON()
21
+ file_input.change(process_pdf, file_input, output)
22
 
23
+ demo.launch()