zenafey commited on
Commit
70bd245
1 Parent(s): d6c5ef7

Batch count functionality + stop button + bug fixes

Browse files
Files changed (1) hide show
  1. app.py +60 -27
app.py CHANGED
@@ -10,6 +10,7 @@ import PIL
10
  from PIL.ExifTags import TAGS
11
  import html
12
  import re
 
13
 
14
 
15
  class Prodia:
@@ -159,8 +160,8 @@ for model_name in model_list:
159
  name_without_ext = remove_id_and_ext(model_name)
160
  model_names[name_without_ext] = model_name
161
 
162
- def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, gallery):
163
- result = prodia_client.generate({
164
  "prompt": prompt,
165
  "negative_prompt": negative_prompt,
166
  "model": model,
@@ -170,17 +171,35 @@ def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, he
170
  "width": width,
171
  "height": height,
172
  "seed": seed
173
- })
174
 
175
- job = prodia_client.wait(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  new_images_list = [img['name'] for img in gallery]
178
- new_images_list.insert(0, job["imageUrl"])
179
 
180
- return {image_output: job["imageUrl"], gallery_obj: new_images_list}
 
181
 
182
- def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, gallery):
183
- result = prodia_client.transform({
 
 
 
 
184
  "imageData": image_to_base64(input_image),
185
  "denoising_strength": denoising,
186
  "prompt": prompt,
@@ -192,21 +211,30 @@ def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampl
192
  "width": width,
193
  "height": height,
194
  "seed": seed
195
- })
196
 
197
- job = prodia_client.wait(result)
 
198
 
199
- new_images_list = [img['name'] for img in gallery]
200
- new_images_list.insert(0, job["imageUrl"])
 
 
201
 
202
- return {i2i_image_output: job["imageUrl"], gallery_obj: new_images_list}
 
 
 
203
 
 
 
204
 
205
- css = """
206
- #generate {
207
- height: 100%;
208
- }
209
- """
 
210
 
211
  samplers = [
212
  "Euler",
@@ -230,7 +258,7 @@ samplers = [
230
  "PLMS",
231
  ]
232
 
233
- with gr.Blocks(css=css) as demo:
234
  with gr.Row():
235
  with gr.Column(scale=6):
236
  model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
@@ -245,8 +273,9 @@ with gr.Blocks(css=css) as demo:
245
  with gr.Column(scale=6, min_width=600):
246
  prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
247
  negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
248
- with gr.Column():
249
  text_button = gr.Button("Generate", variant='primary', elem_id="generate")
 
250
 
251
  with gr.Row():
252
  with gr.Column(scale=3):
@@ -285,14 +314,14 @@ with gr.Blocks(css=css) as demo:
285
 
286
  with gr.Column(scale=1):
287
  batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
288
- batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
289
 
290
  cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
291
  seed = gr.Number(label="Seed", value=-1)
292
 
293
 
294
  with gr.Column(scale=2):
295
- image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
296
 
297
 
298
  with gr.Tab("img2img", id='i2i'):
@@ -300,8 +329,9 @@ with gr.Blocks(css=css) as demo:
300
  with gr.Column(scale=6, min_width=600):
301
  i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
302
  i2i_negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
303
- with gr.Column():
304
  i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
 
305
 
306
  with gr.Row():
307
  with gr.Column(scale=3):
@@ -322,7 +352,7 @@ with gr.Blocks(css=css) as demo:
322
 
323
  with gr.Column(scale=1):
324
  i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
325
- i2i_batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
326
 
327
  i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
328
  i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)
@@ -330,7 +360,7 @@ with gr.Blocks(css=css) as demo:
330
 
331
 
332
  with gr.Column(scale=2):
333
- i2i_image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
334
 
335
 
336
  with gr.Tab("PNG Info"):
@@ -369,12 +399,15 @@ with gr.Blocks(css=css) as demo:
369
  with gr.Tab("Gallery"):
370
  gallery_obj = gr.Gallery(height=500, columns=4)
371
 
372
- text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, gallery_obj], outputs=[image_output, gallery_obj])
 
 
373
  image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
374
  send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt, steps, seed,
375
  model, sampler, width, height, cfg_scale])
376
- i2i_text_button.click(img2img, inputs=[i2i_image_input, i2i_denoising, i2i_prompt, i2i_negative_prompt, model, i2i_steps, i2i_sampler, i2i_cfg_scale, i2i_width, i2i_height, i2i_seed, gallery_obj], outputs=[i2i_image_output, gallery_obj])
377
 
 
 
378
 
379
  demo.queue(concurrency_count=32)
380
  demo.launch()
 
10
  from PIL.ExifTags import TAGS
11
  import html
12
  import re
13
+ from threading import Thread
14
 
15
 
16
  class Prodia:
 
160
  name_without_ext = remove_id_and_ext(model_name)
