gopichandra commited on
Commit
6986de3
·
verified ·
1 Parent(s): eee95a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -28
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
- # Connect Salesforce on startup
6
- SF = connect_salesforce()
 
 
 
 
 
 
 
 
 
 
7
 
8
- def process_image(image_file, agent_id_optional):
9
- if not image_file:
10
- return {"status": "error", "message": "No image uploaded"}
11
- if not SF:
12
- return {"status": "error", "message": "Salesforce connection failed"}
 
 
 
 
 
13
 
14
- kyc_data = extract_kyc_fields(image_file)
 
 
15
 
16
- agent_id = agent_id_optional.strip() if agent_id_optional else None
17
  sf_result = create_kyc_record(
18
- SF,
19
- kyc_data,
20
- file_name=os.path.basename(image_file),
21
- agent_id=agent_id or None, # only send if provided
22
  )
23
 
24
- return {"ocr_result": kyc_data, "salesforce_result": sf_result}
25
-
26
- iface = gr.Interface(
27
- fn=process_image,
28
- inputs=[
29
- gr.Image(type="filepath", label="Upload PAN / Aadhaar"),
30
- gr.Textbox(label="Agent Salesforce ID (optional)", placeholder="Enter Agent__c Id (optional)")
31
- ],
32
- outputs=gr.JSON(),
33
- title="AI KYC OCR Salesforce (KYC_Record__c)",
34
- description=(
35
- "Uploads a PAN/Aadhaar image, extracts fields, and creates a KYC_Record__c with:\n"
36
- "Aadhaar_Name__c, Aadhaar_DOB__c, Aadhaar_Number__c, Pan_Name__c, Pan_DOB__c, PAN_Number__c."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  )
38
- )
39
 
40
  if __name__ == "__main__":
41
- iface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
 
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)))