Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import datetime
|
|
| 2 |
import logging
|
| 3 |
import sys
|
| 4 |
import uuid
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
import csv
|
| 7 |
import gradio as gr
|
|
@@ -16,12 +17,14 @@ logging.basicConfig(
|
|
| 16 |
handlers=[logging.FileHandler('app_log.txt'), logging.StreamHandler(sys.stdout)]
|
| 17 |
)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
-
|
| 20 |
# Salesforce credentials
|
| 21 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 22 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 23 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# Connect to Salesforce
|
| 26 |
def connect_to_salesforce():
|
| 27 |
try:
|
|
@@ -51,13 +54,15 @@ project_choices = [
|
|
| 51 |
|
| 52 |
ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 53 |
|
| 54 |
-
# Generate PDF report
|
| 55 |
def generate_pdf_report(record_id, data_dict):
|
| 56 |
report_id = str(uuid.uuid4())[:8]
|
| 57 |
report_filename = f"report_{report_id}.pdf"
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
|
|
|
| 61 |
c = canvas.Canvas(str(report_path), pagesize=letter)
|
| 62 |
c.setFont("Helvetica", 12)
|
| 63 |
c.drawString(100, 750, f"Equipment Utilization Report")
|
|
@@ -69,14 +74,18 @@ def generate_pdf_report(record_id, data_dict):
|
|
| 69 |
y -= 20
|
| 70 |
|
| 71 |
c.save()
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# Generate CSV report
|
| 75 |
def generate_csv_report(record_id, data_dict):
|
| 76 |
report_id = str(uuid.uuid4())[:8]
|
| 77 |
csv_filename = f"report_{report_id}.csv"
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
with open(csv_path, mode='w', newline='') as f:
|
| 82 |
writer = csv.writer(f)
|
|
@@ -85,15 +94,15 @@ def generate_csv_report(record_id, data_dict):
|
|
| 85 |
for k, v in data_dict.items():
|
| 86 |
writer.writerow([k, v])
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
|
| 91 |
-
# Placeholder response since API endpoint is removed
|
| 92 |
logger.info("AI model endpoint removed; returning placeholder response.")
|
| 93 |
return "No Action", 0.0, 0.0
|
| 94 |
|
| 95 |
def process_equipment_utilization(equipment_name, project_name, usage_hours, idle_hours,
|
| 96 |
-
|
| 97 |
last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
|
| 98 |
|
| 99 |
if not ai_suggestion:
|
|
@@ -112,11 +121,10 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 112 |
"Suggestion": ai_suggestion,
|
| 113 |
"Confidence": suggestion_confidence,
|
| 114 |
"Utilization Score": utilization_score,
|
| 115 |
-
"Cost per Hour": cost_per_hour
|
| 116 |
}
|
| 117 |
|
| 118 |
try:
|
| 119 |
-
# Log the data being sent to Salesforce
|
| 120 |
record_data = {
|
| 121 |
"Equipment_Name__c": equipment_name,
|
| 122 |
"Project_Name__c": project_name,
|
|
@@ -133,17 +141,16 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 133 |
logger.info(f"Creating Salesforce record with data: {record_data}")
|
| 134 |
|
| 135 |
response = sf.Equipment_Utilization_Record__c.create(record_data)
|
| 136 |
-
|
| 137 |
record_id = response.get("id")
|
| 138 |
logger.info(f"Successfully created Salesforce record with ID: {record_id}")
|
| 139 |
|
| 140 |
-
# Generate
|
| 141 |
-
csv_path = generate_csv_report(record_id, summary_data)
|
| 142 |
-
pdf_path = generate_pdf_report(record_id, summary_data)
|
| 143 |
|
| 144 |
-
# Update the Salesforce record with the
|
| 145 |
-
sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c":
|
| 146 |
-
logger.info(f"Updated Salesforce record {record_id} with Report_Link__c: {
|
| 147 |
|
| 148 |
return {
|
| 149 |
"Salesforce_Record_Id": record_id,
|
|
@@ -151,15 +158,16 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 151 |
"AI_Suggestion": ai_suggestion,
|
| 152 |
"Suggestion_Confidence": suggestion_confidence,
|
| 153 |
"Utilization_Score": utilization_score,
|
| 154 |
-
"Report_Link":
|
| 155 |
-
"report_file_path": pdf_path
|
|
|
|
| 156 |
}
|
| 157 |
except Exception as e:
|
| 158 |
logger.error(f"Error creating or updating Salesforce record: {e}")
|
| 159 |
return {"error": str(e)}
|
| 160 |
|
| 161 |
def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
| 162 |
-
|
| 163 |
try:
|
| 164 |
usage_hours = float(usage_hours)
|
| 165 |
idle_hours = float(idle_hours)
|
|
@@ -185,6 +193,12 @@ def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
|
| 185 |
)
|
| 186 |
return result, result.get("report_file_path")
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
with gr.Blocks() as app:
|
| 189 |
gr.Markdown("## π Equipment Utilization Record Uploader")
|
| 190 |
gr.Markdown("Fill in the details below to generate AI suggestions and save them to Salesforce.")
|
|
@@ -214,6 +228,7 @@ with gr.Blocks() as app:
|
|
| 214 |
submit_button = gr.Button("π Submit", variant="primary")
|
| 215 |
output = gr.JSON(label="π Salesforce Record Creation Result")
|
| 216 |
report_file_output = gr.File(label="π Download PDF Report")
|
|
|
|
| 217 |
|
| 218 |
submit_button.click(
|
| 219 |
fn=gradio_upload_process,
|
|
@@ -223,7 +238,7 @@ with gr.Blocks() as app:
|
|
| 223 |
movement_frequency, cost_per_hour,
|
| 224 |
last_maintenance, ai_suggestion_dropdown
|
| 225 |
],
|
| 226 |
-
outputs=[output, report_file_output]
|
| 227 |
)
|
| 228 |
|
| 229 |
app.css = """
|
|
@@ -256,4 +271,6 @@ with gr.Blocks() as app:
|
|
| 256 |
"""
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
| 259 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import logging
|
| 3 |
import sys
|
| 4 |
import uuid
|
| 5 |
+
import os
|
| 6 |
from pathlib import Path
|
| 7 |
import csv
|
| 8 |
import gradio as gr
|
|
|
|
| 17 |
handlers=[logging.FileHandler('app_log.txt'), logging.StreamHandler(sys.stdout)]
|
| 18 |
)
|
| 19 |
logger = logging.getLogger(__name__)
|
|
|
|
| 20 |
# Salesforce credentials
|
| 21 |
SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
| 22 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 23 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 24 |
|
| 25 |
+
# Base URL for your application (replace with your actual domain)
|
| 26 |
+
BASE_URL = "https://huggingface.co/spaces/VaneshDev/EquipmentDashboard" # Change this to your actual domain or use ngrok for testing
|
| 27 |
+
|
| 28 |
# Connect to Salesforce
|
| 29 |
def connect_to_salesforce():
|
| 30 |
try:
|
|
|
|
| 54 |
|
| 55 |
ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 56 |
|
| 57 |
+
# Generate PDF report with public URL
|
| 58 |
def generate_pdf_report(record_id, data_dict):
|
| 59 |
report_id = str(uuid.uuid4())[:8]
|
| 60 |
report_filename = f"report_{report_id}.pdf"
|
| 61 |
+
report_dir = Path("static/reports/")
|
| 62 |
+
report_dir.mkdir(parents=True, exist_ok=True)
|
| 63 |
+
report_path = report_dir / report_filename
|
| 64 |
|
| 65 |
+
# Create PDF content
|
| 66 |
c = canvas.Canvas(str(report_path), pagesize=letter)
|
| 67 |
c.setFont("Helvetica", 12)
|
| 68 |
c.drawString(100, 750, f"Equipment Utilization Report")
|
|
|
|
| 74 |
y -= 20
|
| 75 |
|
| 76 |
c.save()
|
| 77 |
+
|
| 78 |
+
# Generate public URL
|
| 79 |
+
public_url = f"{BASE_URL}/static/reports/{report_filename}"
|
| 80 |
+
return str(report_path), public_url
|
| 81 |
|
| 82 |
+
# Generate CSV report with public URL
|
| 83 |
def generate_csv_report(record_id, data_dict):
|
| 84 |
report_id = str(uuid.uuid4())[:8]
|
| 85 |
csv_filename = f"report_{report_id}.csv"
|
| 86 |
+
csv_dir = Path("static/reports/")
|
| 87 |
+
csv_dir.mkdir(parents=True, exist_ok=True)
|
| 88 |
+
csv_path = csv_dir / csv_filename
|
| 89 |
|
| 90 |
with open(csv_path, mode='w', newline='') as f:
|
| 91 |
writer = csv.writer(f)
|
|
|
|
| 94 |
for k, v in data_dict.items():
|
| 95 |
writer.writerow([k, v])
|
| 96 |
|
| 97 |
+
public_url = f"{BASE_URL}/static/reports/{csv_filename}"
|
| 98 |
+
return str(csv_path), public_url
|
| 99 |
|
| 100 |
def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
|
|
|
|
| 101 |
logger.info("AI model endpoint removed; returning placeholder response.")
|
| 102 |
return "No Action", 0.0, 0.0
|
| 103 |
|
| 104 |
def process_equipment_utilization(equipment_name, project_name, usage_hours, idle_hours,
|
| 105 |
+
movement_frequency, cost_per_hour, last_maintenance, ai_suggestion):
|
| 106 |
last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
|
| 107 |
|
| 108 |
if not ai_suggestion:
|
|
|
|
| 121 |
"Suggestion": ai_suggestion,
|
| 122 |
"Confidence": suggestion_confidence,
|
| 123 |
"Utilization Score": utilization_score,
|
| 124 |
+
"Cost per Hour": cost_per_hour
|
| 125 |
}
|
| 126 |
|
| 127 |
try:
|
|
|
|
| 128 |
record_data = {
|
| 129 |
"Equipment_Name__c": equipment_name,
|
| 130 |
"Project_Name__c": project_name,
|
|
|
|
| 141 |
logger.info(f"Creating Salesforce record with data: {record_data}")
|
| 142 |
|
| 143 |
response = sf.Equipment_Utilization_Record__c.create(record_data)
|
|
|
|
| 144 |
record_id = response.get("id")
|
| 145 |
logger.info(f"Successfully created Salesforce record with ID: {record_id}")
|
| 146 |
|
| 147 |
+
# Generate reports and get public URLs
|
| 148 |
+
csv_path, csv_url = generate_csv_report(record_id, summary_data)
|
| 149 |
+
pdf_path, pdf_url = generate_pdf_report(record_id, summary_data)
|
| 150 |
|
| 151 |
+
# Update the Salesforce record with the public PDF URL
|
| 152 |
+
sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c": pdf_url})
|
| 153 |
+
logger.info(f"Updated Salesforce record {record_id} with Report_Link__c: {pdf_url}")
|
| 154 |
|
| 155 |
return {
|
| 156 |
"Salesforce_Record_Id": record_id,
|
|
|
|
| 158 |
"AI_Suggestion": ai_suggestion,
|
| 159 |
"Suggestion_Confidence": suggestion_confidence,
|
| 160 |
"Utilization_Score": utilization_score,
|
| 161 |
+
"Report_Link": pdf_url,
|
| 162 |
+
"report_file_path": pdf_path,
|
| 163 |
+
"public_report_url": pdf_url
|
| 164 |
}
|
| 165 |
except Exception as e:
|
| 166 |
logger.error(f"Error creating or updating Salesforce record: {e}")
|
| 167 |
return {"error": str(e)}
|
| 168 |
|
| 169 |
def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
| 170 |
+
movement_frequency, cost_per_hour, last_maintenance, ai_suggestion):
|
| 171 |
try:
|
| 172 |
usage_hours = float(usage_hours)
|
| 173 |
idle_hours = float(idle_hours)
|
|
|
|
| 193 |
)
|
| 194 |
return result, result.get("report_file_path")
|
| 195 |
|
| 196 |
+
# Serve static files through Gradio
|
| 197 |
+
def serve_static_files():
|
| 198 |
+
static_dir = Path("static")
|
| 199 |
+
static_dir.mkdir(exist_ok=True)
|
| 200 |
+
return static_dir
|
| 201 |
+
|
| 202 |
with gr.Blocks() as app:
|
| 203 |
gr.Markdown("## π Equipment Utilization Record Uploader")
|
| 204 |
gr.Markdown("Fill in the details below to generate AI suggestions and save them to Salesforce.")
|
|
|
|
| 228 |
submit_button = gr.Button("π Submit", variant="primary")
|
| 229 |
output = gr.JSON(label="π Salesforce Record Creation Result")
|
| 230 |
report_file_output = gr.File(label="π Download PDF Report")
|
| 231 |
+
report_url_output = gr.Textbox(label="π Public Report URL", interactive=False)
|
| 232 |
|
| 233 |
submit_button.click(
|
| 234 |
fn=gradio_upload_process,
|
|
|
|
| 238 |
movement_frequency, cost_per_hour,
|
| 239 |
last_maintenance, ai_suggestion_dropdown
|
| 240 |
],
|
| 241 |
+
outputs=[output, report_file_output, report_url_output]
|
| 242 |
)
|
| 243 |
|
| 244 |
app.css = """
|
|
|
|
| 271 |
"""
|
| 272 |
|
| 273 |
if __name__ == "__main__":
|
| 274 |
+
# Ensure static directory exists
|
| 275 |
+
serve_static_files()
|
| 276 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|