Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,24 +6,16 @@ import json
|
|
| 6 |
import datetime as dt
|
| 7 |
import gradio as gr
|
| 8 |
from utils import extract_kyc_fields
|
|
|
|
| 9 |
|
| 10 |
-
# ------------------
|
| 11 |
SF_USERNAME = "licproject@2025.com"
|
| 12 |
SF_PASSWORD = "Lic@2025"
|
| 13 |
SF_SECURITY_TOKEN = "AmmfRcd6IiYaRtSGntBnzNMQU"
|
| 14 |
-
SF_DOMAIN = "login"
|
| 15 |
-
# -------------------------------------------------------
|
| 16 |
-
|
| 17 |
-
# simple-salesforce 1.11.6
|
| 18 |
-
from simple_salesforce import Salesforce
|
| 19 |
-
|
| 20 |
|
| 21 |
-
# ---------- helpers ----------
|
| 22 |
def _parse_birthdate(dob_text: str):
|
| 23 |
-
"""
|
| 24 |
-
Normalize DOB to YYYY-MM-DD for Salesforce Date fields.
|
| 25 |
-
Supports dd/mm/yyyy, dd-mm-yyyy, dd.mm.yyyy, yyyy-mm-dd, or just YYYY (mapped to mid-year).
|
| 26 |
-
"""
|
| 27 |
if not dob_text or dob_text == "Not found":
|
| 28 |
return None
|
| 29 |
s = dob_text.strip()
|
|
@@ -54,30 +46,17 @@ def _parse_birthdate(dob_text: str):
|
|
| 54 |
|
| 55 |
return None
|
| 56 |
|
| 57 |
-
|
| 58 |
def sf_login():
|
| 59 |
-
"""
|
| 60 |
-
Login with simple-salesforce (v1.11.6). If creds are invalid or API is blocked,
|
| 61 |
-
Salesforce will raise here. We'll surface the raw error to the UI.
|
| 62 |
-
"""
|
| 63 |
sf = Salesforce(
|
| 64 |
username=SF_USERNAME,
|
| 65 |
password=SF_PASSWORD,
|
| 66 |
security_token=SF_SECURITY_TOKEN,
|
| 67 |
domain=SF_DOMAIN,
|
| 68 |
)
|
| 69 |
-
# Lightweight check so bad auth is surfaced immediately
|
| 70 |
sf.query("SELECT Id FROM User LIMIT 1")
|
| 71 |
return sf
|
| 72 |
|
| 73 |
-
|
| 74 |
def sf_create_kyc_via_session(sf, payload: dict):
|
| 75 |
-
"""
|
| 76 |
-
Use the same authenticated session from simple-salesforce to POST directly
|
| 77 |
-
to the REST sObject endpoint. This avoids the SDK's create() wrapper and
|
| 78 |
-
the 'SalesforceError.__init__() missing ...' TypeError you've been seeing.
|
| 79 |
-
"""
|
| 80 |
-
# In simple-salesforce 1.11.6, base_url ends with `/services/data/vXX.X/`
|
| 81 |
url = f"{sf.base_url}sobjects/KYC_Record__c"
|
| 82 |
resp = sf.session.post(url, json=payload, headers=sf.headers, timeout=30)
|
| 83 |
|
|
@@ -88,15 +67,8 @@ def sf_create_kyc_via_session(sf, payload: dict):
|
|
| 88 |
|
| 89 |
if 200 <= resp.status_code < 300:
|
| 90 |
rec_id = body.get("id") if isinstance(body, dict) else None
|
| 91 |
-
return {
|
| 92 |
-
|
| 93 |
-
"id": rec_id,
|
| 94 |
-
"status_code": resp.status_code,
|
| 95 |
-
"url": url,
|
| 96 |
-
"response_json": body
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
# Return *raw* details (no SDK exception wrapper involved)
|
| 100 |
return {
|
| 101 |
"success": False,
|
| 102 |
"status_code": resp.status_code,
|
|
@@ -105,14 +77,7 @@ def sf_create_kyc_via_session(sf, payload: dict):
|
|
| 105 |
"response_text": resp.text,
|
| 106 |
}
|
| 107 |
|
| 108 |
-
|
| 109 |
def build_payload(ocr_results: dict):
|
| 110 |
-
"""
|
| 111 |
-
Map OCR outputs to your Salesforce fields on KYC_Record__c.
|
| 112 |
-
Fields:
|
| 113 |
-
Aadhaar_Number__c, Aadhaar_Name__c, Aadhaar_DOB__c (Date)
|
| 114 |
-
PAN_Number__c, Pan_Name__c, Pan_DOB__c (Date)
|
| 115 |
-
"""
|
| 116 |
a = (ocr_results.get("aadhaar") or {})
|
| 117 |
p = (ocr_results.get("pan") or {})
|
| 118 |
|
|
@@ -124,21 +89,14 @@ def build_payload(ocr_results: dict):
|
|
| 124 |
"Pan_Name__c": p.get("name") if p.get("card_type") == "PAN" else None,
|
| 125 |
"Pan_DOB__c": _parse_birthdate(p.get("dob")) if p.get("card_type") == "PAN" else None,
|
| 126 |
}
|
| 127 |
-
# Drop Nones so we don't try to set blanks on required fields
|
| 128 |
return {k: v for k, v in payload.items() if v is not None}
|
| 129 |
|
| 130 |
-
|
| 131 |
-
# ---------- Gradio callback ----------
|
| 132 |
def process_documents(aadhaar_file, pan_file, push_to_sf):
|
| 133 |
-
"""
|
| 134 |
-
OCR both uploads; optionally push one KYC_Record__c via simple-salesforce session POST.
|
| 135 |
-
"""
|
| 136 |
results = {"aadhaar": None, "pan": None}
|
| 137 |
|
| 138 |
if not aadhaar_file and not pan_file:
|
| 139 |
return {"error": "Please upload at least one file (Aadhaar and/or PAN)."}
|
| 140 |
|
| 141 |
-
# OCR Aadhaar
|
| 142 |
if aadhaar_file:
|
| 143 |
try:
|
| 144 |
res = extract_kyc_fields(aadhaar_file.name)
|
|
@@ -147,7 +105,6 @@ def process_documents(aadhaar_file, pan_file, push_to_sf):
|
|
| 147 |
except Exception as e:
|
| 148 |
results["aadhaar"] = {"error": f"Aadhaar OCR failed: {str(e)}", "card_type": "UNKNOWN"}
|
| 149 |
|
| 150 |
-
# OCR PAN
|
| 151 |
if pan_file:
|
| 152 |
try:
|
| 153 |
res = extract_kyc_fields(pan_file.name)
|
|
@@ -165,40 +122,24 @@ def process_documents(aadhaar_file, pan_file, push_to_sf):
|
|
| 165 |
created = sf_create_kyc_via_session(sf, payload)
|
| 166 |
output["salesforce"] = {"pushed": created.get("success", False), **created}
|
| 167 |
except Exception as e:
|
| 168 |
-
|
| 169 |
-
output["salesforce"] = {
|
| 170 |
-
"pushed": False,
|
| 171 |
-
"error": {"type": e.__class__.__name__, "message": str(e)}
|
| 172 |
-
}
|
| 173 |
|
| 174 |
return output
|
| 175 |
|
| 176 |
-
|
| 177 |
-
# ---------- UI ----------
|
| 178 |
with gr.Blocks(title="Smart KYC OCR β Salesforce (KYC_Record__c)") as demo:
|
| 179 |
-
gr.Markdown(
|
| 180 |
-
"""
|
| 181 |
-
# π§Ύ Smart KYC OCR β Salesforce
|
| 182 |
-
Upload **Aadhaar** and **PAN** in separate boxes, then (optional) push one **KYC_Record__c**.
|
| 183 |
-
"""
|
| 184 |
-
)
|
| 185 |
|
| 186 |
with gr.Row():
|
| 187 |
-
with gr.Column(
|
| 188 |
aadhaar_uploader = gr.File(label="π€ Aadhaar Upload", file_types=[".jpg", ".jpeg", ".png"])
|
| 189 |
-
with gr.Column(
|
| 190 |
pan_uploader = gr.File(label="π€ PAN Upload", file_types=[".jpg", ".jpeg", ".png"])
|
| 191 |
|
| 192 |
-
push_to_sf = gr.Checkbox(label="Push to Salesforce
|
| 193 |
-
|
| 194 |
submit_btn = gr.Button("π Extract KYC Info", variant="primary")
|
| 195 |
-
output_json = gr.JSON(label="π Output
|
| 196 |
|
| 197 |
-
submit_btn.click(
|
| 198 |
-
fn=process_documents,
|
| 199 |
-
inputs=[aadhaar_uploader, pan_uploader, push_to_sf],
|
| 200 |
-
outputs=output_json,
|
| 201 |
-
)
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
demo.launch()
|
|
|
|
| 6 |
import datetime as dt
|
| 7 |
import gradio as gr
|
| 8 |
from utils import extract_kyc_fields
|
| 9 |
+
from simple_salesforce import Salesforce
|
| 10 |
|
| 11 |
+
# ------------------ SALESFORCE CREDS ------------------
|
| 12 |
SF_USERNAME = "licproject@2025.com"
|
| 13 |
SF_PASSWORD = "Lic@2025"
|
| 14 |
SF_SECURITY_TOKEN = "AmmfRcd6IiYaRtSGntBnzNMQU"
|
| 15 |
+
SF_DOMAIN = "login" # change to "test" if sandbox
|
| 16 |
+
# -------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
def _parse_birthdate(dob_text: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if not dob_text or dob_text == "Not found":
|
| 20 |
return None
|
| 21 |
s = dob_text.strip()
|
|
|
|
| 46 |
|
| 47 |
return None
|
| 48 |
|
|
|
|
| 49 |
def sf_login():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
sf = Salesforce(
|
| 51 |
username=SF_USERNAME,
|
| 52 |
password=SF_PASSWORD,
|
| 53 |
security_token=SF_SECURITY_TOKEN,
|
| 54 |
domain=SF_DOMAIN,
|
| 55 |
)
|
|
|
|
| 56 |
sf.query("SELECT Id FROM User LIMIT 1")
|
| 57 |
return sf
|
| 58 |
|
|
|
|
| 59 |
def sf_create_kyc_via_session(sf, payload: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
url = f"{sf.base_url}sobjects/KYC_Record__c"
|
| 61 |
resp = sf.session.post(url, json=payload, headers=sf.headers, timeout=30)
|
| 62 |
|
|
|
|
| 67 |
|
| 68 |
if 200 <= resp.status_code < 300:
|
| 69 |
rec_id = body.get("id") if isinstance(body, dict) else None
|
| 70 |
+
return {"success": True, "id": rec_id, "status_code": resp.status_code, "url": url, "response_json": body}
|
| 71 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return {
|
| 73 |
"success": False,
|
| 74 |
"status_code": resp.status_code,
|
|
|
|
| 77 |
"response_text": resp.text,
|
| 78 |
}
|
| 79 |
|
|
|
|
| 80 |
def build_payload(ocr_results: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
a = (ocr_results.get("aadhaar") or {})
|
| 82 |
p = (ocr_results.get("pan") or {})
|
| 83 |
|
|
|
|
| 89 |
"Pan_Name__c": p.get("name") if p.get("card_type") == "PAN" else None,
|
| 90 |
"Pan_DOB__c": _parse_birthdate(p.get("dob")) if p.get("card_type") == "PAN" else None,
|
| 91 |
}
|
|
|
|
| 92 |
return {k: v for k, v in payload.items() if v is not None}
|
| 93 |
|
|
|
|
|
|
|
| 94 |
def process_documents(aadhaar_file, pan_file, push_to_sf):
|
|
|
|
|
|
|
|
|
|
| 95 |
results = {"aadhaar": None, "pan": None}
|
| 96 |
|
| 97 |
if not aadhaar_file and not pan_file:
|
| 98 |
return {"error": "Please upload at least one file (Aadhaar and/or PAN)."}
|
| 99 |
|
|
|
|
| 100 |
if aadhaar_file:
|
| 101 |
try:
|
| 102 |
res = extract_kyc_fields(aadhaar_file.name)
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
results["aadhaar"] = {"error": f"Aadhaar OCR failed: {str(e)}", "card_type": "UNKNOWN"}
|
| 107 |
|
|
|
|
| 108 |
if pan_file:
|
| 109 |
try:
|
| 110 |
res = extract_kyc_fields(pan_file.name)
|
|
|
|
| 122 |
created = sf_create_kyc_via_session(sf, payload)
|
| 123 |
output["salesforce"] = {"pushed": created.get("success", False), **created}
|
| 124 |
except Exception as e:
|
| 125 |
+
output["salesforce"] = {"pushed": False, "error": {"type": e.__class__.__name__, "message": str(e)}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
return output
|
| 128 |
|
|
|
|
|
|
|
| 129 |
with gr.Blocks(title="Smart KYC OCR β Salesforce (KYC_Record__c)") as demo:
|
| 130 |
+
gr.Markdown("# π§Ύ Smart KYC OCR β Salesforce\nUpload Aadhaar and PAN, then optionally push one record.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
+
with gr.Column():
|
| 134 |
aadhaar_uploader = gr.File(label="π€ Aadhaar Upload", file_types=[".jpg", ".jpeg", ".png"])
|
| 135 |
+
with gr.Column():
|
| 136 |
pan_uploader = gr.File(label="π€ PAN Upload", file_types=[".jpg", ".jpeg", ".png"])
|
| 137 |
|
| 138 |
+
push_to_sf = gr.Checkbox(label="Push to Salesforce", value=False)
|
|
|
|
| 139 |
submit_btn = gr.Button("π Extract KYC Info", variant="primary")
|
| 140 |
+
output_json = gr.JSON(label="π Output")
|
| 141 |
|
| 142 |
+
submit_btn.click(fn=process_documents, inputs=[aadhaar_uploader, pan_uploader, push_to_sf], outputs=output_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
demo.launch()
|