Skip to content

Instantly share code, notes, and snippets.

@w-e-w
Created October 19, 2024 12:33
Show Gist options
  • Save w-e-w/fd0f9728b91e79478922363064a76f6f to your computer and use it in GitHub Desktop.
Save w-e-w/fd0f9728b91e79478922363064a76f6f to your computer and use it in GitHub Desktop.
webui demo txt2img_upscale read generation_parameters from image and pass to script
from modules import patches, txt2img, script_callbacks, infotext_utils, errors, scripts
from functools import wraps
import inspect
import json
global_carry_info = None
def wrap_txt2img_upscale(func):
@wraps(func)
def wrapper(*args, **kwargs):
global global_carry_info
try:
bind_args = inspect.signature(func).bind(*args, **kwargs)
bind_args.apply_defaults()
geninfo = json.loads(bind_args.arguments['generation_info'])
parameters = infotext_utils.parse_generation_parameters(geninfo.get('infotexts')[bind_args.arguments['gallery_index']], [])
global_carry_info = parameters.get('carry info', None)
except Exception:
errors.report('something went wrong while extraction info from hr-button', exc_info=True)
global_carry_info = None
return func(*args, **kwargs)
return wrapper
def patch_txt2img_upscale():
try:
patches.patch(__name__, txt2img, 'txt2img_upscale', wrap_txt2img_upscale(txt2img.txt2img_upscale))
def undo():
patches.undo(__name__, txt2img, 'txt2img_upscale')
script_callbacks.on_script_unloaded(undo)
except RuntimeError:
pass
patch_txt2img_upscale()
class Script(scripts.Script):
def __init__(self):
super().__init__()
self.carry_info = None
def title(self):
return "carry info test"
def show(self, is_img2img):
return scripts.AlwaysVisible
def setup(self, p, *args):
global global_carry_info
self.carry_info = global_carry_info
global_carry_info = None
def process_batch(self, p, **kwargs):
if self.carry_info:
print(f'\nI remember {self.carry_info}')
# timestamp as placeholder data
from datetime import datetime
p.extra_generation_params['carry info'] = f'{datetime.now().isoformat()}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment