Batch count functionality + stop button + bug fixes

#16
by zenafey - opened
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +73 -30
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🔥
4
  colorFrom: yellow
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 3.42.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: yellow
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 3.44.4
8
  app_file: app.py
9
  pinned: false
10
  license: mit
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):
163
- result = prodia_client.generate({
164
  "prompt": prompt,
165
  "negative_prompt": negative_prompt,
166
  "model": model,
@@ -170,14 +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
- 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 +211,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 +258,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 +273,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 +314,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 +352,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 +360,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 +395,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()
 
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'):
328
  with gr.Row():
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"):
367
  def plaintext_to_html(text, classname=None):
368
  content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
 
395
  with gr.Column():
396
  exif_output = gr.HTML(label="EXIF Data")
397
  send_to_txt2img_btn = gr.Button("Send to txt2img")
398
+
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()