Uthar commited on
Commit
3bcddcc
1 Parent(s): 26428ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -61
app.py CHANGED
@@ -110,76 +110,117 @@ function refresh() {
110
  }
111
  """
112
 
113
- with gr.Blocks(theme='NoCrypt/miku@>=1.2.2', fill_width=True, css=CSS) as demo:
114
- gr.HTML("")
115
-
116
  with gr.Tab('6 Models'):
117
  with gr.Column(scale=2):
118
  with gr.Group():
119
  txt_input = gr.Textbox(label='Your prompt:', value=preSetPrompt, lines=3, autofocus=1)
120
  neg_input = gr.Textbox(label='Negative prompt:', value=negPreSetPrompt, lines=1)
121
- with gr.Accordion("Advanced", open=False):
122
- width = gr.Slider(label="Width", maximum=1216, step=32, value=0)
123
- height = gr.Slider(label="Height", maximum=1216, step=32, value=0)
124
- steps = gr.Slider(label="Number of inference steps", maximum=100, step=1, value=0)
125
- cfg = gr.Slider(label="Guidance scale", maximum=30.0, step=0.1, value=0)
126
- seed = gr.Slider(label="Seed", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
127
- seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
128
- seed_rand.click(randomize_seed, None, [seed], queue=False)
129
- gen_button = gr.Button(f'Generate up to {num_models} images', variant='primary', scale=3)
130
- random_button = gr.Button('Randomize Models', variant='secondary', scale=1)
131
- gr.Markdown("")
132
-
133
- with gr.Row():
134
- output = [gr.Image(label=m, show_download_button=True, elem_classes="output", interactive=False, width=112, height=112, format="png", visible=True) for m in default_models]
135
- current_models = [gr.Textbox(m, visible=False) for m in default_models]
 
 
 
 
 
 
 
 
136
 
137
  with gr.Column(scale=2):
138
- gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery", interactive=False, format="png", preview=True, object_fit="cover", columns=2, rows=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
- for m, o in zip(current_models, output):
141
- gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn,
142
- inputs=[m, txt_input, neg_input, height, width, steps, cfg, seed], outputs=[o])
143
- o.change(add_gallery, [o, m, gallery], [gallery])
144
 
145
- model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} models', value=default_models)
146
- model_choice.change(update_imgbox, model_choice, output)
147
- model_choice.change(extend_choices, model_choice, current_models)
148
- random_button.click(random_choices, None, model_choice)
 
 
 
 
 
149
 
150
- with gr.Tab('Single model'):
151
- model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0])
152
- txt_input2 = gr.Textbox(label='Your prompt:', value=preSetPrompt, lines=3, autofocus=1)
153
- neg_input2 = gr.Textbox(label='Negative prompt:', value=negPreSetPrompt, lines=1)
154
- num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
155
-
156
- gen_button2 = gr.Button('Let the machine hallucinate', variant='primary', scale=2)
157
-
158
- output2 = [gr.Image(label='', show_download_button=True, elem_classes="output", interactive=False, width=112, height=112, format="png", show_share_button=False) for _ in range(max_images)]
159
-
160
- with gr.Column(scale=2):
161
- gallery2 = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery", interactive=False, show_share_button=True, format="png", preview=True, object_fit="cover", columns=2, rows=2)
162
-
163
- for i, o in enumerate(output2):
164
- # Change visibility based on num_images
165
- num_images.change(
166
- lambda num_images, i=i: gr.update(visible=(i < num_images)), # `i` corresponds to the index of the image
167
- [num_images], # Only num_images needs to be an input
168
- o, # Outputs the updated visibility of the image `o`
169
- queue=False
170
- )
171
-
172
- # Image generation function
173
- gen_event2 = gr.on(
174
- triggers=[gen_button2.click, txt_input2.submit],
175
- fn=gen_fn,
176
- inputs=[i, num_images, model_choice2, txt_input2, neg_input2, height, width, steps, cfg, seed],
177
- outputs=[o]
178
- )
179
-
180
- # Update gallery when a new image is generated
181
- o.change(add_gallery, [o, model_choice2, gallery2], [gallery2])
182
 
 
183
 
184
- gr.Markdown("")
185
- demo.launch(show_api=False, max_threads=400)
 
110
  }
111
  """
112
 
113
+ with gr.Blocks(fill_width=True, css=CSS) as demo:
114
+ gr.HTML("")
 
115
  with gr.Tab('6 Models'):
116
  with gr.Column(scale=2):
117
  with gr.Group():
118
  txt_input = gr.Textbox(label='Your prompt:', value=preSetPrompt, lines=3, autofocus=1)
119
  neg_input = gr.Textbox(label='Negative prompt:', value=negPreSetPrompt, lines=1)
