""" DigiPal V2 - Enhanced Monster Companion with DW1 Mechanics and 3D Generation Main application entry point """ import asyncio import json import logging import os import sys from pathlib import Path import torch import spaces # Add src to path sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src')) # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) # Environment configuration ENV_CONFIG = { "LOG_LEVEL": os.getenv("LOG_LEVEL", "INFO"), "SERVER_NAME": os.getenv("SERVER_NAME", "0.0.0.0"), "SERVER_PORT": int(os.getenv("SERVER_PORT", "7860")), "SHARE": os.getenv("SHARE", "false").lower() == "true", "DEBUG": os.getenv("DEBUG", "false").lower() == "true", "MAX_THREADS": int(os.getenv("MAX_THREADS", "40")), "ENABLE_3D": os.getenv("ENABLE_3D", "true").lower() == "true", "ENABLE_AI": os.getenv("ENABLE_AI", "true").lower() == "true" } # HuggingFace Spaces detection IS_SPACES = os.getenv("SPACE_ID") is not None def check_dependencies(): """Check and report on available dependencies""" logger.info("=" * 60) logger.info("DigiPal V2 - System Check") logger.info("=" * 60) # Check GPU availability if torch.cuda.is_available(): logger.info(f"✅ GPU Available: {torch.cuda.get_device_name()}") logger.info(f" Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB") else: logger.warning("⚠️ No GPU detected - running on CPU") # Check for 3D pipeline dependencies try: from src.pipelines.opensource_3d_pipeline_v2 import ProductionPipeline logger.info("✅ 3D Generation Pipeline: Available") except ImportError as e: logger.warning(f"⚠️ 3D Generation Pipeline: Not available - {e}") ENV_CONFIG["ENABLE_3D"] = False # Check for AI processor try: from src.ai.qwen_processor import QwenProcessor logger.info("✅ AI Conversation System: Available") except ImportError as e: logger.warning(f"⚠️ AI Conversation System: Not available - {e}") ENV_CONFIG["ENABLE_AI"] = False # Check Gradio version try: import gradio as gr logger.info(f"✅ Gradio Version: {gr.__version__}") except ImportError: logger.error("❌ Gradio not installed!") sys.exit(1) logger.info("=" * 60) def initialize_components(): """Initialize all application components""" components = {} # Initialize AI processor if enabled if ENV_CONFIG["ENABLE_AI"]: try: from src.ai.qwen_processor import QwenProcessor from src.deployment.zero_gpu_optimizer import ZeroGPUOptimizer # Use ZeroGPU optimization for Spaces if IS_SPACES: components["ai_processor"] = QwenProcessor(use_zero_gpu=True) logger.info("AI processor initialized with ZeroGPU optimization") else: components["ai_processor"] = QwenProcessor() logger.info("AI processor initialized") except Exception as e: logger.error(f"Failed to initialize AI processor: {e}") components["ai_processor"] = None else: components["ai_processor"] = None # Initialize 3D pipeline if enabled if ENV_CONFIG["ENABLE_3D"]: try: from src.pipelines.opensource_3d_pipeline_v2 import ProductionConfig # Configure for Spaces if needed config = ProductionConfig() if IS_SPACES: # Optimize for Spaces environment config.image_resolution = 512 config.inference_steps = 4 config.target_polycount = 10000 config.device = "cuda" if torch.cuda.is_available() else "cpu" components["3d_config"] = config logger.info("3D pipeline configuration ready") except Exception as e: logger.error(f"Failed to configure 3D pipeline: {e}") ENV_CONFIG["ENABLE_3D"] = False return components def create_app(components): """Create the Gradio application""" from src.ui.gradio_interface_v2 import DigiPalInterface # Create interface with available components interface = DigiPalInterface( ai_processor=components.get("ai_processor"), enable_3d=ENV_CONFIG["ENABLE_3D"] ) # Create Gradio app app = interface.create_interface() # Add custom footer with app: import gradio as gr gr.Markdown(""" --- ### About DigiPal V2 **Enhanced Features:** - 🎮 Authentic Digimon World 1 mechanics - 🤖 AI-powered conversations with personality - 🎨 Optional 3D model generation - 📊 Complex evolution system - ⏰ Real-time stat degradation - 💀 Mortality and lifespan system **Technologies:** - DW1 reverse engineering insights from SydMontague & Vicen04 - Qwen 2.5 for conversations - Flux + Sparc3D + UniRig for 3D generation (when available) - Gradio 5.34.2 interface Built with ❤️ using the Rick Rubin philosophy: Strip to essentials, amplify emotion. [GitHub](https://github.com/yourusername/digipal) | [Report Issues](https://github.com/yourusername/digipal/issues) """) return app def main(): """Main application entry point""" # Check dependencies check_dependencies() # Initialize components logger.info("Initializing DigiPal components...") components = initialize_components() # Create app logger.info("Creating DigiPal interface...") app = create_app(components) # Launch configuration launch_config = { "server_name": ENV_CONFIG["SERVER_NAME"], "server_port": ENV_CONFIG["SERVER_PORT"], "share": ENV_CONFIG["SHARE"], "max_threads": ENV_CONFIG["MAX_THREADS"], "show_error": ENV_CONFIG["DEBUG"] } # Special handling for HuggingFace Spaces if IS_SPACES: launch_config["server_name"] = "0.0.0.0" launch_config["share"] = False # Spaces handles sharing logger.info("Detected HuggingFace Spaces environment") # Launch app logger.info("=" * 60) logger.info("Launching DigiPal V2...") logger.info(f"Server: {launch_config['server_name']}:{launch_config['server_port']}") logger.info(f"Public URL: {'Enabled' if launch_config['share'] else 'Disabled'}") logger.info(f"3D Generation: {'Enabled' if ENV_CONFIG['ENABLE_3D'] else 'Disabled'}") logger.info(f"AI Conversations: {'Enabled' if ENV_CONFIG['ENABLE_AI'] else 'Disabled'}") logger.info("=" * 60) try: app.launch(**launch_config) except KeyboardInterrupt: logger.info("Shutting down DigiPal...") except Exception as e: logger.error(f"Failed to launch: {e}") if ENV_CONFIG["DEBUG"]: raise # For HuggingFace Spaces app = None if IS_SPACES: # Pre-initialize for Spaces components = initialize_components() app = create_app(components) if __name__ == "__main__": main()