Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,78 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
import gradio as gr
|
5 |
-
from utils import extract_kyc_fields
|
6 |
from simple_salesforce import Salesforce
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
-
# Append token to password for login
|
16 |
sf = Salesforce(
|
17 |
-
username=
|
18 |
-
password=
|
19 |
-
security_token=
|
20 |
-
domain="login" #
|
21 |
)
|
|
|
22 |
return sf
|
23 |
except Exception as e:
|
24 |
-
|
25 |
-
|
26 |
-
sf =
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
try:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"Aadhaar_Gender__c": aadhaar_data.get("gender", ""),
|
43 |
-
"PAN_Name__c": pan_data.get("name", ""),
|
44 |
-
"PAN_Number__c": pan_data.get("pan_number", ""),
|
45 |
-
"PAN_DOB__c": pan_data.get("dob", "")
|
46 |
-
}
|
47 |
-
result = sf.KYC_Record__c.create(record)
|
48 |
-
return {"status": "success", "salesforce_id": result.get("id")}
|
49 |
except Exception as e:
|
50 |
return {"status": "error", "message": str(e)}
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
"
|
64 |
-
|
65 |
-
|
|
|
66 |
}
|
67 |
|
68 |
-
|
69 |
-
with gr.Blocks(title="Smart KYC OCR") as demo:
|
70 |
-
gr.Markdown("# π§Ύ Smart KYC OCR Tool β Aadhaar & PAN Scanner")
|
71 |
-
|
72 |
-
with gr.Row():
|
73 |
-
with gr.Column():
|
74 |
-
aadhaar_uploader = gr.File(label="π€ Upload Aadhaar Card", file_types=[".jpg", ".jpeg", ".png"])
|
75 |
-
pan_uploader = gr.File(label="π€ Upload PAN Card", file_types=[".jpg", ".jpeg", ".png"])
|
76 |
-
submit_btn = gr.Button("π Extract & Send to Salesforce", variant="primary")
|
77 |
-
|
78 |
-
with gr.Column():
|
79 |
-
output_json = gr.JSON(label="π Extracted KYC Data")
|
80 |
-
|
81 |
-
submit_btn.click(fn=process_documents, inputs=[aadhaar_uploader, pan_uploader], outputs=output_json)
|
82 |
-
|
83 |
-
gr.Markdown("---")
|
84 |
-
gr.Markdown("π **Privacy Note:** This app processes your document locally in the cloud. No data is stored or shared.")
|
85 |
-
|
86 |
-
if __name__ == "__main__":
|
87 |
-
demo.launch()
|
|
|
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
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|