DazDin commited on
Commit
5a044c1
·
verified ·
1 Parent(s): fdbb720

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +225 -51
app.py CHANGED
@@ -1,17 +1,6 @@
1
  import gradio as gr
2
  from random import randint
3
  from all_models import models
4
- from datetime import datetime
5
-
6
- now2 = 0
7
-
8
-
9
- def get_current_time():
10
- now = datetime.now()
11
- now2 = now
12
- current_time = now2.strftime("%Y-%m-%d %H:%M:%S")
13
- ki = f'{kii} {current_time}'
14
- return ki
15
 
16
  def load_fn(models):
17
  global models_load
@@ -34,57 +23,242 @@ def extend_choices(choices):
34
 
35
  def update_imgbox(choices):
36
  choices_plus = extend_choices(choices)
37
- return [gr.Image(None, label=m, visible=(m != 'NA')) for m in choices_plus]
38
 
39
  def gen_fn(model_str, prompt):
40
  if model_str == 'NA':
41
  return None
42
- noise = str(randint(0, 9999))
43
  return models_load[model_str](f'{prompt} {noise}')
44
 
45
  def make_me():
46
- # with gr.Tab('The Dream'):
47
- with gr.Row():
48
- #txt_input = gr.Textbox(lines=3, width=600, max_height=100)
49
- txt_input = gr.Textbox(label='Your prompt:', lines=3, width=800, max_height=100)
50
-
51
- gen_button = gr.Button('Generate images', width=30, height=30)
52
- stop_button = gr.Button('Stop', variant='secondary', interactive=False, width=30, height=30)
53
- gen_button.click(lambda s: gr.update(interactive=True), None, stop_button)
54
- gr.HTML("""
55
- <div style="text-align: center; max-width: 100%; margin: 0 auto;">
56
- <body>
57
- </body>
58
- </div>
59
- """)
60
- with gr.Row():
61
- output = [gr.Image(label=m, min_width=250, height=250) for m in default_models]
62
- current_models = [gr.Textbox(m, visible=False) for m in default_models]
63
- for m, o in zip(current_models, output):
64
- gen_event = gen_button.click(gen_fn, [m, txt_input], o)
65
- stop_button.click(lambda s: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
66
- with gr.Accordion('Model selection'):
67
- model_choice = gr.CheckboxGroup(models, label=f' {num_models} different models selected', value=default_models, multiselect=True, max_choices=num_models, interactive=True, filterable=False)
68
- model_choice.change(update_imgbox, model_choice, output)
69
- model_choice.change(extend_choices, model_choice, current_models)
70
- with gr.Row():
71
- gr.HTML()
72
-
73
-
74
-
75
- js_code = """
76
-
77
 
78
- console.log('ghgh');
 
 
 
 
 
 
79
 
80
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- with gr.Blocks(css="div.float.svelte-1mwvhlq { position: absolute; top: var(--block-label-margin); left: var(--block-label-margin); background: none; border: none;}") as demo:
84
- gr.Markdown("<script>" + js_code + "</script>")
85
- make_me()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- demo.queue(concurrency_count=100)
90
- demo.launch()
 
1
  import gradio as gr
2
  from random import randint
3
  from all_models import models
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def load_fn(models):
6
  global models_load
 
23
 
24
  def update_imgbox(choices):
25
  choices_plus = extend_choices(choices)
26
+ return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
27
 
28
  def gen_fn(model_str, prompt):
29
  if model_str == 'NA':
30
  return None
31
+ noise = str(randint(0, 9999999))
32
  return models_load[model_str](f'{prompt} {noise}')
33
 
34
  def make_me():
35
+ with gr.Row():
36
+ with gr.Column(scale=1):
37
+ txt_input = gr.Textbox(label='Your prompt:', lines=3, container=False, elem_id="custom_textbox")
38
+ with gr.Row():
39
+ gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
40
+ stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
41
+
42
+ def on_generate_click():
43
+ return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
44
+
45
+ def on_stop_click():
46
+ return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
47
+
48
+ gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
49
+ stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
50
+
51
+ with gr.Row():
52
+ output = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models]
53
+ current_models = [gr.Textbox(m, visible=False) for m in default_models]
54
+ for m, o in zip(current_models, output):
55
+ gen_event = gen_button.click(gen_fn, [m, txt_input], o)
56
+ stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
 
 
 
 
 
 
 
 
 
57
 
58
+ with gr.Accordion('Model selection', elem_id="custom_accordion"):
59
+ model_choice = gr.CheckboxGroup(models, label=f'{num_models} different models selected', value=default_models, interactive=True, elem_id="custom_checkbox_group")
60
+ model_choice.change(update_imgbox, model_choice, output)
61
+ model_choice.change(extend_choices, model_choice, current_models)
62
+
63
+ with gr.Row():
64
+ gr.HTML("")
65
 
66
+ # Inject JavaScript to handle accordion toggle
67
+ js_code = """
68
+ <script>
69
+ document.addEventListener('DOMContentLoaded', function() {
70
+ const accordionHeaders = document.querySelectorAll('.gr-accordion-header');
71
+ accordionHeaders.forEach(header => {
72
+ header.addEventListener('click', function() {
73
+ const content = this.nextElementSibling;
74
+ if (content.classList.contains('open')) {
75
+ content.classList.remove('open');
76
+ } else {
77
+ content.classList.add('open');
78
+ }
79
+ });
80
+ });
81
+ });
82
+ </script>
83
+ """
84
+ gr.HTML(js_code)
85
 
86
+ custom_css = """
87
+ /* General Styles */
88
+ body {
89
+ background-color: #1a242e;
90
+ color: #c5c6c7;
91
+ margin: 0;
92
+ padding: 0;
93
+ font-family: Arial, sans-serif;
94
+ display: flex;
95
+ justify-content: center;
96
+ align-items: center; /* Align items from the center */
97
+ height: 100vh;
98
+ overflow-y: auto;
99
+ }
100
 
101
+ /* Container Styles */
102
+ .gradio-container {
103
+ background-color: #1a242e;
104
+ color: #c5c6c7;
105
+ padding: 20px;
106
+ border-radius: 8px;
107
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
108
+ width: 100%;
109
+ max-width: 1200px;
110
+ margin: 20px;
111
+ display: flex;
112
+ flex-direction: column;
113
+ align-items: center; /* Center items horizontally */
114
+ min-height: 100vh; /* Adjust the height of the container */
115
+ }
116
+
117
+ /* Header Styles */
118
+ .app_title {
119
+ background-color: #1a242e;
120
+ color: #c5c6c7;
121
+ padding: 10px 20px;
122
+ border-bottom: 1px solid #3b4252;
123
+ text-align: center;
124
+ font-size: 24px;
125
+ font-weight: bold;
126
+ width: 100%;
127
+ box-sizing: border-box;
128
+ margin-bottom: 20px; /* Add margin to separate the header from content */
129
+ }
130
+
131
+ /* Textbox Styles */
132
+ .custom_textbox {
133
+ background-color: #2d343f;
134
+ border: 1px solid #3b4252;
135
+ color: #c5c6c7;
136
+ padding: 10px;
137
+ border-radius: 4px;
138
+ margin-bottom: 10px;
139
+ width: 100%;
140
+ box-sizing: border-box;
141
+ }
142
 
143
+ /* Button Styles */
144
+ .custom_gen_button {
145
+ background-color: #4a90e2;
146
+ border: none;
147
+ color: white;
148
+ padding: 15px 32px;
149
+ text-align: center;
150
+ text-decoration: none;
151
+ display: inline-block;
152
+ font-size: 16px;
153
+ margin: 4px 2px;
154
+ cursor: pointer;
155
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
156
+ transition: transform 0.2s, box-shadow 0.2s;
157
+ border-radius: 4px;
158
+ }
159
+ .custom_gen_button:hover {
160
+ transform: translateY(-2px);
161
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
162
+ }
163
+ .custom_stop_button {
164
+ background-color: #6200ea;
165
+ border: none;
166
+ color: white;
167
+ padding: 15px 32px;
168
+ text-align: center;
169
+ text-decoration: none;
170
+ display: inline-block;
171
+ font-size: 16px;
172
+ margin: 4px 2px;
173
+ cursor: pointer;
174
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
175
+ transition: transform 0.2s, box-shadow 0.2s;
176
+ border-radius: 4px;
177
+ }
178
+ .custom_stop_button:hover {
179
+ transform: translateY(-2px);
180
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
181
+ }
182
 
183
+ /* Image Styles */
184
+ .custom_image {
185
+ border: 1px solid #3b4252;
186
+ background-color: #2d343f;
187
+ border-radius: 4px;
188
+ margin: 10px;
189
+ max-width: 100%;
190
+ box-sizing: border-box;
191
+ }
192
+
193
+ /* Accordion Styles */
194
+ .custom_accordion {
195
+ background-color: #1a242e;
196
+ color: #c5c6c7;
197
+ border: 1px solid #3b4252;
198
+ border-radius: 4px;
199
+ margin-top: 20px;
200
+ width: 100%;
201
+ box-sizing: border-box;
202
+ transition: margin 0.3s ease; /* Smooth transition for margin */
203
+ }
204
+
205
+ .custom_accordion .gr-accordion-header {
206
+ background-color: #3b4252;
207
+ color: #c5c6c7;
208
+ padding: 10px 20px;
209
+ border-bottom: 1px solid #5b6270;
210
+ cursor: pointer;
211
+ font-size: 18px;
212
+ font-weight: bold;
213
+ height: 40px; /* Fixed height for the header */
214
+ display: flex;
215
+ align-items: center;
216
+ }
217
+
218
+ .custom_accordion .gr-accordion-header:hover {
219
+ background-color: #4b5260;
220
+ }
221
+
222
+ .custom_accordion .gr-accordion-content {
223
+ padding: 10px 20px;
224
+ background-color: #2d343f;
225
+ border-top: 1px solid #5b6270;
226
+ max-height: 0; /* Start with no height */
227
+ overflow: hidden;
228
+ transition: max-height 0.3s ease; /* Smooth transition for height */
229
+ }
230
+
231
+ .custom_accordion .gr-accordion-content.open {
232
+ max-height: 500px; /* Set a reasonable max height */
233
+ }
234
+
235
+ /* Checkbox Group Styles */
236
+ .custom_checkbox_group {
237
+ background-color: #2d343f;
238
+ border: 1px solid #3b4252;
239
+ color: #c5c6c7;
240
+ border-radius: 4px;
241
+ padding: 10px;
242
+ width: 100%;
243
+ box-sizing: border-box;
244
+ }
245
+
246
+ /* Responsive Design */
247
+ @media (max-width: 768px) {
248
+ .gradio-container {
249
+ width: 100%;
250
+ margin: 0;
251
+ padding: 10px;
252
+ }
253
+ .custom_textbox,.custom_image,.custom_checkbox_group {
254
+ width: 100%;
255
+ box-sizing: border-box;
256
+ }
257
+ }
258
+ """
259
+
260
+ with gr.Blocks(css=custom_css) as demo:
261
+ make_me()
262
 
263
+ demo.queue(concurrency_count=200)
264
+ demo.launch()