Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,41 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
from
|
4 |
|
5 |
-
#
|
6 |
-
|
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 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
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 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
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 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
"
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
|
|
|
|
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)))
|