Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
|
|
| 1 |
import logging
|
| 2 |
-
import sys
|
| 3 |
import gradio as gr
|
| 4 |
from simple_salesforce import Salesforce
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
-
logging.basicConfig(
|
| 8 |
-
level=logging.INFO,
|
| 9 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 10 |
-
handlers=[logging.StreamHandler(sys.stdout)],
|
| 11 |
-
)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 16 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 17 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
|
@@ -23,126 +19,89 @@ def connect_to_salesforce():
|
|
| 23 |
username=SALESFORCE_USERNAME,
|
| 24 |
password=SALESFORCE_PASSWORD,
|
| 25 |
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 26 |
-
domain="login"
|
| 27 |
)
|
| 28 |
-
logger.info("Connected to Salesforce
|
| 29 |
return sf
|
| 30 |
except Exception as e:
|
| 31 |
-
logger.error(f"
|
| 32 |
raise
|
| 33 |
|
| 34 |
sf = connect_to_salesforce()
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return None
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
confidence = 90.0
|
| 58 |
-
utilization_score = 85.0
|
| 59 |
-
else:
|
| 60 |
-
suggestion = "Pause Rent"
|
| 61 |
-
confidence = 75.0
|
| 62 |
-
utilization_score = 60.0
|
| 63 |
-
return suggestion, confidence, utilization_score
|
| 64 |
-
|
| 65 |
-
def process_equipment_utilization(
|
| 66 |
-
equipment_name,
|
| 67 |
-
project_name,
|
| 68 |
-
usage_hours,
|
| 69 |
-
idle_hours,
|
| 70 |
-
report_link,
|
| 71 |
-
last_maintenance_date,
|
| 72 |
-
):
|
| 73 |
-
equipment_id = lookup_record_id("Equipment__c", equipment_name)
|
| 74 |
-
project_id = lookup_record_id("Project__c", project_name)
|
| 75 |
-
|
| 76 |
-
if not equipment_id or not project_id:
|
| 77 |
-
return {
|
| 78 |
-
"status": "Error",
|
| 79 |
-
"message": "Could not find Salesforce record IDs for Equipment or Project. Please check names.",
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
ai_suggestion, suggestion_confidence, utilization_score = dummy_ai_model(
|
| 83 |
-
usage_hours, idle_hours
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
record = {
|
| 87 |
-
"Equipment_ID__c": equipment_id,
|
| 88 |
-
"Project_ID__c": project_id,
|
| 89 |
-
"Usage_Hours__c": usage_hours,
|
| 90 |
-
"Idle_Hours__c": idle_hours,
|
| 91 |
-
"AI_Suggestion__c": ai_suggestion,
|
| 92 |
-
"Suggestion_Confidence__c": suggestion_confidence,
|
| 93 |
-
"Utilization_Score__c": utilization_score,
|
| 94 |
-
"Report_Link__c": report_link,
|
| 95 |
-
"Last_Maintenance__c": last_maintenance_date,
|
| 96 |
-
"Dashboard_Flag__c": False,
|
| 97 |
-
}
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# Gradio UI function
|
| 114 |
-
def gradio_submit(
|
| 115 |
-
equipment_name,
|
| 116 |
-
project_name,
|
| 117 |
-
usage_hours,
|
| 118 |
-
idle_hours,
|
| 119 |
-
report_link,
|
| 120 |
-
last_maintenance_date,
|
| 121 |
-
):
|
| 122 |
-
return process_equipment_utilization(
|
| 123 |
-
equipment_name,
|
| 124 |
-
project_name,
|
| 125 |
-
usage_hours,
|
| 126 |
-
idle_hours,
|
| 127 |
-
report_link,
|
| 128 |
-
last_maintenance_date,
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
iface = gr.Interface(
|
| 133 |
-
fn=gradio_submit,
|
| 134 |
-
inputs=[
|
| 135 |
-
gr.Textbox(label="Equipment Name (Salesforce Name field)"),
|
| 136 |
-
gr.Textbox(label="Project Name (Salesforce Name field)"),
|
| 137 |
-
gr.Number(label="Usage Hours", value=0, minimum=0),
|
| 138 |
-
gr.Number(label="Idle Hours", value=0, minimum=0),
|
| 139 |
-
gr.Textbox(label="Report Link (URL)", value=""),
|
| 140 |
-
gr.Textbox(label="Last Maintenance Date (YYYY-MM-DD)", value=""),
|
| 141 |
-
],
|
| 142 |
-
outputs=gr.JSON(label="Salesforce Insert Result"),
|
| 143 |
-
title="Equipment Utilization Record Creator with Name Lookup",
|
| 144 |
-
description="Input Equipment and Project names; the app looks up Salesforce IDs and creates the record.",
|
| 145 |
-
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
-
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
import logging
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from simple_salesforce import Salesforce
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
+
# Salesforce credentials
|
| 11 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 12 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 13 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
|
|
|
| 19 |
username=SALESFORCE_USERNAME,
|
| 20 |
password=SALESFORCE_PASSWORD,
|
| 21 |
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 22 |
+
domain="login"
|
| 23 |
)
|
| 24 |
+
logger.info("Connected to Salesforce")
|
| 25 |
return sf
|
| 26 |
except Exception as e:
|
| 27 |
+
logger.error(f"Salesforce connection error: {e}")
|
| 28 |
raise
|
| 29 |
|
| 30 |
sf = connect_to_salesforce()
|
| 31 |
|
| 32 |
+
# Load your equipment CSV data here
|
| 33 |
+
df = pd.read_csv("equipment_data.csv")
|
| 34 |
+
equipment_types = sorted(df["Equipment_Type__c"].dropna().unique().tolist())
|
| 35 |
+
suggestion_types = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 36 |
+
|
| 37 |
+
# Dummy AI model that returns Move with 85% confidence
|
| 38 |
+
def dummy_ai_model(row):
|
| 39 |
+
return "Move", 85
|
| 40 |
+
|
| 41 |
+
# Filter equipment and generate details + confidence output
|
| 42 |
+
def filter_equipment(equipment_type, suggestion):
|
| 43 |
+
if not equipment_type or not suggestion:
|
| 44 |
+
return "", ""
|
| 45 |
+
filtered = df[df["Equipment_Type__c"] == equipment_type].copy()
|
| 46 |
+
if filtered.empty:
|
| 47 |
+
return "No equipment found.", ""
|
| 48 |
+
|
| 49 |
+
filtered["AI_Suggestion__c"] = None
|
| 50 |
+
filtered["Suggestion_Confidence__c"] = 0
|
| 51 |
+
for idx, row in filtered.iterrows():
|
| 52 |
+
s, conf = dummy_ai_model(row)
|
| 53 |
+
filtered.at[idx, "AI_Suggestion__c"] = s
|
| 54 |
+
filtered.at[idx, "Suggestion_Confidence__c"] = conf
|
| 55 |
+
filtered = filtered[filtered["AI_Suggestion__c"] == suggestion]
|
| 56 |
+
if filtered.empty:
|
| 57 |
+
return "No equipment matching suggestion.", ""
|
| 58 |
+
|
| 59 |
+
details_list = []
|
| 60 |
+
confidence_list = []
|
| 61 |
+
for _, row in filtered.iterrows():
|
| 62 |
+
details_list.append(
|
| 63 |
+
f"ID: {row['Equipment_ID__c']} | Usage: {row['Usage_Hours__c']} hrs | Idle: {row['Idle_Hours__c']} hrs | AI Suggestion: {row['AI_Suggestion__c']}"
|
| 64 |
+
)
|
| 65 |
+
confidence_list.append(f"{row['Equipment_ID__c']}: {row['Suggestion_Confidence__c']}%")
|
| 66 |
+
return "\n\n".join(details_list), "\n".join(confidence_list)
|
| 67 |
+
|
| 68 |
+
# Export filtered data as CSV file
|
| 69 |
+
def export_csv(equipment_type, suggestion):
|
| 70 |
+
filtered = df[df["Equipment_Type__c"] == equipment_type].copy()
|
| 71 |
+
if filtered.empty:
|
| 72 |
+
return None
|
| 73 |
+
filtered["AI_Suggestion__c"] = None
|
| 74 |
+
filtered["Suggestion_Confidence__c"] = 0
|
| 75 |
+
for idx, row in filtered.iterrows():
|
| 76 |
+
s, conf = dummy_ai_model(row)
|
| 77 |
+
filtered.at[idx, "AI_Suggestion__c"] = s
|
| 78 |
+
filtered.at[idx, "Suggestion_Confidence__c"] = conf
|
| 79 |
+
filtered = filtered[filtered["AI_Suggestion__c"] == suggestion]
|
| 80 |
+
if filtered.empty:
|
| 81 |
return None
|
| 82 |
+
csv_path = "filtered_equipment.csv"
|
| 83 |
+
filtered.to_csv(csv_path, index=False)
|
| 84 |
+
return csv_path
|
| 85 |
|
| 86 |
+
# Build Gradio UI
|
| 87 |
+
with gr.Blocks(theme=gr.themes.Dark()) as app:
|
| 88 |
+
gr.Markdown("# Equipment Utilization Dashboard")
|
| 89 |
+
gr.Markdown("Filter equipment by type and AI suggestion to optimize utilization.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
with gr.Row():
|
| 92 |
+
etype = gr.Dropdown(choices=equipment_types, label="Equipment Type")
|
| 93 |
+
suggestion = gr.Dropdown(choices=suggestion_types, label="Suggestion Type")
|
| 94 |
+
|
| 95 |
+
details = gr.Textbox(label="Equipment Details", lines=8)
|
| 96 |
+
confidence = gr.Textbox(label="Confidence Scores", lines=5)
|
| 97 |
+
|
| 98 |
+
export_btn = gr.Button("Export to CSV")
|
| 99 |
+
csv_file = gr.File(label="Download CSV")
|
| 100 |
+
|
| 101 |
+
# Callbacks
|
| 102 |
+
etype.change(fn=filter_equipment, inputs=[etype, suggestion], outputs=[details, confidence])
|
| 103 |
+
suggestion.change(fn=filter_equipment, inputs=[etype, suggestion], outputs=[details, confidence])
|
| 104 |
+
export_btn.click(fn=export_csv, inputs=[etype, suggestion], outputs=csv_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
app.launch()
|