Gowthamvemula commited on
Commit
fa7e3f8
ยท
verified ยท
1 Parent(s): 782efb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
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="Gradient Descent Visualizer", layout="wide")
7
- st.markdown("<h1 style='text-align: center; color: #FFD700;'>โœจ Gradient Descent Visualizer โœจ</h1>", unsafe_allow_html=True)
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, #FF7F50, #FF4500); /* Coral to orange 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, #FF4500, #FF7F50); /* Reverse gradient on hover */
25
  }
26
  .stMarkdown h3 {
27
- color: #FFD700; /* Gold color for section titles */
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("### ๐Ÿ“š Input Your Function")
74
  function_input = st.text_input(
75
- "Enter Function (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("### ๐Ÿ”ง Set Parameters")
81
  initial_point = st.number_input(
82
- "Initial Value of x:",
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("### ๐ŸŽฎ Controls")
98
  col1, col2 = st.columns(2)
99
  with col1:
100
- if st.button("๐Ÿš€ Run 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,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("๐Ÿ”„ Reset"):
113
  reset_session_state()
114
 
115
  # Right Column: Visualization
116
  with right_col:
117
- st.markdown("### ๐Ÿ“Š Gradient Descent Visualization")
118
 
119
  # Navigation Buttons
120
  col1, col2, col3 = st.columns(3)
121
  with col1:
122
- if st.button("โฌ…๏ธ Step Back") 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;'>Iteration: <strong>{st.session_state.current_index}</strong></p>", unsafe_allow_html=True)
126
  with col3:
127
- if st.button("โžก๏ธ Step Forward") 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"๐Ÿ“ **x Value:** `{selected_x:.4f}`")
133
- st.markdown(f"๐Ÿ“ˆ **f(x):** `{selected_y:.4f}`")
134
  except IndexError:
135
- st.warning("No iteration data available. Please run a descent step 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='Function', 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 Point', 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', line=dict(dash='dash', color='#00FFFF')))
147
 
148
  fig.update_layout(
149
- title="Gradient Descent Progress",
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",