Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import logging
|
|
| 3 |
import sys
|
| 4 |
import uuid
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
import requests
|
| 7 |
import gradio as gr
|
| 8 |
from reportlab.lib.pagesizes import letter
|
|
@@ -22,6 +23,7 @@ SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
|
| 22 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 23 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 24 |
|
|
|
|
| 25 |
HUGGINGFACE_API_URL = "https://your-huggingface-model-endpoint"
|
| 26 |
|
| 27 |
# Connect to Salesforce
|
|
@@ -53,11 +55,12 @@ project_choices = [
|
|
| 53 |
|
| 54 |
ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 55 |
|
| 56 |
-
# Generate PDF
|
|
|
|
| 57 |
def generate_pdf_report(record_id, data_dict):
|
| 58 |
report_id = str(uuid.uuid4())[:8]
|
| 59 |
report_filename = f"report_{report_id}.pdf"
|
| 60 |
-
report_path = Path(f"reports/{report_filename}")
|
| 61 |
report_path.parent.mkdir(parents=True, exist_ok=True)
|
| 62 |
|
| 63 |
c = canvas.Canvas(str(report_path), pagesize=letter)
|
|
@@ -71,7 +74,25 @@ def generate_pdf_report(record_id, data_dict):
|
|
| 71 |
y -= 20
|
| 72 |
|
| 73 |
c.save()
|
| 74 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
|
| 77 |
try:
|
|
@@ -114,37 +135,35 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 114 |
suggestion_confidence = 0.9
|
| 115 |
utilization_score = 0.85
|
| 116 |
|
| 117 |
-
|
| 118 |
-
"
|
| 119 |
-
"
|
| 120 |
-
"
|
| 121 |
-
"
|
| 122 |
-
"
|
| 123 |
-
"
|
| 124 |
-
"
|
| 125 |
-
"Report_Link__c": "Pending",
|
| 126 |
-
"Last_Maintenance__c": last_maintenance_str,
|
| 127 |
-
"Dashboard_Flag__c": False
|
| 128 |
}
|
| 129 |
|
| 130 |
try:
|
| 131 |
-
response = sf.Equipment_Utilization_Record__c.create(
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
"
|
| 137 |
-
"
|
| 138 |
-
"
|
| 139 |
-
"
|
| 140 |
-
"
|
| 141 |
-
"
|
| 142 |
-
|
| 143 |
-
}
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c":
|
| 148 |
|
| 149 |
return {
|
| 150 |
"Salesforce_Record_Id": record_id,
|
|
@@ -152,8 +171,8 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 152 |
"AI_Suggestion": ai_suggestion,
|
| 153 |
"Suggestion_Confidence": suggestion_confidence,
|
| 154 |
"Utilization_Score": utilization_score,
|
| 155 |
-
"Report_Link":
|
| 156 |
-
"report_file_path":
|
| 157 |
}
|
| 158 |
except Exception as e:
|
| 159 |
logger.error(f"Error creating or updating Salesforce record: {e}")
|
|
@@ -217,7 +236,7 @@ with gr.Blocks() as app:
|
|
| 217 |
with gr.Row():
|
| 218 |
submit_button = gr.Button("π Submit", variant="primary")
|
| 219 |
output = gr.JSON(label="π Salesforce Record Creation Result")
|
| 220 |
-
report_file_output = gr.File(label="π Download
|
| 221 |
|
| 222 |
submit_button.click(
|
| 223 |
fn=gradio_upload_process,
|
|
|
|
| 3 |
import sys
|
| 4 |
import uuid
|
| 5 |
from pathlib import Path
|
| 6 |
+
import csv
|
| 7 |
import requests
|
| 8 |
import gradio as gr
|
| 9 |
from reportlab.lib.pagesizes import letter
|
|
|
|
| 23 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 24 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 25 |
|
| 26 |
+
HUGGINGFACE_SPACE_URL = "https://huggingface.co/spaces/vaneshdev/equipment-utilization-app"
|
| 27 |
HUGGINGFACE_API_URL = "https://your-huggingface-model-endpoint"
|
| 28 |
|
| 29 |
# Connect to Salesforce
|
|
|
|
| 55 |
|
| 56 |
ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 57 |
|
| 58 |
+
# Generate PDF report
|
| 59 |
+
|
| 60 |
def generate_pdf_report(record_id, data_dict):
|
| 61 |
report_id = str(uuid.uuid4())[:8]
|
| 62 |
report_filename = f"report_{report_id}.pdf"
|
| 63 |
+
report_path = Path(f"static/reports/{report_filename}")
|
| 64 |
report_path.parent.mkdir(parents=True, exist_ok=True)
|
| 65 |
|
| 66 |
c = canvas.Canvas(str(report_path), pagesize=letter)
|
|
|
|
| 74 |
y -= 20
|
| 75 |
|
| 76 |
c.save()
|
| 77 |
+
return str(report_path)
|
| 78 |
+
|
| 79 |
+
# Generate CSV report and public link
|
| 80 |
+
|
| 81 |
+
def generate_csv_report(record_id, data_dict):
|
| 82 |
+
report_id = str(uuid.uuid4())[:8]
|
| 83 |
+
csv_filename = f"report_{report_id}.csv"
|
| 84 |
+
csv_path = Path(f"static/reports/{csv_filename}")
|
| 85 |
+
csv_path.parent.mkdir(parents=True, exist_ok=True)
|
| 86 |
+
|
| 87 |
+
with open(csv_path, mode='w', newline='') as f:
|
| 88 |
+
writer = csv.writer(f)
|
| 89 |
+
writer.writerow(["Field", "Value"])
|
| 90 |
+
writer.writerow(["Salesforce Record ID", record_id])
|
| 91 |
+
for k, v in data_dict.items():
|
| 92 |
+
writer.writerow([k, v])
|
| 93 |
+
|
| 94 |
+
public_csv_url = f"{HUGGINGFACE_SPACE_URL}/file/static/reports/{csv_filename}"
|
| 95 |
+
return public_csv_url, str(csv_path)
|
| 96 |
|
| 97 |
def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
|
| 98 |
try:
|
|
|
|
| 135 |
suggestion_confidence = 0.9
|
| 136 |
utilization_score = 0.85
|
| 137 |
|
| 138 |
+
summary_data = {
|
| 139 |
+
"Equipment Name": equipment_name,
|
| 140 |
+
"Project": project_name,
|
| 141 |
+
"Usage Hours": usage_hours,
|
| 142 |
+
"Idle Hours": idle_hours,
|
| 143 |
+
"Suggestion": ai_suggestion,
|
| 144 |
+
"Confidence": suggestion_confidence,
|
| 145 |
+
"Utilization Score": utilization_score
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
|
| 148 |
try:
|
| 149 |
+
response = sf.Equipment_Utilization_Record__c.create({
|
| 150 |
+
"Equipment_Name__c": equipment_name,
|
| 151 |
+
"Project_Name__c": project_name,
|
| 152 |
+
"Usage_Hours__c": usage_hours,
|
| 153 |
+
"Idle_Hours__c": idle_hours,
|
| 154 |
+
"AI_Suggestion__c": ai_suggestion,
|
| 155 |
+
"Suggestion_Confidence__c": suggestion_confidence * 100,
|
| 156 |
+
"Utilization_Score__c": utilization_score * 100,
|
| 157 |
+
"Report_Link__c": "Pending",
|
| 158 |
+
"Last_Maintenance__c": last_maintenance_str,
|
| 159 |
+
"Dashboard_Flag__c": False
|
| 160 |
+
})
|
|
|
|
| 161 |
|
| 162 |
+
record_id = response.get("id")
|
| 163 |
+
csv_url, csv_path = generate_csv_report(record_id, summary_data)
|
| 164 |
+
generate_pdf_report(record_id, summary_data)
|
| 165 |
|
| 166 |
+
sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c": csv_url})
|
| 167 |
|
| 168 |
return {
|
| 169 |
"Salesforce_Record_Id": record_id,
|
|
|
|
| 171 |
"AI_Suggestion": ai_suggestion,
|
| 172 |
"Suggestion_Confidence": suggestion_confidence,
|
| 173 |
"Utilization_Score": utilization_score,
|
| 174 |
+
"Report_Link": csv_url,
|
| 175 |
+
"report_file_path": csv_path
|
| 176 |
}
|
| 177 |
except Exception as e:
|
| 178 |
logger.error(f"Error creating or updating Salesforce record: {e}")
|
|
|
|
| 236 |
with gr.Row():
|
| 237 |
submit_button = gr.Button("π Submit", variant="primary")
|
| 238 |
output = gr.JSON(label="π Salesforce Record Creation Result")
|
| 239 |
+
report_file_output = gr.File(label="π Download CSV Report")
|
| 240 |
|
| 241 |
submit_button.click(
|
| 242 |
fn=gradio_upload_process,
|