selva1909 commited on
Commit
687c512
·
verified ·
1 Parent(s): 2e5d1c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -92
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import gradio as gr
2
  import numpy as np
3
  import plotly.graph_objects as go
4
- import pandas as pd
5
  from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
6
- from sklearn.linear_model import LinearRegression
7
  from sklearn.metrics import mean_squared_error, accuracy_score
8
  from sklearn.tree import plot_tree
9
- import matplotlib.pyplot as plt
10
 
11
  # ------------------------------------------------
12
  # DATA GENERATORS
@@ -25,6 +23,7 @@ def generate_3d_regression(n_points, noise):
25
 
26
 
27
  def generate_classification(n_points, noise):
 
28
  X = np.random.randn(n_points, 2)
29
  y = (X[:, 0]**2 + X[:, 1] > 0.5).astype(int)
30
  X += np.random.randn(*X.shape) * noise * 0.1
@@ -32,10 +31,13 @@ def generate_classification(n_points, noise):
32
 
33
 
34
  # ------------------------------------------------
35
- # TRUE INTERACTIVE 3D USING PLOTLY
36
  # ------------------------------------------------
37
 
38
  def interactive_3d(n_points, noise, n_estimators, max_depth):
 
 
 
39
  X1, X2, X_flat, Z_flat = generate_3d_regression(n_points, noise)
40
 
41
  rf = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
@@ -50,12 +52,7 @@ def interactive_3d(n_points, noise, n_estimators, max_depth):
50
 
51
  fig.update_layout(
52
  title="Interactive 3D Random Forest Surface",
53
- scene=dict(
54
- xaxis_title="X1",
55
- yaxis_title="X2",
56
- zaxis_title="Z",
57
- bgcolor="#0b1e3d"
58
- ),
59
  paper_bgcolor="#0b1e3d",
60
  font=dict(color="white")
61
  )
@@ -85,6 +82,9 @@ def show_tree(rf_model):
85
  # ------------------------------------------------
86
 
87
  def classification_view(n_points, noise, n_estimators, max_depth):
 
 
 
88
  X, y = generate_classification(n_points, noise)
89
 
90
  rf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
@@ -113,73 +113,18 @@ def classification_view(n_points, noise, n_estimators, max_depth):
113
 
114
 
115
  # ------------------------------------------------
116
- # LINEAR VS RANDOM FOREST COMPARISON
117
- # ------------------------------------------------
118
-
119
- def compare_models(n_points, noise):
120
- x = np.linspace(0, 10, n_points).reshape(-1, 1)
121
- y = 2.5 * x.flatten() + 5 + np.random.randn(n_points) * noise
122
-
123
- lr = LinearRegression().fit(x, y)
124
- rf = RandomForestRegressor().fit(x, y)
125
-
126
- y_lr = lr.predict(x)
127
- y_rf = rf.predict(x)
128
-
129
- fig = go.Figure()
130
-
131
- fig.add_scatter(x=x.flatten(), y=y, mode="markers", name="Data")
132
- fig.add_scatter(x=x.flatten(), y=y_lr, mode="lines", name="Linear")
133
- fig.add_scatter(x=x.flatten(), y=y_rf, mode="lines", name="Random Forest")
134
-
135
- fig.update_layout(
136
- title="Linear vs Random Forest",
137
- paper_bgcolor="#0b1e3d",
138
- plot_bgcolor="#0b1e3d",
139
- font=dict(color="white")
140
- )
141
-
142
- return fig
143
-
144
-
145
- # ------------------------------------------------
146
- # DATASET UPLOAD
147
- # ------------------------------------------------
148
-
149
- def train_uploaded(file, target, n_estimators, max_depth):
150
- if file is None:
151
- return None, "Upload CSV to train"
152
-
153
- df = pd.read_csv(file.name)
154
-
155
- if target not in df.columns:
156
- return None, "Invalid target column"
157
-
158
- X = df.drop(columns=[target])
159
- y = df[target]
160
-
161
- rf = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth)
162
- rf.fit(X, y)
163
-
164
- preds = rf.predict(X)
165
- mse = mean_squared_error(y, preds)
166
-
167
- return None, f"Uploaded Data MSE: {mse:.4f}"
168
-
169
-
170
- # ------------------------------------------------
171
- # GRADIO DASHBOARD UI (BLUE THEME)
172
  # ------------------------------------------------
173
 
174
  with gr.Blocks() as demo:
175
 
176
- gr.Markdown("# 🎓 Full Machine Learning Teaching Dashboard")
177
 
178
- with gr.Tab("🌐 Interactive 3D"):
179
- n_points = gr.Slider(20, 100, 40, label="Points")
180
  noise = gr.Slider(0.0, 3.0, 1.0, label="Noise")
181
- n_estimators = gr.Slider(10, 100, 50, label="Trees")
182
- max_depth = gr.Slider(2, 15, 8, label="Depth")
183
 
184
  plot3d = gr.Plot()
185
  mse_text = gr.Markdown()
