Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from utils import extract_kyc_fields
|
| 4 |
from simple_salesforce import Salesforce
|
|
|
|
| 5 |
|
| 6 |
# ------------------------
|
| 7 |
# CONFIGURATION
|
|
@@ -13,11 +15,13 @@ SF_TOKEN = os.getenv("SF_TOKEN", "AmmfRcd6IiYaRtSGntBnzNMQU")
|
|
| 13 |
# Limit Paddle threads to avoid container crashes
|
| 14 |
os.environ["OMP_NUM_THREADS"] = "1"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# ------------------------
|
| 17 |
# CONNECT TO SALESFORCE
|
| 18 |
# ------------------------
|
| 19 |
def connect_salesforce():
|
| 20 |
-
"""Connects to Salesforce (Production first, then Sandbox fallback)"""
|
| 21 |
try:
|
| 22 |
sf = Salesforce(
|
| 23 |
username=SF_USERNAME,
|
|
@@ -26,7 +30,7 @@ def connect_salesforce():
|
|
| 26 |
domain="login" # Production
|
| 27 |
)
|
| 28 |
print("✅ Connected to Salesforce (Production)")
|
| 29 |
-
return
|
| 30 |
except Exception as e:
|
| 31 |
print("❌ Production login failed:", e)
|
| 32 |
try:
|
|
@@ -37,50 +41,76 @@ def connect_salesforce():
|
|
| 37 |
domain="test" # Sandbox
|
| 38 |
)
|
| 39 |
print("✅ Connected to Salesforce (Sandbox)")
|
| 40 |
-
return
|
| 41 |
except Exception as e2:
|
| 42 |
print("❌ Sandbox login failed:", e2)
|
| 43 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# ------------------------
|
| 48 |
# PROCESS IMAGE FUNCTION
|
| 49 |
# ------------------------
|
| 50 |
-
def process_image(image_file):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
if
|
| 56 |
-
return {"
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
return {
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"ocr_result": ocr_result
|
| 65 |
}
|
| 66 |
except Exception as e:
|
| 67 |
-
return {"
|
| 68 |
-
|
| 69 |
|
| 70 |
# ------------------------
|
| 71 |
# GRADIO INTERFACE
|
| 72 |
# ------------------------
|
| 73 |
iface = gr.Interface(
|
| 74 |
fn=process_image,
|
| 75 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 76 |
outputs=gr.JSON(),
|
| 77 |
title="AI KYC OCR & Salesforce Integration",
|
| 78 |
-
description="Upload PAN or Aadhaar card images to extract text and
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# ------------------------
|
| 82 |
-
# LAUNCH APP
|
| 83 |
-
# ------------------------
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
|
| 86 |
-
iface.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from datetime import datetime
|
| 4 |
from utils import extract_kyc_fields
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
+
from paddleocr import PaddleOCR
|
| 7 |
|
| 8 |
# ------------------------
|
| 9 |
# CONFIGURATION
|
|
|
|
| 15 |
# Limit Paddle threads to avoid container crashes
|
| 16 |
os.environ["OMP_NUM_THREADS"] = "1"
|
| 17 |
|
| 18 |
+
# Initialize OCR
|
| 19 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 20 |
+
|
| 21 |
# ------------------------
|
| 22 |
# CONNECT TO SALESFORCE
|
| 23 |
# ------------------------
|
| 24 |
def connect_salesforce():
|
|
|
|
| 25 |
try:
|
| 26 |
sf = Salesforce(
|
| 27 |
username=SF_USERNAME,
|
|
|
|
| 30 |
domain="login" # Production
|
| 31 |
)
|
| 32 |
print("✅ Connected to Salesforce (Production)")
|
| 33 |
+
return sf
|
| 34 |
except Exception as e:
|
| 35 |
print("❌ Production login failed:", e)
|
| 36 |
try:
|
|
|
|
| 41 |
domain="test" # Sandbox
|
| 42 |
)
|
| 43 |
print("✅ Connected to Salesforce (Sandbox)")
|
| 44 |
+
return sf
|
| 45 |
except Exception as e2:
|
| 46 |
print("❌ Sandbox login failed:", e2)
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
sf = connect_salesforce()
|
| 50 |
+
|
| 51 |
+
# ------------------------
|
| 52 |
+
# CREATE KYC RECORD IN SALESFORCE
|
| 53 |
+
# ------------------------
|
| 54 |
+
def create_kyc_record(sf, agent_id, kyc_data, file_name=None):
|
| 55 |
+
try:
|
| 56 |
+
if not sf:
|
| 57 |
+
return {"status": "error", "message": "Salesforce connection failed"}
|
| 58 |
+
|
| 59 |
+
record = {
|
| 60 |
+
"Agent__c": agent_id,
|
| 61 |
+
"Name": kyc_data.get("name", "Unknown KYC"),
|
| 62 |
+
"Document_Number__c": kyc_data.get("pan_number") or kyc_data.get("aadhaar_number"),
|
| 63 |
+
"Document_Type__c": kyc_data.get("card_type", "UNKNOWN"),
|
| 64 |
+
"Upload_Date__c": datetime.now().isoformat()
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Optional: store file name / URL if needed
|
| 68 |
+
if file_name:
|
| 69 |
+
record["KYC_Document_URL__c"] = f"Uploaded File: {file_name}"
|
| 70 |
|
| 71 |
+
response = sf.KYC_Document__c.create(record)
|
| 72 |
+
return {"status": "success", "record_id": response.get("id")}
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return {"status": "error", "message": str(e)}
|
| 75 |
|
| 76 |
# ------------------------
|
| 77 |
# PROCESS IMAGE FUNCTION
|
| 78 |
# ------------------------
|
| 79 |
+
def process_image(image_file, agent_id):
|
| 80 |
+
if not sf:
|
| 81 |
+
return {"status": "error", "message": "Salesforce connection failed"}
|
| 82 |
+
|
| 83 |
+
if not image_file:
|
| 84 |
+
return {"status": "error", "message": "No image uploaded"}
|
| 85 |
|
| 86 |
+
if not agent_id:
|
| 87 |
+
return {"status": "error", "message": "Agent ID is required"}
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
# Extract KYC data using utils.py
|
| 91 |
+
kyc_data = extract_kyc_fields(image_file)
|
| 92 |
+
# Create Salesforce record
|
| 93 |
+
sf_response = create_kyc_record(sf, agent_id, kyc_data, os.path.basename(image_file))
|
| 94 |
return {
|
| 95 |
+
"ocr_result": kyc_data,
|
| 96 |
+
"salesforce_result": sf_response
|
|
|
|
| 97 |
}
|
| 98 |
except Exception as e:
|
| 99 |
+
return {"status": "error", "message": f"OCR/Salesforce error: {str(e)}"}
|
|
|
|
| 100 |
|
| 101 |
# ------------------------
|
| 102 |
# GRADIO INTERFACE
|
| 103 |
# ------------------------
|
| 104 |
iface = gr.Interface(
|
| 105 |
fn=process_image,
|
| 106 |
+
inputs=[
|
| 107 |
+
gr.Image(type="filepath", label="Upload PAN / Aadhaar"),
|
| 108 |
+
gr.Textbox(label="Agent Salesforce ID", placeholder="Enter Agent record ID")
|
| 109 |
+
],
|
| 110 |
outputs=gr.JSON(),
|
| 111 |
title="AI KYC OCR & Salesforce Integration",
|
| 112 |
+
description="Upload PAN or Aadhaar card images to extract text and automatically create KYC records in Salesforce."
|
| 113 |
)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|