Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,151 @@
|
|
| 1 |
-
import datetime
|
| 2 |
-
import logging
|
| 3 |
-
import sys
|
| 4 |
-
import requests
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
from simple_salesforce import Salesforce
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
logging.basicConfig(
|
| 10 |
-
level=logging.INFO,
|
| 11 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 12 |
-
handlers=[logging.StreamHandler(sys.stdout), logging.FileHandler("app_log.txt")]
|
| 13 |
-
)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
-
# Salesforce
|
| 17 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 18 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 19 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
)
|
| 32 |
-
logger.info("Connected to Salesforce successfully.")
|
| 33 |
-
return sf_instance
|
| 34 |
except Exception as e:
|
| 35 |
-
logger.error(f"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
sf = connect_to_salesforce()
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"last_maintenance": last_maintenance_str or ""
|
| 48 |
-
}
|
| 49 |
-
logger.info(f"Calling AI model with payload: {payload}")
|
| 50 |
-
response = requests.post(HUGGINGFACE_API_URL, json=payload, timeout=15)
|
| 51 |
-
response.raise_for_status()
|
| 52 |
-
data = response.json()
|
| 53 |
-
logger.info(f"Received AI response: {data}")
|
| 54 |
-
|
| 55 |
-
suggestion = data.get("suggestion", "No Action")
|
| 56 |
-
confidence = float(data.get("confidence", 0))
|
| 57 |
-
utilization_score = float(data.get("utilization_score", 0))
|
| 58 |
-
return suggestion, confidence, utilization_score
|
| 59 |
except Exception as e:
|
| 60 |
-
logger.error(f"Error
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
"Equipment_ID__c": equipment_id,
|
| 75 |
"Project_ID__c": project_id,
|
| 76 |
"Usage_Hours__c": usage_hours,
|
| 77 |
"Idle_Hours__c": idle_hours,
|
|
|
|
|
|
|
| 78 |
"AI_Suggestion__c": ai_suggestion,
|
| 79 |
-
"Suggestion_Confidence__c": suggestion_confidence,
|
| 80 |
-
"
|
| 81 |
-
"Report_Link__c": report_link
|
| 82 |
-
"Last_Maintenance__c": last_maintenance_str,
|
| 83 |
-
"Dashboard_Flag__c": False
|
| 84 |
}
|
| 85 |
|
| 86 |
try:
|
| 87 |
-
response = sf.Equipment_Utilization_Record__c.create(
|
| 88 |
-
logger.info(f"Record
|
| 89 |
-
return {
|
| 90 |
-
"Salesforce_Record_Id": response.get("id"),
|
| 91 |
-
"status": "Success",
|
| 92 |
-
"AI_Suggestion": ai_suggestion,
|
| 93 |
-
"Suggestion_Confidence": suggestion_confidence,
|
| 94 |
-
"Utilization_Score": utilization_score
|
| 95 |
-
}
|
| 96 |
except Exception as e:
|
| 97 |
-
logger.error(f"
|
| 98 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
try:
|
| 104 |
-
usage_hours = float(usage_hours)
|
| 105 |
-
idle_hours = float(idle_hours)
|
| 106 |
-
movement_frequency = float(movement_frequency)
|
| 107 |
-
cost_per_hour = float(cost_per_hour)
|
| 108 |
-
except Exception as e:
|
| 109 |
-
return {"error": f"Invalid numeric input: {e}"}
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
except Exception as e:
|
| 115 |
-
return {"error": f"Invalid date format for Last Maintenance (expected YYYY-MM-DD): {e}"}
|
| 116 |
-
|
| 117 |
-
return process_equipment_utilization(
|
| 118 |
-
equipment_id,
|
| 119 |
-
project_id,
|
| 120 |
-
usage_hours,
|
| 121 |
-
idle_hours,
|
| 122 |
-
movement_frequency,
|
| 123 |
-
cost_per_hour,
|
| 124 |
-
report_link,
|
| 125 |
-
last_maintenance_dt
|
| 126 |
-
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
from simple_salesforce import Salesforce
|
| 4 |
|
| 5 |
+
# Configure logging
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
+
# Salesforce Credentials
|
| 10 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 11 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 12 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 13 |
|
| 14 |
+
# Connect to Salesforce
|
| 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 |
+
query = "SELECT Id, Name FROM Equipment__c LIMIT 100"
|
| 31 |
+
result = sf.query(query)
|
| 32 |
+
choices = [(rec["Id"], rec["Name"]) for rec in result["records"]]
|
| 33 |
+
logger.info(f"Fetched {len(choices)} Equipment records")
|
| 34 |
+
return choices
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
logger.error(f"Error fetching equipment records: {e}")
|
| 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"Error fetching project records: {e}")
|
| 49 |
+
return []
|
| 50 |
+
|
| 51 |
+
equipment_choices = fetch_equipment_records()
|
| 52 |
+
project_choices = fetch_project_records()
|
| 53 |
+
|
| 54 |
+
# AI Suggestion options (must match Salesforce picklist exactly)
|
| 55 |
+
ai_suggestion_options = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 56 |
+
|
| 57 |
+
def create_equipment_utilization_record(
|
| 58 |
+
equipment_name,
|
| 59 |
+
project_name,
|
| 60 |
+
usage_hours,
|
| 61 |
+
idle_hours,
|
| 62 |
+
last_maintenance,
|
| 63 |
+
utilization_score,
|
| 64 |
+
ai_suggestion,
|
| 65 |
+
suggestion_confidence,
|
| 66 |
+
dashboard_flag,
|
| 67 |
+
report_link
|
| 68 |
+
):
|
| 69 |
+
# Find Salesforce IDs from the selected names
|
| 70 |
+
equipment_id = next((eid for eid, ename in equipment_choices if ename == equipment_name), None)
|
| 71 |
+
project_id = next((pid for pid, pname in project_choices if pname == project_name), None)
|
| 72 |
+
|
| 73 |
+
if not equipment_id or not project_id:
|
| 74 |
+
return {"error": "Invalid Equipment or Project selected. Please select valid options from dropdown."}
|
| 75 |
+
|
| 76 |
+
record = {
|
| 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 if suggestion_confidence else 0,
|
| 85 |
+
"Dashboard_Flag__c": dashboard_flag if dashboard_flag is not None else False,
|
| 86 |
+
"Report_Link__c": report_link if report_link else ""
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
response = sf.Equipment_Utilization_Record__c.create(record)
|
| 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 |
+
logger.error(f"Error creating Equipment Utilization Record: {e}")
|
| 95 |
+
return {"error": f"Failed to create record: {str(e)}"}
|
| 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 |
+
label="Equipment",
|
| 105 |
+
choices=[ename for _, ename in equipment_choices],
|
| 106 |
+
interactive=True
|
| 107 |
+
)
|
| 108 |
+
project_dropdown = gr.Dropdown(
|
| 109 |
+
label="Project",
|
| 110 |
+
choices=[pname for _, pname in project_choices],
|
| 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 |
+
submit_btn = gr.Button("Submit Record")
|
| 133 |
+
submit_btn.click(
|
| 134 |
+
fn=create_equipment_utilization_record,
|
| 135 |
+
inputs=[
|
| 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 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
+
app.launch()
|