gopichandra commited on
Commit
20e1d9b
Β·
verified Β·
1 Parent(s): 30c1744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -68
app.py CHANGED
@@ -4,77 +4,42 @@ os.environ["OMP_NUM_THREADS"] = "1"
4
  import gradio as gr
5
  from utils import extract_kyc_fields
6
 
7
- def process_documents(files):
 
8
  """
9
- Upload BOTH Aadhaar and PAN together.
10
- Returns:
11
- {
12
- "summary": {
13
- "aadhaar": [ {...}, ... ],
14
- "pan": [ {...}, ... ],
15
- "unknown": [ {...}, ... ]
16
- },
17
- "results": [
18
- { ... , "source_file": "file1.png" },
19
- { ... , "source_file": "file2.jpg" }
20
- ]
21
- }
22
  """
23
- if not files:
24
- return {"error": "Please upload both files (Aadhaar and PAN)."}
25
-
26
- # Gradio may pass a single file or list depending on version
27
- if not isinstance(files, list):
28
- files = [files]
29
 
30
- if len(files) < 2:
31
- # soft validation; still process but nudge user
32
- warning = "Tip: You uploaded fewer than 2 files. Upload both Aadhaar and PAN for best results."
33
- else:
34
- warning = None
35
-
36
- grouped = {"aadhaar": [], "pan": [], "unknown": []}
37
- flat_results = []
38
 
39
- for f in files:
40
  try:
41
- fp = getattr(f, "name", None) or getattr(f, "path", None)
42
- if not fp:
43
- res = {"error": "Could not read uploaded file path."}
44
- else:
45
- res = extract_kyc_fields(fp)
46
 
47
- res["source_file"] = os.path.basename(fp) if fp else "uploaded_file"
48
- flat_results.append(res)
49
 
50
- ct = (res.get("card_type") or "UNKNOWN").upper()
51
- if ct == "AADHAAR":
52
- grouped["aadhaar"].append(res)
53
- elif ct == "PAN":
54
- grouped["pan"].append(res)
55
- else:
56
- grouped["unknown"].append(res)
57
 
58
- except Exception as e:
59
- err = {"error": f"OCR failed for {getattr(f, 'name', 'file')}: {str(e)}",
60
- "card_type": "UNKNOWN",
61
- "source_file": getattr(f, "name", "file")}
62
- flat_results.append(err)
63
- grouped["unknown"].append(err)
64
-
65
- payload = {"summary": grouped, "results": flat_results}
66
- if warning:
67
- payload["note"] = warning
68
- return payload
69
 
70
  with gr.Blocks(title="Smart KYC OCR") as demo:
71
  gr.Markdown(
72
  """
73
  # 🧾 Smart KYC OCR Tool
74
- Upload **both**: an **Aadhaar** image and a **PAN** image (you can also upload more).
75
- The app will group the output by card type and show all results.
76
-
77
- **How to upload both:** hold **Ctrl/⌘** to select two files, or drag & drop both images together.
78
 
79
  ---
80
  """
@@ -82,23 +47,29 @@ with gr.Blocks(title="Smart KYC OCR") as demo:
82
 
83
  with gr.Row():
84
  with gr.Column(scale=1):
85
- # Works with older Gradio: file_count="multiple"
86
- uploader = gr.File(
87
- label="πŸ“€ Upload Aadhaar & PAN Image(s)",
88
- file_types=[".jpg", ".jpeg", ".png"],
89
- file_count="multiple"
90
  )
91
- submit_btn = gr.Button("πŸ” Extract KYC Info", variant="primary")
92
-
93
  with gr.Column(scale=1):
94
- output_json = gr.JSON(label="πŸ“‹ Grouped Output (aadhaar / pan) + All Results")
 
 
 
 
 
 
95
 
96
- submit_btn.click(fn=process_documents, inputs=uploader, outputs=output_json)
 
 
 
 
97
 
98
  gr.Markdown("---")
99
  gr.Markdown(
100
  """
101
- πŸ”’ **Privacy Note:** This app processes your document locally in the cloud.
102
  No data is stored or shared.
103
  """
104
  )
 
4
  import gradio as gr
5
  from utils import extract_kyc_fields
6
 
7
+
8
+ def process_documents(aadhaar_file, pan_file):
9
  """
10
+ Takes one Aadhaar file and one PAN file,
11
+ extracts KYC fields, and returns combined JSON.
 
 
 
 
 
 
 
 
 
 
 
12
  """
13
+ results = {"aadhaar": None, "pan": None}
 
 
 
 
 
14
 
15
+ if aadhaar_file:
16
+ try:
17
+ res = extract_kyc_fields(aadhaar_file.name)
18
+ res["source_file"] = os.path.basename(aadhaar_file.name)
19
+ results["aadhaar"] = res
20
+ except Exception as e:
21
+ results["aadhaar"] = {"error": f"Aadhaar OCR failed: {str(e)}"}
 
22
 
23
+ if pan_file:
24
  try:
25
+ res = extract_kyc_fields(pan_file.name)
26
+ res["source_file"] = os.path.basename(pan_file.name)
27
+ results["pan"] = res
28
+ except Exception as e:
29
+ results["pan"] = {"error": f"PAN OCR failed: {str(e)}"}
30
 
31
+ if not aadhaar_file and not pan_file:
32
+ return {"error": "Please upload both Aadhaar and PAN files."}
33
 
34
+ return results
 
 
 
 
 
 
35
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  with gr.Blocks(title="Smart KYC OCR") as demo:
38
  gr.Markdown(
39
  """
40
  # 🧾 Smart KYC OCR Tool
41
+ Upload an **Aadhaar card** and a **PAN card** separately.
42
+ Click **Extract KYC Info** to get structured output.
 
 
43
 
44
  ---
45
  """
 
47
 
48
  with gr.Row():
49
  with gr.Column(scale=1):
50
+ aadhaar_uploader = gr.File(
51
+ label="πŸ“€ Aadhaar Upload",
52
+ file_types=[".jpg", ".jpeg", ".png"]
 
 
53
  )
 
 
54
  with gr.Column(scale=1):
55
+ pan_uploader = gr.File(
56
+ label="πŸ“€ PAN Upload",
57
+ file_types=[".jpg", ".jpeg", ".png"]
58
+ )
59
+
60
+ submit_btn = gr.Button("πŸ” Extract KYC Info", variant="primary")
61
+ output_json = gr.JSON(label="πŸ“‹ Extracted KYC Fields")
62
 
63
+ submit_btn.click(
64
+ fn=process_documents,
65
+ inputs=[aadhaar_uploader, pan_uploader],
66
+ outputs=output_json,
67
+ )
68
 
69
  gr.Markdown("---")
70
  gr.Markdown(
71
  """
72
+ πŸ”’ **Privacy Note:** This app processes your documents locally in the cloud.
73
  No data is stored or shared.
74
  """
75
  )