""" ImageGen AI - A text-to-image generation application. This is the main entry point for the application, which initializes the model and UI components and launches the Gradio interface. """ import logging import os import sys # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(sys.stdout) ] ) logger = logging.getLogger(__name__) # Import application modules from model import ModelManager from ui import ImageGenUI def main(): """Initialize and launch the application.""" try: logger.info("Initializing ImageGen AI application") # Initialize the model logger.info("Loading AI model") model_manager = ModelManager() model_manager.load_model() # Create wrapper function for image generation def generate_image( prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress_callback=None ): return model_manager.generate_image( prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress_callback ) # Initialize and launch the UI logger.info("Setting up user interface") ui = ImageGenUI(generate_image) ui.build_ui() logger.info("Launching application") # Try multiple ports in case some are already in use for port in range(7860, 7870): try: logger.info(f"Attempting to launch on port {port}") ui.launch(share=False, server_port=port) logger.info(f"Successfully launched on port {port}") break except OSError as e: logger.warning(f"Port {port} is in use, trying next port. Error: {str(e)}") if port == 7869: # Last port in range logger.error("Could not find an available port in range 7860-7869") raise except Exception as e: logger.error(f"Error starting application: {str(e)}") raise if __name__ == "__main__": main()