selva1909 commited on
Commit
a822c68
Β·
verified Β·
1 Parent(s): 0fcc5e8

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +19 -21
src/streamlit_app.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  import numpy as np
3
- import time
4
  import matplotlib.pyplot as plt
5
  from sklearn.linear_model import LinearRegression
6
  from sklearn.metrics import mean_squared_error
@@ -22,7 +21,7 @@ noise_level = st.sidebar.slider("Noise Level", 0.0, 5.0, 1.0)
22
  # ⭐ SHOW ROTATION OPTION ONLY IN 3D MODE
23
  rotate_3d = False
24
  if mode == "3D Regression":
25
- rotate_3d = st.sidebar.toggle("πŸ”„ Rotate 3D Model (Smooth Animation)", value=False)
26
 
27
  train_btn = st.sidebar.button("Generate & Train Model")
28
 
@@ -41,8 +40,8 @@ if mode != st.session_state.current_mode:
41
  # ------------------------------------
42
  if train_btn:
43
 
44
- with st.spinner("⏳ Training model... Please wait πŸ˜„"):
45
- time.sleep(1.0)
46
 
47
  if mode == "2D Regression":
48
  X = np.linspace(0, 10, num_points).reshape(-1, 1)
@@ -88,16 +87,15 @@ if st.session_state.trained:
88
  col1, col2 = st.columns([2, 1])
89
 
90
  with col1:
91
- fig, ax = plt.subplots(figsize=(6, 4))
92
- ax.scatter(X, y, color="orange", label="Data")
93
  ax.plot(X, y_pred, color="blue", linewidth=2, label="Regression Line")
94
  ax.set_title("2D Linear Regression")
95
  ax.legend()
96
- st.pyplot(fig)
97
 
98
  with col2:
99
  st.metric("MSE", f"{mse:.4f}")
100
- st.write("### Equation:")
101
  st.code(f"y = {model.coef_[0]:.3f}x + {model.intercept_:.3f}")
102
 
103
  # ----------------- 3D -----------------
@@ -110,39 +108,39 @@ if st.session_state.trained:
110
 
111
  # Static 3D plot
112
  if not rotate_3d:
113
- fig = plt.figure(figsize=(6, 5))
114
  ax = fig.add_subplot(111, projection="3d")
115
 
116
- idx = np.random.choice(len(Z.ravel()), min(400, len(Z.ravel())), replace=False)
117
  ax.scatter(X1.ravel()[idx], X2.ravel()[idx], Z.ravel()[idx],
118
- color="orange", alpha=0.3, s=6)
119
 
120
- ax.plot_surface(X1, X2, Z_pred, alpha=0.7, color="blue")
121
  ax.set_title("3D Linear Regression")
122
- st.pyplot(fig)
123
 
124
- # Smooth rotation animation
125
  else:
126
  placeholder = st.empty()
127
 
128
- for angle in range(0, 360, 3):
129
- fig = plt.figure(figsize=(6, 5))
130
  ax = fig.add_subplot(111, projection="3d")
131
 
132
- idx = np.random.choice(len(Z.ravel()), min(350, len(Z.ravel())), replace=False)
133
  ax.scatter(X1.ravel()[idx], X2.ravel()[idx], Z.ravel()[idx],
134
- alpha=0.22, color="orange", s=5)
135
 
136
  ax.plot_surface(X1, X2, Z_pred, alpha=0.75, color="blue")
137
 
138
  ax.view_init(elev=25, azim=angle)
139
- ax.set_title("πŸ”„ Rotating 3D Linear Regression")
140
 
141
- placeholder.pyplot(fig)
 
142
 
143
  with col2:
144
  st.metric("MSE", f"{mse:.4f}")
145
- st.write("### Equation:")
146
  st.code(f"z = {model.coef_[0]:.3f}x₁ + {model.coef_[1]:.3f}xβ‚‚ + {model.intercept_:.3f}")
147
 
148
  else:
 
1
  import streamlit as st
2
  import numpy as np
 
3
  import matplotlib.pyplot as plt
4
  from sklearn.linear_model import LinearRegression
5
  from sklearn.metrics import mean_squared_error
 
21
  # ⭐ SHOW ROTATION OPTION ONLY IN 3D MODE
22
  rotate_3d = False
23
  if mode == "3D Regression":
24
+ rotate_3d = st.sidebar.toggle("πŸ”„ Rotate 3D Model", value=False)
25
 
26
  train_btn = st.sidebar.button("Generate & Train Model")
27
 
 
40
  # ------------------------------------
41
  if train_btn:
42
 
43
+ with st.spinner("⏳ Training model..."):
44
+ st.sleep(0.5)
45
 
46
  if mode == "2D Regression":
47
  X = np.linspace(0, 10, num_points).reshape(-1, 1)
 
87
  col1, col2 = st.columns([2, 1])
88
 
89
  with col1:
90
+ fig, ax = plt.subplots(figsize=(4.5, 4))
91
+ ax.scatter(X, y, color="orange", label="Data", s=18)
92
  ax.plot(X, y_pred, color="blue", linewidth=2, label="Regression Line")
93
  ax.set_title("2D Linear Regression")
94
  ax.legend()
95
+ st.pyplot(fig, clear_figure=True)
96
 
97
  with col2:
98
  st.metric("MSE", f"{mse:.4f}")
 
99
  st.code(f"y = {model.coef_[0]:.3f}x + {model.intercept_:.3f}")
100
 
101
  # ----------------- 3D -----------------
 
108
 
109
  # Static 3D plot
110
  if not rotate_3d:
111
+ fig = plt.figure(figsize=(4.5, 4))
112
  ax = fig.add_subplot(111, projection="3d")
113
 
114
+ idx = np.random.choice(len(Z.ravel()), min(350, len(Z.ravel())), replace=False)
115
  ax.scatter(X1.ravel()[idx], X2.ravel()[idx], Z.ravel()[idx],
116
+ color="orange", alpha=0.25, s=8)
117
 
118
+ ax.plot_surface(X1, X2, Z_pred, alpha=0.75, color="blue")
119
  ax.set_title("3D Linear Regression")
120
+ st.pyplot(fig, clear_figure=True)
121
 
122
+ # Smooth rotation animation for HF
123
  else:
124
  placeholder = st.empty()
125
 
126
+ for angle in range(0, 360, 5): # Less frequent frames β†’ smoother on HF
127
+ fig = plt.figure(figsize=(4.5, 4))
128
  ax = fig.add_subplot(111, projection="3d")
129
 
130
+ idx = np.random.choice(len(Z.ravel()), min(300, len(Z.ravel())), replace=False)
131
  ax.scatter(X1.ravel()[idx], X2.ravel()[idx], Z.ravel()[idx],
132
+ alpha=0.2, color="orange", s=6)
133
 
134
  ax.plot_surface(X1, X2, Z_pred, alpha=0.75, color="blue")
135
 
136
  ax.view_init(elev=25, azim=angle)
137
+ ax.set_title("πŸ”„ Rotating 3D Regression Model")
138
 
139
+ placeholder.pyplot(fig, clear_figure=True)
140
+ st.sleep(0.07) # REQUIRED for HF to animate
141
 
142
  with col2:
143
  st.metric("MSE", f"{mse:.4f}")
 
144
  st.code(f"z = {model.coef_[0]:.3f}x₁ + {model.coef_[1]:.3f}xβ‚‚ + {model.intercept_:.3f}")
145
 
146
  else: