dschandra commited on
Commit
c586aa6
·
verified ·
1 Parent(s): aee34e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -1,14 +1,18 @@
1
  import json
2
- from paddleocr import PaddleOCR
 
 
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
- IMAGE_PATH = "sample.jpg" # Change this to your actual image file path
 
 
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 sf
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 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))
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)