Batch count functionality + stop btn

#15
by zenafey - opened
Files changed (1) hide show
  1. app.py +75 -31
app.py CHANGED
@@ -7,9 +7,11 @@ import base64
7
  import os
8
  from io import BytesIO
9
  import PIL
 
10
  from PIL.ExifTags import TAGS
11
  import html
12
  import re
 
13
 
14
 
15
  class Prodia:
@@ -159,8 +161,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):
163
- result = prodia_client.generate({
164
  "prompt": prompt,
165
  "negative_prompt": negative_prompt,
166
  "model": model,
@@ -170,14 +172,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
- return job["imageUrl"]
 
 
 
178
 
179
- def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
180
- result = prodia_client.transform({
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  "imageData": image_to_base64(input_image),
182
  "denoising_strength": denoising,
183
  "prompt": prompt,
@@ -189,18 +212,30 @@ def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampl
189
  "width": width,
190
  "height": height,
191
  "seed": seed
192
- })
 
 
 
 
 
 
 
 
193
 
194
- job = prodia_client.wait(result)
 
 
 
195
 
196
- return job["imageUrl"]
 
197
 
 
198
 
199
- css = """
200
- #generate {
201
- height: 100%;
202
- }
203
- """
204
 
205
  samplers = [
206
  "Euler",
@@ -224,7 +259,7 @@ samplers = [
224
  "PLMS",
225
  ]
226
 
227
- with gr.Blocks(css=css) as demo:
228
  with gr.Row():
229
  with gr.Column(scale=6):
230
  model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
@@ -239,8 +274,9 @@ with gr.Blocks(css=css) as demo:
239
  with gr.Column(scale=6, min_width=600):
240
  prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
241
  negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
242
- with gr.Column():
243
- text_button = gr.Button("Generate", variant='primary', elem_id="generate")
 
244
 
245
  with gr.Row():
246
  with gr.Column(scale=3):
@@ -279,24 +315,24 @@ with gr.Blocks(css=css) as demo:
279
 
280
  with gr.Column(scale=1):
281
  batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
282
- batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
283
 
284
  cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
285
  seed = gr.Number(label="Seed", value=-1)
286
 
287
 
288
  with gr.Column(scale=2):
289
- image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
290
 
291
- text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
292
-
293
  with gr.Tab("img2img", id='i2i'):
294
  with gr.Row():
295
  with gr.Column(scale=6, min_width=600):
296
  i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
297
  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")
298
- with gr.Column():
299
  i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
 
300
 
301
  with gr.Row():
302
  with gr.Column(scale=3):
@@ -317,7 +353,7 @@ with gr.Blocks(css=css) as demo:
317
 
318
  with gr.Column(scale=1):
319
  i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
320
- i2i_batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
321
 
322
  i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
323
  i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)
@@ -325,10 +361,9 @@ with gr.Blocks(css=css) as demo:
325
 
326
 
327
  with gr.Column(scale=2):
328
- i2i_image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
329
 
330
- 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], outputs=i2i_image_output)
331
-
332
  with gr.Tab("PNG Info"):
333
  def plaintext_to_html(text, classname=None):
334
  content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
@@ -361,10 +396,19 @@ with gr.Blocks(css=css) as demo:
361
  with gr.Column():
362
  exif_output = gr.HTML(label="EXIF Data")
363
  send_to_txt2img_btn = gr.Button("Send to txt2img")
364
-
365
- image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
366
- send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt, steps, seed,
 
 
 
 
 
 
367
  model, sampler, width, height, cfg_scale])
368
-
 
 
 
369
  demo.queue(concurrency_count=32)
370
  demo.launch()
 
7
  import os
8
  from io import BytesIO
9
  import PIL
10
+ from PIL import Image
11
  from PIL.ExifTags import TAGS
12
  import html
13
  import re
14
+ from threading import Thread
15
 
16
 
17
  class Prodia:
 
161
  name_without_ext = remove_id_and_ext(model_name)
162
  model_names[name_without_ext] = model_name
163
 
164
+ def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, batch_count, gallery):
165
+ data = {
166
  "prompt": prompt,
167
  "negative_prompt": negative_prompt,
168
  "model": model,
 
172
  "width": width,
173
  "height": height,
174
  "seed": seed
175
+ }
176
 
177
+ total_images = []
178
+ threads = []
179
 
