Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,16 +6,12 @@ import datetime as dt
|
|
6 |
import gradio as gr
|
7 |
from utils import extract_kyc_fields
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
SF_DEFAULT_PASSWORD = os.getenv("Lic@2025", "REPLACE_ME_PASSWORD")
|
16 |
-
SF_DEFAULT_TOKEN = os.getenv("AmmfRcd6IiYaRtSGntBnzNMQU", "REPLACE_ME_SECURITY_TOKEN")
|
17 |
-
SF_DEFAULT_DOMAIN = os.getenv("SF_DOMAIN", "login") # "login" (prod) or "test" (sandbox)
|
18 |
-
# ------------------------------------------------------------------
|
19 |
|
20 |
# simple-salesforce + exceptions
|
21 |
try:
|
@@ -72,15 +68,10 @@ def _parse_birthdate(dob_text: str):
|
|
72 |
|
73 |
|
74 |
def _fmt_sf_error(err: Exception):
|
75 |
-
"""
|
76 |
-
Produce a clear, JSON-safe dict from any simple-salesforce error.
|
77 |
-
"""
|
78 |
-
# Default
|
79 |
info = {"type": err.__class__.__name__, "message": str(err)}
|
80 |
|
81 |
-
# Enrich known exceptions
|
82 |
if isinstance(err, SalesforceAuthenticationFailed):
|
83 |
-
# Has attributes: code, response.content, status, message, etc.
|
84 |
content = getattr(err, "content", None) or getattr(err, "response", None)
|
85 |
info.update({
|
86 |
"category": "AUTHENTICATION",
|
@@ -88,7 +79,6 @@ def _fmt_sf_error(err: Exception):
|
|
88 |
"content": getattr(content, "content", None) if hasattr(content, "content") else content,
|
89 |
})
|
90 |
elif isinstance(err, (SalesforceMalformedRequest, SalesforceGeneralError, SalesforceRefusedRequest)):
|
91 |
-
# These typically have status/resource_name/content/url
|
92 |
info.update({
|
93 |
"category": "REQUEST",
|
94 |
"status": getattr(err, "status", None),
|
@@ -100,34 +90,25 @@ def _fmt_sf_error(err: Exception):
|
|
100 |
info.update({"category": "NOT_FOUND"})
|
101 |
elif isinstance(err, SalesforceExpiredSession):
|
102 |
info.update({"category": "AUTH_EXPIRED"})
|
103 |
-
|
104 |
return info
|
105 |
|
106 |
|
107 |
-
def
|
108 |
"""
|
109 |
-
|
110 |
-
|
111 |
-
Also runs a quick auth test query to surface auth issues early.
|
112 |
"""
|
113 |
if not SF_AVAILABLE:
|
114 |
raise RuntimeError("simple-salesforce is not installed. Add `simple-salesforce` to requirements.txt.")
|
115 |
|
116 |
-
username = os.getenv("SF_USERNAME", SF_DEFAULT_USERNAME)
|
117 |
-
password = os.getenv("SF_PASSWORD", SF_DEFAULT_PASSWORD)
|
118 |
-
token = os.getenv("SF_SECURITY_TOKEN", SF_DEFAULT_TOKEN)
|
119 |
-
domain = os.getenv("SF_DOMAIN", SF_DEFAULT_DOMAIN or "login")
|
120 |
-
|
121 |
-
if any(v.startswith("REPLACE_ME") for v in [username, password, token]):
|
122 |
-
raise ValueError(
|
123 |
-
"Salesforce credentials missing. "
|
124 |
-
"Set env vars (SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_DOMAIN) "
|
125 |
-
"or replace the REPLACE_ME_* constants in app.py."
|
126 |
-
)
|
127 |
-
|
128 |
try:
|
129 |
-
sf = Salesforce(
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
131 |
sf.query("SELECT Id FROM User LIMIT 1")
|
132 |
return sf
|
133 |
except Exception as e:
|
@@ -137,7 +118,7 @@ def sf_connect_from_env_or_defaults():
|
|
137 |
def sf_push_kyc_record(sf, ocr_results):
|
138 |
"""
|
139 |
Create one KYC_Record__c combining Aadhaar + PAN.
|
140 |
-
|
141 |
Aadhaar_Number__c, Aadhaar_Name__c, Aadhaar_DOB__c (Date)
|
142 |
PAN_Number__c, Pan_Name__c, Pan_DOB__c (Date)
|
143 |
"""
|
@@ -145,29 +126,27 @@ def sf_push_kyc_record(sf, ocr_results):
|
|
145 |
p = ocr_results.get("pan") or {}
|
146 |
|
147 |
aadhaar_number = a.get("aadhaar_number") if (a.get("card_type") == "AADHAAR") else None
|
148 |
-
aadhaar_name = a.get("name")
|
149 |
aadhaar_dob = _parse_birthdate(a.get("dob")) if (a.get("card_type") == "AADHAAR") else None
|
150 |
|
151 |
pan_number = p.get("pan_number") if (p.get("card_type") == "PAN") else None
|
152 |
-
pan_name = p.get("name")
|
153 |
pan_dob = _parse_birthdate(p.get("dob")) if (p.get("card_type") == "PAN") else None
|
154 |
|
155 |
payload = {
|
156 |
"Aadhaar_Number__c": aadhaar_number,
|
157 |
"Aadhaar_Name__c": aadhaar_name,
|
158 |
-
"Aadhaar_DOB__c": aadhaar_dob,
|
159 |
"PAN_Number__c": pan_number,
|
160 |
"Pan_Name__c": pan_name,
|
161 |
-
"Pan_DOB__c": pan_dob,
|
162 |
}
|
163 |
-
# Remove None keys to avoid nulling non-nullable fields
|
164 |
payload = {k: v for k, v in payload.items() if v is not None}
|
165 |
|
166 |
try:
|
167 |
result = sf.KYC_Record__c.create(payload)
|
168 |
return {"success": True, "id": result.get("id"), "payload": payload}
|
169 |
except Exception as e:
|
170 |
-
# Return rich error info
|
171 |
return {"success": False, "error": _fmt_sf_error(e), "payload": payload}
|
172 |
|
173 |
|
@@ -204,7 +183,7 @@ def process_documents(aadhaar_file, pan_file, push_to_sf):
|
|
204 |
|
205 |
if push_to_sf:
|
206 |
try:
|
207 |
-
sf =
|
208 |
created = sf_push_kyc_record(sf, results)
|
209 |
output["salesforce"] = {"pushed": created.get("success", False), **created}
|
210 |
except Exception as e:
|
@@ -219,10 +198,6 @@ with gr.Blocks(title="Smart KYC OCR → Salesforce (KYC_Record__c)") as demo:
|
|
219 |
"""
|
220 |
# 🧾 Smart KYC OCR → Salesforce
|
221 |
Upload **Aadhaar** and **PAN** in separate boxes, then (optional) push one **KYC_Record__c**.
|
222 |
-
|
223 |
-
**Creds**: Set env vars (preferred)
|
224 |
-
`SF_USERNAME`, `SF_PASSWORD`, `SF_SECURITY_TOKEN`, `SF_DOMAIN` (login|test)
|
225 |
-
or replace the placeholders in `app.py` (not recommended).
|
226 |
"""
|
227 |
)
|
228 |
|
@@ -252,7 +227,7 @@ with gr.Blocks(title="Smart KYC OCR → Salesforce (KYC_Record__c)") as demo:
|
|
252 |
gr.Markdown("---")
|
253 |
gr.Markdown(
|
254 |
"""
|
255 |
-
|
256 |
"""
|
257 |
)
|
258 |
|
|
|
6 |
import gradio as gr
|
7 |
from utils import extract_kyc_fields
|
8 |
|
9 |
+
# ------------------ HARD-CODED SALESFORCE CREDS (as requested) ------------------
|
10 |
+
SF_USERNAME = "[email protected]"
|
11 |
+
SF_PASSWORD = "Lic@2025"
|
12 |
+
SF_SECURITY_TOKEN = "AmmfRcd6IiYaRtSGntBnzNMQU"
|
13 |
+
SF_DOMAIN = "login" # "login" for prod, "test" for sandbox
|
14 |
+
# -------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# simple-salesforce + exceptions
|
17 |
try:
|
|
|
68 |
|
69 |
|
70 |
def _fmt_sf_error(err: Exception):
|
71 |
+
"""Produce a clear, JSON-safe dict from any simple-salesforce error."""
|
|
|
|
|
|
|
72 |
info = {"type": err.__class__.__name__, "message": str(err)}
|
73 |
|
|
|
74 |
if isinstance(err, SalesforceAuthenticationFailed):
|
|
|
75 |
content = getattr(err, "content", None) or getattr(err, "response", None)
|
76 |
info.update({
|
77 |
"category": "AUTHENTICATION",
|
|
|
79 |
"content": getattr(content, "content", None) if hasattr(content, "content") else content,
|
80 |
})
|
81 |
elif isinstance(err, (SalesforceMalformedRequest, SalesforceGeneralError, SalesforceRefusedRequest)):
|
|
|
82 |
info.update({
|
83 |
"category": "REQUEST",
|
84 |
"status": getattr(err, "status", None),
|
|
|
90 |
info.update({"category": "NOT_FOUND"})
|
91 |
elif isinstance(err, SalesforceExpiredSession):
|
92 |
info.update({"category": "AUTH_EXPIRED"})
|
|
|
93 |
return info
|
94 |
|
95 |
|
96 |
+
def sf_connect():
|
97 |
"""
|
98 |
+
Connect to Salesforce using the hardcoded credentials.
|
99 |
+
Also runs a quick auth query to surface any auth issues clearly.
|
|
|
100 |
"""
|
101 |
if not SF_AVAILABLE:
|
102 |
raise RuntimeError("simple-salesforce is not installed. Add `simple-salesforce` to requirements.txt.")
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
try:
|
105 |
+
sf = Salesforce(
|
106 |
+
username=SF_USERNAME,
|
107 |
+
password=SF_PASSWORD,
|
108 |
+
security_token=SF_SECURITY_TOKEN,
|
109 |
+
domain=SF_DOMAIN,
|
110 |
+
)
|
111 |
+
# quick auth sanity check
|
112 |
sf.query("SELECT Id FROM User LIMIT 1")
|
113 |
return sf
|
114 |
except Exception as e:
|
|
|
118 |
def sf_push_kyc_record(sf, ocr_results):
|
119 |
"""
|
120 |
Create one KYC_Record__c combining Aadhaar + PAN.
|
121 |
+
Salesforce custom object fields:
|
122 |
Aadhaar_Number__c, Aadhaar_Name__c, Aadhaar_DOB__c (Date)
|
123 |
PAN_Number__c, Pan_Name__c, Pan_DOB__c (Date)
|
124 |
"""
|
|
|
126 |
p = ocr_results.get("pan") or {}
|
127 |
|
128 |
aadhaar_number = a.get("aadhaar_number") if (a.get("card_type") == "AADHAAR") else None
|
129 |
+
aadhaar_name = a.get("name") if (a.get("card_type") == "AADHAAR") else None
|
130 |
aadhaar_dob = _parse_birthdate(a.get("dob")) if (a.get("card_type") == "AADHAAR") else None
|
131 |
|
132 |
pan_number = p.get("pan_number") if (p.get("card_type") == "PAN") else None
|
133 |
+
pan_name = p.get("name") if (p.get("card_type") == "PAN") else None
|
134 |
pan_dob = _parse_birthdate(p.get("dob")) if (p.get("card_type") == "PAN") else None
|
135 |
|
136 |
payload = {
|
137 |
"Aadhaar_Number__c": aadhaar_number,
|
138 |
"Aadhaar_Name__c": aadhaar_name,
|
139 |
+
"Aadhaar_DOB__c": aadhaar_dob,
|
140 |
"PAN_Number__c": pan_number,
|
141 |
"Pan_Name__c": pan_name,
|
142 |
+
"Pan_DOB__c": pan_dob,
|
143 |
}
|
|
|
144 |
payload = {k: v for k, v in payload.items() if v is not None}
|
145 |
|
146 |
try:
|
147 |
result = sf.KYC_Record__c.create(payload)
|
148 |
return {"success": True, "id": result.get("id"), "payload": payload}
|
149 |
except Exception as e:
|
|
|
150 |
return {"success": False, "error": _fmt_sf_error(e), "payload": payload}
|
151 |
|
152 |
|
|
|
183 |
|
184 |
if push_to_sf:
|
185 |
try:
|
186 |
+
sf = sf_connect()
|
187 |
created = sf_push_kyc_record(sf, results)
|
188 |
output["salesforce"] = {"pushed": created.get("success", False), **created}
|
189 |
except Exception as e:
|
|
|
198 |
"""
|
199 |
# 🧾 Smart KYC OCR → Salesforce
|
200 |
Upload **Aadhaar** and **PAN** in separate boxes, then (optional) push one **KYC_Record__c**.
|
|
|
|
|
|
|
|
|
201 |
"""
|
202 |
)
|
203 |
|
|
|
227 |
gr.Markdown("---")
|
228 |
gr.Markdown(
|
229 |
"""
|
230 |
+
⚠️ Credentials are embedded in this file. Keep this code private and rotate the token if shared.
|
231 |
"""
|
232 |
)
|
233 |
|