import os import spaces import torch from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler import gradio as gr import random import tqdm # Enable TQDM progress tracking tqdm.monitor_interval = 0 # Load the diffusion pipelines pipe1 = StableDiffusionXLPipeline.from_pretrained( "kayfahaarukku/UrangDiffusion-1.4", torch_dtype=torch.float16, custom_pipeline="lpw_stable_diffusion_xl", ) pipe1.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe1.scheduler.config) pipe2 = StableDiffusionXLPipeline.from_pretrained( "kayfahaarukku/UrangDiffusion-2.0", torch_dtype=torch.float16, custom_pipeline="lpw_stable_diffusion_xl", ) pipe2.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe2.scheduler.config) # Function to generate images from both models @spaces.GPU def generate_comparison(prompt, negative_prompt, use_defaults, resolution, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()): pipe1.to('cuda') pipe2.to('cuda') if randomize_seed: seed = random.randint(0, 99999999) if use_defaults: prompt = f"{prompt}, best quality, amazing quality, very aesthetic" negative_prompt = f"nsfw, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], {negative_prompt}" generator = torch.manual_seed(seed) def callback(step, timestep, latents): progress(step / (2 * num_inference_steps)) return width, height = map(int, resolution.split('x')) # Generate image with UrangDiffusion-1.4 image1 = pipe1( prompt, negative_prompt=negative_prompt, width=width, height=height, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, generator=generator, callback=callback, callback_steps=1 ).images[0] # Generate image with UrangDiffusion-2.0 image2 = pipe2( prompt, negative_prompt=negative_prompt, width=width, height=height, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, generator=generator, callback=callback, callback_steps=1 ).images[0] torch.cuda.empty_cache() metadata_text = f"{prompt}\nNegative prompt: {negative_prompt}\nSteps: {num_inference_steps}, Sampler: Euler a, Size: {width}x{height}, Seed: {seed}, CFG scale: {guidance_scale}" return image1, image2, seed, metadata_text # Define Gradio interface def interface_fn(prompt, negative_prompt, use_defaults, resolution, guidance_scale, num_inference_steps, seed, randomize_seed, progress=gr.Progress()): image1, image2, seed, metadata_text = generate_comparison(prompt, negative_prompt, use_defaults, resolution, guidance_scale, num_inference_steps, seed, randomize_seed, progress) return image1, image2, seed, gr.update(value=metadata_text) def reset_inputs(): return gr.update(value=''), gr.update(value=''), gr.update(value=True), gr.update(value='832x1216'), gr.update(value=7), gr.update(value=28), gr.update(value=0), gr.update(value=True), gr.update(value='') with gr.Blocks(title="UrangDiffusion Comparison Demo", theme="NoCrypt/miku@1.2.1") as demo: gr.HTML( "