MilesCranmer commited on
Commit
dd65136
1 Parent(s): e1cf25c

Refactor app

Browse files
Files changed (1) hide show
  1. gui/app.py +114 -83
gui/app.py CHANGED
@@ -110,112 +110,143 @@ model.fit(X, y)"""
110
  return df, msg
111
 
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  def main():
 
114
  with gr.Blocks() as demo:
115
  with gr.Row():
116
  with gr.Column():
117
  with gr.Row():
118
- with gr.Tab("Example Data"):
119
- # Plot of the example data:
120
- example_plot = gr.ScatterPlot(
121
- x="x",
122
- y="y",
123
- tooltip=["x", "y"],
124
- x_lim=[0, 10],
125
- y_lim=[-5, 5],
126
- width=350,
127
- height=300,
128
- )
129
- test_equation = gr.Radio(
130
- test_equations,
131
- value=test_equations[0],
132
- label="Test Equation"
133
- )
134
- num_points = gr.Slider(
135
- minimum=10,
136
- maximum=1000,
137
- value=100,
138
- label="Number of Data Points",
139
- step=1,
140
- )
141
- noise_level = gr.Slider(
142
- minimum=0, maximum=1, value=0.1, label="Noise Level"
143
- )
144
- with gr.Tab("Upload Data"):
145
- file_input = gr.File(label="Upload a CSV File")
146
- gr.Markdown("Upload a CSV file with the data to fit. The last column will be used as the target variable.")
147
  with gr.Row():
148
- binary_operators = gr.CheckboxGroup(
149
- choices=["+", "-", "*", "/", "^"],
150
- label="Binary Operators",
151
- value=["+", "-", "*", "/"],
152
- )
153
- unary_operators = gr.CheckboxGroup(
154
- choices=[
155
- "sin",
156
- "cos",
157
- "exp",
158
- "log",
159
- "square",
160
- "cube",
161
- "sqrt",
162
- "abs",
163
- "tan",
164
- ],
165
- label="Unary Operators",
166
- value=[],
167
- )
168
- niterations = gr.Slider(
169
- minimum=1,
170
- maximum=1000,
171
- value=40,
172
- label="Number of Iterations",
173
- step=1,
174
- )
175
- maxsize = gr.Slider(
176
- minimum=7,
177
- maximum=35,
178
- value=20,
179
- label="Maximum Complexity",
180
- step=1,
181
- )
182
- force_run = gr.Checkbox(
183
- value=False,
184
- label="Ignore Warnings",
185
- )
186
 
187
  with gr.Column():
188
  with gr.Row():
189
- df = gr.Dataframe(
190
  headers=["Equation", "Loss", "Complexity"],
191
  datatype=["str", "number", "number"],
192
  )
193
- error_log = gr.Textbox(label="Error Log")
194
  with gr.Row():
195
- run_button = gr.Button()
196
 
197
- run_button.click(
198
  greet,
199
  inputs=[
200
- file_input,
201
- test_equation,
202
- num_points,
203
- noise_level,
204
- niterations,
205
- maxsize,
206
- binary_operators,
207
- unary_operators,
208
- force_run,
 
 
 
209
  ],
210
- outputs=[df, error_log],
211
  )
212
 
213
  # Any update to the equation choice will trigger a replot:
214
- for eqn_component in [test_equation, num_points, noise_level]:
215
- eqn_component.change(replot, [test_equation, num_points, noise_level], example_plot)
 
 
 
 
 
216
 
217
  demo.launch()
218
 
 
219
  def replot(test_equation, num_points, noise_level):
220
  X, y = generate_data(test_equation, num_points, noise_level)
221
  df = pd.DataFrame({"x": X["x"], "y": y})
 
110
  return df, msg
111
 
112
 
113
+ def _data_layout():
114
+ with gr.Tab("Example Data"):
115
+ # Plot of the example data:
116
+ example_plot = gr.ScatterPlot(
117
+ x="x",
118
+ y="y",
119
+ tooltip=["x", "y"],
120
+ x_lim=[0, 10],
121
+ y_lim=[-5, 5],
122
+ width=350,
123
+ height=300,
124
+ )
125
+ test_equation = gr.Radio(
126
+ test_equations, value=test_equations[0], label="Test Equation"
127
+ )
128
+ num_points = gr.Slider(
129
+ minimum=10,
130
+ maximum=1000,
131
+ value=100,
132
+ label="Number of Data Points",
133
+ step=1,
134
+ )
135
+ noise_level = gr.Slider(minimum=0, maximum=1, value=0.1, label="Noise Level")
136
+ with gr.Tab("Upload Data"):
137
+ file_input = gr.File(label="Upload a CSV File")
138
+ gr.Markdown(
139
+ "Upload a CSV file with the data to fit. The last column will be used as the target variable."
140
+ )
141
+
142
+ return dict(
143
+ file_input=file_input,
144
+ test_equation=test_equation,
145
+ num_points=num_points,
146
+ noise_level=noise_level,
147
+ example_plot=example_plot,
148
+ )
149
+
150
+
151
+ def _settings_layout():
152
+ binary_operators = gr.CheckboxGroup(
153
+ choices=["+", "-", "*", "/", "^"],
154
+ label="Binary Operators",
155
+ value=["+", "-", "*", "/"],
156
+ )
157
+ unary_operators = gr.CheckboxGroup(
158
+ choices=[
159
+ "sin",
160
+ "cos",
161
+ "exp",
162
+ "log",
163
+ "square",
164
+ "cube",
165
+ "sqrt",
166
+ "abs",
167
+ "tan",
168
+ ],
169
+ label="Unary Operators",
170
+ value=[],
171
+ )
172
+ niterations = gr.Slider(
173
+ minimum=1,
174
+ maximum=1000,
175
+ value=40,
176
+ label="Number of Iterations",
177
+ step=1,
178
+ )
179
+ maxsize = gr.Slider(
180
+ minimum=7,
181
+ maximum=35,
182
+ value=20,
183
+ label="Maximum Complexity",
184
+ step=1,
185
+ )
186
+ force_run = gr.Checkbox(
187
+ value=False,
188
+ label="Ignore Warnings",
189
+ )
190
+ return dict(
191
+ binary_operators=binary_operators,
192
+ unary_operators=unary_operators,
193
+ niterations=niterations,
194
+ maxsize=maxsize,
195
+ force_run=force_run,
196
+ )
197
+
198
+
199
  def main():
200
+ blocks = {}
201
  with gr.Blocks() as demo:
202
  with gr.Row():
203
  with gr.Column():
204
  with gr.Row():
205
+ blocks = {**blocks, **_data_layout()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  with gr.Row():
207
+ blocks = {**blocks, **_settings_layout()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  with gr.Column():
210
  with gr.Row():
211
+ blocks["df"] = gr.Dataframe(
212
  headers=["Equation", "Loss", "Complexity"],
213
  datatype=["str", "number", "number"],
214
  )
215
+ blocks["error_log"] = gr.Textbox(label="Error Log")
216
  with gr.Row():
217
+ blocks["run"] = gr.Button()
218
 
219
+ blocks["run"].click(
220
  greet,
221
  inputs=[
222
+ blocks[k]
223
+ for k in [
224
+ "file_input",
225
+ "test_equation",
226
+ "num_points",
227
+ "noise_level",
228
+ "niterations",
229
+ "maxsize",
230
+ "binary_operators",
231
+ "unary_operators",
232
+ "force_run",
233
+ ]
234
  ],
235
+ outputs=[blocks["df"], blocks["error_log"]],
236
  )
237
 
238
  # Any update to the equation choice will trigger a replot:
239
+ eqn_components = [
240
+ blocks["test_equation"],
241
+ blocks["num_points"],
242
+ blocks["noise_level"],
243
+ ]
244
+ for eqn_component in eqn_components:
245
+ eqn_component.change(replot, eqn_components, blocks["example_plot"])
246
 
247
  demo.launch()
248
 
249
+
250
  def replot(test_equation, num_points, noise_level):
251
  X, y = generate_data(test_equation, num_points, noise_level)
252
  df = pd.DataFrame({"x": X["x"], "y": y})