Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
import json
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
from simple_salesforce import Salesforce
|
| 4 |
|
| 5 |
# ------------------------
|
| 6 |
# CONFIGURATION
|
| 7 |
# ------------------------
|
| 8 |
-
SF_USERNAME = "licproject@2025.com"
|
| 9 |
-
SF_PASSWORD = "Lic@2025"
|
| 10 |
-
SF_TOKEN = "AmmfRcd6IiYaRtSGntBnzNMQU"
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ------------------------
|
| 14 |
# CONNECT TO SALESFORCE
|
|
@@ -22,7 +26,7 @@ def connect_salesforce():
|
|
| 22 |
domain="login" # Production
|
| 23 |
)
|
| 24 |
print("✅ Connected to Salesforce (Production)")
|
| 25 |
-
return
|
| 26 |
except Exception as e:
|
| 27 |
print("❌ Production login failed:", e)
|
| 28 |
try:
|
|
@@ -33,49 +37,44 @@ def connect_salesforce():
|
|
| 33 |
domain="test" # Sandbox
|
| 34 |
)
|
| 35 |
print("✅ Connected to Salesforce (Sandbox)")
|
| 36 |
-
return
|
| 37 |
except Exception as e2:
|
| 38 |
print("❌ Sandbox login failed:", e2)
|
| 39 |
-
return
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# ------------------------
|
| 42 |
-
#
|
| 43 |
# ------------------------
|
| 44 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return {"
|
| 52 |
|
| 53 |
# ------------------------
|
| 54 |
-
#
|
| 55 |
# ------------------------
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 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 |
-
|
| 79 |
-
|
| 80 |
-
iface.launch()
|
| 81 |
-
|
|
|
|
| 1 |
import json
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from utils import extract_kyc_fields
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
|
| 7 |
# ------------------------
|
| 8 |
# CONFIGURATION
|
| 9 |
# ------------------------
|
| 10 |
+
SF_USERNAME = os.getenv("SF_USERNAME", "licproject@2025.com")
|
| 11 |
+
SF_PASSWORD = os.getenv("SF_PASSWORD", "Lic@2025")
|
| 12 |
+
SF_TOKEN = os.getenv("SF_TOKEN", "AmmfRcd6IiYaRtSGntBnzNMQU")
|
| 13 |
+
|
| 14 |
+
# Limit Paddle threads to avoid container crashes
|
| 15 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 16 |
|
| 17 |
# ------------------------
|
| 18 |
# CONNECT TO SALESFORCE
|
|
|
|
| 26 |
domain="login" # Production
|
| 27 |
)
|
| 28 |
print("✅ Connected to Salesforce (Production)")
|
| 29 |
+
return {"status": "success", "env": "Production"}
|
| 30 |
except Exception as e:
|
| 31 |
print("❌ Production login failed:", e)
|
| 32 |
try:
|
|
|
|
| 37 |
domain="test" # Sandbox
|
| 38 |
)
|
| 39 |
print("✅ Connected to Salesforce (Sandbox)")
|
| 40 |
+
return {"status": "success", "env": "Sandbox"}
|
| 41 |
except Exception as e2:
|
| 42 |
print("❌ Sandbox login failed:", e2)
|
| 43 |
+
return {"status": "error", "message": "Salesforce login failed"}
|
| 44 |
+
|
| 45 |
+
sf_status = connect_salesforce()
|
| 46 |
|
| 47 |
# ------------------------
|
| 48 |
+
# PROCESS IMAGE FUNCTION
|
| 49 |
# ------------------------
|
| 50 |
+
def process_image(image_file):
|
| 51 |
+
if sf_status["status"] != "success":
|
| 52 |
+
return {"salesforce_status": "error", "message": sf_status.get("message", "Login failed")}
|
| 53 |
+
|
| 54 |
+
if image_file is None:
|
| 55 |
+
return {"ocr_status": "error", "message": "No image uploaded"}
|
| 56 |
+
|
| 57 |
try:
|
| 58 |
+
# Run OCR extraction from utils.py
|
| 59 |
+
ocr_result = extract_kyc_fields(image_file.name)
|
| 60 |
+
return {
|
| 61 |
+
"salesforce_status": "connected",
|
| 62 |
+
"ocr_result": ocr_result
|
| 63 |
+
}
|
| 64 |
except Exception as e:
|
| 65 |
+
return {"ocr_status": "error", "message": str(e)}
|
| 66 |
|
| 67 |
# ------------------------
|
| 68 |
+
# GRADIO INTERFACE
|
| 69 |
# ------------------------
|
| 70 |
+
iface = gr.Interface(
|
| 71 |
+
fn=process_image,
|
| 72 |
+
inputs=gr.Image(type="file"),
|
| 73 |
+
outputs=gr.JSON(),
|
| 74 |
+
title="AI KYC OCR & Salesforce Integration",
|
| 75 |
+
description="Upload PAN or Aadhaar card images to extract text and verify Salesforce connection."
|
| 76 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
# Launch server to keep container alive
|
| 80 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|