161
  model_names[name_without_ext] = model_name
162
 
163
+ def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, batch_count, gallery):
164
+ data = {
165
  "prompt": prompt,
166
  "negative_prompt": negative_prompt,
167
  "model": model,
 
171
  "width": width,
172
  "height": height,
173
  "seed": seed
174
+ }
175
 
176
+ total_images = []
177
+ threads = []
178
+
179
+ def generate_one_image():
180
+ result = prodia_client.generate(data)
181
+ job = prodia_client.wait(result)
182
+ total_images.append(job['imageUrl'])
183
+
184
+ for x in range(batch_count):
185
+ t = Thread(target=generate_one_image)
186
+ threads.append(t)
187
+ t.start()
188
+
189
+ for t in threads:
190
+ t.join()
191
 
192
  new_images_list = [img['name'] for img in gallery]
 
193
 
194
+ for image in total_images:
195
+ new_images_list.insert(0, image)
196
 
197
+ return {image_output: total_images, gallery_obj: new_images_list}
198
+
199
+
200
+ def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed,
201
+ batch_count, gallery):
202
+ data = {
203
  "imageData": image_to_base64(input_image),
204
  "denoising_strength": denoising,
205
  "prompt": prompt,
 
211
  "width": width,
212
  "height": height,
213
  "seed": seed
214
+ }
215
 
216
+ total_images = []
217
+ threads = []
218
 
219
+ def generate_one_image():
220
+ result = prodia_client.transform(data)
221
+ job = prodia_client.wait(result)
222
+ total_images.append(job['imageUrl'])
223
 
224
+ for x in range(batch_count):
225
+ t = Thread(target=generate_one_image)
226
+ threads.append(t)
227
+ t.start()
228
 
229
+ for t in threads:
230
+ t.join()
231
 
232
+ new_images_list = [img['name'] for img in gallery]
233
+
234
+ for image in total_images:
235
+ new_images_list.insert(0, image)
236
+
237
+ return {i2i_image_output: total_images, gallery_obj: new_images_list}
238
 
239
  samplers = [
240
  "Euler",
 
258
  "PLMS",
259
  ]
260
 
261
+ with gr.Blocks() as demo:
262
  with gr.Row():
263
  with gr.Column(scale=6):
264
  model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
 
273
  with gr.Column(scale=6, min_width=600):
274
  prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
275
  negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
276
+ with gr.Row():
277
  text_button = gr.Button("Generate", variant='primary', elem_id="generate")
278
+ stop_btn = gr.Button("Cancel", elem_id="cancel")
279
 
280
  with gr.Row():
281
  with gr.Column(scale=3):
 
314
 
315
  with gr.Column(scale=1):
316
  batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
317
+ batch_count = gr.Slider(label="Batch Count", minimum=1, maximum=4, value=1, step=1)
318
 
319
  cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
320
  seed = gr.Number(label="Seed", value=-1)
321
 
322
 
323
  with gr.Column(scale=2):
324
+ image_output = gr.Gallery(value=["https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png"])
325
 
326
 
327
  with gr.Tab("img2img", id='i2i'):
 
329
  with gr.Column(scale=6, min_width=600):
330
  i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
331
  i2i_negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
332
+ with gr.Row():
333
  i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
334
+ i2i_stop_btn = gr.Button("Cancel")
335
 
336
  with gr.Row():
337
  with gr.Column(scale=3):
 
352
 
353
  with gr.Column(scale=1):
354
  i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
355
+ i2i_batch_count = gr.Slider(label="Batch Count", minimum=1, maximum=4, value=1, step=1)
356
 
357
  i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
358
  i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)
 
360
 
361
 
362
  with gr.Column(scale=2):
363
+ i2i_image_output = gr.Gallery(value=["https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png"])
364
 
365
 
366
  with gr.Tab("PNG Info"):
 
399
  with gr.Tab("Gallery"):
400
  gallery_obj = gr.Gallery(height=500, columns=4)
401
 
402
+ t2i_event = text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, batch_count, gallery_obj], outputs=[image_output, gallery_obj])
403
+ stop_btn.click(fn=None, outputs=None, cancels=[t2i_event])
404
+
405
  image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
406
  send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt, steps, seed,
407
  model, sampler, width, height, cfg_scale])
 
408
 
409
+ i2i_event = i2i_text_button.click(img2img, inputs=[i2i_image_input, i2i_denoising, i2i_prompt, i2i_negative_prompt, model, i2i_steps, i2i_sampler, i2i_cfg_scale, i2i_width, i2i_height, i2i_seed, i2i_batch_count, gallery_obj], outputs=[i2i_image_output, gallery_obj])
410
+ i2i_stop_btn.click(fn=None, outputs=None, cancels=[i2i_event])
411
 
412
  demo.queue(concurrency_count=32)
413
  demo.launch()