Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import pandas as pd
|
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
import plotly.express as px
|
|
|
|
| 8 |
|
| 9 |
from sklearn.model_selection import train_test_split
|
| 10 |
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
|
@@ -221,6 +222,45 @@ def predict_and_story(pclass, sex, age, sibsp, parch, fare, embarked):
|
|
| 221 |
**Avontuur:** De nacht is stil; fluiten, geroep, voetstappen. {ending}
|
| 222 |
"""
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
# ======================================================
|
| 225 |
# UI + LAYOUT
|
| 226 |
# ======================================================
|
|
@@ -274,7 +314,10 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Default(primary_hue="blue")) as d
|
|
| 274 |
|
| 275 |
# Interactieve voorspelling
|
| 276 |
with gr.Column(elem_classes=["panel"]):
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
| 278 |
Hier kun je ontdekken **hoe groot jouw kans op overleving** zou zijn geweest aan boord van de *Titanic* — en meteen het **verhaal van jouw nacht** lezen.
|
| 279 |
|
| 280 |
1. **Kies je profiel**
|
|
@@ -294,6 +337,9 @@ Hier kun je ontdekken **hoe groot jouw kans op overleving** zou zijn geweest aan
|
|
| 294 |
|
| 295 |
> 💡 *De voorspelling is een statistische schatting, geen oordeel.
|
| 296 |
> Ze helpt je zien hoe factoren zoals klasse, geslacht en leeftijd destijds iemands lot konden bepalen.*""")
|
|
|
|
|
|
|
|
|
|
| 297 |
with gr.Row():
|
| 298 |
ui_pclass = gr.Slider(1, 3, value=2, step=1, label="Klasse (1=1e, 3=3e)")
|
| 299 |
ui_sex = gr.Radio(["Man","Vrouw"], value="Man", label="Geslacht")
|
|
@@ -309,8 +355,22 @@ Hier kun je ontdekken **hoe groot jouw kans op overleving** zou zijn geweest aan
|
|
| 309 |
# Loads & acties
|
| 310 |
demo.load(fn=train_and_embed_solid, inputs=[], outputs=[status_md, train_plot])
|
| 311 |
demo.load(lambda: (plot_age_hist(df), plot_gender(df), plot_fare_box(df)), inputs=[], outputs=[g2, g3, g4])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
btn.click(predict_and_story,
|
| 313 |
inputs=[ui_pclass, ui_sex, ui_age, ui_sibsp, ui_parch, ui_fare, ui_emb],
|
| 314 |
outputs=story_out)
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
demo.launch()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
import plotly.express as px
|
| 8 |
+
import plotly.graph_objects as go # voor de gauge
|
| 9 |
|
| 10 |
from sklearn.model_selection import train_test_split
|
| 11 |
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
|
|
|
| 222 |
**Avontuur:** De nacht is stil; fluiten, geroep, voetstappen. {ending}
|
| 223 |
"""
|
| 224 |
|
| 225 |
+
# ======================================================
|
| 226 |
+
# LIVE GAUGE VOOR JOUW SCENARIO (met kleurbanden + threshold)
|
| 227 |
+
# ======================================================
|
| 228 |
+
def live_viz(pclass, sex, age, sibsp, parch, fare, embarked):
|
| 229 |
+
# Retourneer een gauge die live de kans toont (0–100%) met kleurbanden
|
| 230 |
+
if MODEL is None:
|
| 231 |
+
return make_plot(go.Figure(), "Jouw overlevingskans (live)")
|
| 232 |
+
X_row = pd.DataFrame([{
|
| 233 |
+
"pclass": int(pclass), "sex": sex, "age": float(age),
|
| 234 |
+
"sibsp": int(sibsp), "parch": int(parch), "fare": float(fare),
|
| 235 |
+
"embarked": embarked, "family_size": int(sibsp)+int(parch)+1
|
| 236 |
+
}])
|
| 237 |
+
prob = float(MODEL.predict_proba(X_row)[0,1]) * 100.0
|
| 238 |
+
|
| 239 |
+
fig = go.Figure(go.Indicator(
|
| 240 |
+
mode="gauge+number",
|
| 241 |
+
value=prob,
|
| 242 |
+
number={"suffix": "%", "valueformat": ".1f"},
|
| 243 |
+
gauge={
|
| 244 |
+
"axis": {"range": [0, 100]},
|
| 245 |
+
"bar": {"thickness": 0.25},
|
| 246 |
+
# Kleurbanden (0–25 rood, 25–50 oranje, 50–75 geel, 75–100 groen)
|
| 247 |
+
"steps": [
|
| 248 |
+
{"range": [0, 25], "color": "#FDECEC"},
|
| 249 |
+
{"range": [25, 50], "color": "#FFF2E0"},
|
| 250 |
+
{"range": [50, 75], "color": "#FFF9D6"},
|
| 251 |
+
{"range": [75, 100], "color": "#E8F6EA"},
|
| 252 |
+
],
|
| 253 |
+
# Threshold-lijn op actuele waarde
|
| 254 |
+
"threshold": {
|
| 255 |
+
"line": {"color": "#1B4B91", "width": 4},
|
| 256 |
+
"thickness": 0.9,
|
| 257 |
+
"value": prob
|
| 258 |
+
},
|
| 259 |
+
},
|
| 260 |
+
title={"text": "Jouw overlevingskans (live)"}
|
| 261 |
+
))
|
| 262 |
+
return make_plot(fig, "Jouw overlevingskans (live)")
|
| 263 |
+
|
| 264 |
# ======================================================
|
| 265 |
# UI + LAYOUT
|
| 266 |
# ======================================================
|
|
|
|
| 314 |
|
| 315 |
# Interactieve voorspelling
|
| 316 |
with gr.Column(elem_classes=["panel"]):
|
| 317 |
+
# Tekstblok + gauge naast elkaar
|
| 318 |
+
with gr.Row():
|
| 319 |
+
with gr.Column(scale=2, min_width=420):
|
| 320 |
+
gr.Markdown("""## 🔮 Jouw scenario — bereken je overlevingskans en lees je scène
|
| 321 |
Hier kun je ontdekken **hoe groot jouw kans op overleving** zou zijn geweest aan boord van de *Titanic* — en meteen het **verhaal van jouw nacht** lezen.
|
| 322 |
|
| 323 |
1. **Kies je profiel**
|
|
|
|
| 337 |
|
| 338 |
> 💡 *De voorspelling is een statistische schatting, geen oordeel.
|
| 339 |
> Ze helpt je zien hoe factoren zoals klasse, geslacht en leeftijd destijds iemands lot konden bepalen.*""")
|
| 340 |
+
with gr.Column(scale=1, min_width=320):
|
| 341 |
+
viz_plot = gr.Plot(label="Jouw overlevingskans (live)")
|
| 342 |
+
|
| 343 |
with gr.Row():
|
| 344 |
ui_pclass = gr.Slider(1, 3, value=2, step=1, label="Klasse (1=1e, 3=3e)")
|
| 345 |
ui_sex = gr.Radio(["Man","Vrouw"], value="Man", label="Geslacht")
|
|
|
|
| 355 |
# Loads & acties
|
| 356 |
demo.load(fn=train_and_embed_solid, inputs=[], outputs=[status_md, train_plot])
|
| 357 |
demo.load(lambda: (plot_age_hist(df), plot_gender(df), plot_fare_box(df)), inputs=[], outputs=[g2, g3, g4])
|
| 358 |
+
|
| 359 |
+
# Initiele gauge (na modeltraining): gebruik de default UI-waarden
|
| 360 |
+
demo.load(lambda: live_viz(2, "Man", 30, 1, 0, 50, "S"), inputs=[], outputs=[viz_plot])
|
| 361 |
+
|
| 362 |
+
# Live updates bij elke wijziging
|
| 363 |
+
for comp in [ui_pclass, ui_sex, ui_age, ui_sibsp, ui_parch, ui_fare, ui_emb]:
|
| 364 |
+
comp.change(live_viz,
|
| 365 |
+
inputs=[ui_pclass, ui_sex, ui_age, ui_sibsp, ui_parch, ui_fare, ui_emb],
|
| 366 |
+
outputs=viz_plot)
|
| 367 |
+
|
| 368 |
+
# Ook updaten bij de knop
|
| 369 |
btn.click(predict_and_story,
|
| 370 |
inputs=[ui_pclass, ui_sex, ui_age, ui_sibsp, ui_parch, ui_fare, ui_emb],
|
| 371 |
outputs=story_out)
|
| 372 |
+
btn.click(live_viz,
|
| 373 |
+
inputs=[ui_pclass, ui_sex, ui_age, ui_sibsp, ui_parch, ui_fare, ui_emb],
|
| 374 |
+
outputs=viz_plot)
|
| 375 |
|
| 376 |
demo.launch()
|