wjnwjn59 commited on
Commit
392d72d
Β·
1 Parent(s): b9ff16f

fix update error

Browse files
Files changed (1) hide show
  1. app.py +18 -35
app.py CHANGED
@@ -27,17 +27,14 @@ DATA_PATH = "data/cleveland.csv"
27
  def init_page():
28
  """
29
  Load dataset from disk, fit models once, and return preview + metrics.
 
30
  """
31
  if not os.path.exists(DATA_PATH):
32
  msg = f"❌ Dataset not found at '{DATA_PATH}'. Please place Cleveland CSV there."
33
- return (
34
- gr.Markdown.update(value=msg),
35
- gr.DataFrame.update(value=pd.DataFrame()),
36
- gr.DataFrame.update(value=pd.DataFrame())
37
- )
38
 
39
- df = pd.read_csv(DATA_PATH)
40
- df = load_cleveland_dataframe(uploaded_df=df) # cleans, binarizes target
41
 
42
  models, metrics = fit_all_models(df)
43
  STATE["df"] = df
@@ -45,12 +42,8 @@ def init_page():
45
  STATE["metrics"] = metrics
46
 
47
  head = df.head(8)
48
- msg = "βœ… **Cleveland dataset loaded** from `data/cleveland.csv` and models trained (80/20 split)."
49
- return (
50
- gr.Markdown.update(value=msg),
51
- gr.DataFrame.update(value=head, interactive=False),
52
- gr.DataFrame.update(value=metrics, interactive=False)
53
- )
54
 
55
 
56
  # -----------------------------
@@ -63,6 +56,7 @@ def fill_example(idx_text: str):
63
  "Example 3 (likely positive)": 2
64
  }[idx_text]
65
  ex = example_patient(idx)
 
66
  return [ex[c] for c in CLEVELAND_FEATURES_ORDER]
67
 
68
 
@@ -81,26 +75,19 @@ def _bar_for_models(results: dict):
81
  height=420,
82
  margin=dict(l=30, r=20, t=60, b=40)
83
  )
84
- # Emphasize ensemble bar (assumes last entry named "Ensemble (Soft Voting)")
85
- if len(names) >= 1:
86
- colors = ["#9BB8D3"] * len(names)
87
- try:
88
- idx = names.index("Ensemble (Soft Voting)")
89
- colors[idx] = APP_ACCENT
90
- except ValueError:
91
- colors[-1] = APP_ACCENT
92
- fig.data[0].marker.color = colors
93
  return fig
94
 
95
 
96
  def run_predict(*vals):
97
  if STATE["df"] is None or STATE["models"] is None:
98
- return (
99
- gr.Markdown.update(value="❌ Models not initialized. Reload the app."),
100
- gr.Plot.update(None),
101
- gr.Markdown.update(visible=False),
102
- gr.DataFrame.update(visible=False)
103
- )
104
 
105
  input_dict = {col: vals[i] for i, col in enumerate(CLEVELAND_FEATURES_ORDER)}
106
  results = predict_all(STATE["models"], input_dict)
@@ -124,16 +111,12 @@ def run_predict(*vals):
124
 
125
  fig = _bar_for_models(results)
126
 
127
- return (
128
- gr.Markdown.update(value=title_md),
129
- gr.Plot.update(value=fig),
130
- gr.Markdown.update(value="**Per-Model Predictions**", visible=True),
131
- gr.DataFrame.update(value=table_df, visible=True, interactive=False)
132
- )
133
 
134
 
135
  # -----------------------------
136
- # UI (no gr.Box to avoid your error)
137
  # -----------------------------
138
  with gr.Blocks(theme="soft", css=f"""
139
  :root {{
 
27
  def init_page():
28
  """
29
  Load dataset from disk, fit models once, and return preview + metrics.
30
+ Returns plain values (no .update), to maximize Gradio compatibility.
31
  """
32
  if not os.path.exists(DATA_PATH):
33
  msg = f"❌ Dataset not found at '{DATA_PATH}'. Please place Cleveland CSV there."
34
+ return msg, pd.DataFrame(), pd.DataFrame()
 
 
 
 
35
 
36
+ raw = pd.read_csv(DATA_PATH)
37
+ df = load_cleveland_dataframe(uploaded_df=raw) # cleans, binarizes target
38
 
39
  models, metrics = fit_all_models(df)
40
  STATE["df"] = df
 
42
  STATE["metrics"] = metrics
43
 
44
  head = df.head(8)
45
+ msg = "βœ… Cleveland dataset loaded from `data/cleveland.csv` and models trained (80/20 split)."
46
+ return msg, head, metrics
 
 
 
 
47
 
48
 
49
  # -----------------------------
 
56
  "Example 3 (likely positive)": 2
57
  }[idx_text]
58
  ex = example_patient(idx)
59
+ # Return in the strict feature order so Gradio can assign to outputs 1:1
60
  return [ex[c] for c in CLEVELAND_FEATURES_ORDER]
61
 
62
 
 
75
  height=420,
76
  margin=dict(l=30, r=20, t=60, b=40)
77
  )
78
+ # Emphasize ensemble bar
79
+ colors = ["#9BB8D3"] * len(names)
80
+ if "Ensemble (Soft Voting)" in names:
81
+ colors[names.index("Ensemble (Soft Voting)")] = APP_ACCENT
82
+ elif len(colors) > 0:
83
+ colors[-1] = APP_ACCENT
84
+ fig.data[0].marker.color = colors
 
 
85
  return fig
86
 
87
 
88
  def run_predict(*vals):
89
  if STATE["df"] is None or STATE["models"] is None:
90
+ return "❌ Models not initialized. Reload the app.", None, "", pd.DataFrame()
 
 
 
 
 
91
 
92
  input_dict = {col: vals[i] for i, col in enumerate(CLEVELAND_FEATURES_ORDER)}
93
  results = predict_all(STATE["models"], input_dict)
 
111
 
112
  fig = _bar_for_models(results)
113
 
114
+ # Return plain values for Markdown, Plot, Markdown, DataFrame
115
+ return title_md, fig, "**Per-Model Predictions**", table_df
 
 
 
 
116
 
117
 
118
  # -----------------------------
119
+ # UI (no gr.Box to avoid older-Gradio issues)
120
  # -----------------------------
121
  with gr.Blocks(theme="soft", css=f"""
122
  :root {{