Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from paddleocr import PaddleOCR
|
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
import re
|
| 6 |
-
from simple_salesforce import Salesforce
|
| 7 |
import pandas as pd
|
| 8 |
|
| 9 |
# Attribute mappings: readable names to Salesforce API names, in desired order
|
|
@@ -24,10 +24,10 @@ GENDER_MAPPING = {
|
|
| 24 |
"Other": "Others" # Map 'Other' to 'Others' to match picklist
|
| 25 |
}
|
| 26 |
|
| 27 |
-
# Salesforce credentials
|
| 28 |
-
SALESFORCE_USERNAME = "sathkruthatech@hms.com"
|
| 29 |
-
SALESFORCE_PASSWORD = "Hms@2025"
|
| 30 |
-
SALESFORCE_SECURITY_TOKEN = "5W0grf0aX0M9cD3yDZ2C5F"
|
| 31 |
|
| 32 |
# Initialize PaddleOCR
|
| 33 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
@@ -49,7 +49,7 @@ def extract_attributes(extracted_text):
|
|
| 49 |
"Name": r"Name[:\-]?\s*([A-Za-z]+)", # Allow no space after Name:
|
| 50 |
"Age": r"Age[:\-]?\s*(\d{1,3})",
|
| 51 |
"Gender": r"Gender[:\-]?\s*(Male|Female|Other)",
|
| 52 |
-
"Phone Number": r"(?:(?:Phone Number)|Phone|Mobile|Phonenumber)[:\-]?\s*(?:\+91)?([6-9]\d{9})"
|
| 53 |
}
|
| 54 |
|
| 55 |
for readable_attr, pattern in patterns.items():
|
|
@@ -63,42 +63,39 @@ def extract_attributes(extracted_text):
|
|
| 63 |
|
| 64 |
return attributes
|
| 65 |
|
| 66 |
-
# Function to filter attributes for valid Salesforce fields
|
| 67 |
-
def filter_valid_attributes(attributes, valid_fields):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
filtered = {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return filtered
|
| 70 |
|
| 71 |
-
# Function to interact with Salesforce
|
| 72 |
def interact_with_salesforce(attributes):
|
| 73 |
try:
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
SALESFORCE_USERNAME = "sathkruthatech@hms.com",
|
| 77 |
-
SALESFORCE_PASSWORD = "Hms@2025",
|
| 78 |
-
SALESFORCE_SECURITY_TOKEN = "5W0grf0aX0M9cD3yDZ2C5F",
|
| 79 |
-
domain="test" # Set for sandbox instance
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
object_name = "Patient_Registration__c"
|
| 83 |
-
sf_object = sf.__getattr__(object_name)
|
| 84 |
-
schema = sf_object.describe()
|
| 85 |
-
valid_fields = {field["name"] for field in schema["fields"]}
|
| 86 |
-
|
| 87 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 88 |
|
| 89 |
-
# Log the attributes
|
| 90 |
-
print(f"Attributes
|
| 91 |
|
| 92 |
-
# Ensure Age__c is a number
|
| 93 |
if "Age__c" in filtered_attributes:
|
| 94 |
filtered_attributes["Age__c"] = int(filtered_attributes["Age__c"])
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
return f"✅ Successfully created Patient Registration record with ID: {result['id']}."
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
-
return f"❌ Error
|
| 102 |
|
| 103 |
# Function to process image and extract attributes
|
| 104 |
def process_image(image):
|
|
@@ -115,18 +112,18 @@ def process_image(image):
|
|
| 115 |
df = pd.DataFrame(list(ordered_attributes.items()), columns=["Attribute", "Value"])
|
| 116 |
return f"Extracted Text:\n{extracted_text}", df, None
|
| 117 |
|
| 118 |
-
# Function to handle edited attributes and export to Salesforce
|
| 119 |
def export_to_salesforce(edited_df):
|
| 120 |
try:
|
| 121 |
# Convert edited DataFrame back to dictionary
|
| 122 |
edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
|
| 123 |
|
| 124 |
-
# Export to Salesforce
|
| 125 |
message = interact_with_salesforce(edited_attributes)
|
| 126 |
return message
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
-
return f"❌ Error exporting
|
| 130 |
|
| 131 |
# Gradio Interface
|
| 132 |
def app():
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
import re
|
| 6 |
+
# from simple_salesforce import Salesforce # Commented out to disable Salesforce integration
|
| 7 |
import pandas as pd
|
| 8 |
|
| 9 |
# Attribute mappings: readable names to Salesforce API names, in desired order
|
|
|
|
| 24 |
"Other": "Others" # Map 'Other' to 'Others' to match picklist
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# Salesforce credentials (commented out since Salesforce integration is disabled)
|
| 28 |
+
# SALESFORCE_USERNAME = "sathkruthatech@hms.com"
|
| 29 |
+
# SALESFORCE_PASSWORD = "Hms@2025"
|
| 30 |
+
# SALESFORCE_SECURITY_TOKEN = "5W0grf0aX0M9cD3yDZ2C5F"
|
| 31 |
|
| 32 |
# Initialize PaddleOCR
|
| 33 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
|
|
| 49 |
"Name": r"Name[:\-]?\s*([A-Za-z]+)", # Allow no space after Name:
|
| 50 |
"Age": r"Age[:\-]?\s*(\d{1,3})",
|
| 51 |
"Gender": r"Gender[:\-]?\s*(Male|Female|Other)",
|
| 52 |
+
"Phone Number": r"(?:(?:Phone Number)|Phone|Mobile|Phonenumber)[:\-]?\s*(?:\+91)?([6-9]\d{9})"
|
| 53 |
}
|
| 54 |
|
| 55 |
for readable_attr, pattern in patterns.items():
|
|
|
|
| 63 |
|
| 64 |
return attributes
|
| 65 |
|
| 66 |
+
# Function to filter attributes for valid Salesforce fields (mocked for now)
|
| 67 |
+
def filter_valid_attributes(attributes, valid_fields=None):
|
| 68 |
+
# Mock valid fields since Salesforce is disabled
|
| 69 |
+
if valid_fields is None:
|
| 70 |
+
valid_fields = {"Name", "Name__c", "Age__c", "Gender__c", "Phone_Number__c"}
|
| 71 |
+
|
| 72 |
filtered = {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
| 73 |
+
|
| 74 |
+
# Add the standard 'Name' field using the extracted Name value
|
| 75 |
+
if "Name" in attributes and "Name" in valid_fields:
|
| 76 |
+
filtered["Name"] = attributes["Name"]
|
| 77 |
+
|
| 78 |
return filtered
|
| 79 |
|
| 80 |
+
# Function to interact with Salesforce (mocked for now)
|
| 81 |
def interact_with_salesforce(attributes):
|
| 82 |
try:
|
| 83 |
+
# Mock Salesforce interaction since we cannot initialize Salesforce
|
| 84 |
+
valid_fields = {"Name", "Name__c", "Age__c", "Gender__c", "Phone_Number__c"} # Mocked schema
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 86 |
|
| 87 |
+
# Log the attributes that would be sent to Salesforce
|
| 88 |
+
print(f"Attributes that would be sent to Salesforce: {filtered_attributes}")
|
| 89 |
|
| 90 |
+
# Ensure Age__c is a number (for compatibility with Salesforce)
|
| 91 |
if "Age__c" in filtered_attributes:
|
| 92 |
filtered_attributes["Age__c"] = int(filtered_attributes["Age__c"])
|
| 93 |
|
| 94 |
+
# Mock a successful Salesforce response
|
| 95 |
+
return f"✅ Mock Success: Attributes processed (Salesforce integration disabled). Data: {filtered_attributes}"
|
|
|
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
+
return f"❌ Error processing attributes: {str(e)}"
|
| 99 |
|
| 100 |
# Function to process image and extract attributes
|
| 101 |
def process_image(image):
|
|
|
|
| 112 |
df = pd.DataFrame(list(ordered_attributes.items()), columns=["Attribute", "Value"])
|
| 113 |
return f"Extracted Text:\n{extracted_text}", df, None
|
| 114 |
|
| 115 |
+
# Function to handle edited attributes and export to Salesforce (mocked)
|
| 116 |
def export_to_salesforce(edited_df):
|
| 117 |
try:
|
| 118 |
# Convert edited DataFrame back to dictionary
|
| 119 |
edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
|
| 120 |
|
| 121 |
+
# Export to Salesforce (mocked)
|
| 122 |
message = interact_with_salesforce(edited_attributes)
|
| 123 |
return message
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
+
return f"❌ Error exporting attributes: {str(e)}"
|
| 127 |
|
| 128 |
# Gradio Interface
|
| 129 |
def app():
|