import time import gradio as gr import torch from PIL import Image from diffusers.image_processor import VaeImageProcessor from diffusers.schedulers import ( DDPMScheduler, DDIMScheduler, EulerAncestralDiscreteScheduler, DPMSolverMultistepScheduler, PNDMScheduler, EulerDiscreteScheduler, ) from diffusers.utils.torch_utils import randn_tensor MODEL_IDS = { 'Stable Diffusion v1.4': 'CompVis/stable-diffusion-v1-4', 'Stable Diffusion v3 medium': 'stabilityai/stable-diffusion-3-medium-diffusers', } SCHEDULERS = { "DDPMScheduler": DDPMScheduler, "DDIMScheduler": DDIMScheduler, "PNDMScheduler": PNDMScheduler, "EulerDiscreteScheduler": EulerDiscreteScheduler, "EulerAncestralDiscreteScheduler": EulerAncestralDiscreteScheduler, "DPMSolverMultistepScheduler": DPMSolverMultistepScheduler, } def inference( image_pil: Image.Image, model_name: str, scheduler_name: str, per_step_time: int = 1, n_total_steps: int = 1000, ): yield image_pil, "" scheduler = SCHEDULERS[scheduler_name].from_pretrained(MODEL_IDS[model_name], subfolder='scheduler') scheduler.set_timesteps(num_inference_steps=n_total_steps) timesteps = torch.flip(scheduler.timesteps, dims=[0]) image_processor = VaeImageProcessor() image_tensor = image_processor.preprocess(image_pil) # Fix seed generator = torch.Generator().manual_seed(1117) noise = randn_tensor(image_tensor.shape, generator) for i, t in enumerate(timesteps): t = torch.tensor([t]) noised_image_tensor = scheduler.add_noise(image_tensor, noise, timesteps=t) noised_image_pil = image_processor.postprocess(noised_image_tensor)[0] time.sleep(per_step_time) # language=HTML info_html = f"""