import gradio as gr import torch from diffusers import DiffusionPipeline, AutoencoderKL from PIL import Image import spaces # Initialize the VAE model and Diffusion Pipeline outside the GPU-enabled function for efficiency vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) pipe = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) pipe.load_lora_weights('ritwikraha/khabib_sketch_LoRA') if torch.cuda.is_available(): _ = pipe.to("cuda") # Define the image generation function @spaces.GPU(enable_queue=True) def generate_sketch(prompt, negative_prompt="ugly face, multiple bodies, bad anatomy, disfigured, extra fingers", guidance_scale=3, num_inference_steps=50): """Generate a sketch image based on a prompt using Stable Diffusion XL with LoRA weights. Args: prompt (str): Description of the image to generate. negative_prompt (str, optional): Negative prompt to avoid certain features. Defaults to common undesirables. guidance_scale (int, optional): The strength of the guidance. Defaults to 3. num_inference_steps (int, optional): The number of steps for the diffusion process. Defaults to 50. Returns: PIL.Image: The generated sketch image. """ result = pipe( prompt=prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, ) return result.images[0].convert("RGB") # Ensure the image is in RGB format # Gradio Interface description = """ This demo utilizes the SDXL model LoRA adaption weights for stabilityai/stable-diffusion-xl-base-1.0. The weights were trained on sketches of Khabib by ritwikraha using DreamBooth and can be found here: https://huggingface.co/ritwikraha/khabib_sketch_LoRA """ # Setup Gradio interface with gr.Blocks() as demo: gr.HTML("