rayochoajr commited on
Commit
9d560e2
·
1 Parent(s): 7645ec5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +323 -0
app.py ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ from random import choice
4
+ import requests
5
+ import base64
6
+ import io
7
+
8
+ from PIL import Image
9
+ import plotly.graph_objects as go
10
+ import numpy as np
11
+ from PIL import Image, ImageDraw
12
+ import random
13
+ import re
14
+
15
+ url = "http://73.255.78.150:7909/sdapi/v1/txt2img"
16
+ session_seed = None
17
+ last_session_seed = None
18
+ # Predefined arrays for design parameters and their corresponding values
19
+ ALL_TITLES = [
20
+ 'Minimalism', 'Bauhaus', 'Organic Design', 'Brutalism', 'Mid-century Modern',
21
+ 'Retro-Vintage', 'Futurism', 'Tesselated', 'Streamlined', 'Timeless',
22
+ 'Industrial Chic', 'Art Deco', 'Elegant', 'Biomorphic Design', 'High Contrast',
23
+ 'Deconstructivism', 'Zen Design', 'Pop Art', 'Cyberpunk', 'Sustainable Design',
24
+ 'Angular', 'Textured', 'Symmetric', 'Utilitarian', 'Dynamic Energy Flow'
25
+ ]
26
+ ALL_VALUES = [
27
+ 1.5, 1.2, 0.8, 1.0, 0.5, -1.0, 0.3, -0.5, 1.1, -0.7,
28
+ 0.9, 1.3, 0.2, -1.3, -0.9, -1.1, 0.7, 0.6, -0.8, 0.4,
29
+ -0.2, 0.1, -1.2, 0.0, -0.4
30
+ ]
31
+
32
+ # Base payload for API call
33
+ base_payload = {
34
+ "steps": 20,
35
+ "seed": 1,
36
+ "width": 768,
37
+ "height": 512,
38
+ }
39
+
40
+
41
+ # New function to generate radar chart using Plotly
42
+ def generate_figure(r_values, theta_values, chart_title):
43
+ fig = go.Figure(data=go.Scatterpolar(
44
+ r=r_values, theta=theta_values, mode='lines+markers',
45
+ marker=dict(size=10), fill='toself')
46
+ )
47
+ fig.update_layout(
48
+ polar=dict(radialaxis=dict(visible=True, range=[0, 1.5])),
49
+ showlegend=False, title=chart_title
50
+ )
51
+ return fig
52
+
53
+ # New function for generating the radar chart
54
+ def display_radar_chart(input_parameters, randomize_values, randomize_titles, randomize_param_count, chart_title, param_count, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10):
55
+ if randomize_param_count:
56
+ param_count = random.randint(3, 10)
57
+
58
+ # 🤖 Create a temporary array from comma-separated input_parameters
59
+ if input_parameters:
60
+ parameters = re.split(r'\s*,\s*', input_parameters.strip())
61
+ if len(parameters) > param_count:
62
+ parameters = parameters[:param_count]
63
+ elif randomize_titles:
64
+ parameters = random.sample(ALL_TITLES, param_count)
65
+ else:
66
+ parameters = []
67
+
68
+ # 🤖 Fill in missing parameters based on param_count
69
+ if len(parameters) < param_count:
70
+ remaining_count = param_count - len(parameters)
71
+
72
+ if randomize_titles:
73
+ additional_params = random.sample(set(ALL_TITLES) - set(parameters), remaining_count)
74
+ else:
75
+ additional_params = ALL_TITLES[len(parameters):len(parameters) + remaining_count]
76
+
77
+ parameters.extend(additional_params)
78
+
79
+ elif len(parameters) > param_count:
80
+ parameters = parameters[:param_count]
81
+
82
+ # Existing code for randomizing values remains the same
83
+ if randomize_values:
84
+ r_values = random.sample(ALL_VALUES, len(parameters))
85
+ else:
86
+ r_values = [param1, param2, param3, param4, param5, param6, param7, param8, param9, param10][:len(parameters)]
87
+
88
+ chart_figure = generate_figure(r_values, parameters, chart_title)
89
+ slider_labels = ", ".join([f"{parameters[i]}: {r_values[i]:.2f}" for i in range(len(r_values))])
90
+
91
+ return chart_figure, slider_labels
92
+
93
+
94
+ def generate_image(prompt, steps, reset_seed, increment_seed):
95
+ global session_seed
96
+ global last_session_seed
97
+ if reset_seed:
98
+ if session_seed != -1:
99
+ last_session_seed = session_seed
100
+ session_seed = -1
101
+ elif not reset_seed and last_session_seed is not None:
102
+ session_seed = last_session_seed
103
+ elif session_seed is None:
104
+ session_seed = random.randint(0, 10000)
105
+ if increment_seed and session_seed != -1:
106
+ session_seed += 1
107
+ last_session_seed = session_seed
108
+ payload = {
109
+ "prompt": prompt,
110
+ "steps": steps,
111
+ "seed": session_seed,
112
+ "height": 768,
113
+ "width": 1024,
114
+ }
115
+ response = requests.post(url, json=payload)
116
+ response_data = response.json()
117
+ try:
118
+ image = Image.open(io.BytesIO(base64.b64decode(response_data['images'][0])))
119
+ return image
120
+ except KeyError:
121
+ error_message = response_data.get('error', 'Unknown error')
122
+ error_image = Image.new('RGB', (512, 512), color=(73, 109, 137))
123
+ d = ImageDraw.Draw(error_image)
124
+ d.text((10, 10), f"Error: {error_message}", fill=(255, 255, 0))
125
+ return error_image
126
+
127
+ image_list = [f"Images/Image {i}.png" for i in range(1, 19)]
128
+
129
+ def update_moodboard(btn_value):
130
+ return {moodboard_image: choice(image_list)}
131
+
132
+
133
+ with gr.Blocks() as app:
134
+ gr.Markdown("# Magazine Layouts")
135
+
136
+ with gr.Tab("Dynamic Dashboard"):
137
+ with gr.Row():
138
+ gr.Textbox(lines=1, value="Dashboard: Dynamic View")
139
+ gr.Dropdown(options=["Overview", "Details", "Summary"], label="Dashboard Mode")
140
+
141
+ with gr.Row():
142
+ gr.Textbox(lines=1, value="Status: Active")
143
+ gr.Slider(minimum=0, maximum=100, label="Dashboard Progress")
144
+
145
+ # Image Generation Section
146
+ with gr.Column():
147
+ gr.Markdown("### Image Generation")
148
+ prompt_input = gr.Textbox(lines=2, placeholder="Imagine something...", label="Prompt")
149
+ steps_input = gr.Slider(minimum=15, maximum=50, step=15, default=5, label="Steps")
150
+ reset_seed_input = gr.Checkbox(label="Randomize Composition")
151
+ increment_seed_input = gr.Checkbox(label="New Composition")
152
+ img_output = gr.Image(label="Generated Image")
153
+
154
+ # Radar Chart Section
155
+ with gr.Column():
156
+ gr.Markdown("### Radar Chart")
157
+ input_parameters = gr.Textbox(placeholder="Enter parameters, separated by commas", label="Parameters")
158
+ randomize_values = gr.Checkbox(label="Randomize Values")
159
+ chart_title = gr.Textbox(value="Radar Chart", label="Chart Title")
160
+ param_count = gr.Slider(minimum=3, maximum=10, step=1, label="Parameter Count")
161
+ radar_output = gr.Plot(label="Radar Chart")
162
+
163
+ with gr.Row():
164
+ gr.Image(choice(image_list))
165
+ gr.Textbox(lines=4, value="Dashboard Insights: Real-time updates and metrics")
166
+ gr.Button("Refresh Dashboard", label="Refresh Dashboard")
167
+
168
+ with gr.Tab("API Call Interface"):
169
+ with gr.Blocks() as main_blocks:
170
+ with gr.Row():
171
+ with gr.Column():
172
+ img_output = gr.Image(label="Generated Image")
173
+ with gr.Row():
174
+ with gr.Column(scale=1):
175
+ prompt_input = gr.Textbox(lines=2, placeholder="Imagine something...", label="Prompt")
176
+ steps_input = gr.Slider(minimum=15, maximum=50, step=15, default=5, label="Steps")
177
+ with gr.Column(scale=1):
178
+ reset_seed_input = gr.Checkbox(label="Randomize Composition")
179
+ increment_seed_input = gr.Checkbox(label="New Composition")
180
+ iface = gr.Interface(
181
+ fn=generate_image,
182
+ inputs=[prompt_input, steps_input, reset_seed_input, increment_seed_input],
183
+ outputs=[img_output],
184
+ live=False,
185
+ layout=main_blocks
186
+ )
187
+
188
+ # Design Proposal Board with Multi-layered Layouts
189
+ with gr.Tab("Design Proposal Board"):
190
+ with gr.Row():
191
+ gr.Textbox(lines=1, value="Project: Redesign UI")
192
+ gr.Dropdown(options=["Planning", "Execution", "Review"], label="Project Stage")
193
+ with gr.Row():
194
+ gr.Textbox(lines=1, value="Timeline: 3 months")
195
+ gr.Slider(minimum=0, maximum=100, label="Progress")
196
+ gr.Image(choice(image_list))
197
+ gr.Button("View Milestones", label="View Milestones")
198
+ gr.Textbox(lines=4, value="Goals: Improve user engagement by 20%")
199
+ with gr.Tab("The Minimalist Layout"):
200
+ with gr.Column():
201
+ gr.Markdown("## Minimalist")
202
+ gr.Image("Images/Image 1.png")
203
+ gr.Textbox(lines=4, value="It was a stormy night, and the vintage car roared down the highway.")
204
+
205
+ with gr.Tab("The Grid Layout"):
206
+ with gr.Row():
207
+ with gr.Column(scale=1):
208
+ gr.Image("Images/Image 2.png")
209
+ gr.Textbox(lines=4, value="The old leather seats had stories to tell.")
210
+ with gr.Column(scale=1):
211
+ gr.Image("Images/Image 3.png")
212
+ gr.Textbox(lines=4, value="With every curve and turn, the car seemed to whisper.")
213
+
214
+ with gr.Tab("The Asymmetrical Layout"):
215
+ with gr.Row():
216
+ with gr.Column(scale=2):
217
+ gr.Markdown("## Artistic Layout")
218
+ gr.Image("Images/Image 4.png")
219
+ with gr.Column(scale=1):
220
+ gr.Image("Images/Image 5.png")
221
+ gr.Textbox(lines=3, value="As the dawn broke, the car finally stopped.")
222
+
223
+ with gr.Tab("The F-Layout"):
224
+ with gr.Row():
225
+ with gr.Column(scale=3):
226
+ gr.Image("Images/Image 6.png")
227
+ with gr.Column(scale=1):
228
+ gr.Textbox(lines=4, value="It was a stormy night, and the vintage car roared down the highway.")
229
+
230
+ with gr.Tab("The Radial Layout"):
231
+ with gr.Row():
232
+ with gr.Column(scale=1):
233
+ gr.Image("Images/Image 9.png")
234
+ with gr.Column(scale=1):
235
+ gr.Image("Images/Image 10.png")
236
+ with gr.Row():
237
+ with gr.Column(scale=1):
238
+ gr.Image("Images/Image 11.png")
239
+ with gr.Column(scale=1):
240
+ gr.Textbox(lines=4, value="As the dawn broke, the car finally stopped.")
241
+ with gr.Column(scale=1):
242
+ gr.Image("Images/Image 12.png")
243
+
244
+ with gr.Tab("Trading Card Layout 1"):
245
+ with gr.Column():
246
+ gr.Image(choice(image_list))
247
+ gr.Textbox(lines=1, value="Car Model: Mustang")
248
+ gr.Textbox(lines=1, value="Year: 1965")
249
+ gr.Textbox(lines=2, value="Stats: Speed 200mph, Mileage 15mpg")
250
+ gr.Textbox(lines=3, value="The Mustang is a classic American muscle car.")
251
+
252
+ # Museum Art Gallery: Spotlight Layout
253
+ with gr.Tab("Spotlight Layout"):
254
+ gr.Image(choice(image_list))
255
+ with gr.Row():
256
+ gr.Textbox(lines=1, value="Art: Moonlit Sonata")
257
+ gr.Textbox(lines=1, value="Artist: L. Vinci")
258
+ gr.Textbox(lines=1, value="Year: 1911")
259
+ gr.Textbox(lines=3, value="A single spotlight shines on this masterpiece, illuminating its intricate details and vivid colors.")
260
+
261
+ # Museum Art Gallery: Interactive Kiosk Layout
262
+ with gr.Tab("Interactive Kiosk Layout"):
263
+ with gr.Row():
264
+ with gr.Column(scale=1):
265
+ gr.Image(choice(image_list))
266
+ with gr.Column(scale=1):
267
+ gr.Textbox(lines=1, value="Art: Pixelated Reality")
268
+ gr.Textbox(lines=1, value="Artist: G. O'Keeffe")
269
+ gr.Textbox(lines=1, value="Year: 2001")
270
+ gr.Textbox(lines=4, value="Step up to the interactive kiosk to dive deeper into the story and significance of the artwork.")
271
+
272
+ # Moodboard for a Designer
273
+ with gr.Tab("Moodboard for a Designer"):
274
+ with gr.Row():
275
+ gr.Image(choice(image_list))
276
+ gr.Textbox(lines=1, value="Color: #FF5733")
277
+ gr.Textbox(lines=1, value="Font: Arial")
278
+ with gr.Row():
279
+ gr.Textbox(lines=1, value="Quote: Design is intelligence made visible.")
280
+ gr.Image(choice(image_list))
281
+ gr.Textbox(lines=1, value="Project: Logo Design")
282
+
283
+ # Comic Book with Dynamic Content Loading
284
+ with gr.Tab("Comic Book"):
285
+ with gr.Row():
286
+ gr.Textbox(lines=1, value="Title: The Adventures of Grado")
287
+ gr.Image(choice(image_list))
288
+ with gr.Row():
289
+ gr.Textbox(lines=1, value="BOOM!")
290
+ gr.Image(choice(image_list))
291
+ gr.Textbox(lines=2, value="Character: Oh no!")
292
+ gr.Button("Next Page", label="Next Page")
293
+ gr.Textbox(lines=1, value="Caption: To be continued...")
294
+
295
+ with gr.Tab("Radar Chart"):
296
+ with gr.Row():
297
+ input_parameters = gr.Textbox(placeholder="Enter parameters, separated by commas", label="Parameters")
298
+ randomize_values = gr.Checkbox(label="Randomize Values")
299
+ randomize_titles = gr.Checkbox(label="Randomize Titles")
300
+ randomize_param_count = gr.Checkbox(label="Randomize Parameter Count")
301
+ chart_title = gr.Textbox(value="Radar Chart", label="Chart Title")
302
+ param_count = gr.Slider(minimum=3, maximum=10, step=1, label="Parameter Count")
303
+ param1 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 1")
304
+ param2 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 2")
305
+ param3 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 3")
306
+ param4 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 4")
307
+ param5 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 5")
308
+ param6 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 6")
309
+ param7 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 7")
310
+ param8 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 8")
311
+ param9 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 9")
312
+ param10 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 10")
313
+
314
+ radar_output = gr.Plot(label="Radar Chart")
315
+ slider_labels_output = gr.Textbox(label="Prompt: Slider Labels")
316
+
317
+ radar_interface = gr.Interface(
318
+ fn=display_radar_chart,
319
+ inputs=[input_parameters, randomize_values, randomize_titles, randomize_param_count, chart_title, param_count, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10], # Include param6 to param10
320
+ outputs=[radar_output, slider_labels_output]
321
+ )
322
+
323
+ app.launch()