Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,10 +67,10 @@ def call_ai_model(usage, idle, freq, cost, last):
|
|
| 67 |
cost = float(cost) if cost is not None and not np.isnan(cost) else 0.0
|
| 68 |
|
| 69 |
total = usage + idle
|
| 70 |
-
|
| 71 |
-
utilization_percent =
|
| 72 |
|
| 73 |
-
# AI Suggestion
|
| 74 |
if utilization_percent < 30:
|
| 75 |
sug = "Pause Rent"
|
| 76 |
elif utilization_percent < 60:
|
|
@@ -80,16 +80,17 @@ def call_ai_model(usage, idle, freq, cost, last):
|
|
| 80 |
else:
|
| 81 |
sug = "Replace"
|
| 82 |
|
| 83 |
-
# Confidence calculation
|
| 84 |
-
base_conf =
|
|
|
|
| 85 |
if idle > usage:
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
freq_factor = min(freq / 10.0, 1.0)
|
| 88 |
base_conf *= (0.7 + 0.3 * freq_factor)
|
| 89 |
-
confidence
|
| 90 |
-
|
| 91 |
-
if np.isnan(confidence) or np.isnan(utilization_percent):
|
| 92 |
-
raise ValueError("Computed values resulted in NaN. Please check input data.")
|
| 93 |
|
| 94 |
return sug, confidence, utilization_percent
|
| 95 |
|
|
@@ -154,14 +155,12 @@ def process_equipment_utilization(equip, proj, use_h, idle_h, move_f, cost_h, la
|
|
| 154 |
if not rec_id:
|
| 155 |
raise ValueError("Salesforce record creation succeeded, but no record ID was returned.")
|
| 156 |
|
| 157 |
-
# Create safe strings for filename
|
| 158 |
safe_equip = equip.replace(" ", "_")
|
| 159 |
safe_proj = proj.replace(" ", "_")
|
| 160 |
uid = uuid.uuid4().hex[:8]
|
| 161 |
pdf_path = Path(f"static/reports/report_{safe_equip}_{safe_proj}_{uid}.pdf")
|
| 162 |
pdf_path.parent.mkdir(parents=True, exist_ok=True)
|
| 163 |
|
| 164 |
-
# Create PDF with dynamic title
|
| 165 |
c = canvas.Canvas(str(pdf_path), pagesize=letter)
|
| 166 |
c.setFont("Helvetica-Bold", 14)
|
| 167 |
title_str = f"Equipment Utilization Report - {equip} ({proj})"
|
|
|
|
| 67 |
cost = float(cost) if cost is not None and not np.isnan(cost) else 0.0
|
| 68 |
|
| 69 |
total = usage + idle
|
| 70 |
+
utilization_ratio = usage / total if total > 0 else 0.0
|
| 71 |
+
utilization_percent = utilization_ratio * 100
|
| 72 |
|
| 73 |
+
# AI Suggestion thresholds based on utilization percent
|
| 74 |
if utilization_percent < 30:
|
| 75 |
sug = "Pause Rent"
|
| 76 |
elif utilization_percent < 60:
|
|
|
|
| 80 |
else:
|
| 81 |
sug = "Replace"
|
| 82 |
|
| 83 |
+
# Confidence calculation
|
| 84 |
+
base_conf = utilization_ratio
|
| 85 |
+
# Penalize confidence proportionally if idle > usage
|
| 86 |
if idle > usage:
|
| 87 |
+
diff_ratio = (idle - usage) / total
|
| 88 |
+
base_conf -= diff_ratio * 0.5
|
| 89 |
+
# Boost confidence by movement frequency (normalized 0-1)
|
| 90 |
freq_factor = min(freq / 10.0, 1.0)
|
| 91 |
base_conf *= (0.7 + 0.3 * freq_factor)
|
| 92 |
+
# Clamp confidence between 0.05 and 1.0
|
| 93 |
+
confidence = max(0.05, min(base_conf, 1.0))
|
|
|
|
|
|
|
| 94 |
|
| 95 |
return sug, confidence, utilization_percent
|
| 96 |
|
|
|
|
| 155 |
if not rec_id:
|
| 156 |
raise ValueError("Salesforce record creation succeeded, but no record ID was returned.")
|
| 157 |
|
|
|
|
| 158 |
safe_equip = equip.replace(" ", "_")
|
| 159 |
safe_proj = proj.replace(" ", "_")
|
| 160 |
uid = uuid.uuid4().hex[:8]
|
| 161 |
pdf_path = Path(f"static/reports/report_{safe_equip}_{safe_proj}_{uid}.pdf")
|
| 162 |
pdf_path.parent.mkdir(parents=True, exist_ok=True)
|
| 163 |
|
|
|
|
| 164 |
c = canvas.Canvas(str(pdf_path), pagesize=letter)
|
| 165 |
c.setFont("Helvetica-Bold", 14)
|
| 166 |
title_str = f"Equipment Utilization Report - {equip} ({proj})"
|