Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
with ui.card(full_screen=True):
|
| 80 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
| 81 |
-
"Total bill vs tip"
|
| 82 |
-
with ui.popover(title="Add a color variable", placement="top"):
|
| 83 |
-
ICONS["ellipsis"]
|
| 84 |
-
ui.input_radio_buttons(
|
| 85 |
-
"scatter_color",
|
| 86 |
-
None,
|
| 87 |
-
["none", "sex", "smoker", "day", "time"],
|
| 88 |
-
inline=True,
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
@render_plotly
|
| 92 |
-
def scatterplot():
|
| 93 |
-
color = input.scatter_color()
|
| 94 |
-
return px.scatter(
|
| 95 |
-
tips_data(),
|
| 96 |
-
x="total_bill",
|
| 97 |
-
y="tip",
|
| 98 |
-
color=None if color == "none" else color,
|
| 99 |
-
trendline="lowess",
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
with ui.card(full_screen=True):
|
| 103 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
| 104 |
-
"Tip percentages"
|
| 105 |
-
with ui.popover(title="Add a color variable"):
|
| 106 |
-
ICONS["ellipsis"]
|
| 107 |
-
ui.input_radio_buttons(
|
| 108 |
-
"tip_perc_y",
|
| 109 |
-
"Split by:",
|
| 110 |
-
["sex", "smoker", "day", "time"],
|
| 111 |
-
selected="day",
|
| 112 |
-
inline=True,
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
@render_plotly
|
| 116 |
-
def tip_perc():
|
| 117 |
-
from ridgeplot import ridgeplot
|
| 118 |
-
|
| 119 |
-
dat = tips_data()
|
| 120 |
-
dat["percent"] = dat.tip / dat.total_bill
|
| 121 |
-
yvar = input.tip_perc_y()
|
| 122 |
-
uvals = dat[yvar].unique()
|
| 123 |
-
|
| 124 |
-
samples = [[dat.percent[dat[yvar] == val]] for val in uvals]
|
| 125 |
-
|
| 126 |
-
plt = ridgeplot(
|
| 127 |
-
samples=samples,
|
| 128 |
-
labels=uvals,
|
| 129 |
-
bandwidth=0.01,
|
| 130 |
-
colorscale="viridis",
|
| 131 |
-
colormode="row-index",
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
plt.update_layout(
|
| 135 |
-
legend=dict(
|
| 136 |
-
orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5
|
| 137 |
-
)
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
return plt
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
ui.include_css(app_dir / "styles.css")
|
| 144 |
-
|
| 145 |
-
# --------------------------------------------------------
|
| 146 |
-
# Reactive calculations and effects
|
| 147 |
-
# --------------------------------------------------------
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
@reactive.calc
|
| 151 |
-
def tips_data():
|
| 152 |
-
bill = input.total_bill()
|
| 153 |
-
idx1 = tips.total_bill.between(bill[0], bill[1])
|
| 154 |
-
idx2 = tips.time.isin(input.time())
|
| 155 |
-
return tips[idx1 & idx2]
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
@reactive.effect
|
| 159 |
-
@reactive.event(input.reset)
|
| 160 |
-
def _():
|
| 161 |
-
ui.update_slider("total_bill", value=bill_rng)
|
| 162 |
-
ui.update_checkbox_group("time", selected=["Lunch", "Dinner"])
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from typing import List
|
| 8 |
+
|
| 9 |
+
# Inisialisasi aplikasi FastAPI
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# 1. Tentukan struktur data input menggunakan Pydantic
|
| 13 |
+
# Ini akan menjadi format JSON yang diterima oleh API Anda.
|
| 14 |
+
# Pastikan nama field sama persis dengan kolom yang Anda gunakan saat training.
|
| 15 |
+
class StudentFeatures(BaseModel):
|
| 16 |
+
IPK_Terakhir: float
|
| 17 |
+
IPS_Terakhir: float
|
| 18 |
+
Total_SKS: int
|
| 19 |
+
IPS_Tertinggi: float
|
| 20 |
+
IPS_Terendah: float
|
| 21 |
+
Rentang_IPS: float
|
| 22 |
+
Jumlah_MK_Gagal: int
|
| 23 |
+
Total_SKS_Gagal: int
|
| 24 |
+
Tren_IPS_Slope: float
|
| 25 |
+
Perubahan_Kinerja_Terakhir: float
|
| 26 |
+
IPK_Ternormalisasi_SKS: float
|
| 27 |
+
Profil_Tren: str # Ini adalah fitur kategorikal sebelum di-encode
|
| 28 |
+
|
| 29 |
+
# 2. Muat model yang sudah Anda simpan
|
| 30 |
+
# Model akan dimuat sekali saat aplikasi dimulai untuk efisiensi
|
| 31 |
+
model = joblib.load('model_risiko_akademik.joblib')
|
| 32 |
+
|
| 33 |
+
# Daftar kolom fitur yang diharapkan oleh model setelah one-hot encoding
|
| 34 |
+
MODEL_FEATURES = [
|
| 35 |
+
'IPK_Terakhir', 'IPS_Terakhir', 'Total_SKS', 'IPS_Tertinggi',
|
| 36 |
+
'IPS_Terendah', 'Rentang_IPS', 'Jumlah_MK_Gagal', 'Total_SKS_Gagal',
|
| 37 |
+
'Tren_IPS_Slope', 'Perubahan_Kinerja_Terakhir',
|
| 38 |
+
'IPK_Ternormalisasi_SKS', 'Tren_Menaik', 'Tren_Menurun', 'Tren_Stabil'
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
@app.get("/")
|
| 42 |
+
def read_root():
|
| 43 |
+
return {"message": "API untuk Prediksi Risiko Akademik Mahasiswa"}
|
| 44 |
+
|
| 45 |
+
# 3. Buat endpoint untuk prediksi
|
| 46 |
+
@app.post("/predict/")
|
| 47 |
+
def predict_risk(student_data: StudentFeatures):
|
| 48 |
+
# Konversi data input Pydantic ke dictionary
|
| 49 |
+
data = student_data.dict()
|
| 50 |
+
|
| 51 |
+
# Buat DataFrame dari input (hanya satu baris)
|
| 52 |
+
input_df = pd.DataFrame([data])
|
| 53 |
+
|
| 54 |
+
# Lakukan pra-pemrosesan yang SAMA PERSIS seperti di notebook
|
| 55 |
+
# a. One-Hot Encoding
|
| 56 |
+
input_encoded = pd.get_dummies(input_df, columns=['Profil_Tren'], prefix='Tren')
|
| 57 |
+
|
| 58 |
+
# b. Pastikan semua kolom yang diharapkan model ada
|
| 59 |
+
# Ini PENTING. Jika input 'Profil_Tren' adalah 'Menaik', kolom 'Tren_Menurun'
|
| 60 |
+
# dan 'Tren_Stabil' tidak akan dibuat. Kita harus menambahkannya secara manual.
|
| 61 |
+
input_encoded = input_encoded.reindex(columns=MODEL_FEATURES, fill_value=False)
|
| 62 |
+
|
| 63 |
+
# Lakukan prediksi
|
| 64 |
+
prediction = model.predict(input_encoded)
|
| 65 |
+
|
| 66 |
+
# Ambil probabilitas prediksi (opsional, tapi informatif)
|
| 67 |
+
prediction_proba = model.predict_proba(input_encoded)
|
| 68 |
+
|
| 69 |
+
# Ambil label kelas dari probabilitas
|
| 70 |
+
classes = model.classes_
|
| 71 |
+
probabilities = dict(zip(classes, prediction_proba[0]))
|
| 72 |
+
|
| 73 |
+
# Kembalikan hasil dalam format JSON
|
| 74 |
+
return {
|
| 75 |
+
"prediction": prediction[0],
|
| 76 |
+
"probabilities": probabilities
|
| 77 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|