Spaces:
Sleeping
Sleeping
File size: 11,062 Bytes
770d448 d518d87 484caec d518d87 484caec 770d448 484caec 770d448 484caec 0c4e50c 770d448 484caec 770d448 484caec 770d448 484caec 770d448 484caec 770d448 484caec 770d448 484caec 770d448 b1278c1 770d448 b1278c1 484caec 770d448 b43e0ab b1278c1 770d448 484caec 770d448 484caec 770d448 484caec 770d448 484caec 770d448 b1278c1 770d448 484caec 770d448 6cac300 770d448 85ac76b 770d448 6cac300 484caec d904d83 484caec d904d83 484caec d904d83 770d448 6cac300 770d448 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | from typing import Literal
import gradio as gr
from matplotlib.figure import Figure
import sys
from pathlib import Path
root_dir = Path(__file__).resolve().parent.parent.parent
backend_src = root_dir / "backend" / "src"
if str(backend_src) not in sys.path:
sys.path.append(str(backend_src))
from manager import Manager
CSS = """
.hidden-button {
display: none;
}
"""
def handle_dataset_type_change(dataset_type: Literal["Generate", "CSV"]):
if dataset_type == "Generate":
return (
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
)
return (
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
)
def handle_generate_plots(
manager: Manager,
dataset_type: str,
function: str,
x1_range_input: str,
x2_range_input: str,
x_selection_method: str,
sigma: float,
nsample: int,
csv_file: str,
has_header: bool,
x1_col: int,
x2_col: int,
y_col: int,
loss_type: str,
regularizer_type: str,
resolution: int,
) -> tuple[Manager, Figure, Figure, Figure]:
try:
return manager.handle_generate_plots(
dataset_type,
function,
x1_range_input,
x2_range_input,
x_selection_method,
sigma,
nsample,
csv_file,
has_header,
x1_col,
x2_col,
y_col,
loss_type,
regularizer_type,
resolution,
)
except Exception as e:
raise gr.Error("Error generating plots: " + str(e))
def launch():
default_dataset_type = "Generate"
default_function = "-50 * x1 + 30 * x2"
default_x1_range = "-1, 1"
default_x2_range = "-1, 1"
default_x_selection_method = "Grid"
default_sigma = 0.1
default_num_points = 100
default_csv_file = ""
default_has_header = False
default_x1_col = 0
default_x2_col = 1
default_y_col = 2
default_loss_type = "l2"
default_regularizer_type = "l2"
default_resolution = 100
manager = Manager()
manager, default_contour_plot, default_data_plot, default_strength_plot = manager.handle_generate_plots(
default_dataset_type,
default_function,
default_x1_range,
default_x2_range,
default_x_selection_method,
default_sigma,
default_num_points,
default_csv_file,
default_has_header,
default_x1_col,
default_x2_col,
default_y_col,
default_loss_type,
default_regularizer_type,
default_resolution,
)
with gr.Blocks() as demo:
gr.HTML("<div style='text-align:left; font-size:40px; font-weight: bold;'>Regularization visualizer</div>")
manager_state = gr.State(manager)
with gr.Row():
with gr.Column(scale=2):
with gr.Tab("Contours"):
main_plot = gr.Plot(value=default_contour_plot)
with gr.Tab("Data"):
data_plot = gr.Plot(value=default_data_plot)
with gr.Tab("Strength"):
strength_plot = gr.Plot(value=default_strength_plot)
with gr.Column(scale=1):
with gr.Tab("Data"):
with gr.Row():
dataset_type = gr.Radio(
label="Dataset type",
choices=["Generate", "CSV"],
value=default_dataset_type,
interactive=True,
)
with gr.Row():
function = gr.Textbox(
label="Function (in terms of x1 and x2)",
value=default_function,
interactive=True,
)
with gr.Row():
x1_textbox = gr.Textbox(
label="x1 range",
value=default_x1_range,
interactive=True,
)
x2_textbox = gr.Textbox(
label="x2 range",
value=default_x2_range,
interactive=True,
)
with gr.Row():
x_selection_method = gr.Radio(
label="How to select x points",
choices=["Grid", "Random"],
value=default_x_selection_method,
interactive=True,
)
with gr.Row():
sigma = gr.Number(
label="Gaussian noise standard deviation",
value=default_sigma,
interactive=True,
)
with gr.Row():
nsample = gr.Slider(
label="Number of points",
value=default_num_points,
interactive=True,
minimum=2, # todo - set to 1 after fixing weird cases
maximum=100,
step=1,
)
with gr.Row():
csv_file = gr.File(
label="Upload CSV file - must have columns: (x1, x2, y)",
file_types=[".csv"],
visible=False,
)
with gr.Row():
has_header = gr.Checkbox(
label="CSV has header row",
value=default_has_header,
visible=False,
)
with gr.Row():
x1_col = gr.Number(
label="x1 column index (0-based)",
value=default_x1_col,
visible=False,
)
x2_col = gr.Number(
label="x2 column index (0-based)",
value=default_x2_col,
visible=False,
)
with gr.Row():
y_col = gr.Number(
label="y column index (0-based)",
value=default_y_col,
visible=False,
)
dataset_type.change(
fn=handle_dataset_type_change,
inputs=[dataset_type],
outputs=[
function,
x1_textbox,
x2_textbox,
x_selection_method,
sigma,
nsample,
csv_file,
has_header,
x1_col,
x2_col,
y_col,
],
)
regenerate_plots_button1 = gr.Button("Regenerate Plots")
with gr.Tab("Regularization"):
with gr.Row():
loss_type_dropdown = gr.Dropdown(
label="Loss type",
choices=["l1", "l2"],
value=default_loss_type,
interactive=True,
)
regularizer_type_dropdown = gr.Dropdown(
label="Regularizer type",
choices=["l1", "l2"],
value=default_regularizer_type,
interactive=True,
)
resolution_slider = gr.Slider(
label="Grid resolution",
value=default_resolution,
minimum=100,
maximum=400,
step=1,
interactive=True,
)
regenerate_plots_button2 = gr.Button("Regenerate Plots")
with gr.Tab("Usage"):
with open(root_dir / "usage.md", "r") as f:
gr.Markdown(f.read())
regenerate_plots_button1.click(
fn=handle_generate_plots,
inputs=[
manager_state,
dataset_type,
function,
x1_textbox,
x2_textbox,
x_selection_method,
sigma,
nsample,
csv_file,
has_header,
x1_col,
x2_col,
y_col,
loss_type_dropdown,
regularizer_type_dropdown,
resolution_slider,
],
outputs=[manager_state, main_plot, data_plot, strength_plot],
)
regenerate_plots_button2.click(
fn=handle_generate_plots,
inputs=[
manager_state,
dataset_type,
function,
x1_textbox,
x2_textbox,
x_selection_method,
sigma,
nsample,
csv_file,
has_header,
x1_col,
x2_col,
y_col,
loss_type_dropdown,
regularizer_type_dropdown,
resolution_slider,
],
outputs=[manager_state, main_plot, data_plot, strength_plot],
)
demo.launch(css=CSS)
if __name__ == "__main__":
launch()
|