Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Predefined hyperparameter sets
|
| 6 |
+
PARAM_SETS = {
|
| 7 |
+
"Set A": {"param1": 0.1, "param2": 0.01, "param3": 100, "param4": 50},
|
| 8 |
+
"Set B": {"param1": 0.2, "param2": 0.02, "param3": 200, "param4": 100}
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def generate_plot(param1, param2, param3, param4):
|
| 12 |
+
"""Generate visualization based on hyperparameters"""
|
| 13 |
+
plt.figure(figsize=(10, 6))
|
| 14 |
+
x = np.linspace(0, 10, int(param3))
|
| 15 |
+
y = np.sin(x * param1) * np.cos(x * param2) * param4
|
| 16 |
+
plt.plot(x, y)
|
| 17 |
+
plt.title(f'Parameter Visualization (p1={param1}, p2={param2}, p3={param3}, p4={param4})')
|
| 18 |
+
plt.grid(True)
|
| 19 |
+
return plt
|
| 20 |
+
|
| 21 |
+
def process_inputs(param_set, custom_param1, custom_param2, custom_param3, custom_param4,
|
| 22 |
+
input1, input2):
|
| 23 |
+
"""Process inputs and return results"""
|
| 24 |
+
# Determine which parameter set to use
|
| 25 |
+
if param_set in PARAM_SETS:
|
| 26 |
+
params = PARAM_SETS[param_set]
|
| 27 |
+
p1, p2, p3, p4 = params.values()
|
| 28 |
+
else:
|
| 29 |
+
p1, p2, p3, p4 = custom_param1, custom_param2, custom_param3, custom_param4
|
| 30 |
+
|
| 31 |
+
# Generate plot
|
| 32 |
+
plot = generate_plot(p1, p2, p3, p4)
|
| 33 |
+
|
| 34 |
+
# Calculate result (example calculation)
|
| 35 |
+
result = (input1 * p1 + input2 * p2) * (p3 + p4)
|
| 36 |
+
|
| 37 |
+
return plot, result
|
| 38 |
+
|
| 39 |
+
# Create interface
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("# Hyperparameter Calculation and Visualization System")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column():
|
| 45 |
+
# Hyperparameter selection section
|
| 46 |
+
param_set = gr.Dropdown(
|
| 47 |
+
choices=["Custom"] + list(PARAM_SETS.keys()),
|
| 48 |
+
value="Custom",
|
| 49 |
+
label="Select Hyperparameter Set"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Custom parameter inputs
|
| 53 |
+
custom_param1 = gr.Number(value=0.1, label="Parameter 1 (Learning Rate)")
|
| 54 |
+
custom_param2 = gr.Number(value=0.01, label="Parameter 2 (Weight Decay)")
|
| 55 |
+
custom_param3 = gr.Number(value=100, label="Parameter 3 (Iterations)")
|
| 56 |
+
custom_param4 = gr.Number(value=50, label="Parameter 4 (Batch Size)")
|
| 57 |
+
|
| 58 |
+
# Input values
|
| 59 |
+
input1 = gr.Number(value=1.0, label="Input Value 1")
|
| 60 |
+
input2 = gr.Number(value=1.0, label="Input Value 2")
|
| 61 |
+
|
| 62 |
+
submit_btn = gr.Button("Calculate")
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
# Output section
|
| 66 |
+
plot_output = gr.Plot(label="Parameter Visualization")
|
| 67 |
+
result_output = gr.Number(label="Calculation Result")
|
| 68 |
+
|
| 69 |
+
# Auto-fill parameters when selecting predefined sets
|
| 70 |
+
def update_params(param_set):
|
| 71 |
+
if param_set in PARAM_SETS:
|
| 72 |
+
params = PARAM_SETS[param_set]
|
| 73 |
+
return [params["param1"], params["param2"], params["param3"], params["param4"]]
|
| 74 |
+
return [gr.skip(), gr.skip(), gr.skip(), gr.skip()]
|
| 75 |
+
|
| 76 |
+
param_set.change(
|
| 77 |
+
update_params,
|
| 78 |
+
inputs=[param_set],
|
| 79 |
+
outputs=[custom_param1, custom_param2, custom_param3, custom_param4]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Submit button event
|
| 83 |
+
submit_btn.click(
|
| 84 |
+
process_inputs,
|
| 85 |
+
inputs=[param_set, custom_param1, custom_param2, custom_param3, custom_param4,
|
| 86 |
+
input1, input2],
|
| 87 |
+
outputs=[plot_output, result_output]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Launch application
|
| 91 |
+
demo.launch()
|