🎨 AI Image Generator — powered by Stable Diffusion
Enter a prompt to generate an image in seconds
""" AI Image Generator - Gradio Web App A web application for generating images using Stable Diffusion with a user-friendly Gradio interface. """ import json import logging import os from typing import List, Optional, Tuple import gradio as gr import torch from PIL import Image from utils.generation import ImageGenerator, save_image_with_metadata # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) # Global image generator instance image_generator: Optional[ImageGenerator] = None def load_examples() -> dict: """Load example prompts from JSON file.""" try: with open('examples.json', 'r', encoding='utf-8') as f: return json.load(f) except FileNotFoundError: logger.warning("examples.json not found, using default examples") return { "examples": [] } except Exception as e: logger.error(f"Failed to load examples: {str(e)}") return { "examples": [], } def initialize_generator() -> ImageGenerator: """ Initialize the image generator with error handling. Returns: ImageGenerator: The initialized generator instance. Raises: RuntimeError: If initialization fails. """ try: logger.info("Initializing Stable Diffusion model...") generator = ImageGenerator() logger.info("Model initialized successfully") return generator except Exception as e: logger.error(f"Failed to initialize model: {str(e)}") raise RuntimeError(f"Model initialization failed: {str(e)}") def generate_images_interface( prompt: str, num_inference_steps: int, # guidance_scale: float, # Commented out - using default value 0.0 seed: Optional[int], width: int, height: int, num_images: int ) -> Tuple[List[str], str]: """ Generate images based on user input parameters. Args: prompt: Text prompt for image generation. num_inference_steps: Number of denoising steps. # guidance_scale: How closely to follow the prompt. # Commented out seed: Random seed for reproducibility. width: Image width. height: Image height. num_images: Number of images to generate. Returns: Tuple[List[str], str]: (list of image paths, status message) """ global image_generator try: # Initialize generator if not already done if image_generator is None: image_generator = initialize_generator() # Validate inputs if not prompt or not prompt.strip(): return [], "❌ Please enter a prompt" logger.info(f"Generating {num_images} image(s) with prompt: '{prompt[:50]}...'") # Generate images images = image_generator.generate_images( prompt=prompt, num_images=num_images, num_inference_steps=num_inference_steps, guidance_scale=0.0, # Using default value since guidance_scale is removed from UI width=width, height=height, seed=seed ) # Save images and collect paths saved_paths = [] for i, image in enumerate(images): metadata = { "num_inference_steps": num_inference_steps, "guidance_scale": 0.0, # Using default value "seed": seed, "width": width, "height": height, "image_number": i + 1 } filepath = save_image_with_metadata( image=image, prompt=prompt, metadata=metadata ) saved_paths.append(filepath) status_msg = f"✅ Successfully generated {len(images)} image(s)!" logger.info(f"Generation completed: {status_msg}") return saved_paths, status_msg except Exception as e: error_msg = f"❌ Generation failed: {str(e)}" logger.error(f"Generation error: {str(e)}") return [], error_msg def create_interface() -> gr.Blocks: """ Create the Gradio interface for the AI Image Generator. Returns: gr.Blocks: The configured Gradio interface. """ # Custom CSS for better styling css = """ .gradio-container { max-width: 100% !important; margin: 0 !important; padding: 0 !important; } .main-header { text-align: center; margin-bottom: 2rem; } .param-section { background: #f8f9fa; padding: 1rem; border-radius: 8px; margin-bottom: 1rem; } .examples-section { background: #f0f8ff; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem; border: 2px solid #e1f5fe; } .gradio-row { gap: 2rem !important; max-width: 100% !important; } .gradio-column { min-width: 0 !important; flex: 1 !important; } .gradio-blocks { max-width: 100% !important; } """ with gr.Blocks(css=css, title="AI Image Generator") as interface: # Header gr.HTML("""
Enter a prompt to generate an image in seconds
Powered by Stable Diffusion XL Turbo | Built with Gradio