Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
|
| 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
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
flat_results = []
|
| 38 |
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 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
|
| 75 |
-
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
gr.Markdown("---")
|
| 99 |
gr.Markdown(
|
| 100 |
"""
|
| 101 |
-
π **Privacy Note:** This app processes your
|
| 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 |
)
|