from __future__ import annotations import math import random import gradio as gr import torch from PIL import Image, ImageOps from diffusers import StableDiffusionInstructPix2PixPipeline # Path to the SafeTensor model in Colab model_path = "/content/uberRealisticPornMerge_urpmv12.instruct-pix2pix.safetensors" def main(): # Load the SafeTensor model from Colab safe_pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_path, torch_dtype=torch.float16, safety_checker=None).to("cuda") example_image = Image.open("imgs/example.jpg").convert("RGB") def load_example( steps: int, randomize_seed: bool, seed: int, randomize_cfg: bool, text_cfg_scale: float, image_cfg_scale: float, ): example_instruction = random.choice(example_instructions) return [example_image, example_instruction] + generate( example_image, example_instruction, steps, randomize_seed, seed, randomize_cfg, text_cfg_scale, image_cfg_scale, ) def generate( input_image: Image.Image, instruction: str, steps: int, randomize_seed: bool, seed: int, randomize_cfg: bool, text_cfg_scale: float, image_cfg_scale: float, ): seed = random.randint(0, 100000) if randomize_seed else seed text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale width, height = input_image.size factor = 512 / max(width, height) factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height) width = int((width * factor) // 64) * 64 height = int((height * factor) // 64) * 64 input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS) if instruction == "": return [input_image, seed] generator = torch.manual_seed(seed) edited_image = safe_pipe( instruction, image=input_image, guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale, num_inference_steps=steps, generator=generator, ).images[0] return [seed, text_cfg_scale, image_cfg_scale, edited_image] def reset(): return [0, "Randomize Seed", 1371, "Fix CFG", 7.5, 1.5, None] with gr.Blocks() as demo: gr.HTML("""
For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.