Update app.py
Browse files
app.py
CHANGED
|
@@ -86,6 +86,31 @@ st.markdown(
|
|
| 86 |
border-radius: 5px;
|
| 87 |
padding: 10px;
|
| 88 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
</style>
|
| 90 |
""",
|
| 91 |
unsafe_allow_html=True,
|
|
@@ -100,19 +125,24 @@ col1, col2 = st.columns([1, 2])
|
|
| 100 |
with col1:
|
| 101 |
st.subheader("🔧 Define Your Function")
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# Use text input for the user to define a function
|
| 117 |
func_input = st.text_input(
|
| 118 |
"Enter a function of 'x':",
|
|
@@ -135,116 +165,4 @@ with col1:
|
|
| 135 |
value=0.25,
|
| 136 |
step=0.01,
|
| 137 |
format="%.2f",
|
| 138 |
-
|
| 139 |
-
on_change=reset_state
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
col3, col4 = st.columns(2)
|
| 143 |
-
with col3:
|
| 144 |
-
if st.button("🔄 Set Up Function"):
|
| 145 |
-
reset_state()
|
| 146 |
-
with col4:
|
| 147 |
-
if st.button("▶️ Next Iteration"):
|
| 148 |
-
try:
|
| 149 |
-
grad = derivative(st.session_state.func_input, st.session_state.x)
|
| 150 |
-
st.session_state.x = st.session_state.x - learning_rate * grad
|
| 151 |
-
st.session_state.iteration += 1
|
| 152 |
-
st.session_state.x_vals.append(st.session_state.x)
|
| 153 |
-
st.session_state.y_vals.append(safe_eval(st.session_state.func_input, st.session_state.x))
|
| 154 |
-
except Exception as e:
|
| 155 |
-
st.error(f"⚠️ Error: {str(e)}")
|
| 156 |
-
|
| 157 |
-
# Right Section: Visualization
|
| 158 |
-
with col2:
|
| 159 |
-
st.subheader("📊 Gradient Descent Visualization")
|
| 160 |
-
try:
|
| 161 |
-
# Plot the function and all current and previous gradient descent points
|
| 162 |
-
x_plot = np.linspace(-10, 10, 400)
|
| 163 |
-
y_plot = [safe_eval(st.session_state.func_input, x) for x in x_plot]
|
| 164 |
-
|
| 165 |
-
fig = go.Figure()
|
| 166 |
-
|
| 167 |
-
# Function curve
|
| 168 |
-
fig.add_trace(go.Scatter(
|
| 169 |
-
x=x_plot,
|
| 170 |
-
y=y_plot,
|
| 171 |
-
mode="lines+markers",
|
| 172 |
-
line=dict(color="blue", width=2),
|
| 173 |
-
marker=dict(size=4, color="blue", symbol="circle"),
|
| 174 |
-
name="Function"
|
| 175 |
-
))
|
| 176 |
-
|
| 177 |
-
# All gradient descent points (red points without coordinates)
|
| 178 |
-
fig.add_trace(go.Scatter(
|
| 179 |
-
x=st.session_state.x_vals,
|
| 180 |
-
y=st.session_state.y_vals,
|
| 181 |
-
mode="markers",
|
| 182 |
-
marker=dict(color="red", size=10),
|
| 183 |
-
name="Gradient Descent Points"
|
| 184 |
-
))
|
| 185 |
-
|
| 186 |
-
# Tangent line at the current gradient descent point
|
| 187 |
-
current_x = st.session_state.x
|
| 188 |
-
tangent_x = np.linspace(current_x - 5, current_x + 5, 200) # Extended range for tangent line
|
| 189 |
-
tangent_y = tangent_line(st.session_state.func_input, current_x, tangent_x)
|
| 190 |
-
fig.add_trace(go.Scatter(
|
| 191 |
-
x=tangent_x,
|
| 192 |
-
y=tangent_y,
|
| 193 |
-
mode="lines",
|
| 194 |
-
line=dict(color="orange", width=3),
|
| 195 |
-
name="Tangent Line"
|
| 196 |
-
))
|
| 197 |
-
|
| 198 |
-
# Dynamic zoom for better visibility
|
| 199 |
-
fig.update_layout(
|
| 200 |
-
xaxis=dict(
|
| 201 |
-
title="x-axis",
|
| 202 |
-
range=[-10, 10],
|
| 203 |
-
showline=True,
|
| 204 |
-
linecolor="white",
|
| 205 |
-
tickcolor="white",
|
| 206 |
-
tickfont=dict(color="white"),
|
| 207 |
-
ticks="outside",
|
| 208 |
-
),
|
| 209 |
-
yaxis=dict(
|
| 210 |
-
title="y-axis",
|
| 211 |
-
range=[min(y_plot) - 5, max(y_plot) + 5],
|
| 212 |
-
showline=True,
|
| 213 |
-
linecolor="white",
|
| 214 |
-
tickcolor="white",
|
| 215 |
-
tickfont=dict(color="white"),
|
| 216 |
-
ticks="outside",
|
| 217 |
-
),
|
| 218 |
-
plot_bgcolor="black",
|
| 219 |
-
paper_bgcolor="black",
|
| 220 |
-
title="",
|
| 221 |
-
margin=dict(l=10, r=10, t=10, b=10),
|
| 222 |
-
width=800,
|
| 223 |
-
height=400,
|
| 224 |
-
showlegend=True,
|
| 225 |
-
legend=dict(
|
| 226 |
-
x=1.1,
|
| 227 |
-
y=0.5,
|
| 228 |
-
xanchor="left",
|
| 229 |
-
yanchor="middle",
|
| 230 |
-
orientation="v",
|
| 231 |
-
font=dict(size=12, color="white"),
|
| 232 |
-
bgcolor="black",
|
| 233 |
-
bordercolor="white",
|
| 234 |
-
borderwidth=2,
|
| 235 |
-
)
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
# Axis lines for quadrants
|
| 239 |
-
fig.add_shape(type="line", x0=-10, x1=10, y0=0, y1=0, line=dict(color="white", width=2)) # x-axis
|
| 240 |
-
fig.add_shape(type="line", x0=0, x1=0, y0=-100, y1=100, line=dict(color="white", width=2)) # y-axis
|
| 241 |
-
|
| 242 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 243 |
-
|
| 244 |
-
except Exception as e:
|
| 245 |
-
st.error(f"⚠️ Error in visualization: {str(e)}")
|
| 246 |
-
|
| 247 |
-
# Iteration stats and download
|
| 248 |
-
col5, col6 = st.columns(2)
|
| 249 |
-
col5.info(f"🧑💻 Iteration: {st.session_state.iteration}")
|
| 250 |
-
col6.success(f"✅ Current x: {st.session_state.x:.4f}, Current f(x): {st.session_state.y_vals[-1]:.4f}")
|
|
|
|
| 86 |
border-radius: 5px;
|
| 87 |
padding: 10px;
|
| 88 |
}
|
| 89 |
+
/* Hoverable tooltip styling */
|
| 90 |
+
.tooltip:hover .tooltiptext {
|
| 91 |
+
visibility: visible;
|
| 92 |
+
opacity: 1;
|
| 93 |
+
}
|
| 94 |
+
.tooltip {
|
| 95 |
+
position: relative;
|
| 96 |
+
display: inline-block;
|
| 97 |
+
}
|
| 98 |
+
.tooltip .tooltiptext {
|
| 99 |
+
visibility: hidden;
|
| 100 |
+
opacity: 0;
|
| 101 |
+
width: 300px;
|
| 102 |
+
background-color: #001A6E;
|
| 103 |
+
color: #fff;
|
| 104 |
+
text-align: center;
|
| 105 |
+
border-radius: 5px;
|
| 106 |
+
padding: 5px;
|
| 107 |
+
position: absolute;
|
| 108 |
+
z-index: 1;
|
| 109 |
+
bottom: 125%; /* Position the tooltip above */
|
| 110 |
+
left: 50%;
|
| 111 |
+
margin-left: -150px;
|
| 112 |
+
transition: opacity 0.3s;
|
| 113 |
+
}
|
| 114 |
</style>
|
| 115 |
""",
|
| 116 |
unsafe_allow_html=True,
|
|
|
|
| 125 |
with col1:
|
| 126 |
st.subheader("🔧 Define Your Function")
|
| 127 |
|
| 128 |
+
# Tooltip with instructions when hovering over the function input
|
| 129 |
+
st.markdown(
|
| 130 |
+
"""
|
| 131 |
+
<div class="tooltip">
|
| 132 |
+
<input type="text" placeholder="Enter function" readonly style="border: none; color: transparent; background-color: transparent;">
|
| 133 |
+
<span class="tooltiptext">
|
| 134 |
+
**How to input your function:**
|
| 135 |
+
- Use `x` for the variable.
|
| 136 |
+
- Example: For \(x^2\), input `x**2`.
|
| 137 |
+
- For sine function, input `np.sin(x)`.
|
| 138 |
+
- For logarithm, input `np.log(x)`.
|
| 139 |
+
- For other mathematical operations, use `np` (e.g., `np.exp(x)`).
|
| 140 |
+
</span>
|
| 141 |
+
</div>
|
| 142 |
+
""",
|
| 143 |
+
unsafe_allow_html=True
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
# Use text input for the user to define a function
|
| 147 |
func_input = st.text_input(
|
| 148 |
"Enter a function of 'x':",
|
|
|
|
| 165 |
value=0.25,
|
| 166 |
step=0.01,
|
| 167 |
format="%.2f",
|
| 168 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|