import torch import os import gradio as gr from PIL import Image from diffusers import ( DiffusionPipeline, StableDiffusionControlNetPipeline, ControlNetModel, StableDiffusionLatentUpscalePipeline, DPMSolverMultistepScheduler, # <-- Added import EulerDiscreteScheduler # <-- Added import ) # Initialize both pipelines init_pipe = DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V2.0", torch_dtype=torch.float16).to("cuda") controlnet = ControlNetModel.from_pretrained("monster-labs/control_v1p_sd15_qrcode_monster", torch_dtype=torch.float16) main_pipe = StableDiffusionControlNetPipeline.from_pretrained( "SG161222/Realistic_Vision_V2.0", controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16, ).to("cuda") model_id = "stabilityai/sd-x2-latent-upscaler" upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16) upscaler.to("cuda") # Sampler map SAMPLER_MAP = { "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"), "Euler": lambda config: EulerDiscreteScheduler.from_config(config), } def center_crop_resize(img, output_size=(512, 512)): width, height = img.size # Calculate dimensions to crop to the center new_dimension = min(width, height) left = (width - new_dimension)/2 top = (height - new_dimension)/2 right = (width + new_dimension)/2 bottom = (height + new_dimension)/2 # Crop and resize img = img.crop((left, top, right, bottom)) img = img.resize(output_size) return img # Inference function def inference( control_image: Image.Image, prompt: str, negative_prompt: str, guidance_scale: float = 8.0, controlnet_conditioning_scale: float = 1, seed: int = -1, sampler = "DPM++ Karras SDE", progress = gr.Progress(track_tqdm=True) ): if prompt is None or prompt == "": raise gr.Error("Prompt is required") # Generate the initial image #init_image = init_pipe(prompt).images[0] # Rest of your existing code control_image = center_crop_resize(control_image) main_pipe.scheduler = SAMPLER_MAP[sampler](main_pipe.scheduler.config) generator = torch.manual_seed(seed) if seed != -1 else torch.Generator() out = main_pipe( prompt=prompt, negative_prompt=negative_prompt, image=control_image, #control_image=control_image, guidance_scale=float(guidance_scale), controlnet_conditioning_scale=float(controlnet_conditioning_scale), generator=generator, #strength=strength, num_inference_steps=30, #output_type="latent" ).images[0] return out with gr.Blocks() as app: gr.Markdown( '''