gopichandra commited on
Commit
e3cb5a5
Β·
verified Β·
1 Parent(s): 7147400

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -71
app.py CHANGED
@@ -1,78 +1,41 @@
1
- import json
2
- from paddleocr import PaddleOCR
3
- from simple_salesforce import Salesforce
4
 
5
- # ------------------------
6
- # CONFIGURATION
7
- # ------------------------
8
- SF_USERNAME = "[email protected]"
9
- SF_PASSWORD = "Lic@2025"
10
- SF_TOKEN = "AmmfRcd6IiYaRtSGntBnzNMQU"
11
- IMAGE_PATH = "sample.jpg" # Change this to your actual image file path
12
 
13
- # ------------------------
14
- # CONNECT TO SALESFORCE
15
- # ------------------------
16
- def connect_salesforce():
17
- try:
18
- sf = Salesforce(
19
- username=SF_USERNAME,
20
- password=SF_PASSWORD,
21
- security_token=SF_TOKEN,
22
- domain="login" # Production
23
- )
24
- print("βœ… Connected to Salesforce (Production)")
25
- return sf
26
- except Exception as e:
27
- print("❌ Production login failed:", e)
28
- try:
29
- sf = Salesforce(
30
- username=SF_USERNAME,
31
- password=SF_PASSWORD,
32
- security_token=SF_TOKEN,
33
- domain="test" # Sandbox
34
- )
35
- print("βœ… Connected to Salesforce (Sandbox)")
36
- return sf
37
- except Exception as e2:
38
- print("❌ Sandbox login failed:", e2)
39
- return None
40
 
41
- # ------------------------
42
- # RUN OCR
43
- # ------------------------
44
- def extract_text_from_image(image_path):
45
- try:
46
- ocr = PaddleOCR(use_angle_cls=True, lang='en')
47
- results = ocr.ocr(image_path, cls=True)
48
- extracted_text = [line[1][0] for line in results[0]]
49
- return {"status": "success", "text": extracted_text}
50
- except Exception as e:
51
- return {"status": "error", "message": str(e)}
52
 
53
- # ------------------------
54
- # MAIN EXECUTION
55
- # ------------------------
56
- if __name__ == "__main__":
57
- sf_conn = connect_salesforce()
58
-
59
- if not sf_conn:
60
- output = {
61
- "salesforce_result": {
62
- "status": "error",
63
- "message": "Salesforce not connected"
64
- }
65
- }
66
- print(json.dumps(output, indent=4))
67
- exit()
68
 
69
- ocr_output = extract_text_from_image(IMAGE_PATH)
70
 
71
- final_output = {
72
- "salesforce_result": {
73
- "status": "connected"
74
- },
75
- "ocr_result": ocr_output
76
- }
 
 
 
 
 
 
 
77
 
78
- print(json.dumps(final_output, indent=4))
 
 
1
+ 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)))