Update app.py
Browse files
app.py
CHANGED
|
@@ -1469,34 +1469,53 @@ def get_llm_client():
|
|
| 1469 |
_HF_CLIENT = None
|
| 1470 |
return None
|
| 1471 |
|
| 1472 |
-
def build_context(summary_jenis: pd.DataFrame, agg_total: pd.DataFrame, wilayah: str, kew: str) -> str:
|
| 1473 |
lines = []
|
| 1474 |
lines.append(f"Wilayah filter: {wilayah}")
|
| 1475 |
lines.append(f"Kewenangan: {kew}")
|
| 1476 |
-
lines.append("Metode: Indeks dasar dihitung per entitas (Yeo-Johnson + MinMax nasional), lalu
|
| 1477 |
-
lines.append("Penyesuaian berbasis kecukupan sampel
|
| 1478 |
-
lines.append("
|
| 1479 |
|
| 1480 |
if summary_jenis is not None and not summary_jenis.empty:
|
| 1481 |
lines.append("\nRingkasan (jenis + keseluruhan):")
|
| 1482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1483 |
lines.append(
|
| 1484 |
-
f"- {
|
| 1485 |
-
f"
|
| 1486 |
-
f"coverage={float(r['Coverage_Target68_Jenis_%']):.2f}%, "
|
| 1487 |
-
f"dasar={float(r['Indeks_Dasar_0_100']):.2f}, final={float(r['Indeks_Final_Disesuaikan_0_100']):.2f}"
|
| 1488 |
)
|
| 1489 |
|
| 1490 |
if agg_total is not None and not agg_total.empty:
|
| 1491 |
label_col = "Kab/Kota" if "Kab/Kota" in agg_total.columns else ("Provinsi" if "Provinsi" in agg_total.columns else None)
|
| 1492 |
-
|
| 1493 |
-
|
| 1494 |
-
|
| 1495 |
-
|
| 1496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1497 |
|
| 1498 |
return "\n".join(lines)
|
| 1499 |
|
|
|
|
| 1500 |
def generate_llm_analysis(summary_jenis, agg_total, verif_total, wilayah, kew):
|
| 1501 |
ctx = build_context(summary_jenis, agg_total, verif_total, wilayah, kew)
|
| 1502 |
client = get_llm_client()
|
|
|
|
| 1469 |
_HF_CLIENT = None
|
| 1470 |
return None
|
| 1471 |
|
| 1472 |
+
def build_context(summary_jenis: pd.DataFrame, agg_total: pd.DataFrame, verif_total: pd.DataFrame, wilayah: str, kew: str) -> str:
|
| 1473 |
lines = []
|
| 1474 |
lines.append(f"Wilayah filter: {wilayah}")
|
| 1475 |
lines.append(f"Kewenangan: {kew}")
|
| 1476 |
+
lines.append("Metode: Indeks dasar dihitung per entitas (Yeo-Johnson + MinMax nasional per indikator), lalu diagregasi per wilayah×jenis.")
|
| 1477 |
+
lines.append("Penyesuaian berbasis kecukupan sampel minimum 68% memakai faktor = min(terkumpul/target68, 1.0).")
|
| 1478 |
+
lines.append("Nilai keseluruhan (FIX): rata-rata 3 jenis (sekolah+umum+khusus) ÷ 3 (missing=0, tetap ÷3).")
|
| 1479 |
|
| 1480 |
if summary_jenis is not None and not summary_jenis.empty:
|
| 1481 |
lines.append("\nRingkasan (jenis + keseluruhan):")
|
| 1482 |
+
tmp = summary_jenis.copy()
|
| 1483 |
+
for _, r in tmp.iterrows():
|
| 1484 |
+
jenis = r.get("Jenis", "")
|
| 1485 |
+
jw = int(pd.to_numeric(r.get("Jumlah_Wilayah", 0), errors="coerce") or 0)
|
| 1486 |
+
tp = int(pd.to_numeric(r.get("Total_Perpus", 0), errors="coerce") or 0)
|
| 1487 |
+
pop = int(pd.to_numeric(r.get("Pop_Total_Jenis", 0), errors="coerce") or 0)
|
| 1488 |
+
tgt = int(pd.to_numeric(r.get("Target68_Total_Jenis", 0), errors="coerce") or 0)
|
| 1489 |
+
terk = int(pd.to_numeric(r.get("Terkumpul_Jenis", 0), errors="coerce") or 0)
|
| 1490 |
+
cov = float(pd.to_numeric(r.get("Coverage_Target68_Jenis_%", 0), errors="coerce") or 0.0)
|
| 1491 |
+
|
| 1492 |
+
dasar = float(pd.to_numeric(r.get("Indeks_Dasar_0_100", 0), errors="coerce") or 0.0)
|
| 1493 |
+
final = float(pd.to_numeric(r.get("Indeks_Final_Disesuaikan_0_100", 0), errors="coerce") or 0.0)
|
| 1494 |
+
adj = float(pd.to_numeric(r.get("Penyesuaian_Poin", 0), errors="coerce") or 0.0)
|
| 1495 |
+
|
| 1496 |
lines.append(
|
| 1497 |
+
f"- {jenis}: wilayah={jw}, perpus={tp}, pop={pop}, target68={tgt}, terkumpul={terk}, coverage={cov:.2f}%, "
|
| 1498 |
+
f"dasar={dasar:.2f}, final={final:.2f}, penyesuaian={adj:.2f}"
|
|
|
|
|
|
|
| 1499 |
)
|
| 1500 |
|
| 1501 |
if agg_total is not None and not agg_total.empty:
|
| 1502 |
label_col = "Kab/Kota" if "Kab/Kota" in agg_total.columns else ("Provinsi" if "Provinsi" in agg_total.columns else None)
|
| 1503 |
+
if label_col and "Indeks_Final_Wilayah_0_100" in agg_total.columns:
|
| 1504 |
+
lines.append("\nTop 5 wilayah (Final disesuaikan tertinggi):")
|
| 1505 |
+
top = agg_total.sort_values("Indeks_Final_Wilayah_0_100", ascending=False).head(5)
|
| 1506 |
+
for _, r in top.iterrows():
|
| 1507 |
+
wl = r.get(label_col, "(wilayah)")
|
| 1508 |
+
fin = float(pd.to_numeric(r.get("Indeks_Final_Wilayah_0_100", 0), errors="coerce") or 0.0)
|
| 1509 |
+
ntt = int(pd.to_numeric(r.get("n_total", 0), errors="coerce") or 0)
|
| 1510 |
+
lines.append(f"- {wl}: Final={fin:.2f} | n_total={ntt}")
|
| 1511 |
+
|
| 1512 |
+
# verif_total optional
|
| 1513 |
+
if verif_total is not None and not verif_total.empty:
|
| 1514 |
+
pass
|
| 1515 |
|
| 1516 |
return "\n".join(lines)
|
| 1517 |
|
| 1518 |
+
|
| 1519 |
def generate_llm_analysis(summary_jenis, agg_total, verif_total, wilayah, kew):
|
| 1520 |
ctx = build_context(summary_jenis, agg_total, verif_total, wilayah, kew)
|
| 1521 |
client = get_llm_client()
|