diff --git a/cli/simple-img2img.py b/cli/simple-img2img.py index ceb89fd81..8590fc62a 100755 --- a/cli/simple-img2img.py +++ b/cli/simple-img2img.py @@ -1,19 +1,24 @@ #!/usr/bin/env python +import os import io import sys import base64 import logging import requests +import urllib3 from PIL import Image +sd_url = os.environ.get('SDAPI_URL', "http://127.0.0.1:7860") +sd_username = os.environ.get('SDAPI_USR', None) +sd_password = os.environ.get('SDAPI_PWD', None) + logging.basicConfig(level = logging.INFO, format = '%(asctime)s %(levelname)s: %(message)s') log = logging.getLogger(__name__) -sd_url = "http://127.0.0.1:7860" options = { "init_images": [], "prompt": "city at night", "negative_prompt": "foggy, blurry", - "steps": 1, + "steps": 20, "batch_size": 1, "n_iter": 1, "seed": -1, @@ -24,9 +29,17 @@ options = { "save_images": False, "send_images": True, } +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + + +def auth(): + if sd_username is not None and sd_password is not None: + return requests.auth.HTTPBasicAuth(sd_username, sd_password) + return None + def post(endpoint: str, dct: dict = None): - req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300) + req = requests.post(f'{sd_url}{endpoint}', json = dct, timeout=300, verify=False, auth=auth()) if req.status_code != 200: return { 'error': req.status_code, 'reason': req.reason, 'url': req.url } else: @@ -44,7 +57,8 @@ def encode(f): def generate(num: int = 0): log.info(f'sending generate request: {num+1} {options}') - options['init_images'] = [encode('../html/logo.png')] + options['init_images'] = [encode('html/logo-dark.png')] + options['batch_size'] = len(options['init_images']) data = post('/sdapi/v1/img2img', options) if 'images' in data: for i in range(len(data['images'])): diff --git a/cli/simple-txt2img.py b/cli/simple-txt2img.py index d07110b24..70e60a916 100755 --- a/cli/simple-txt2img.py +++ b/cli/simple-txt2img.py @@ -5,6 +5,7 @@ import sys import base64 import logging import requests +import urllib3 from PIL import Image sd_url = os.environ.get('SDAPI_URL', "http://127.0.0.1:7860") @@ -13,6 +14,7 @@ sd_password = os.environ.get('SDAPI_PWD', None) logging.basicConfig(level = logging.INFO, format = '%(asctime)s %(levelname)s: %(message)s') log = logging.getLogger(__name__) +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) filename='/tmp/simple-txt2img.jpg' model = None # desired model name, will be set if not none @@ -31,6 +33,7 @@ options = { "send_images": True, } + def auth(): if sd_username is not None and sd_password is not None: return requests.auth.HTTPBasicAuth(sd_username, sd_password) diff --git a/modules/processing.py b/modules/processing.py index 6c0264ae5..cb64d352e 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -1058,6 +1058,7 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing): if add_color_corrections: self.color_corrections = [] imgs = [] + unprocessed = [] for img in self.init_images: # Save init image if shared.opts.save_init_img: @@ -1077,6 +1078,8 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing): if crop_region is not None: image = image.crop(crop_region) image = images.resize_image(3, image, self.width, self.height) + if shared.backend == shared.Backend.DIFFUSERS: + unprocessed.append(image) self.init_images = [image] # assign early for diffusers if image_mask is not None: if self.inpainting_fill != 1: @@ -1086,6 +1089,8 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing): image = np.array(image).astype(np.float32) / 255.0 image = np.moveaxis(image, 2, 0) imgs.append(image) + if shared.backend == shared.Backend.DIFFUSERS: + self.init_images = unprocessed # assign early for diffusers if len(imgs) == 1: batch_images = np.expand_dims(imgs[0], axis=0).repeat(self.batch_size, axis=0) if self.overlay_images is not None: diff --git a/modules/processing_diffusers.py b/modules/processing_diffusers.py index 66dfbf25f..9686f9ae3 100644 --- a/modules/processing_diffusers.py +++ b/modules/processing_diffusers.py @@ -29,7 +29,10 @@ def process_diffusers(p: StableDiffusionProcessing, seeds, prompts, negative_pro def vae_decode(latents, model, output_type='np'): if hasattr(model, 'vae') and torch.is_tensor(latents): - shared.log.debug(f'Diffusers VAE decode: name={sd_vae.loaded_vae_file} dtype={model.vae.dtype} upcast={model.vae.config.get("force_upcast", None)}') + if latents.shape[0] == 0: + shared.log.error(f'VAE nothing to decode: {latents.shape}') + return [] + shared.log.debug(f'Diffusers VAE decode: name={sd_vae.loaded_vae_file} dtype={model.vae.dtype} upcast={model.vae.config.get("force_upcast", None)} images={latents.shape[0]}') if shared.opts.diffusers_move_unet and not model.has_accelerate: shared.log.debug('Diffusers: Moving UNet to CPU') unet_device = model.unet.device @@ -159,6 +162,9 @@ def process_diffusers(p: StableDiffusionProcessing, seeds, prompts, negative_pro sd_samplers.create_sampler(sampler.name, shared.sd_model) # TODO(Patrick): For wrapped pipelines this is currently a no-op cross_attention_kwargs={} + if p.init_images is not None and len(p.init_images) > 0: + while len(p.init_images) < len(prompts): + p.init_images.append(p.init_images[-1]) if lora_state['active']: cross_attention_kwargs['scale'] = lora_state['multiplier'] task_specific_kwargs={} @@ -196,7 +202,6 @@ def process_diffusers(p: StableDiffusionProcessing, seeds, prompts, negative_pro **task_specific_kwargs ) output = shared.sd_model(**pipe_args) # pylint: disable=not-callable - if shared.state.interrupted or shared.state.skipped: unload_diffusers_lora() return results diff --git a/modules/sd_models.py b/modules/sd_models.py index 41b800f74..1cf408a96 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -874,7 +874,6 @@ def get_diffusers_task(pipe: diffusers.DiffusionPipeline) -> DiffusersTaskType: return DiffusersTaskType.IMAGE_2_IMAGE elif pipe.__class__ in diffusers.pipelines.auto_pipeline.AUTO_INPAINT_PIPELINES_MAPPING.values(): return DiffusersTaskType.INPAINTING - return DiffusersTaskType.TEXT_2_IMAGE