Spaces:
Running
Running
Update App.py
Browse files
App.py
CHANGED
@@ -1,158 +1,167 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import json
|
3 |
-
import pandas as pd
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
if
|
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 |
-
gr.
|
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 |
-
info='
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import pandas as pd
|
4 |
+
import os
|
5 |
+
from Engine import Engine
|
6 |
+
|
7 |
+
def run_study(mode, benchmark_func, optimizers, dim, dataset, epochs, batch_size, lr, use_sa, sa_temp, sa_cooling_rate):
|
8 |
+
# Ensure optimizers is a list
|
9 |
+
if not isinstance(optimizers, list):
|
10 |
+
optimizers = [optimizers] if optimizers else []
|
11 |
+
|
12 |
+
if not optimizers:
|
13 |
+
raise gr.Error("Please select at least one optimizer.")
|
14 |
+
if mode == "Benchmark Optimization" and not benchmark_func:
|
15 |
+
raise gr.Error("Please select a benchmark function.")
|
16 |
+
if mode == "ML Task Training" and not dataset:
|
17 |
+
raise gr.Error("Please select a dataset.")
|
18 |
+
|
19 |
+
config = {
|
20 |
+
'mode': 'benchmark' if mode == 'Benchmark Optimization' else 'ml_task',
|
21 |
+
'benchmark_func': benchmark_func,
|
22 |
+
'optimizers': optimizers,
|
23 |
+
'dim': int(dim),
|
24 |
+
'dataset': dataset,
|
25 |
+
'epochs': int(epochs) if epochs else 10,
|
26 |
+
'batch_size': int(batch_size) if batch_size else 32,
|
27 |
+
'lr': float(lr) if lr else 0.001,
|
28 |
+
'use_sa': use_sa if 'AzureSky' in optimizers else None,
|
29 |
+
'sa_temp': float(sa_temp) if 'AzureSky' in optimizers and use_sa else None,
|
30 |
+
'sa_cooling_rate': float(sa_cooling_rate) if 'AzureSky' in optimizers and use_sa else None,
|
31 |
+
'max_iter': 100
|
32 |
+
}
|
33 |
+
runner = Engine()
|
34 |
+
results = runner.run(config)
|
35 |
+
|
36 |
+
if config['mode'] == 'benchmark':
|
37 |
+
metrics_df = pd.DataFrame(results['metrics'], index=config['optimizers'])
|
38 |
+
return results['plot'], None, metrics_df, results['metrics'], json.dumps(results, indent=2), "Study completed successfully."
|
39 |
+
else:
|
40 |
+
metrics_df = pd.DataFrame(results['metrics'], index=config['optimizers'])
|
41 |
+
return results['plot_acc'], results['plot_loss'], metrics_df, results['metrics'], json.dumps(results, indent=2), "Study completed successfully."
|
42 |
+
|
43 |
+
def export_results(results_json):
|
44 |
+
return results_json, "results.json"
|
45 |
+
|
46 |
+
def toggle_azure_settings(optimizers):
|
47 |
+
# Handle case where optimizers is a single value or None
|
48 |
+
optimizers = [optimizers] if isinstance(optimizers, str) else optimizers or []
|
49 |
+
return gr.update(visible='AzureSky' in optimizers)
|
50 |
+
|
51 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Nexa R&D Studio", css="""
|
52 |
+
.gr-button { margin-top: 10px; }
|
53 |
+
.gr-box { border-radius: 8px; }
|
54 |
+
.status-message { color: green; font-weight: bold; }
|
55 |
+
""") as app:
|
56 |
+
gr.Markdown("""
|
57 |
+
# Nexa R&D Studio
|
58 |
+
A visual research tool for comparing and evaluating optimizers on benchmark functions and ML tasks.
|
59 |
+
Select a mode, configure your study, and analyze results with interactive plots and metrics.
|
60 |
+
""")
|
61 |
+
|
62 |
+
with gr.Tabs() as tabs:
|
63 |
+
with gr.TabItem("Study Configuration"):
|
64 |
+
mode = gr.Radio(
|
65 |
+
['Benchmark Optimization', 'ML Task Training'],
|
66 |
+
label='Study Mode',
|
67 |
+
value='Benchmark Optimization',
|
68 |
+
info='Choose between optimizing benchmark functions or training on ML datasets.'
|
69 |
+
)
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column():
|
73 |
+
optimizers = gr.CheckboxGroup(
|
74 |
+
['AzureSky', 'Adam', 'SGD', 'AdamW', 'RMSprop'],
|
75 |
+
label='Optimizers',
|
76 |
+
info='Select optimizers to compare. AzureSky includes a Simulated Annealing option.'
|
77 |
+
)
|
78 |
+
with gr.Accordion("AzureSky Ablation Settings", open=False, visible=False) as azure_settings:
|
79 |
+
use_sa = gr.Checkbox(
|
80 |
+
label='Enable Simulated Annealing (AzureSky)',
|
81 |
+
value=True,
|
82 |
+
info='Toggle Simulated Annealing for AzureSky optimizer.'
|
83 |
+
)
|
84 |
+
sa_temp = gr.Number(
|
85 |
+
label='Initial SA Temperature',
|
86 |
+
value=1.0,
|
87 |
+
minimum=0.1,
|
88 |
+
info='Controls exploration in Simulated Annealing (higher = more exploration).'
|
89 |
+
)
|
90 |
+
sa_cooling_rate = gr.Number(
|
91 |
+
label='SA Cooling Rate',
|
92 |
+
value=0.95,
|
93 |
+
minimum=0.1,
|
94 |
+
maximum=0.99,
|
95 |
+
info='Rate at which SA temperature decreases (closer to 1 = slower cooling).'
|
96 |
+
)
|
97 |
+
|
98 |
+
with gr.Column():
|
99 |
+
with gr.Group(visible=True) as benchmark_tab:
|
100 |
+
benchmark_func = gr.Dropdown(
|
101 |
+
['Himmelblau', 'Ackley', 'Adjiman', 'Brent'],
|
102 |
+
label='Benchmark Function',
|
103 |
+
info='Select a mathematical function to optimize.'
|
104 |
+
)
|
105 |
+
dim = gr.Number(
|
106 |
+
label='Dimensionality',
|
107 |
+
value=2,
|
108 |
+
minimum=2,
|
109 |
+
info='Number of dimensions for the benchmark function.'
|
110 |
+
)
|
111 |
+
with gr.Group(visible=False) as ml_task_tab:
|
112 |
+
dataset = gr.Dropdown(
|
113 |
+
['MNIST', 'CIFAR-10'],
|
114 |
+
label='Dataset',
|
115 |
+
info='Select a dataset for ML training.'
|
116 |
+
)
|
117 |
+
epochs = gr.Number(
|
118 |
+
label='Epochs',
|
119 |
+
value=10,
|
120 |
+
minimum=1,
|
121 |
+
info='Number of training epochs.'
|
122 |
+
)
|
123 |
+
batch_size = gr.Number(
|
124 |
+
label='Batch Size',
|
125 |
+
value=32,
|
126 |
+
minimum=1,
|
127 |
+
info='Number of samples per training batch.'
|
128 |
+
)
|
129 |
+
lr = gr.Number(
|
130 |
+
label='Learning Rate',
|
131 |
+
value=0.001,
|
132 |
+
minimum=0,
|
133 |
+
info='Learning rate for optimizers.'
|
134 |
+
)
|
135 |
+
|
136 |
+
run_button = gr.Button('Run Study', variant='primary')
|
137 |
+
|
138 |
+
with gr.TabItem("Results"):
|
139 |
+
status_message = gr.Markdown("Configure and run a study to view results.", elem_classes=["status-message"])
|
140 |
+
with gr.Row():
|
141 |
+
plot1 = gr.Plot(label='Main Plot (Benchmark or Accuracy)')
|
142 |
+
plot2 = gr.Plot(label='Loss Plot (ML Mode)')
|
143 |
+
metrics_df = gr.Dataframe(label='Metrics Table', headers=['Optimizer'] + [
|
144 |
+
'distance', 'final_loss', 'convergence_rate',
|
145 |
+
'final_train_acc', 'final_val_acc', 'generalization_gap',
|
146 |
+
'final_train_loss', 'final_val_loss', 'best_epoch'
|
147 |
+
])
|
148 |
+
metrics_json = gr.JSON(label='Detailed Metrics')
|
149 |
+
export_data = gr.State()
|
150 |
+
export_button = gr.Button('Export Results as JSON')
|
151 |
+
export_file = gr.File(label='Download Results')
|
152 |
+
|
153 |
+
def toggle_tabs(mode):
|
154 |
+
return gr.update(visible=mode == 'Benchmark Optimization'), gr.update(visible=mode == 'ML Task Training')
|
155 |
+
|
156 |
+
mode.change(toggle_tabs, inputs=mode, outputs=[benchmark_tab, ml_task_tab])
|
157 |
+
optimizers.change(toggle_azure_settings, inputs=optimizers, outputs=azure_settings)
|
158 |
+
run_button.click(
|
159 |
+
run_study,
|
160 |
+
inputs=[mode, benchmark_func, optimizers, dim, dataset, epochs, batch_size, lr, use_sa, sa_temp, sa_cooling_rate],
|
161 |
+
outputs=[plot1, plot2, metrics_df, metrics_json, export_data, status_message]
|
162 |
+
)
|
163 |
+
export_button.click(export_results, inputs=[export_data], outputs=[export_file, gr.File()])
|
164 |
+
|
165 |
+
# Configure launch based on environment
|
166 |
+
is_huggingface = os.getenv("HF_SPACE") is not None
|
167 |
+
app.launch(share=is_huggingface, server_name="0.0.0.0" if is_huggingface else None)
|