# Copyright (c) 2025 All rights reserved. import os import torch import gradio as gr import huggingface_hub from huggingface_hub import snapshot_download from PIL import Image, ImageDraw, ImageFont # Import the base pipeline from diffusers from diffusers import StableDiffusionControlNetPipeline, ControlNetModel from transformers import CLIPTextModel, CLIPTokenizer # Define default parameters DEFAULT_SEED = 42 DEFAULT_STEPS = 30 DEFAULT_GUIDANCE_SCALE = 7.5 RED_BG_COLOR = "#ffcccc" # Light red background # Initialize the model def download_model(): # Download the model (using a simple SD model as example) snapshot_download(repo_id='runwayml/stable-diffusion-v1-5', local_dir='./models/stable-diffusion', local_dir_use_symlinks=False) def init_pipeline(): # Initialize a simple text-to-image pipeline pipeline = StableDiffusionControlNetPipeline.from_pretrained( "./models/stable-diffusion", torch_dtype=torch.float16, safety_checker=None ) pipeline = pipeline.to("cuda") return pipeline # Generate image function def generate_image(prompt, seed, num_steps, guidance_scale): try: # Make sure we have a valid seed if seed == 0: seed = torch.seed() & 0xFFFFFFFF # Set up generator for reproducibility generator = torch.Generator("cuda").manual_seed(seed) # Generate the image image = pipeline( prompt=prompt, num_inference_steps=num_steps, guidance_scale=guidance_scale, generator=generator ).images[0] # Add watermark image = add_safety_watermark(image) except Exception as e: print(f"Error generating image: {e}") return gr.update() return gr.update(value=image, label=f"Generated Image, seed = {seed}") # Add watermark to image def add_safety_watermark(image, text='AI Generated'): width, height = image.size draw = ImageDraw.Draw(image) # Set font size based on image height font_size = int(height * 0.028) font = ImageFont.load_default() # Calculate text position text_width = len(text) * font_size * 0.6 # Approximate width x = width - text_width - 10 y = height - font_size - 20 # Add shadow and text draw.text((x+2, y+2), text, fill="black") draw.text((x, y), text, fill="white") return image # Create example function def generate_example(prompt, seed): return generate_image(prompt, seed, DEFAULT_STEPS, DEFAULT_GUIDANCE_SCALE) # Sample examples sample_list = [ ['A majestic mountain landscape with snow peaks and pine trees', 123], ['A futuristic city with flying cars and tall skyscrapers', 456], ['A serene beach scene with clear blue waters', 789], ] # Create the Gradio interface with gr.Blocks(css=f".gradio-container {{ background-color: {RED_BG_COLOR} !important; }}") as demo: gr.HTML("""