Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,149 +1,158 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from simple_salesforce import Salesforce
|
| 4 |
|
| 5 |
# Configure logging
|
| 6 |
-
logging.basicConfig(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
-
# Salesforce
|
| 10 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 11 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 12 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
try:
|
| 16 |
-
sf = Salesforce(
|
| 17 |
-
username=SALESFORCE_USERNAME,
|
| 18 |
-
password=SALESFORCE_PASSWORD,
|
| 19 |
-
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 20 |
-
domain="login"
|
| 21 |
-
)
|
| 22 |
-
logger.info("Connected to Salesforce successfully.")
|
| 23 |
-
except Exception as e:
|
| 24 |
-
logger.error(f"Failed to connect to Salesforce: {e}")
|
| 25 |
-
raise
|
| 26 |
-
|
| 27 |
-
# Fetch Equipment records (Id, Name)
|
| 28 |
-
def fetch_equipment_records():
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
logger.
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
# Fetch Project records (Id, Name)
|
| 40 |
-
def fetch_project_records():
|
| 41 |
-
try:
|
| 42 |
-
query = "SELECT Id, Name FROM Project__c LIMIT 100"
|
| 43 |
-
result = sf.query(query)
|
| 44 |
-
choices = [(rec["Id"], rec["Name"]) for rec in result["records"]]
|
| 45 |
-
logger.info(f"Fetched {len(choices)} Project records")
|
| 46 |
-
return choices
|
| 47 |
except Exception as e:
|
| 48 |
-
logger.error(f"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
if not equipment_id or not project_id:
|
| 74 |
-
return {"error": "
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"Equipment_ID__c": equipment_id,
|
| 78 |
"Project_ID__c": project_id,
|
| 79 |
"Usage_Hours__c": usage_hours,
|
| 80 |
"Idle_Hours__c": idle_hours,
|
| 81 |
-
"Last_Maintenance__c": last_maintenance if last_maintenance else None,
|
| 82 |
-
"Utilization_Score__c": utilization_score if utilization_score else 0,
|
| 83 |
"AI_Suggestion__c": ai_suggestion,
|
| 84 |
-
"Suggestion_Confidence__c": suggestion_confidence
|
| 85 |
-
"
|
| 86 |
-
"Report_Link__c": report_link
|
|
|
|
|
|
|
| 87 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
|
| 91 |
-
logger.info(f"Created Equipment Utilization Record with Id: {response['id']}")
|
| 92 |
-
return {"success": f"Record created successfully! Id: {response['id']}"}
|
| 93 |
except Exception as e:
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
# Build Gradio UI
|
| 98 |
with gr.Blocks() as app:
|
| 99 |
gr.Markdown("# Equipment Utilization Record Uploader")
|
| 100 |
gr.Markdown("Select Equipment and Project, enter utilization details, and save to Salesforce.")
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
-
equipment_dropdown = gr.Dropdown(
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
interactive=True
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
usage_hours = gr.Number(label="Usage Hours", value=0, precision=2)
|
| 116 |
-
idle_hours = gr.Number(label="Idle Hours", value=0, precision=2)
|
| 117 |
-
|
| 118 |
last_maintenance = gr.Textbox(label="Last Maintenance Date (YYYY-MM-DD)", placeholder="Optional")
|
| 119 |
-
utilization_score = gr.Number(label="Utilization Score (%)", value=0, precision=2)
|
| 120 |
-
suggestion_confidence = gr.Number(label="Suggestion Confidence (%)", value=0, precision=2)
|
| 121 |
-
|
| 122 |
-
ai_suggestion = gr.Dropdown(
|
| 123 |
-
label="AI Suggestion",
|
| 124 |
-
choices=ai_suggestion_options,
|
| 125 |
-
value=ai_suggestion_options[0]
|
| 126 |
-
)
|
| 127 |
-
dashboard_flag = gr.Checkbox(label="Dashboard Flag", value=False)
|
| 128 |
-
report_link = gr.Textbox(label="Report Link (URL)", placeholder="Optional")
|
| 129 |
|
|
|
|
| 130 |
output = gr.JSON(label="Salesforce Record Creation Result")
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
equipment_dropdown,
|
| 137 |
-
project_dropdown,
|
| 138 |
-
usage_hours,
|
| 139 |
-
idle_hours,
|
| 140 |
-
last_maintenance,
|
| 141 |
-
utilization_score,
|
| 142 |
-
ai_suggestion,
|
| 143 |
-
suggestion_confidence,
|
| 144 |
-
dashboard_flag,
|
| 145 |
-
report_link
|
| 146 |
-
],
|
| 147 |
outputs=output
|
| 148 |
)
|
| 149 |
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import io
|
| 3 |
import logging
|
| 4 |
+
import sys
|
| 5 |
+
import requests
|
| 6 |
+
import traceback
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import pandas as pd
|
| 9 |
from simple_salesforce import Salesforce
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
level=logging.INFO,
|
| 14 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 15 |
+
handlers=[logging.FileHandler('app_log.txt'), logging.StreamHandler(sys.stdout)]
|
| 16 |
+
)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
+
# Replace with your actual Salesforce credentials
|
| 20 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 21 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 22 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 23 |
|
| 24 |
+
def connect_to_salesforce():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
+
sf_instance = Salesforce(
|
| 27 |
+
username=SALESFORCE_USERNAME,
|
| 28 |
+
password=SALESFORCE_PASSWORD,
|
| 29 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 30 |
+
domain="login"
|
| 31 |
+
)
|
| 32 |
+
logger.info("Connected to Salesforce successfully.")
|
| 33 |
+
return sf_instance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
+
logger.error(f"Salesforce connection failed: {e}")
|
| 36 |
+
raise
|
| 37 |
+
|
| 38 |
+
sf = connect_to_salesforce()
|
| 39 |
+
|
| 40 |
+
# Sample Equipment and Project entries (Name, Salesforce Record ID)
|
| 41 |
+
equipment_choices = [
|
| 42 |
+
("Bulldozer", "a0B3X00000FfPqLUAV"),
|
| 43 |
+
("Crane", "a0B3X00000FfPqLVAV"),
|
| 44 |
+
("Excavator", "a0B3X00000FfPqLWAV")
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
project_choices = [
|
| 48 |
+
("Project Alpha", "a0C3X00000PqPqXUAV"),
|
| 49 |
+
("Project Beta", "a0C3X00000PqPqYUAV"),
|
| 50 |
+
("Project Gamma", "a0C3X00000PqPqZUAV")
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
def get_id_from_name(name, choices):
|
| 54 |
+
for n, id_ in choices:
|
| 55 |
+
if n == name:
|
| 56 |
+
return id_
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
def process_equipment_utilization(equipment_name, project_name, usage_hours, idle_hours,
|
| 60 |
+
movement_frequency, cost_per_hour, report_link, last_maintenance):
|
| 61 |
+
equipment_id = get_id_from_name(equipment_name, equipment_choices)
|
| 62 |
+
project_id = get_id_from_name(project_name, project_choices)
|
| 63 |
|
| 64 |
if not equipment_id or not project_id:
|
| 65 |
+
return {"error": "Could not find Salesforce record IDs for Equipment or Project. Please check names."}
|
| 66 |
|
| 67 |
+
last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
|
| 68 |
+
|
| 69 |
+
# Dummy AI suggestion for example
|
| 70 |
+
ai_suggestion = "Move"
|
| 71 |
+
suggestion_confidence = 90
|
| 72 |
+
utilization_score = 85
|
| 73 |
+
|
| 74 |
+
salesforce_record = {
|
| 75 |
"Equipment_ID__c": equipment_id,
|
| 76 |
"Project_ID__c": project_id,
|
| 77 |
"Usage_Hours__c": usage_hours,
|
| 78 |
"Idle_Hours__c": idle_hours,
|
|
|
|
|
|
|
| 79 |
"AI_Suggestion__c": ai_suggestion,
|
| 80 |
+
"Suggestion_Confidence__c": suggestion_confidence,
|
| 81 |
+
"Utilization_Score__c": utilization_score,
|
| 82 |
+
"Report_Link__c": report_link,
|
| 83 |
+
"Last_Maintenance__c": last_maintenance_str,
|
| 84 |
+
"Dashboard_Flag__c": False
|
| 85 |
}
|
| 86 |
+
try:
|
| 87 |
+
response = sf.Equipment_Utilization_Record__c.create(salesforce_record)
|
| 88 |
+
logger.info(f"Salesforce Equipment Utilization Record created: {response}")
|
| 89 |
+
return {
|
| 90 |
+
"Salesforce_Record_Id": response["id"],
|
| 91 |
+
"status": "Success",
|
| 92 |
+
"AI_Suggestion": ai_suggestion,
|
| 93 |
+
"Suggestion_Confidence": suggestion_confidence,
|
| 94 |
+
"Utilization_Score": utilization_score,
|
| 95 |
+
"Report_Link": report_link
|
| 96 |
+
}
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logger.error(f"Error creating Salesforce record: {e}")
|
| 99 |
+
return {"error": str(e)}
|
| 100 |
+
|
| 101 |
+
def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
| 102 |
+
movement_frequency, cost_per_hour,
|
| 103 |
+
report_link, last_maintenance):
|
| 104 |
+
try:
|
| 105 |
+
usage_hours = float(usage_hours)
|
| 106 |
+
idle_hours = float(idle_hours)
|
| 107 |
+
movement_frequency = float(movement_frequency)
|
| 108 |
+
cost_per_hour = float(cost_per_hour)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return {"error": f"Invalid numeric input: {str(e)}"}
|
| 111 |
|
| 112 |
try:
|
| 113 |
+
last_maintenance_dt = datetime.datetime.strptime(last_maintenance, "%Y-%m-%d") if last_maintenance else None
|
|
|
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
+
return {"error": f"Invalid date format for Last Maintenance (expected YYYY-MM-DD): {str(e)}"}
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
result = process_equipment_utilization(
|
| 119 |
+
equipment_name,
|
| 120 |
+
project_name,
|
| 121 |
+
usage_hours,
|
| 122 |
+
idle_hours,
|
| 123 |
+
movement_frequency,
|
| 124 |
+
cost_per_hour,
|
| 125 |
+
report_link,
|
| 126 |
+
last_maintenance_dt
|
| 127 |
+
)
|
| 128 |
+
return result
|
| 129 |
+
except Exception as e:
|
| 130 |
+
tb = traceback.format_exc()
|
| 131 |
+
logger.error(f"Unexpected error in upload process: {tb}")
|
| 132 |
+
return {"error": f"Unexpected error: {str(e)}\n{tb}"}
|
| 133 |
|
|
|
|
| 134 |
with gr.Blocks() as app:
|
| 135 |
gr.Markdown("# Equipment Utilization Record Uploader")
|
| 136 |
gr.Markdown("Select Equipment and Project, enter utilization details, and save to Salesforce.")
|
| 137 |
|
| 138 |
with gr.Row():
|
| 139 |
+
equipment_dropdown = gr.Dropdown(choices=[name for name, _ in equipment_choices], label="Equipment")
|
| 140 |
+
project_dropdown = gr.Dropdown(choices=[name for name, _ in project_choices], label="Project")
|
| 141 |
+
|
| 142 |
+
usage_hours = gr.Number(label="Usage Hours", value=0, minimum=0)
|
| 143 |
+
idle_hours = gr.Number(label="Idle Hours", value=0, minimum=0)
|
| 144 |
+
movement_frequency = gr.Number(label="Movement Frequency", value=0, minimum=0)
|
| 145 |
+
cost_per_hour = gr.Number(label="Cost per Hour", value=0, minimum=0)
|
| 146 |
+
report_link = gr.Textbox(label="Report Link (URL)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
last_maintenance = gr.Textbox(label="Last Maintenance Date (YYYY-MM-DD)", placeholder="Optional")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
submit_button = gr.Button("Submit")
|
| 150 |
output = gr.JSON(label="Salesforce Record Creation Result")
|
| 151 |
|
| 152 |
+
submit_button.click(
|
| 153 |
+
fn=gradio_upload_process,
|
| 154 |
+
inputs=[equipment_dropdown, project_dropdown, usage_hours, idle_hours,
|
| 155 |
+
movement_frequency, cost_per_hour, report_link, last_maintenance],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
outputs=output
|
| 157 |
)
|
| 158 |
|