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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -21
app.py CHANGED
@@ -1,49 +1,80 @@
1
  import os
2
- os.environ["OMP_NUM_THREADS"] = "1" # Avoid multithread warning from some BLAS libs
3
 
4
  import gradio as gr
5
  from utils import extract_kyc_fields
6
 
7
  def process_documents(files):
8
  """
9
- Accepts multiple files, OCRs each, and returns a single JSON payload:
 
10
  {
 
 
 
 
 
11
  "results": [
12
- { ...result for file 1..., "source_file": "name1.png" },
13
- { ...result for file 2..., "source_file": "name2.jpg" },
14
- ...
15
  ]
16
  }
17
  """
18
  if not files:
19
- return {"error": "No file(s) uploaded."}
20
 
21
- # gr.File may return a single object or a list depending on Gradio version.
22
  if not isinstance(files, list):
23
  files = [files]
24
 
25
- results = []
 
 
 
 
 
 
 
 
26
  for f in files:
27
  try:
28
  fp = getattr(f, "name", None) or getattr(f, "path", None)
29
  if not fp:
30
- results.append({"error": "Could not read uploaded file path."})
31
- continue
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- res = extract_kyc_fields(fp)
34
- res["source_file"] = os.path.basename(fp)
35
- results.append(res)
36
  except Exception as e:
37
- results.append({"error": f"OCR failed for {getattr(f, 'name', 'file')}: {str(e)}"})
 
 
 
 
38
 
39
- return {"results": results}
 
 
 
40
 
41
  with gr.Blocks(title="Smart KYC OCR") as demo:
42
  gr.Markdown(
43
  """
44
  # 🧾 Smart KYC OCR Tool
45
- Upload images of **Aadhaar** and/or **PAN** cards β€” multiple at once β€” and get structured KYC data instantly.
46
- Powered by **PaddleOCR** and smart field detection.
 
 
47
 
48
  ---
49
  """
@@ -51,15 +82,16 @@ with gr.Blocks(title="Smart KYC OCR") as demo:
51
 
52
  with gr.Row():
53
  with gr.Column(scale=1):
 
54
  uploader = gr.File(
55
- label="πŸ“€ Upload Aadhaar / PAN Image(s)",
56
  file_types=[".jpg", ".jpeg", ".png"],
57
- file_count="multiple", # works on older Gradio; for newer, you can use gr.Files
58
  )
59
  submit_btn = gr.Button("πŸ” Extract KYC Info", variant="primary")
60
 
61
  with gr.Column(scale=1):
62
- output_json = gr.JSON(label="πŸ“‹ Extracted KYC Fields (All Files)")
63
 
64
  submit_btn.click(fn=process_documents, inputs=uploader, outputs=output_json)
65
 
@@ -72,5 +104,4 @@ with gr.Blocks(title="Smart KYC OCR") as demo:
72
  )
73
 
74
  if __name__ == "__main__":
75
- # Change host/port if you need to expose it elsewhere
76
  demo.launch()
 
1
  import os
2
+ os.environ["OMP_NUM_THREADS"] = "1"
3
 
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
 
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
 
 
104
  )
105
 
106
  if __name__ == "__main__":
 
107
  demo.launch()