180
+ def generate_one_image():
181
+ result = prodia_client.generate(data)
182
+ job = prodia_client.wait(result)
183
+ total_images.append(job['imageUrl'])
184
 
185
+ for x in range(batch_count):
186
+ t = Thread(target=generate_one_image)
187
+ threads.append(t)
188
+ t.start()
189
+
190
+ for t in threads:
191
+ t.join()
192
+
193
+ new_images_list = [img['name'] for img in gallery]
194
+
195
+ for image in total_images:
196
+ new_images_list.insert(0, image)
197
+
198
+ return {image_output: total_images, gallery_obj: new_images_list}
199
+
200
+
201
+ def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed,
202
+ batch_count, gallery):
203
+ data = {
204
  "imageData": image_to_base64(input_image),
205
  "denoising_strength": denoising,
206
  "prompt": prompt,
 
212
  "width": width,
213
  "height": height,
214
  "seed": seed
215
+ }
216
+
217
+ total_images = []
218
+ threads = []
219
+
220
+ def generate_one_image():
221
+ result = prodia_client.transform(data)
222
+ job = prodia_client.wait(result)
223
+ total_images.append(job['imageUrl'])
224
 
225
+ for x in range(batch_count):
226
+ t = Thread(target=generate_one_image)
227
+ threads.append(t)
228
+ t.start()
229
 
230
+ for t in threads:
231
+ t.join()
232
 
233
+ new_images_list = [img['name'] for img in gallery]
234
 
235
+ for image in total_images:
236
+ new_images_list.insert(0, image)
237
+
238
+ return {i2i_image_output: total_images, gallery_obj: new_images_list}
 
239
 
240
  samplers = [
241
  "Euler",
 
259
  "PLMS",
260
  ]
261
 
262
+ with gr.Blocks() as demo:
263
  with gr.Row():
264
  with gr.Column(scale=6):
265
  model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
 
274
  with gr.Column(scale=6, min_width=600):
275
  prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
276
  negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
277
+ with gr.Row():
278
+ text_button = gr.Button("Generate", variant='primary')
279
+ stop_btn = gr.Button("Cancel", elem_id="cancel")
280
 
281
  with gr.Row():
282
  with gr.Column(scale=3):
 
315
 
316
  with gr.Column(scale=1):
317
  batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
318
+ batch_count = gr.Slider(label="Batch Count", minimum=1, maximum=4, value=1, step=1)
319
 
320
  cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
321
  seed = gr.Number(label="Seed", value=-1)
322
 
323
 
324
  with gr.Column(scale=2):
325
+ image_output = gr.Gallery(value=["https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png"], preview=True)
326
 
327
+
 
328
  with gr.Tab("img2img", id='i2i'):
329
  with gr.Row():
330
  with gr.Column(scale=6, min_width=600):
331
  i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
332
  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")
333
+ with gr.Row():
334
  i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
335
+ i2i_stop_btn = gr.Button("Cancel", elem_id="cancel")
336
 
337
  with gr.Row():
338
  with gr.Column(scale=3):
 
353
 
354
  with gr.Column(scale=1):
355
  i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
356
+ i2i_batch_count = gr.Slider(label="Batch Count", minimum=1, maximum=100, value=1, step=1)
357
 
358
  i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
359
  i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)
 
361
 
362
 
363
  with gr.Column(scale=2):
364
+ i2i_image_output = gr.Gallery(value=["https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png"], preview=True)
365
 
366
+
 
367
  with gr.Tab("PNG Info"):
368
  def plaintext_to_html(text, classname=None):
369
  content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
 
396
  with gr.Column():
397
  exif_output = gr.HTML(label="EXIF Data")
398
  send_to_txt2img_btn = gr.Button("Send to txt2img")
399
+
400
+ with gr.Tab("Gallery"):
401
+ gallery_obj = gr.Gallery(height=500, columns=4)
402
+
403
+ 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])
404
+ stop_btn.click(fn=None, outputs=None, cancels=[t2i_event])
405
+
406
+ image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
407
+ send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt, steps, seed,
408
  model, sampler, width, height, cfg_scale])
409
+
410
+ 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])
411
+ i2i_stop_btn.click(fn=None, outputs=None, cancels=[i2i_event])
412
+
413
  demo.queue(concurrency_count=32)
414
  demo.launch()