Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,13 @@ import gradio as gr
|
|
| 5 |
import re
|
| 6 |
from simple_salesforce import Salesforce
|
| 7 |
import pandas as pd
|
|
|
|
| 8 |
# Attribute mappings: readable names to Salesforce API names
|
| 9 |
ATTRIBUTE_MAPPING = {
|
| 10 |
"Name": "Name__c",
|
| 11 |
"Age": "Age__c",
|
| 12 |
"Gender": "Gender__c",
|
| 13 |
-
"Phone Number": "
|
| 14 |
}
|
| 15 |
|
| 16 |
# Salesforce credentials
|
|
@@ -38,7 +39,7 @@ def extract_attributes(extracted_text):
|
|
| 38 |
"Name": r"Name[:\-]?\s*([A-Za-z\s]+)",
|
| 39 |
"Age": r"Age[:\-]?\s*(\d{1,3})",
|
| 40 |
"Gender": r"Gender[:\-]?\s*(Male|Female|Other)",
|
| 41 |
-
"Phone Number": r"Phone[:\-]?\s*(\+?\d{10,12})"
|
| 42 |
}
|
| 43 |
|
| 44 |
for readable_attr, pattern in patterns.items():
|
|
@@ -57,17 +58,21 @@ def interact_with_salesforce(attributes):
|
|
| 57 |
try:
|
| 58 |
sf = Salesforce(
|
| 59 |
username=SALESFORCE_USERNAME,
|
| 60 |
-
password=SALESFORCE_PASSWORD,
|
| 61 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 62 |
)
|
| 63 |
|
| 64 |
-
object_name = "Patient_Registration__c"
|
| 65 |
sf_object = sf.__getattr__(object_name)
|
| 66 |
schema = sf_object.describe()
|
| 67 |
valid_fields = {field["name"] for field in schema["fields"]}
|
| 68 |
|
| 69 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Create a new record in Salesforce
|
| 72 |
result = sf_object.create(filtered_attributes)
|
| 73 |
return f"β
Successfully created Patient Registration record with ID: {result['id']}."
|
|
@@ -105,55 +110,6 @@ def export_to_salesforce(edited_df):
|
|
| 105 |
except Exception as e:
|
| 106 |
return f"β Error exporting to Salesforce: {str(e)}"
|
| 107 |
|
| 108 |
-
# Function to pull structured data from Salesforce and display as a table
|
| 109 |
-
def pull_data_from_salesforce():
|
| 110 |
-
try:
|
| 111 |
-
sf = Salesforce(
|
| 112 |
-
username=SALESFORCE_USERNAME,
|
| 113 |
-
password=SALESFORCE_PASSWORD,
|
| 114 |
-
security_token=SALESFORCE_SECURITY_TOKEN
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
query = "SELECT Name__c, Age__c, Gender__c, Phone__c FROM Patient_Registration__c WHERE Age__c != NULL LIMIT 100"
|
| 118 |
-
response = sf.query_all(query)
|
| 119 |
-
records = response.get("records", [])
|
| 120 |
-
|
| 121 |
-
if not records:
|
| 122 |
-
return "No data found in Salesforce.", None, None, None
|
| 123 |
-
|
| 124 |
-
df = pd.DataFrame(records)
|
| 125 |
-
df = df.drop(columns=['attributes'], errors='ignore')
|
| 126 |
-
|
| 127 |
-
# Rename columns for better readability
|
| 128 |
-
df.rename(columns={
|
| 129 |
-
"Name__c": "Name",
|
| 130 |
-
"Age__c": "Age",
|
| 131 |
-
"Gender__c": "Gender",
|
| 132 |
-
"Phone__c": "Phone Number"
|
| 133 |
-
}, inplace=True)
|
| 134 |
-
|
| 135 |
-
excel_path = "salesforce_patient_registration.xlsx"
|
| 136 |
-
df.to_excel(excel_path, index=False)
|
| 137 |
-
|
| 138 |
-
# Generate a bar graph for age distribution
|
| 139 |
-
fig, ax = plt.subplots(figsize=(12, 8))
|
| 140 |
-
df['Age'] = pd.to_numeric(df['Age'], errors='coerce')
|
| 141 |
-
df.groupby('Age').size().plot(kind='bar', ax=ax)
|
| 142 |
-
ax.set_title("Age Distribution of Patient Registrations")
|
| 143 |
-
ax.set_xlabel("Age")
|
| 144 |
-
ax.set_ylabel("Number of Patients")
|
| 145 |
-
plt.xticks(rotation=45, ha="right", fontsize=10)
|
| 146 |
-
plt.tight_layout()
|
| 147 |
-
buffer = BytesIO()
|
| 148 |
-
plt.savefig(buffer, format="png")
|
| 149 |
-
buffer.seek(0)
|
| 150 |
-
img = Image.open(buffer)
|
| 151 |
-
|
| 152 |
-
return df, excel_path, img
|
| 153 |
-
|
| 154 |
-
except Exception as e:
|
| 155 |
-
return f"Error fetching data: {str(e)}", None, None, None
|
| 156 |
-
|
| 157 |
# Gradio Interface
|
| 158 |
def app():
|
| 159 |
with gr.Blocks() as demo:
|
|
@@ -166,12 +122,6 @@ def app():
|
|
| 166 |
ok_button = gr.Button("OK")
|
| 167 |
result_output = gr.Text(label="π Result")
|
| 168 |
|
| 169 |
-
with gr.Tab("π Salesforce Data"):
|
| 170 |
-
pull_button = gr.Button("Pull Data from Salesforce")
|
| 171 |
-
salesforce_data_output = gr.Dataframe(label="π Salesforce Data")
|
| 172 |
-
excel_download_output = gr.File(label="π₯ Download Excel")
|
| 173 |
-
graph_output = gr.Image(label="π Age Distribution Graph")
|
| 174 |
-
|
| 175 |
# Define button actions
|
| 176 |
extract_button.click(
|
| 177 |
fn=process_image,
|
|
@@ -183,11 +133,6 @@ def app():
|
|
| 183 |
inputs=[editable_df_output],
|
| 184 |
outputs=[result_output]
|
| 185 |
)
|
| 186 |
-
pull_button.click(
|
| 187 |
-
fn=pull_data_from_salesforce,
|
| 188 |
-
inputs=[],
|
| 189 |
-
outputs=[salesforce_data_output, excel_download_output, graph_output]
|
| 190 |
-
)
|
| 191 |
|
| 192 |
return demo
|
| 193 |
|
|
|
|
| 5 |
import re
|
| 6 |
from simple_salesforce import Salesforce
|
| 7 |
import pandas as pd
|
| 8 |
+
|
| 9 |
# Attribute mappings: readable names to Salesforce API names
|
| 10 |
ATTRIBUTE_MAPPING = {
|
| 11 |
"Name": "Name__c",
|
| 12 |
"Age": "Age__c",
|
| 13 |
"Gender": "Gender__c",
|
| 14 |
+
"Phone Number": "Phone_Number__c"
|
| 15 |
}
|
| 16 |
|
| 17 |
# Salesforce credentials
|
|
|
|
| 39 |
"Name": r"Name[:\-]?\s*([A-Za-z\s]+)",
|
| 40 |
"Age": r"Age[:\-]?\s*(\d{1,3})",
|
| 41 |
"Gender": r"Gender[:\-]?\s*(Male|Female|Other)",
|
| 42 |
+
"Phone Number": r"(?:Phone|Mobile)[:\-]?\s*(\+?\d{10,12})"
|
| 43 |
}
|
| 44 |
|
| 45 |
for readable_attr, pattern in patterns.items():
|
|
|
|
| 58 |
try:
|
| 59 |
sf = Salesforce(
|
| 60 |
username=SALESFORCE_USERNAME,
|
| 61 |
+
password=S=SALESFORCE_PASSWORD,
|
| 62 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 63 |
)
|
| 64 |
|
| 65 |
+
object_name = "Patient_Registration__c"
|
| 66 |
sf_object = sf.__getattr__(object_name)
|
| 67 |
schema = sf_object.describe()
|
| 68 |
valid_fields = {field["name"] for field in schema["fields"]}
|
| 69 |
|
| 70 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 71 |
|
| 72 |
+
# Ensure Age__c is a number
|
| 73 |
+
if "Age__c" in filtered_attributes:
|
| 74 |
+
filtered_attributes["Age__c"] = int(filtered_attributes["Age__c"])
|
| 75 |
+
|
| 76 |
# Create a new record in Salesforce
|
| 77 |
result = sf_object.create(filtered_attributes)
|
| 78 |
return f"β
Successfully created Patient Registration record with ID: {result['id']}."
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
return f"β Error exporting to Salesforce: {str(e)}"
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Gradio Interface
|
| 114 |
def app():
|
| 115 |
with gr.Blocks() as demo:
|
|
|
|
| 122 |
ok_button = gr.Button("OK")
|
| 123 |
result_output = gr.Text(label="π Result")
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Define button actions
|
| 126 |
extract_button.click(
|
| 127 |
fn=process_image,
|
|
|
|
| 133 |
inputs=[editable_df_output],
|
| 134 |
outputs=[result_output]
|
| 135 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
return demo
|
| 138 |
|