Spaces:
Runtime error
Runtime error
Commit ·
42cae27
1
Parent(s): 437c1c8
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,260 +1,86 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import subprocess
|
| 3 |
-
import threading
|
| 4 |
-
import http.server
|
| 5 |
-
import socketserver
|
| 6 |
-
from urllib.parse import urlencode, parse_qs, urlencode
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
<title>D3.js with Gradio</title>
|
| 34 |
-
<script src="https://d3js.org/d3.v6.min.js"></script>
|
| 35 |
-
</head>
|
| 36 |
-
<body>
|
| 37 |
-
<div>
|
| 38 |
-
<label for="radiusSlider">Radius:</label>
|
| 39 |
-
<input type="range" min="50" max="200" step="10" id="radiusSlider" value="100">
|
| 40 |
-
<input type="text" id="radiusText" value="100">
|
| 41 |
-
</div>
|
| 42 |
-
<div>
|
| 43 |
-
<label for="xSlider">X-coordinate:</label>
|
| 44 |
-
<input type="range" min="100" max="400" step="10" id="xSlider" value="250">
|
| 45 |
-
<input type="text" id="xText" value="250">
|
| 46 |
-
</div>
|
| 47 |
-
<div>
|
| 48 |
-
<label for="ySlider">Y-coordinate:</label>
|
| 49 |
-
<input type="range" min="100" max="400" step="10" id="ySlider" value="250">
|
| 50 |
-
<input type="text" id="yText" value="250">
|
| 51 |
-
</div>
|
| 52 |
-
<script>
|
| 53 |
-
var counter = 0;
|
| 54 |
-
var svg = d3.select("body").append("svg")
|
| 55 |
-
.attr("width", 500)
|
| 56 |
-
.attr("height", 500);
|
| 57 |
-
|
| 58 |
-
var circle = svg.append("circle")
|
| 59 |
-
.attr("cx", 250)
|
| 60 |
-
.attr("cy", 250)
|
| 61 |
-
.attr("r", 100)
|
| 62 |
-
.attr("fill", "red");
|
| 63 |
-
|
| 64 |
-
var text = svg.append("text")
|
| 65 |
-
.attr("x", 250)
|
| 66 |
-
.attr("y", 250)
|
| 67 |
-
.attr("dy", ".35em")
|
| 68 |
-
.attr("text-anchor", "middle")
|
| 69 |
-
.style("fill", "white")
|
| 70 |
-
.text(counter);
|
| 71 |
-
|
| 72 |
-
// Function to update circle attributes based on slider values
|
| 73 |
-
function updateCircleAttributes() {
|
| 74 |
-
var radius = document.getElementById("radiusSlider").value;
|
| 75 |
-
var x = document.getElementById("xSlider").value;
|
| 76 |
-
var y = document.getElementById("ySlider").value;
|
| 77 |
-
|
| 78 |
-
circle.attr("r", radius)
|
| 79 |
-
.attr("cx", x)
|
| 80 |
-
.attr("cy", y);
|
| 81 |
-
|
| 82 |
-
// Update text values
|
| 83 |
-
document.getElementById("radiusText").value = radius;
|
| 84 |
-
document.getElementById("xText").value = x;
|
| 85 |
-
document.getElementById("yText").value = y;
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
// Add event listeners to sliders
|
| 89 |
-
document.getElementById("radiusSlider").addEventListener("input", updateCircleAttributes);
|
| 90 |
-
document.getElementById("xSlider").addEventListener("input", updateCircleAttributes);
|
| 91 |
-
document.getElementById("ySlider").addEventListener("input", updateCircleAttributes);
|
| 92 |
-
function sendUpdatesToBackend() {
|
| 93 |
-
var radius = document.getElementById("radiusSlider").value;
|
| 94 |
-
var x = document.getElementById("xSlider").value;
|
| 95 |
-
var y = document.getElementById("ySlider").value;
|
| 96 |
-
|
| 97 |
-
fetch('/update', {
|
| 98 |
-
method: 'POST',
|
| 99 |
-
headers: {
|
| 100 |
-
'Content-Type': 'application/x-www-form-urlencoded',
|
| 101 |
-
},
|
| 102 |
-
body: `radius=${radius}&x=${x}&y=${y}`
|
| 103 |
-
});
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
document.getElementById("radiusSlider").addEventListener("input", function() {
|
| 107 |
-
updateCircleAttributes();
|
| 108 |
-
sendUpdatesToBackend();
|
| 109 |
-
});
|
| 110 |
-
document.getElementById("xSlider").addEventListener("input", function() {
|
| 111 |
-
updateCircleAttributes();
|
| 112 |
-
sendUpdatesToBackend();
|
| 113 |
-
});
|
| 114 |
-
document.getElementById("ySlider").addEventListener("input", function() {
|
| 115 |
-
updateCircleAttributes();
|
| 116 |
-
sendUpdatesToBackend();
|
| 117 |
-
});
|
| 118 |
-
|
| 119 |
-
circle.on("click", function() {
|
| 120 |
-
counter++;
|
| 121 |
-
text.text(counter);
|
| 122 |
-
circle.attr("fill", "darkred");
|
| 123 |
-
setTimeout(() => circle.attr("fill", "red"), 300);
|
| 124 |
-
// AJAX call to update the backend
|
| 125 |
-
fetch('/increment', {
|
| 126 |
-
method: 'POST',
|
| 127 |
-
headers: {
|
| 128 |
-
'Content-Type': 'application/x-www-form-urlencoded',
|
| 129 |
-
},
|
| 130 |
-
body: `counter=${counter}`
|
| 131 |
-
});
|
| 132 |
-
});
|
| 133 |
-
</script>
|
| 134 |
-
</body>
|
| 135 |
-
</html>
|
| 136 |
-
'''
|
| 137 |
-
with open("index.html", "w") as f:
|
| 138 |
-
f.write(d3_content)
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
| 142 |
-
def do_POST(self):
|
| 143 |
-
global counter, slider_radius, slider_x, slider_y
|
| 144 |
-
content_length = int(self.headers['Content-Length'])
|
| 145 |
-
post_data = self.rfile.read(content_length).decode('utf-8')
|
| 146 |
-
parsed_data = parse_qs(post_data)
|
| 147 |
-
if 'counter' in parsed_data:
|
| 148 |
-
counter = int(parsed_data['counter'][0])
|
| 149 |
-
if 'radius' in parsed_data:
|
| 150 |
-
slider_radius = int(parsed_data['radius'][0])
|
| 151 |
-
if 'x' in parsed_data:
|
| 152 |
-
slider_x = int(parsed_data['x'][0])
|
| 153 |
-
if 'y' in parsed_data:
|
| 154 |
-
slider_y = int(parsed_data['y'][0])
|
| 155 |
-
self.send_response(200)
|
| 156 |
-
self.end_headers()
|
| 157 |
-
|
| 158 |
-
def run_d3_server():
|
| 159 |
-
Handler = CustomHandler
|
| 160 |
-
with socketserver.TCPServer(("", 8001), Handler) as httpd:
|
| 161 |
-
print("serving D3.js at port 8001")
|
| 162 |
-
httpd.serve_forever()
|
| 163 |
-
|
| 164 |
-
def update_from_gradio(parsed_data):
|
| 165 |
-
global slider_radius, slider_x, slider_y
|
| 166 |
-
slider_radius = int(parsed_data['radius'][0])
|
| 167 |
-
slider_x = int(parsed_data['x'][0])
|
| 168 |
-
slider_y = int(parsed_data['y'][0])
|
| 169 |
-
|
| 170 |
-
# Function to update values from Gradio or JavaScript
|
| 171 |
-
def update_values(parsed_data):
|
| 172 |
-
global slider_radius, slider_x, slider_y, counter
|
| 173 |
-
if 'counter' in parsed_data:
|
| 174 |
-
counter_str = parsed_data['counter'][0]
|
| 175 |
-
# Convert the counter value to an integer, handling the possibility of a decimal point.
|
| 176 |
-
try:
|
| 177 |
-
counter = int(float(counter_str))
|
| 178 |
-
except ValueError:
|
| 179 |
-
# Handle the case where the counter value cannot be converted to an integer.
|
| 180 |
-
print(f"Invalid counter value: {counter_str}")
|
| 181 |
-
if 'radius' in parsed_data:
|
| 182 |
-
slider_radius = int(parsed_data['radius'][0])
|
| 183 |
-
if 'x' in parsed_data:
|
| 184 |
-
slider_x = int(parsed_data['x'][0])
|
| 185 |
-
if 'y' in parsed_data:
|
| 186 |
-
slider_y = int(parsed_data['y'][0])
|
| 187 |
-
|
| 188 |
-
def process_inputs_gradio(input_mode_switch, input_radius_gradio, input_x_gradio, input_y_gradio, input_counter_gradio):
|
| 189 |
-
global slider_radius, slider_x, slider_y, counter
|
| 190 |
-
if input_mode_switch == "Switch Mode":
|
| 191 |
-
# This block is executed whenever the mode switch radio option is selected.
|
| 192 |
-
_, slider_radius, slider_x, slider_y = fetch_updated_values()
|
| 193 |
else:
|
| 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 |
-
update_from_gradio(parse_qs(update_data))
|
| 233 |
-
elif input_action == "Update from JavaScript":
|
| 234 |
-
_, slider_radius, slider_x, slider_y = fetch_updated_values()
|
| 235 |
-
return f'Radius: {slider_radius}, X: {slider_x}, Y: {slider_y}', slider_radius, slider_x, slider_y, f'Counter value is {counter}'
|
| 236 |
-
|
| 237 |
-
if __name__ == "__main__":
|
| 238 |
-
write_d3_to_file()
|
| 239 |
-
d3_thread = threading.Thread(target=run_d3_server)
|
| 240 |
-
d3_thread.daemon = True
|
| 241 |
-
d3_thread.start()
|
| 242 |
-
iface = gr.Interface(
|
| 243 |
-
fn=process_inputs_gradio,
|
| 244 |
inputs=[
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
],
|
| 251 |
outputs=[
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
parameter3,
|
| 256 |
-
output_counter
|
| 257 |
-
],
|
| 258 |
-
description='<iframe src="http://localhost:8001" width="500" height="500"></iframe>'
|
| 259 |
)
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
import numpy as np
|
| 4 |
+
import random
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Predefined arrays
|
| 8 |
+
ALL_TITLES = ['Minimalism', 'Bauhaus', 'Organic Design', 'Brutalism', 'Mid-century Modern', 'Retro/Vintage', 'Futurism', 'Tesselated', 'Streamlined', 'Timeless', 'Industrial Chic', 'Art Deco', 'Elegant', 'Biomorphic Design', 'High Contrast', 'Deconstructivism', 'Zen Design', 'Pop Art', 'Cyberpunk', 'Sustainable Design', 'Angular', 'Textured', 'Symmetric', 'Utilitarian', 'Dynamic Energy Flow']
|
| 9 |
+
ALL_VALUES = [1.5, 1.2, 0.8, 1.0, 0.5, -1.0, 0.3, -0.5, 1.1, -0.7, 0.9, 1.3, 0.2, -1.3, -0.9, -1.1, 0.7, 0.6, -0.8, 0.4, -0.2, 0.1, -1.2, 0.0, -0.4]
|
| 10 |
+
|
| 11 |
+
def generate_axes(parameters):
|
| 12 |
+
return [{'angle': np.degrees((2 * np.pi / len(parameters)) * i - np.pi / 2), 'label': parameters[i]} for i in range(len(parameters))]
|
| 13 |
+
|
| 14 |
+
def handle_randomize(num_parameters, values_list):
|
| 15 |
+
return random.sample(values_list, num_parameters)
|
| 16 |
+
|
| 17 |
+
def generate_figure(r_values, theta_values, chart_title="Radar Chart"):
|
| 18 |
+
fig = go.Figure(data=go.Scatterpolar(r=r_values, theta=theta_values, mode='lines+markers', marker=dict(size=10), fill='toself'))
|
| 19 |
+
fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[-1.5, 1.5])), showlegend=False, title=chart_title)
|
| 20 |
+
return fig
|
| 21 |
+
|
| 22 |
+
def display_radar_chart(input_parameters=None, randomize_values=False, randomize_titles=False, chart_title="Radar Chart", param_count=5, param1=1.5, param2=1.2, param3=0.8, param4=1.0, param5=0.5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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
+
parameters = ALL_TITLES[:param_count]
|
| 29 |
+
|
| 30 |
+
# Create a temp array for text input parameters
|
| 31 |
+
text_input_parameters = []
|
| 32 |
+
if input_parameters:
|
| 33 |
+
text_input_parameters = re.split(r'\s*,\s*', input_parameters.strip())
|
| 34 |
+
|
| 35 |
+
# Randomize titles if the checkbox is selected
|
| 36 |
+
if randomize_titles:
|
| 37 |
+
parameters = handle_randomize(param_count, ALL_TITLES)
|
| 38 |
+
|
| 39 |
+
# Merge text input parameters into final array, only replacing what's necessary
|
| 40 |
+
for i in range(min(len(parameters), len(text_input_parameters))):
|
| 41 |
+
parameters[i] = text_input_parameters[i]
|
| 42 |
+
|
| 43 |
+
# Final output stage for cleanup
|
| 44 |
+
if len(parameters) != param_count:
|
| 45 |
+
parameters = parameters[:param_count]
|
| 46 |
+
|
| 47 |
+
# Randomize or set to default values to fill in any gaps.
|
| 48 |
+
if randomize_values:
|
| 49 |
+
r_values = handle_randomize(len(parameters), ALL_VALUES)
|
| 50 |
+
else:
|
| 51 |
+
r_values = [param1, param2, param3, param4, param5][:len(parameters)]
|
| 52 |
+
|
| 53 |
+
theta_values = [axis['label'] for axis in generate_axes(parameters)]
|
| 54 |
+
|
| 55 |
+
# Generate and return the radar chart figure
|
| 56 |
+
chart_figure = generate_figure(r_values, theta_values, chart_title)
|
| 57 |
+
|
| 58 |
+
# Generate text output for sliders
|
| 59 |
+
slider_labels = ", ".join([f"({parameters[i]}: {r_values[i]:.2f})" for i in range(len(r_values))])
|
| 60 |
+
|
| 61 |
+
return chart_figure, f"{slider_labels}"
|
| 62 |
+
|
| 63 |
+
# Gr.Interface definition
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=display_radar_chart,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
inputs=[
|
| 67 |
+
gr.Textbox(default=None, placeholder="Enter parameters, separated by commas", label="Parameters (Comma-separated)"),
|
| 68 |
+
gr.components.Checkbox(label="Randomize Values", default=False),
|
| 69 |
+
gr.components.Checkbox(label="Randomize Titles", default=False),
|
| 70 |
+
gr.Textbox(default="Radar Chart", label="Chart Title", value="Stable Diffusion Nodes"),
|
| 71 |
+
gr.Slider(3, 6, value=3, step=1, label="Parameter Count", info="Choose between 3 and 6"),
|
| 72 |
+
gr.inputs.Slider(minimum=-1.5, maximum=1.5, step=0.1, default=1.5, label="Parameter 1"),
|
| 73 |
+
gr.inputs.Slider(minimum=-1.5, maximum=1.5, step=0.1, default=1.2, label="Parameter 2"),
|
| 74 |
+
gr.inputs.Slider(minimum=-1.5, maximum=1.5, step=0.1, default=0.8, label="Parameter 3"),
|
| 75 |
+
gr.inputs.Slider(minimum=-1.5, maximum=1.5, step=0.1, default=1.0, label="Parameter 4"),
|
| 76 |
+
gr.inputs.Slider(minimum=-1.5, maximum=1.5, step=0.1, default=0.5, label="Parameter 5")
|
| 77 |
],
|
| 78 |
outputs=[
|
| 79 |
+
gr.Plot(label="Radar Chart Display"),
|
| 80 |
+
gr.Textbox(label="Prompt: Slider Labels") # "Prompt:" moved here
|
| 81 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
# Launching the interface
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
iface.launch()
|