Upload 3 files
Browse files- app_diabetes.py +99 -0
- diabetes_dataset.csv +0 -0
- requirements.txt +3 -0
app_diabetes.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---ADVANCED INTERACTIVE DASHBOARD (REAL DATA) ---
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Load ulang data untuk memastikan aman di fungsi Gradio
|
| 7 |
+
df = pd.read_csv('diabetes_dataset.csv')
|
| 8 |
+
df['Diabetes_Label'] = df['diabetes'].map({0: 'Negative', 1: 'Positive'})
|
| 9 |
+
|
| 10 |
+
def update_dashboard(age_start, age_end, selected_smoking, selected_gender):
|
| 11 |
+
# 1. Filter Data (Aman & Anti-Error)
|
| 12 |
+
if age_start > age_end:
|
| 13 |
+
age_start, age_end = age_end, age_start
|
| 14 |
+
|
| 15 |
+
if not selected_smoking:
|
| 16 |
+
selected_smoking = df['smoking_history'].unique().tolist()
|
| 17 |
+
|
| 18 |
+
if not selected_gender:
|
| 19 |
+
selected_gender = df['gender'].unique().tolist()
|
| 20 |
+
|
| 21 |
+
filtered_df = df[(df['age'] >= age_start) &
|
| 22 |
+
(df['age'] <= age_end) &
|
| 23 |
+
(df['smoking_history'].isin(selected_smoking)) &
|
| 24 |
+
(df['gender'].isin(selected_gender))]
|
| 25 |
+
|
| 26 |
+
# Handle jika data kosong setelah difilter
|
| 27 |
+
if filtered_df.empty:
|
| 28 |
+
empty_fig = px.scatter(title="⚠️ Tidak ada data pasien dengan kriteria ini.")
|
| 29 |
+
return empty_fig, empty_fig, empty_fig, "### ⚠️ Data Kosong. Silakan ubah filter."
|
| 30 |
+
|
| 31 |
+
# 2. GRAFIK 1: SCATTER PLOT INTERAKTIF
|
| 32 |
+
fig_scatter = px.scatter(filtered_df, x='hbA1c_level', y='blood_glucose_level',
|
| 33 |
+
color='Diabetes_Label', size='bmi', hover_data=['age', 'gender'],
|
| 34 |
+
color_discrete_map={'Negative': '#636EFA', 'Positive': '#EF553B'},
|
| 35 |
+
title=f"🩺 Indikator Klinis (Usia {age_start} - {age_end})",
|
| 36 |
+
template="plotly_white")
|
| 37 |
+
fig_scatter.add_vline(x=6.5, line_dash="dash", line_color="red", annotation_text="Batas HbA1c")
|
| 38 |
+
|
| 39 |
+
# 3. GRAFIK 2: PIE CHART (Rasio Diabetes)
|
| 40 |
+
diabetes_counts = filtered_df['Diabetes_Label'].value_counts().reset_index()
|
| 41 |
+
diabetes_counts.columns = ['Status', 'Count']
|
| 42 |
+
fig_pie = px.pie(diabetes_counts, values='Count', names='Status',
|
| 43 |
+
color='Status', color_discrete_map={'Negative': '#636EFA', 'Positive': '#EF553B'},
|
| 44 |
+
title="📊 Rasio Positif vs Negatif", hole=0.4, template="plotly_white")
|
| 45 |
+
|
| 46 |
+
# 4. GRAFIK 3: HISTOGRAM (Distribusi BMI)
|
| 47 |
+
fig_hist = px.histogram(filtered_df, x='bmi', color='Diabetes_Label',
|
| 48 |
+
barmode='overlay', title="👥 Distribusi Obesitas (BMI)",
|
| 49 |
+
color_discrete_map={'Negative': '#636EFA', 'Positive': '#EF553B'},
|
| 50 |
+
template="plotly_white")
|
| 51 |
+
|
| 52 |
+
# 5. RINGKASAN EKSEKUTIF
|
| 53 |
+
total_patients = len(filtered_df)
|
| 54 |
+
positive_cases = len(filtered_df[filtered_df['diabetes'] == 1])
|
| 55 |
+
prevalence = (positive_cases / total_patients) * 100 if total_patients > 0 else 0
|
| 56 |
+
|
| 57 |
+
summary = f"""
|
| 58 |
+
### 📋 Executive Summary
|
| 59 |
+
* **Populasi Terfilter:** **{total_patients:,}** pasien
|
| 60 |
+
* **Kasus Positif:** **{positive_cases:,}** pasien
|
| 61 |
+
* **Prevalensi Risiko:** **{prevalence:.1f}%**
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
return fig_scatter, fig_pie, fig_hist, summary
|
| 65 |
+
|
| 66 |
+
# --- UI GRADIO ---
|
| 67 |
+
with gr.Blocks(title="Diabetes Risk Dashboard") as app:
|
| 68 |
+
gr.Markdown("# 🩸 Advanced Diabetes Risk Analytics")
|
| 69 |
+
gr.Markdown("Eksplorasi profil pasien menggunakan dataset riil untuk mengidentifikasi korelasi medis.")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column(scale=2):
|
| 73 |
+
in_smoking = gr.Dropdown(choices=df['smoking_history'].unique().tolist(),
|
| 74 |
+
value=['never', 'current', 'former'],
|
| 75 |
+
multiselect=True, label="Riwayat Merokok")
|
| 76 |
+
in_gender = gr.Dropdown(choices=df['gender'].unique().tolist(),
|
| 77 |
+
value=df['gender'].unique().tolist(),
|
| 78 |
+
multiselect=True, label="Gender")
|
| 79 |
+
with gr.Column(scale=2):
|
| 80 |
+
with gr.Row():
|
| 81 |
+
in_age_start = gr.Slider(0, 100, value=20, step=1, label="Umur (Min)")
|
| 82 |
+
in_age_end = gr.Slider(0, 100, value=80, step=1, label="Umur (Max)")
|
| 83 |
+
with gr.Column(scale=1):
|
| 84 |
+
btn = gr.Button("🔄 Analisis Data", variant="primary")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
out_summary = gr.Markdown()
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
out_scatter = gr.Plot()
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
out_pie = gr.Plot()
|
| 94 |
+
out_hist = gr.Plot()
|
| 95 |
+
|
| 96 |
+
btn.click(update_dashboard, inputs=[in_age_start, in_age_end, in_smoking, in_gender], outputs=[out_scatter, out_pie, out_hist, out_summary])
|
| 97 |
+
app.load(update_dashboard, inputs=[in_age_start, in_age_end, in_smoking, in_gender], outputs=[out_scatter, out_pie, out_hist, out_summary])
|
| 98 |
+
|
| 99 |
+
app.launch()
|
diabetes_dataset.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
plotly
|
| 3 |
+
gradio
|