@@ -190,31 +135,19 @@ with gr.Blocks() as demo:
190
  tree_fig = show_tree(rf)
191
  return fig, text, tree_fig
192
 
193
- gr.Button("Run 3D").click(run_3d, [n_points, noise, n_estimators, max_depth], [plot3d, mse_text, tree_plot])
 
 
 
194
 
195
  with gr.Tab("🧩 Classification"):
196
  cls_plot = gr.Plot()
197
  cls_text = gr.Markdown()
198
- gr.Button("Run Classification").click(
199
- classification_view,
200
- [n_points, noise, n_estimators, max_depth],
201
- [cls_plot, cls_text],
202
- )
203
-
204
- with gr.Tab("🧪 Compare Models"):
205
- cmp_plot = gr.Plot()
206
- gr.Button("Compare").click(compare_models, [n_points, noise], cmp_plot)
207
-
208
- with gr.Tab("📂 Upload Dataset"):
209
- file = gr.File(file_types=[".csv"])
210
- target = gr.Textbox(label="Target Column")
211
- upload_text = gr.Markdown()
212
-
213
- gr.Button("Train Uploaded Data").click(
214
- train_uploaded,
215
- [file, target, n_estimators, max_depth],
216
- [gr.Plot(visible=False), upload_text],
217
- )
218
 
219
 
220
  demo.launch(theme=gr.themes.Soft(primary_hue="blue"))
 
1
  import gradio as gr
2
  import numpy as np
3
  import plotly.graph_objects as go
4
+ import matplotlib.pyplot as plt
5
  from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
 
6
  from sklearn.metrics import mean_squared_error, accuracy_score
7
  from sklearn.tree import plot_tree
 
8
 
9
  # ------------------------------------------------
10
  # DATA GENERATORS
 
23
 
24
 
25
  def generate_classification(n_points, noise):
26
+ n_points = int(n_points)
27
  X = np.random.randn(n_points, 2)
28
  y = (X[:, 0]**2 + X[:, 1] > 0.5).astype(int)
29
  X += np.random.randn(*X.shape) * noise * 0.1
 
31
 
32
 
33
  # ------------------------------------------------
34
+ # INTERACTIVE 3D RANDOM FOREST
35
  # ------------------------------------------------
36
 
37
  def interactive_3d(n_points, noise, n_estimators, max_depth):
38
+ n_estimators = int(n_estimators)
39
+ max_depth = int(max_depth)
40
+
41
  X1, X2, X_flat, Z_flat = generate_3d_regression(n_points, noise)
42
 
43
  rf = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
 
52
 
53
  fig.update_layout(
54
  title="Interactive 3D Random Forest Surface",
55
+ scene=dict(bgcolor="#0b1e3d"),
 
 
 
 
 
56
  paper_bgcolor="#0b1e3d",
57
  font=dict(color="white")
58
  )
 
82
  # ------------------------------------------------
83
 
84
  def classification_view(n_points, noise, n_estimators, max_depth):
85
+ n_estimators = int(n_estimators)
86
+ max_depth = int(max_depth)
87
+
88
  X, y = generate_classification(n_points, noise)
89
 
90
  rf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
 
113
 
114
 
115
  # ------------------------------------------------
116
+ # GRADIO UI (AUTO RUN, BLUE THEME)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # ------------------------------------------------
118
 
119
  with gr.Blocks() as demo:
120
 
121
+ gr.Markdown("# 🌲 Random Forest Teaching Dashboard")
122
 
123
+ with gr.Tab("🌐 3D Regression"):
124
+ n_points = gr.Slider(20, 100, 40, step=1, label="Points")
125
  noise = gr.Slider(0.0, 3.0, 1.0, label="Noise")
126
+ n_estimators = gr.Slider(10, 100, 50, step=1, label="Trees")
127
+ max_depth = gr.Slider(2, 15, 8, step=1, label="Depth")
128
 
129
  plot3d = gr.Plot()
130
  mse_text = gr.Markdown()
 
135
  tree_fig = show_tree(rf)
136
  return fig, text, tree_fig
137
 
138
+ for inp in [n_points, noise, n_estimators, max_depth]:
139
+ inp.change(run_3d, [n_points, noise, n_estimators, max_depth], [plot3d, mse_text, tree_plot])
140
+
141
+ demo.load(run_3d, [n_points, noise, n_estimators, max_depth], [plot3d, mse_text, tree_plot])
142
 
143
  with gr.Tab("🧩 Classification"):
144
  cls_plot = gr.Plot()
145
  cls_text = gr.Markdown()
146
+
147
+ for inp in [n_points, noise, n_estimators, max_depth]:
148
+ inp.change(classification_view, [n_points, noise, n_estimators, max_depth], [cls_plot, cls_text])
149
+
150
+ demo.load(classification_view, [n_points, noise, n_estimators, max_depth], [cls_plot, cls_text])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
 
153
  demo.launch(theme=gr.themes.Soft(primary_hue="blue"))