Skip to content

Instantly share code, notes, and snippets.

@camenduru
Last active April 2, 2024 13:04
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save camenduru/9ec5f8141db9902e375967e93250860f to your computer and use it in GitHub Desktop.
Save camenduru/9ec5f8141db9902e375967e93250860f to your computer and use it in GitHub Desktop.
import math
import os
import sys
import traceback
import modules.scripts as scripts
import gradio as gr
from modules.processing import Processed, process_images
class Script(scripts.Script):
def title(self):
return "Run n times"
def ui(self, is_img2img):
n = gr.Textbox(label="n")
return [n]
def run(self, p, n):
for x in range(int(n)):
p.seed = -1
proc = process_images(p)
image = proc.images
return Processed(p, image, p.seed, proc.info)
@smokeyhallow
Copy link

Hey, great script! But is there a way to make this work for txt2img also?

@camenduru
Copy link
Author

@smokeyhallow it works with txt2img. check discussions #2194

@smokeyhallow
Copy link

smokeyhallow commented Oct 13, 2022

Yes, it does thanks! I got confused because of the script name! Didn't expect it to be shown as "To Infinity and Beyond" was looking for "Run n times". Anyways, renamed it & got it working :)

Also, I was wondering whether it would be possible to add it to the galley/preview system of the app?

Now, only the first image gets populated in the gallery.

@tuangd
Copy link

tuangd commented Nov 2, 2022

I think something is happening with the current repo.
I got this error when trying to use the script.

Traceback (most recent call last):
  File "C:\temp\stable-diffusion-webui\modules\ui.py", line 185, in f
    res = list(func(*args, **kwargs))
  File "C:\temp\stable-diffusion-webui\webui.py", line 54, in f
    res = func(*args, **kwargs)
  File "C:\temp\stable-diffusion-webui\modules\txt2img.py", line 45, in txt2img
    processed = modules.scripts.scripts_txt2img.run(p, *args)
  File "C:\temp\stable-diffusion-webui\modules\scripts.py", line 282, in run
    processed = script.run(p, *script_args)
  File "C:\temp\stable-diffusion-webui\scripts\run_n_times.py", line 22, in run
    proc = process_images(p)
  File "C:\temp\stable-diffusion-webui\modules\processing.py", line 427, in process_images
    res = process_images_inner(p)
  File "C:\temp\stable-diffusion-webui\modules\processing.py", line 487, in process_images_inner
    with torch.no_grad(), p.sd_model.ema_scope():
AttributeError: 'NoneType' object has no attribute 'ema_scope'

Edited the latest ropo 4a8cf fix this already

@camenduru
Copy link
Author

@tuangd maybe problem in the model try with different model

@Harvester62
Copy link

Hello, I am the translator of the AUTOMATIC1111 SDWebUI curating the Italian localization of the interface, as well as the official Scripts and Extensions. Now, I am facing a problem with your script because the recursive run parameter has the label "n" which is already being used inside the Settings, therefore i cannot translate it properly without overwriting the latter. I wish to suggest you to change it into the following: "Run N times". Would it be possible? Otherwise I will not be able to translate your script because the SDWebUI interface has the priority and precedence. Thank you for your attention and consideration.

@camenduru
Copy link
Author

I am facing a problem with your script because the recursive run parameter has the label "n" which is already being used inside the Settings

sorry I don't understand can you explain with detail is it crushing? is it a bug?

@Harvester62
Copy link

No, it's no a bug, it's that I wished to give to the "n" label a more meaningful translation in Italian, therefore I initially translated it as "Run N times" ("Esegui N volte"), which makes easier to understand its function.
The problem is that the label "n" is already used in the Settings and associated to the type of image under "Allowed categories for random artists selection when using the Roll button", therefore also that label get visualized as "Run N times" which doesn't make sense in that context. The only way I can rename the label without messing up with that one in the Settings is to have your label given a different value/name. I changed it inside your Python script and it worked perfectly, so I am suggesting you to modify your script at row 16 as follows:
OLD
n = gr.Textbox(label="n")
NEW
n = gr.Textbox(label="Run N times")

Hope now it's clearer.

@camenduru
Copy link
Author

user entering the value n from the run n times n = gr.Textbox(label="Run N times") no make sense "Allowed categories for random artists selection when using the Roll button" n has no meaning please change that n

@Harvester62
Copy link

If you don't want to make the suggested change to your script that is fine. I will leave it as is in the Italian localization file. Regards.

@shitaginashi
Copy link

this badboy is a real time saver. nice work, OP!

@ludzeller
Copy link

Great script!

Having more control over seed behaviour in img2img batch (and all the other modes) is really important. Hope this gets integrated into vanilla auto1111.

@Neytiri7
Copy link

What should I put in the "n" item? I entered "5" to make it 5 times, but the seed value continues to increase by +1.

@aarmstrongc
Copy link

It works fine! Thanks!
The scrip ignores the manual output directory, and uses the default output folder of the SD webui dou

@Trader5050
Copy link

A bug is created when using the saved styles dropdown with this script. Each iteration includes another copy of the style, meaning that it gets copied over and over into the prompt, generating very weird results.

@Woisek
Copy link

Woisek commented Mar 3, 2024

Since yesterday, I have found that using the script for batch generation and using ReActor, it no longer works for the batch. ReActor swaps the first image, but after that, it says "source not found".
23:22:34 - ReActor - ERROR - Cannot detect any Source
23:22:34 - ReActor - ERROR - Please provide a source face

Any idea what this could be?
A1111 1.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment