Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,81 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from utils import extract_kyc_fields, connect_salesforce, create_kyc_record
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
sf_result = create_kyc_record(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
agent_id=agent_id
|
| 22 |
)
|
| 23 |
|
| 24 |
-
return {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
-
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from utils import extract_kyc_fields, connect_salesforce, create_kyc_record
|
| 4 |
|
| 5 |
+
def process(
|
| 6 |
+
aadhaar_image,
|
| 7 |
+
pan_image,
|
| 8 |
+
agent_id,
|
| 9 |
+
sf_username,
|
| 10 |
+
sf_password,
|
| 11 |
+
sf_token,
|
| 12 |
+
sf_domain
|
| 13 |
+
):
|
| 14 |
+
# Validate at least one image
|
| 15 |
+
if not aadhaar_image and not pan_image:
|
| 16 |
+
return {"status": "error", "message": "Upload at least one image (Aadhaar or PAN)."}
|
| 17 |
|
| 18 |
+
# Connect Salesforce (show *real* error cause back to UI)
|
| 19 |
+
try:
|
| 20 |
+
sf = connect_salesforce(
|
| 21 |
+
username=(sf_username or None),
|
| 22 |
+
password=(sf_password or None),
|
| 23 |
+
token=(sf_token or None),
|
| 24 |
+
domain=(sf_domain or None),
|
| 25 |
+
)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return {"status": "error", "message": f"Salesforce connection failed: {str(e)}"}
|
| 28 |
|
| 29 |
+
# OCR each card separately so we can fill both sides of the record
|
| 30 |
+
aadhaar_out = extract_kyc_fields(aadhaar_image, force_type="AADHAAR") if aadhaar_image else None
|
| 31 |
+
pan_out = extract_kyc_fields(pan_image, force_type="PAN") if pan_image else None
|
| 32 |
|
| 33 |
+
# Create the record
|
| 34 |
sf_result = create_kyc_record(
|
| 35 |
+
sf=sf,
|
| 36 |
+
aadhaar=aadhaar_out,
|
| 37 |
+
pan=pan_out,
|
| 38 |
+
agent_id=(agent_id.strip() if agent_id else None)
|
| 39 |
)
|
| 40 |
|
| 41 |
+
return {
|
| 42 |
+
"aadhaar_ocr": aadhaar_out,
|
| 43 |
+
"pan_ocr": pan_out,
|
| 44 |
+
"salesforce_result": sf_result
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
with gr.Blocks(title="AI KYC OCR → Salesforce (KYC_Record__c)") as demo:
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
"## AI KYC OCR → Salesforce (KYC_Record__c)\n"
|
| 50 |
+
"Upload **Aadhaar** and/or **PAN** images. We’ll extract fields and create a **KYC_Record__c** "
|
| 51 |
+
"with the following fields: `Aadhaar_Name__c`, `Aadhaar_DOB__c`, `Aadhaar_Number__c`, "
|
| 52 |
+
"`Pan_Name__c`, `Pan_DOB__c`, `PAN_Number__c`."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
aadhaar_img = gr.Image(type="filepath", label="Upload Aadhaar (Front)")
|
| 57 |
+
pan_img = gr.Image(type="filepath", label="Upload PAN")
|
| 58 |
+
|
| 59 |
+
agent_id = gr.Textbox(label="Agent Salesforce ID (optional)", placeholder="Agent__c (optional)")
|
| 60 |
+
|
| 61 |
+
with gr.Accordion("Salesforce connection (optional — leave blank to use env vars)", open=False):
|
| 62 |
+
sf_username = gr.Textbox(label="SF Username", placeholder="e.g. name@example.com", lines=1)
|
| 63 |
+
sf_password = gr.Textbox(label="SF Password", type="password")
|
| 64 |
+
sf_token = gr.Textbox(label="SF Security Token", type="password")
|
| 65 |
+
sf_domain = gr.Dropdown(
|
| 66 |
+
label="SF Domain",
|
| 67 |
+
choices=["login", "test"],
|
| 68 |
+
value="login",
|
| 69 |
+
info="Use 'login' for Production, 'test' for Sandbox"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
out = gr.JSON(label="output")
|
| 73 |
+
|
| 74 |
+
run_btn = gr.Button("Extract & Create KYC_Record__c", variant="primary")
|
| 75 |
+
run_btn.click(
|
| 76 |
+
fn=process,
|
| 77 |
+
inputs=[aadhaar_img, pan_img, agent_id, sf_username, sf_password, sf_token, sf_domain],
|
| 78 |
+
outputs=[out]
|
| 79 |
)
|
|
|
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|