Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ import numpy as np
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
# Page Configurations
|
| 6 |
-
st.set_page_config(page_title="
|
| 7 |
-
st.markdown("<h1 style='text-align: center; color: #FFD700;'
|
| 8 |
|
| 9 |
# Custom CSS for Background and Buttons
|
| 10 |
st.markdown("""
|
|
@@ -14,17 +14,17 @@ st.markdown("""
|
|
| 14 |
color: white; /* White text for contrast */
|
| 15 |
}
|
| 16 |
.stButton>button {
|
| 17 |
-
background: linear-gradient(to right, #
|
| 18 |
color: white; /* White text */
|
| 19 |
border-radius: 8px;
|
| 20 |
padding: 8px 16px;
|
| 21 |
font-size: 16px;
|
| 22 |
}
|
| 23 |
.stButton>button:hover {
|
| 24 |
-
background: linear-gradient(to right, #
|
| 25 |
}
|
| 26 |
.stMarkdown h3 {
|
| 27 |
-
color: #
|
| 28 |
}
|
| 29 |
</style>
|
| 30 |
""", unsafe_allow_html=True)
|
|
@@ -70,16 +70,16 @@ left_col, right_col = st.columns([1, 2])
|
|
| 70 |
|
| 71 |
# Left Column: Inputs
|
| 72 |
with left_col:
|
| 73 |
-
st.markdown("###
|
| 74 |
function_input = st.text_input(
|
| 75 |
-
"
|
| 76 |
"x**2 + x",
|
| 77 |
key="math_function",
|
| 78 |
on_change=reset_session_state
|
| 79 |
)
|
| 80 |
-
st.markdown("### ๐ง
|
| 81 |
initial_point = st.number_input(
|
| 82 |
-
"
|
| 83 |
value=4.0,
|
| 84 |
step=0.1,
|
| 85 |
format="%.2f",
|
|
@@ -87,17 +87,17 @@ with left_col:
|
|
| 87 |
on_change=reset_session_state
|
| 88 |
)
|
| 89 |
st.number_input(
|
| 90 |
-
"Learning Rate:",
|
| 91 |
value=st.session_state.learning_rate,
|
| 92 |
step=0.01,
|
| 93 |
format="%.2f",
|
| 94 |
key="learning_rate"
|
| 95 |
)
|
| 96 |
|
| 97 |
-
st.markdown("###
|
| 98 |
col1, col2 = st.columns(2)
|
| 99 |
with col1:
|
| 100 |
-
if st.button("
|
| 101 |
try:
|
| 102 |
gradient = compute_derivative(function_input, st.session_state.x_current)
|
| 103 |
st.session_state.x_current -= st.session_state.learning_rate * gradient
|
|
@@ -109,44 +109,44 @@ with left_col:
|
|
| 109 |
except Exception as e:
|
| 110 |
st.error(f"Error: {str(e)}")
|
| 111 |
with col2:
|
| 112 |
-
if st.button("
|
| 113 |
reset_session_state()
|
| 114 |
|
| 115 |
# Right Column: Visualization
|
| 116 |
with right_col:
|
| 117 |
-
st.markdown("###
|
| 118 |
|
| 119 |
# Navigation Buttons
|
| 120 |
col1, col2, col3 = st.columns(3)
|
| 121 |
with col1:
|
| 122 |
-
if st.button("โฌ
๏ธ Step
|
| 123 |
st.session_state.current_index -= 1
|
| 124 |
with col2:
|
| 125 |
-
st.markdown(f"<p style='text-align: center;'>
|
| 126 |
with col3:
|
| 127 |
-
if st.button("โก๏ธ Step
|
| 128 |
st.session_state.current_index += 1
|
| 129 |
|
| 130 |
try:
|
| 131 |
selected_x, selected_y = st.session_state.history[st.session_state.current_index]
|
| 132 |
-
st.markdown(f"๐ **x
|
| 133 |
-
st.markdown(f"๐ **f(x):** `{selected_y:.4f}`")
|
| 134 |
except IndexError:
|
| 135 |
-
st.warning("No
|
| 136 |
|
| 137 |
# Visualization
|
| 138 |
x_range = np.linspace(-10, 10, 500)
|
| 139 |
y_range = [evaluate_function(function_input, x) for x in x_range]
|
| 140 |
|
| 141 |
fig = go.Figure()
|
| 142 |
-
fig.add_trace(go.Scatter(x=x_range, y=y_range, mode='lines', name='
|
| 143 |
x_current, y_current = st.session_state.history[st.session_state.current_index]
|
| 144 |
-
fig.add_trace(go.Scatter(x=[x_current], y=[y_current], mode='markers', name='Current
|
| 145 |
tangent_y = calculate_tangent(function_input, x_current, x_range)
|
| 146 |
-
fig.add_trace(go.Scatter(x=x_range, y=tangent_y, mode='lines', name='Tangent
|
| 147 |
|
| 148 |
fig.update_layout(
|
| 149 |
-
title="
|
| 150 |
xaxis_title="x",
|
| 151 |
yaxis_title="f(x)",
|
| 152 |
template="plotly_dark",
|
|
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
# Page Configurations
|
| 6 |
+
st.set_page_config(page_title="Optimization Explorer", layout="wide")
|
| 7 |
+
st.markdown("<h1 style='text-align: center; color: #FFD700;'>โก Optimization Explorer โก</h1>", unsafe_allow_html=True)
|
| 8 |
|
| 9 |
# Custom CSS for Background and Buttons
|
| 10 |
st.markdown("""
|
|
|
|
| 14 |
color: white; /* White text for contrast */
|
| 15 |
}
|
| 16 |
.stButton>button {
|
| 17 |
+
background: linear-gradient(to right, #4CAF50, #2E7D32); /* Green gradient */
|
| 18 |
color: white; /* White text */
|
| 19 |
border-radius: 8px;
|
| 20 |
padding: 8px 16px;
|
| 21 |
font-size: 16px;
|
| 22 |
}
|
| 23 |
.stButton>button:hover {
|
| 24 |
+
background: linear-gradient(to right, #2E7D32, #4CAF50); /* Reverse gradient on hover */
|
| 25 |
}
|
| 26 |
.stMarkdown h3 {
|
| 27 |
+
color: #03A9F4; /* Blue color for section titles */
|
| 28 |
}
|
| 29 |
</style>
|
| 30 |
""", unsafe_allow_html=True)
|
|
|
|
| 70 |
|
| 71 |
# Left Column: Inputs
|
| 72 |
with left_col:
|
| 73 |
+
st.markdown("### ๐ Define Your Equation")
|
| 74 |
function_input = st.text_input(
|
| 75 |
+
"Input Equation (e.g., `x**2`, `np.sin(x)`):",
|
| 76 |
"x**2 + x",
|
| 77 |
key="math_function",
|
| 78 |
on_change=reset_session_state
|
| 79 |
)
|
| 80 |
+
st.markdown("### ๐ง Configure Settings")
|
| 81 |
initial_point = st.number_input(
|
| 82 |
+
"Starting Value of x:",
|
| 83 |
value=4.0,
|
| 84 |
step=0.1,
|
| 85 |
format="%.2f",
|
|
|
|
| 87 |
on_change=reset_session_state
|
| 88 |
)
|
| 89 |
st.number_input(
|
| 90 |
+
"Step Size (Learning Rate):",
|
| 91 |
value=st.session_state.learning_rate,
|
| 92 |
step=0.01,
|
| 93 |
format="%.2f",
|
| 94 |
key="learning_rate"
|
| 95 |
)
|
| 96 |
|
| 97 |
+
st.markdown("### ๐น๏ธ Actions")
|
| 98 |
col1, col2 = st.columns(2)
|
| 99 |
with col1:
|
| 100 |
+
if st.button("๐ Compute Next Step"):
|
| 101 |
try:
|
| 102 |
gradient = compute_derivative(function_input, st.session_state.x_current)
|
| 103 |
st.session_state.x_current -= st.session_state.learning_rate * gradient
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
st.error(f"Error: {str(e)}")
|
| 111 |
with col2:
|
| 112 |
+
if st.button("๐ Restart"):
|
| 113 |
reset_session_state()
|
| 114 |
|
| 115 |
# Right Column: Visualization
|
| 116 |
with right_col:
|
| 117 |
+
st.markdown("### ๐ Exploration of Optimization Steps")
|
| 118 |
|
| 119 |
# Navigation Buttons
|
| 120 |
col1, col2, col3 = st.columns(3)
|
| 121 |
with col1:
|
| 122 |
+
if st.button("โฌ
๏ธ Previous Step") and st.session_state.current_index > 0:
|
| 123 |
st.session_state.current_index -= 1
|
| 124 |
with col2:
|
| 125 |
+
st.markdown(f"<p style='text-align: center;'>Step Count: <strong>{st.session_state.current_index}</strong></p>", unsafe_allow_html=True)
|
| 126 |
with col3:
|
| 127 |
+
if st.button("โก๏ธ Next Step") and st.session_state.current_index < st.session_state.iter_count:
|
| 128 |
st.session_state.current_index += 1
|
| 129 |
|
| 130 |
try:
|
| 131 |
selected_x, selected_y = st.session_state.history[st.session_state.current_index]
|
| 132 |
+
st.markdown(f"๐ **Current x:** `{selected_x:.4f}`")
|
| 133 |
+
st.markdown(f"๐ **f(x) at Current Step:** `{selected_y:.4f}`")
|
| 134 |
except IndexError:
|
| 135 |
+
st.warning("No data to display. Perform a computation first.")
|
| 136 |
|
| 137 |
# Visualization
|
| 138 |
x_range = np.linspace(-10, 10, 500)
|
| 139 |
y_range = [evaluate_function(function_input, x) for x in x_range]
|
| 140 |
|
| 141 |
fig = go.Figure()
|
| 142 |
+
fig.add_trace(go.Scatter(x=x_range, y=y_range, mode='lines', name='Equation', line=dict(color='#FFD700')))
|
| 143 |
x_current, y_current = st.session_state.history[st.session_state.current_index]
|
| 144 |
+
fig.add_trace(go.Scatter(x=[x_current], y=[y_current], mode='markers', name='Current Position', marker=dict(size=12, color='#FF4500')))
|
| 145 |
tangent_y = calculate_tangent(function_input, x_current, x_range)
|
| 146 |
+
fig.add_trace(go.Scatter(x=x_range, y=tangent_y, mode='lines', name='Tangent', line=dict(dash='dash', color='#00FFFF')))
|
| 147 |
|
| 148 |
fig.update_layout(
|
| 149 |
+
title="Optimization Process",
|
| 150 |
xaxis_title="x",
|
| 151 |
yaxis_title="f(x)",
|
| 152 |
template="plotly_dark",
|