Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,80 @@
|
|
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 |
-
|
|
|
10 |
{
|
|
|
|
|
|
|
|
|
|
|
11 |
"results": [
|
12 |
-
{ ...
|
13 |
-
{ ...
|
14 |
-
...
|
15 |
]
|
16 |
}
|
17 |
"""
|
18 |
if not files:
|
19 |
-
return {"error": "
|
20 |
|
21 |
-
#
|
22 |
if not isinstance(files, list):
|
23 |
files = [files]
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
for f in files:
|
27 |
try:
|
28 |
fp = getattr(f, "name", None) or getattr(f, "path", None)
|
29 |
if not fp:
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
40 |
|
41 |
with gr.Blocks(title="Smart KYC OCR") as demo:
|
42 |
gr.Markdown(
|
43 |
"""
|
44 |
# π§Ύ Smart KYC OCR Tool
|
45 |
-
Upload
|
46 |
-
|
|
|
|
|
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
|
56 |
file_types=[".jpg", ".jpeg", ".png"],
|
57 |
-
file_count="multiple"
|
58 |
)
|
59 |
submit_btn = gr.Button("π Extract KYC Info", variant="primary")
|
60 |
|
61 |
with gr.Column(scale=1):
|
62 |
-
output_json = gr.JSON(label="π
|
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()
|