120
+ with gr.Accordion("Advanced", open=False, visible=True):
121
+ with gr.Row():
122
+ width = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
123
+ height = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
124
+ with gr.Row():
125
+ steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
126
+ cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
127
+ seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
128
+ seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary")
129
+ seed_rand.click(randomize_seed, None, [seed], queue=False)
130
+ with gr.Row():
131
+ gen_button = gr.Button(f'Generate up to {int(num_models)} images', variant='primary', scale=3)
132
+ random_button = gr.Button(f'Randomize Models', variant='secondary', scale=1)
133
+
134
+ gr.Markdown("", elem_classes="guide")
135
+
136
+ with gr.Column(scale=1):
137
+ with gr.Group():
138
+ with gr.Row():
139
+ output = [gr.Image(label=m, show_download_button=True, elem_classes="output",
140
+ interactive=False, width=112, height=112, show_share_button=False, format="png",
141
+ visible=True) for m in default_models]
142
+ current_models = [gr.Textbox(m, visible=False) for m in default_models]
143
 
144
  with gr.Column(scale=2):
145
+ gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
146
+ interactive=False, show_share_button=False, container=True, format="png",
147
+ preview=True, object_fit="cover", columns=2, rows=2)
148
+
149
+ # Inside the `gr.Blocks` context where the components are defined:
150
+ for i, o in enumerate(output):
151
+ # Ensure event handler is inside `gr.Blocks`
152
+ num_images.change(
153
+ lambda num_images, i=i: gr.update(visible=(i < num_images)), # `i` corresponds to the index of the image
154
+ [num_images], # Only num_images needs to be an input
155
+ o, # Outputs the updated visibility of the image `o`
156
+ queue=False
157
+ )
158
+
159
+ # Image generation function
160
+ gen_event2 = gr.on(
161
+ triggers=[gen_button.click, txt_input.submit],
162
+ fn=gen_fn,
163
+ inputs=[i, num_images, model_choice2, txt_input, neg_input, height, width, steps, cfg, seed],
164
+ outputs=[o]
165
+ )
166
+
167
+ # Update gallery when a new image is generated
168
+ o.change(add_gallery, [o, model_choice2, gallery], [gallery])
169
+
170
+ # Model selection and updating outputs
171
+ with gr.Column(scale=4):
172
+ with gr.Accordion('Model selection'):
173
+ model_choice = gr.CheckboxGroup(models, label=f'Choose up to {int(num_models)} different models from the {len(models)} available!', value=default_models, interactive=True)
174
+ model_choice.change(update_imgbox, model_choice, output)
175
+ model_choice.change(extend_choices, model_choice, current_models)
176
+ random_button.click(random_choices, None, model_choice)
177
+
178
+ # Single model section
179
+ with gr.Tab('Single model'):
180
+ with gr.Column(scale=2):
181
+ model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0])
182
+ with gr.Group():
183
+ txt_input2 = gr.Textbox(label='Your prompt:', value=preSetPrompt, lines=3, autofocus=1)
184
+ neg_input2 = gr.Textbox(label='Negative prompt:', value=negPreSetPrompt, lines=1)
185
+ with gr.Accordion("Advanced", open=False, visible=True):
186
+ with gr.Row():
187
+ width2 = gr.Slider(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
188
+ height2 = gr.Slider(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
189
+ with gr.Row():
190
+ steps2 = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
191
+ cfg2 = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
192
+ seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
193
+ seed_rand2 = gr.Button("Randomize Seed", size="sm", variant="secondary")
194
+ seed_rand2.click(randomize_seed, None, [seed2], queue=False)
195
+
196
+ num_images2 = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
197
+
198
+ with gr.Row():
199
+ gen_button2 = gr.Button('Let the machine hallucinate', variant='primary', scale=2)
200
+
201
+ with gr.Column(scale=1):
202
+ with gr.Group():
203
+ with gr.Row():
204
+ output2 = [gr.Image(label='', show_download_button=True, elem_classes="output",
205
+ interactive=False, width=112, height=112, visible=True, format="png",
206
+ show_share_button=False, show_label=False) for _ in range(max_images)]
207
 
208
+ with gr.Column(scale=2):
209
+ gallery2 = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
210
+ interactive=False, show_share_button=True, container=True, format="png",
211
+ preview=True, object_fit="cover", columns=2, rows=2)
212
 
213
+ for i, o in enumerate(output2):
214
+ img_i = gr.Number(i, visible=False)
215
+ num_images2.change(lambda i, n: gr.update(visible=(i < n)), [img_i, num_images2], o, queue=False)
216
+ gen_event2 = gr.on(
217
+ triggers=[gen_button2.click, txt_input2.submit],
218
+ fn=gen_fn,
219
+ inputs=[img_i, num_images2, model_choice2, txt_input2, neg_input2, height2, width2, steps2, cfg2, seed2],
220
+ outputs=[o]
221
+ )
222
 
223
+ o.change(add_gallery, [o, model_choice2, gallery2], [gallery2])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
+ demo.launch(show_api=False, max_threads=400)
226