Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import numpy as np
|
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from typing import List
|
| 9 |
import os
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# =================== PAGE CONFIG ===================
|
| 12 |
st.set_page_config(
|
|
@@ -1070,8 +1072,447 @@ except Exception as e:
|
|
| 1070 |
st.error(f"⚠️ Error Risk Matrix: {e}")
|
| 1071 |
# st.exception(e) # Uncomment for debugging
|
| 1072 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1073 |
# =================== 6. ✅ AI INSIGHT ENGINE (BARU - BERDASARKAN DATA & RATIO) ===================
|
| 1074 |
-
|
|
|
|
| 1075 |
|
| 1076 |
def compute_ai_insights(df: pd.DataFrame) -> List[dict]:
|
| 1077 |
"""
|
|
|
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from typing import List
|
| 9 |
import os
|
| 10 |
+
import sklearn
|
| 11 |
+
import kaleido
|
| 12 |
|
| 13 |
# =================== PAGE CONFIG ===================
|
| 14 |
st.set_page_config(
|
|
|
|
| 1072 |
st.error(f"⚠️ Error Risk Matrix: {e}")
|
| 1073 |
# st.exception(e) # Uncomment for debugging
|
| 1074 |
|
| 1075 |
+
# =================== 7. PREDICTIVE INSIGHTS (FINAL — PLN BLUE EDITION v2) ===================
|
| 1076 |
+
# ✅ Panel 1: ONLY Coverage < 90% AND Slope < 0
|
| 1077 |
+
# ✅ Panel 3: ONLY Coverage = 100% AND Slope > 0 → Avg/Month
|
| 1078 |
+
# ✅ Estetik: Sortable, Hover, Zebra, PLN Blue, No Emoticons
|
| 1079 |
+
|
| 1080 |
+
st.markdown("<h3 class='section-title'>OBJECTIVE 6 - Predictive Dashboard & Early Warning Signals</h3>", unsafe_allow_html=True)
|
| 1081 |
+
|
| 1082 |
+
|
| 1083 |
+
# ✅ Enhanced CSS + Minimal Sortable JS
|
| 1084 |
+
st.markdown("""
|
| 1085 |
+
<style>
|
| 1086 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 1087 |
+
|
| 1088 |
+
.predictive-panel {
|
| 1089 |
+
margin-bottom: 28px;
|
| 1090 |
+
background: white;
|
| 1091 |
+
border-radius: 12px;
|
| 1092 |
+
box-shadow: 0 4px 16px rgba(0,0,0,0.05);
|
| 1093 |
+
overflow: hidden;
|
| 1094 |
+
border: 1px solid #edf2f7;
|
| 1095 |
+
}
|
| 1096 |
+
.predictive-header {
|
| 1097 |
+
background: #E3F2FD;
|
| 1098 |
+
color: #003DA5;
|
| 1099 |
+
font-weight: 700;
|
| 1100 |
+
font-size: 1.1em;
|
| 1101 |
+
padding: 14px 20px;
|
| 1102 |
+
border-left: 4px solid #003DA5;
|
| 1103 |
+
}
|
| 1104 |
+
.predictive-table-wrapper {
|
| 1105 |
+
padding: 0 20px 20px;
|
| 1106 |
+
}
|
| 1107 |
+
.predictive-table-wrapper table {
|
| 1108 |
+
width: 100%;
|
| 1109 |
+
border-collapse: collapse;
|
| 1110 |
+
font-family: 'Inter', sans-serif;
|
| 1111 |
+
font-size: 0.94em;
|
| 1112 |
+
margin-top: 12px;
|
| 1113 |
+
}
|
| 1114 |
+
.predictive-table-wrapper th,
|
| 1115 |
+
.predictive-table-wrapper td {
|
| 1116 |
+
text-align: center !important;
|
| 1117 |
+
padding: 12px 10px;
|
| 1118 |
+
border: 1px solid #e5e7eb;
|
| 1119 |
+
vertical-align: middle;
|
| 1120 |
+
}
|
| 1121 |
+
.predictive-table-wrapper th {
|
| 1122 |
+
background-color: #f8fafc;
|
| 1123 |
+
font-weight: 600;
|
| 1124 |
+
color: #003DA5;
|
| 1125 |
+
cursor: pointer;
|
| 1126 |
+
user-select: none;
|
| 1127 |
+
position: relative;
|
| 1128 |
+
}
|
| 1129 |
+
.predictive-table-wrapper th:hover {
|
| 1130 |
+
background-color: #edf2f7;
|
| 1131 |
+
}
|
| 1132 |
+
.predictive-table-wrapper th::after {
|
| 1133 |
+
content: " ⇵";
|
| 1134 |
+
opacity: 0.4;
|
| 1135 |
+
margin-left: 4px;
|
| 1136 |
+
}
|
| 1137 |
+
.predictive-table-wrapper th.asc::after {
|
| 1138 |
+
content: " ↑";
|
| 1139 |
+
opacity: 1;
|
| 1140 |
+
color: #003DA5;
|
| 1141 |
+
}
|
| 1142 |
+
.predictive-table-wrapper th.desc::after {
|
| 1143 |
+
content: " ↓";
|
| 1144 |
+
opacity: 1;
|
| 1145 |
+
color: #c62828;
|
| 1146 |
+
}
|
| 1147 |
+
.predictive-table-wrapper tr:nth-child(even) {
|
| 1148 |
+
background-color: #fafcff;
|
| 1149 |
+
}
|
| 1150 |
+
.predictive-table-wrapper tr:hover {
|
| 1151 |
+
background-color: #f0f7ff !important;
|
| 1152 |
+
}
|
| 1153 |
+
.predictive-note {
|
| 1154 |
+
font-size: 0.86em;
|
| 1155 |
+
color: #64748b;
|
| 1156 |
+
margin-top: 10px;
|
| 1157 |
+
padding: 0 20px;
|
| 1158 |
+
line-height: 1.5;
|
| 1159 |
+
}
|
| 1160 |
+
.spark {
|
| 1161 |
+
font-family: 'Courier New', monospace;
|
| 1162 |
+
font-weight: bold;
|
| 1163 |
+
}
|
| 1164 |
+
.status-active { color: #2e7d32; font-weight: bold; }
|
| 1165 |
+
.status-neutral { color: #f57c00; }
|
| 1166 |
+
.status-inactive { color: #c62828; font-weight: bold; }
|
| 1167 |
+
.trend-rising { color: #c62828; font-weight: 600; }
|
| 1168 |
+
.trend-stable { color: #388e3c; }
|
| 1169 |
+
.trend-declining { color: #d32f2f; }
|
| 1170 |
+
.footer-insight {
|
| 1171 |
+
background: #003DA5;
|
| 1172 |
+
color: white;
|
| 1173 |
+
padding: 14px 24px;
|
| 1174 |
+
border-radius: 10px;
|
| 1175 |
+
font-weight: 600;
|
| 1176 |
+
margin-top: 20px;
|
| 1177 |
+
text-align: center;
|
| 1178 |
+
font-size: 1.08em;
|
| 1179 |
+
box-shadow: 0 3px 10px rgba(0,61,165,0.15);
|
| 1180 |
+
}
|
| 1181 |
+
.warning-box {
|
| 1182 |
+
background: #fff8e1;
|
| 1183 |
+
border-left: 4px solid #ffc107;
|
| 1184 |
+
padding: 12px 16px;
|
| 1185 |
+
font-size: 0.9em;
|
| 1186 |
+
color: #5d4037;
|
| 1187 |
+
margin: 10px 20px 0;
|
| 1188 |
+
border-radius: 0 6px 6px 0;
|
| 1189 |
+
}
|
| 1190 |
+
</style>
|
| 1191 |
+
|
| 1192 |
+
<script>
|
| 1193 |
+
function makeSortable(tableId) {
|
| 1194 |
+
const table = document.getElementById(tableId);
|
| 1195 |
+
if (!table) return;
|
| 1196 |
+
let headers = table.querySelectorAll("th");
|
| 1197 |
+
headers.forEach((header, i) => {
|
| 1198 |
+
header.onclick = () => {
|
| 1199 |
+
headers.forEach(h => h.classList.remove('asc', 'desc'));
|
| 1200 |
+
let rows = Array.from(table.querySelectorAll("tr:nth-child(n+2)"));
|
| 1201 |
+
let isNumeric = !isNaN(rows[0]?.cells[i]?.textContent.replace(/[^0-9.-]/g, ''));
|
| 1202 |
+
rows.sort((a, b) => {
|
| 1203 |
+
let aVal = a.cells[i].textContent.trim();
|
| 1204 |
+
let bVal = b.cells[i].textContent.trim();
|
| 1205 |
+
if (isNumeric) {
|
| 1206 |
+
aVal = parseFloat(aVal.replace(/[^0-9.-]/g, '')) || 0;
|
| 1207 |
+
bVal = parseFloat(bVal.replace(/[^0-9.-]/g, '')) || 0;
|
| 1208 |
+
}
|
| 1209 |
+
return header.classList.contains('asc') ? bVal - aVal : aVal - bVal;
|
| 1210 |
+
});
|
| 1211 |
+
header.classList.toggle('asc');
|
| 1212 |
+
header.classList.toggle('desc');
|
| 1213 |
+
rows.forEach(row => table.querySelector('tbody').appendChild(row));
|
| 1214 |
+
};
|
| 1215 |
+
});
|
| 1216 |
+
}
|
| 1217 |
+
setTimeout(() => {
|
| 1218 |
+
makeSortable('tbl-locations');
|
| 1219 |
+
makeSortable('tbl-divisions');
|
| 1220 |
+
makeSortable('tbl-issues');
|
| 1221 |
+
}, 800);
|
| 1222 |
+
</script>
|
| 1223 |
+
""", unsafe_allow_html=True)
|
| 1224 |
+
|
| 1225 |
+
# 🔹 Helper: ASCII Sparkline in PLN Blue
|
| 1226 |
+
def ascii_sparkline_pln(data):
|
| 1227 |
+
if not data or len(data) == 0:
|
| 1228 |
+
return ""
|
| 1229 |
+
try:
|
| 1230 |
+
data = [float(x) for x in data]
|
| 1231 |
+
min_val, max_val = min(data), max(data)
|
| 1232 |
+
if max_val == min_val:
|
| 1233 |
+
norm = [3] * len(data)
|
| 1234 |
+
else:
|
| 1235 |
+
norm = [int(7 * (x - min_val) / (max_val - min_val + 1e-9)) for x in data]
|
| 1236 |
+
blocks = "▁▂▃▄▅▆▇█"
|
| 1237 |
+
spark = "".join(blocks[min(max(0, i), 7)] for i in norm)
|
| 1238 |
+
return f"<span class='spark' style='color:#003DA5;'>{spark}</span>"
|
| 1239 |
+
except:
|
| 1240 |
+
return "<span class='spark' style='color:#999;'>▁▁▁</span>"
|
| 1241 |
+
|
| 1242 |
+
# ——————— 1. Locations: ONLY Coverage < 90% AND Slope < 0 ———————
|
| 1243 |
+
def predict_locations(df):
|
| 1244 |
+
if 'nama_lokasi_full' not in df.columns or df.empty:
|
| 1245 |
+
return pd.DataFrame()
|
| 1246 |
+
|
| 1247 |
+
start_month = df['created_at'].min().to_period('M')
|
| 1248 |
+
end_month = df['created_at'].max().to_period('M')
|
| 1249 |
+
all_months = pd.period_range(start=start_month, end=end_month, freq='M')
|
| 1250 |
+
|
| 1251 |
+
df_monthly = (
|
| 1252 |
+
df.groupby(['nama_lokasi_full', df['created_at'].dt.to_period('M')])
|
| 1253 |
+
.size()
|
| 1254 |
+
.unstack(fill_value=0)
|
| 1255 |
+
.reindex(columns=all_months, fill_value=0)
|
| 1256 |
+
.stack()
|
| 1257 |
+
.reset_index(name='count')
|
| 1258 |
+
)
|
| 1259 |
+
df_monthly.columns = ['Location', 'Month', 'Count']
|
| 1260 |
+
|
| 1261 |
+
results = []
|
| 1262 |
+
for lokasi, group in df_monthly.groupby('Location'):
|
| 1263 |
+
ts = group.set_index('Month')['Count']
|
| 1264 |
+
total = len(all_months)
|
| 1265 |
+
active = (ts > 0).sum()
|
| 1266 |
+
coverage = active / total if total > 0 else 0
|
| 1267 |
+
avg_rate = ts.mean()
|
| 1268 |
+
|
| 1269 |
+
if len(ts) >= 2:
|
| 1270 |
+
try:
|
| 1271 |
+
slope = np.polyfit(np.arange(len(ts)), ts.values, 1)[0]
|
| 1272 |
+
# ✅ FILTER: Coverage < 90% AND Slope < 0
|
| 1273 |
+
if slope < 0 and coverage < 0.9:
|
| 1274 |
+
reason = f"Slope = {slope:.3f}, Coverage = {coverage*100:.1f}%. Avg: {avg_rate:.2f}/mo."
|
| 1275 |
+
results.append({
|
| 1276 |
+
'Location': lokasi,
|
| 1277 |
+
'Avg Reports/Month': round(avg_rate, 2),
|
| 1278 |
+
'Coverage (%)': round(coverage * 100, 1),
|
| 1279 |
+
'Trend Slope': round(slope, 3),
|
| 1280 |
+
'Trend': ascii_sparkline_pln(ts.values.tolist()),
|
| 1281 |
+
'Reason': reason
|
| 1282 |
+
})
|
| 1283 |
+
except:
|
| 1284 |
+
continue
|
| 1285 |
+
df_res = pd.DataFrame(results)
|
| 1286 |
+
return df_res.sort_values('Trend Slope', ascending=True) if not df_res.empty else df_res # most negative first
|
| 1287 |
+
|
| 1288 |
+
# ——————— 2. Divisions ———————
|
| 1289 |
+
def predict_divisions(df):
|
| 1290 |
+
if 'nama' not in df.columns:
|
| 1291 |
+
return pd.DataFrame()
|
| 1292 |
+
|
| 1293 |
+
start_month = df['created_at'].min().to_period('M')
|
| 1294 |
+
end_month = df['created_at'].max().to_period('M')
|
| 1295 |
+
all_months = pd.period_range(start=start_month, end=end_month, freq='M')
|
| 1296 |
+
|
| 1297 |
+
df_monthly = (
|
| 1298 |
+
df.groupby(['nama', df['created_at'].dt.to_period('M')])
|
| 1299 |
+
.size()
|
| 1300 |
+
.unstack(fill_value=0)
|
| 1301 |
+
.reindex(columns=all_months, fill_value=0)
|
| 1302 |
+
.stack()
|
| 1303 |
+
.reset_index(name='count')
|
| 1304 |
+
)
|
| 1305 |
+
df_monthly.columns = ['Division', 'Month', 'Count']
|
| 1306 |
+
|
| 1307 |
+
results = []
|
| 1308 |
+
for div, group in df_monthly.groupby('Division'):
|
| 1309 |
+
ts = group.set_index('Month')['Count']
|
| 1310 |
+
total = len(all_months)
|
| 1311 |
+
active = (ts > 0).sum()
|
| 1312 |
+
gaps = total - active
|
| 1313 |
+
coverage = active / total if total > 0 else 0
|
| 1314 |
+
|
| 1315 |
+
if gaps > 2:
|
| 1316 |
+
status = "<span class='status-inactive'>Inactive</span>"
|
| 1317 |
+
elif gaps == 0:
|
| 1318 |
+
status = "<span class='status-active'>Active</span>"
|
| 1319 |
+
else:
|
| 1320 |
+
status = "<span class='status-neutral'>Neutral</span>"
|
| 1321 |
+
|
| 1322 |
+
bar = ''.join(['●' if c > 0 else '○' for c in ts.values])
|
| 1323 |
+
trend_line = f"<span class='spark' style='color:#003DA5;'>{bar}</span>"
|
| 1324 |
+
results.append({
|
| 1325 |
+
'Division': div,
|
| 1326 |
+
'Active Months': int(active),
|
| 1327 |
+
'Total Months': int(total),
|
| 1328 |
+
'Coverage (%)': round(coverage * 100, 1),
|
| 1329 |
+
'Status': status,
|
| 1330 |
+
'Trend': trend_line
|
| 1331 |
+
})
|
| 1332 |
+
df_res = pd.DataFrame(results)
|
| 1333 |
+
return df_res.sort_values('Coverage (%)', ascending=True) if not df_res.empty else df_res
|
| 1334 |
+
|
| 1335 |
+
# ——————— 3. Issues: ONLY Coverage=100% & Trend Slope > 0 → Avg/Month ———————
|
| 1336 |
+
def predict_issues(df):
|
| 1337 |
+
if 'kategori' not in df.columns or df.empty:
|
| 1338 |
+
return pd.DataFrame()
|
| 1339 |
+
|
| 1340 |
+
start_month = df['created_at'].min().to_period('M')
|
| 1341 |
+
end_month = df['created_at'].max().to_period('M')
|
| 1342 |
+
all_months = pd.period_range(start=start_month, end=end_month, freq='M')
|
| 1343 |
+
n_months = len(all_months)
|
| 1344 |
+
|
| 1345 |
+
results = []
|
| 1346 |
+
for cat, group in df.groupby('kategori'):
|
| 1347 |
+
ts_data = (
|
| 1348 |
+
group.groupby(group['created_at'].dt.to_period('M'))
|
| 1349 |
+
.size()
|
| 1350 |
+
.reindex(all_months, fill_value=0)
|
| 1351 |
+
)
|
| 1352 |
+
total_reports = ts_data.sum()
|
| 1353 |
+
avg_per_month = total_reports / n_months if n_months > 0 else 0
|
| 1354 |
+
active_months = (ts_data > 0).sum()
|
| 1355 |
+
coverage = active_months / n_months if n_months > 0 else 0
|
| 1356 |
+
|
| 1357 |
+
slope = 0.0
|
| 1358 |
+
if len(ts_data) >= 2:
|
| 1359 |
+
try:
|
| 1360 |
+
slope = np.polyfit(np.arange(len(ts_data)), ts_data.values, 1)[0]
|
| 1361 |
+
except:
|
| 1362 |
+
pass
|
| 1363 |
+
|
| 1364 |
+
results.append({
|
| 1365 |
+
'Category': cat,
|
| 1366 |
+
'Avg/Month': round(avg_per_month, 2),
|
| 1367 |
+
'Coverage (%)': round(coverage * 100, 1),
|
| 1368 |
+
'Trend Slope': round(slope, 3),
|
| 1369 |
+
'Trend': ascii_sparkline_pln(ts_data.values.tolist())
|
| 1370 |
+
})
|
| 1371 |
+
|
| 1372 |
+
df_res = pd.DataFrame(results)
|
| 1373 |
+
|
| 1374 |
+
# ✅ FILTER: Coverage = 100% AND Trend Slope > 0
|
| 1375 |
+
if not df_res.empty:
|
| 1376 |
+
df_res = df_res[
|
| 1377 |
+
(df_res['Coverage (%)'] == 100.0) &
|
| 1378 |
+
(df_res['Trend Slope'] > 0)
|
| 1379 |
+
].copy()
|
| 1380 |
+
|
| 1381 |
+
df_res['Status'] = df_res['Trend Slope'].apply(
|
| 1382 |
+
lambda s: "<span class='trend-rising'>High-Risk Rising</span>" if s > 0.2 else
|
| 1383 |
+
"<span class='trend-stable'>Emerging Rising</span>"
|
| 1384 |
+
)
|
| 1385 |
+
df_res = df_res.sort_values('Trend Slope', ascending=False)
|
| 1386 |
+
|
| 1387 |
+
return df_res.reset_index(drop=True)
|
| 1388 |
+
|
| 1389 |
+
# ——————— RUN ———————
|
| 1390 |
+
df_loc = predict_locations(df_filtered)
|
| 1391 |
+
df_div = predict_divisions(df_filtered)
|
| 1392 |
+
df_issue = predict_issues(df_filtered)
|
| 1393 |
+
|
| 1394 |
+
# 🎯 PANEL 1: Locations (FILTERED: Coverage < 90% & Slope < 0)
|
| 1395 |
+
st.markdown("<div class='predictive-panel'>", unsafe_allow_html=True)
|
| 1396 |
+
st.markdown("<div class='predictive-header'>1. Which areas are predicted to have no future inspections?</div>", unsafe_allow_html=True)
|
| 1397 |
+
if not df_loc.empty:
|
| 1398 |
+
cols = ['Location', 'Avg Reports/Month', 'Coverage (%)', 'Trend Slope', 'Trend', 'Reason']
|
| 1399 |
+
|
| 1400 |
+
# 🔥 Rename hanya untuk DISPLAY, bukan data asli
|
| 1401 |
+
df_display = df_loc[cols].rename(columns={
|
| 1402 |
+
"Reason": "Reason Forecast"
|
| 1403 |
+
})
|
| 1404 |
+
|
| 1405 |
+
html = df_display.to_html(escape=False, index=False, table_id="tbl-locations")
|
| 1406 |
+
st.markdown(f"<div class='predictive-table-wrapper'>{html}</div>", unsafe_allow_html=True)
|
| 1407 |
+
|
| 1408 |
+
st.markdown(
|
| 1409 |
+
"<div class='predictive-note'>"
|
| 1410 |
+
"<strong>Criteria:</strong> Coverage < 90% AND negative slope. "
|
| 1411 |
+
"High-risk: steep negative slope + low baseline activity."
|
| 1412 |
+
"</div>",
|
| 1413 |
+
unsafe_allow_html=True
|
| 1414 |
+
)
|
| 1415 |
+
|
| 1416 |
+
else:
|
| 1417 |
+
st.markdown(
|
| 1418 |
+
"<div class='predictive-table-wrapper'>"
|
| 1419 |
+
"<p style='text-align:center; color:#666; padding:24px; font-style:italic;'>"
|
| 1420 |
+
"No locations meet criteria: Coverage < 90% and negative trend."
|
| 1421 |
+
"</p>"
|
| 1422 |
+
"<div class='warning-box'>"
|
| 1423 |
+
"💡 Note: Locations with Coverage ≥ 90% are excluded — they are considered stable reporting zones."
|
| 1424 |
+
"</div>"
|
| 1425 |
+
"</div>",
|
| 1426 |
+
unsafe_allow_html=True
|
| 1427 |
+
)
|
| 1428 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 1429 |
+
|
| 1430 |
+
# 🎯 PANEL 2: Divisions
|
| 1431 |
+
st.markdown("<div class='predictive-panel'>", unsafe_allow_html=True)
|
| 1432 |
+
st.markdown("<div class='predictive-header'>2. Which Areas Are Likely to Be Frequently or Rarely Inspected?</div>", unsafe_allow_html=True)
|
| 1433 |
+
if not df_div.empty:
|
| 1434 |
+
cols = ['Division', 'Active Months', 'Total Months', 'Coverage (%)', 'Status', 'Trend']
|
| 1435 |
+
|
| 1436 |
+
# 🔵 Rename ONLY for display (Status → Forecast Inspection)
|
| 1437 |
+
df_display = df_div[cols].rename(columns={
|
| 1438 |
+
"Status": "Forecast Inspection"
|
| 1439 |
+
})
|
| 1440 |
+
|
| 1441 |
+
html = df_display.to_html(escape=False, index=False, table_id="tbl-divisions")
|
| 1442 |
+
st.markdown(f"<div class='predictive-table-wrapper'>{html}</div>", unsafe_allow_html=True)
|
| 1443 |
+
|
| 1444 |
+
st.markdown(
|
| 1445 |
+
"<div class='predictive-note'>"
|
| 1446 |
+
"<strong>Forecast Inspection:</strong> "
|
| 1447 |
+
"<span class='status-active'>Active</span> (0 gaps), "
|
| 1448 |
+
"<span class='status-neutral'>Neutral</span> (1–2 gaps), "
|
| 1449 |
+
"<span class='status-inactive'>Inactive</span> (>2 gaps)."
|
| 1450 |
+
"</div>",
|
| 1451 |
+
unsafe_allow_html=True
|
| 1452 |
+
)
|
| 1453 |
+
else:
|
| 1454 |
+
st.markdown(
|
| 1455 |
+
"<div class='predictive-table-wrapper'>"
|
| 1456 |
+
"<p style='text-align:center; color:#666; padding:24px; font-style:italic;'>"
|
| 1457 |
+
"Insufficient division data (≥2 months required)."
|
| 1458 |
+
"</p></div>",
|
| 1459 |
+
unsafe_allow_html=True
|
| 1460 |
+
)
|
| 1461 |
+
|
| 1462 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 1463 |
+
|
| 1464 |
+
|
| 1465 |
+
# 🎯 PANEL 3: Issues (FILTERED: Coverage=100% & Rising)
|
| 1466 |
+
st.markdown("<div class='predictive-panel'>", unsafe_allow_html=True)
|
| 1467 |
+
st.markdown(
|
| 1468 |
+
"<div class='predictive-header'>"
|
| 1469 |
+
"3. Which Issue Categories Are Likely to Appear in the Next 3 Months"
|
| 1470 |
+
"<span style='font-size:0.75em; font-weight:400; color:#003DA5;'>"
|
| 1471 |
+
" (* Categorization uses NLP — Natural Language Processing from random text)"
|
| 1472 |
+
"</span>"
|
| 1473 |
+
"</div>",
|
| 1474 |
+
unsafe_allow_html=True
|
| 1475 |
+
)
|
| 1476 |
+
|
| 1477 |
+
if not df_issue.empty:
|
| 1478 |
+
cols = ['Category', 'Avg/Month', 'Coverage (%)', 'Trend Slope', 'Status', 'Trend']
|
| 1479 |
+
|
| 1480 |
+
# 🔵 Rename ONLY for display
|
| 1481 |
+
df_display = df_issue[cols].rename(columns={
|
| 1482 |
+
"Status": "Status Issue for Next Month"
|
| 1483 |
+
})
|
| 1484 |
+
|
| 1485 |
+
html = df_display.to_html(escape=False, index=False, table_id="tbl-issues")
|
| 1486 |
+
st.markdown(f"<div class='predictive-table-wrapper'>{html}</div>", unsafe_allow_html=True)
|
| 1487 |
+
|
| 1488 |
+
st.markdown(
|
| 1489 |
+
"<div class='predictive-note'>"
|
| 1490 |
+
"<strong>Filtered:</strong> Reported every month (100% coverage) with increasing trend. "
|
| 1491 |
+
"<strong>Avg/Month</strong> = total ÷ months. "
|
| 1492 |
+
"<span class='trend-rising'>High-Risk Rising</span> = slope > 0.2."
|
| 1493 |
+
"</div>",
|
| 1494 |
+
unsafe_allow_html=True
|
| 1495 |
+
)
|
| 1496 |
+
|
| 1497 |
+
else:
|
| 1498 |
+
st.markdown(
|
| 1499 |
+
"<div class='predictive-table-wrapper'>"
|
| 1500 |
+
"<p style='text-align:center; color:#c62828; padding:24px; font-weight:500;'>"
|
| 1501 |
+
"⚠️ No rising categories with 100% monthly coverage."
|
| 1502 |
+
"</p>"
|
| 1503 |
+
"<p style='text-align:center; color:#666; font-size:0.9em;'>"
|
| 1504 |
+
"Consider relaxing coverage filter if data is sparse."
|
| 1505 |
+
"</p></div>",
|
| 1506 |
+
unsafe_allow_html=True
|
| 1507 |
+
)
|
| 1508 |
+
|
| 1509 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 1510 |
+
|
| 1511 |
+
|
| 1512 |
+
|
| 1513 |
# =================== 6. ✅ AI INSIGHT ENGINE (BARU - BERDASARKAN DATA & RATIO) ===================
|
| 1514 |
+
|
| 1515 |
+
st.markdown("<h3 class='section-title'>OBJECTIVE 7 - Insight and Recommendation</h3>", unsafe_allow_html=True)
|
| 1516 |
|
| 1517 |
def compute_ai_insights(df: pd.DataFrame) -> List[dict]:
|
| 1518 |
"""
|