#!/usr/bin/env python3 """ MiloMusic - Hugging Face Spaces Version AI-powered music generation platform optimized for cloud deployment with high-performance configuration. """ import multiprocessing import os import sys import subprocess import tempfile import gradio as gr import soundfile as sf from dataclasses import dataclass, field from typing import Any import xxhash import numpy as np import spaces import groq # Import environment setup for Spaces def setup_spaces_environment(): """Setup environment variables and paths for Hugging Face Spaces""" # Set HuggingFace cache directory os.environ["HF_HOME"] = "/tmp/hf_cache" os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache" os.environ["HF_HUB_CACHE"] = "/tmp/hf_hub_cache" # 1.PyTorch CUDA memory optimization 2.用PyTorch的可扩展内存段分配, 提高GPU内存使用效率, 减少内存碎片问题 os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True" # Set temp directory for audio files os.environ["TMPDIR"] = "/tmp" print("🚀 Environment setup complete for Spaces") # Install flash-attn if not already installed def install_flash_attn() -> bool: """Install flash-attn from source with proper compilation flags""" try: import flash_attn print("✅ flash-attn already installed") return True except ImportError: print("📦 Installing flash-attn from source...") try: # Install with optimized settings for Spaces cmd = [ sys.executable, "-m", "pip", "install", "--no-build-isolation", "--no-cache-dir", "flash-attn", "--verbose" ] # Use more parallel jobs for faster compilation in Spaces env = os.environ.copy() max_jobs = min(4, multiprocessing.cpu_count()) # Utilize more CPU cores env["MAX_JOBS"] = str(max_jobs) env["NVCC_PREPEND_FLAGS"] = "-ccbin /usr/bin/gcc" result = subprocess.run(cmd, env=env, capture_output=True, text=True, timeout=1800) # 30 min timeout if result.returncode == 0: print("✅ flash-attn installed successfully") return True else: print(f"❌ flash-attn installation failed: {result.stderr}") return False except subprocess.TimeoutExpired: print("⏰ flash-attn installation timed out") return False except Exception as e: print(f"❌ Error installing flash-attn: {e}") return False # Setup environment first setup_spaces_environment() # Download required models for YuEGP inference def download_required_models(): """Download required model files at startup""" try: from download_models import ensure_model_availability print("🚀 Checking and downloading required models...") success = ensure_model_availability() if success: print("✅ Model setup completed successfully") else: print("⚠️ Some models may be missing - continuing with available resources") return success except ImportError as e: print(f"⚠️ Model download script not found: {e}") return False except Exception as e: print(f"❌ Error during model download: {e}") return False # Download models before other setup models_ready = download_required_models() # Install flash-attn if needed flash_attn_available = install_flash_attn() # Apply transformers patches for performance optimization def apply_transformers_patch(): """ Apply YuEGP transformers patches for high-performance generation. This function applies optimized transformers patches that provide: - 2x speed improvement for low VRAM profiles - 3x speed improvement for Stage 1 generation (16GB+ VRAM) - 2x speed improvement for Stage 2 generation (all profiles) The patches replace two key files in the transformers library: - models/llama/modeling_llama.py (LLaMA model optimizations) - generation/utils.py (generation utilities optimizations) Includes smart detection to avoid re-applying patches on restart. """ try: import shutil import site import hashlib # Define source and target directories source_dir = os.path.join(project_root, "YuEGP", "transformers") # Get the site-packages directory where transformers is installed site_packages = site.getsitepackages() if not site_packages: # Fallback for some environments import transformers transformers_path = os.path.dirname(transformers.__file__) target_base = os.path.dirname(transformers_path) else: target_base = site_packages[0] target_dir = os.path.join(target_base, "transformers") # Check if source patches exist if not os.path.exists(source_dir): print("⚠️ YuEGP transformers patches not found, skipping optimization") return False if not os.path.exists(target_dir): print("⚠️ Transformers library not found, skipping patches") return False # Check if patches are already applied by comparing file hashes def get_file_hash(filepath): """Get MD5 hash of file content""" if not os.path.exists(filepath): return None with open(filepath, 'rb') as f: return hashlib.md5(f.read()).hexdigest() # Key files to check for patch status key_patches = [ "models/llama/modeling_llama.py", "generation/utils.py" ] patches_needed = False for patch_file in key_patches: source_file = os.path.join(source_dir, patch_file) target_file = os.path.join(target_dir, patch_file) if os.path.exists(source_file): source_hash = get_file_hash(source_file) target_hash = get_file_hash(target_file) if source_hash != target_hash: patches_needed = True break if not patches_needed: print("✅ YuEGP transformers patches already applied, skipping re-installation") print(" 📈 High-performance optimizations are active:") print(" • Stage 1 generation: 3x faster (16GB+ VRAM)") print(" • Stage 2 generation: 2x faster (all profiles)") return True # Apply patches by copying optimized files print("🔧 Applying YuEGP transformers patches for high-performance generation...") # Copy the patched files, preserving directory structure for root, dirs, files in os.walk(source_dir): # Calculate relative path from source_dir rel_path = os.path.relpath(root, source_dir) target_subdir = os.path.join(target_dir, rel_path) if rel_path != '.' else target_dir # Ensure target subdirectory exists os.makedirs(target_subdir, exist_ok=True) # Copy all Python files in this directory for file in files: if file.endswith('.py'): src_file = os.path.join(root, file) dst_file = os.path.join(target_subdir, file) shutil.copy2(src_file, dst_file) print(f" ✅ Patched: {os.path.relpath(dst_file, target_base)}") print("🚀 Transformers patches applied successfully!") print(" 📈 Expected performance gains:") print(" • Stage 1 generation: 3x faster (16GB+ VRAM)") print(" • Stage 2 generation: 2x faster (all profiles)") return True except Exception as e: print(f"❌ Error applying transformers patches: {e}") print(" Continuing without patches - performance may be reduced") return False # Now import the rest of the dependencies # Add project root to Python path for imports project_root = os.path.dirname(os.path.abspath(__file__)) if project_root not in sys.path: sys.path.insert(0, project_root) from tools.groq_client import client as groq_client from openai import OpenAI from tools.generate_lyrics import generate_structured_lyrics, format_lyrics # Apply patches after all imports are set up patch_applied = apply_transformers_patch() # Import CUDA info after flash-attn setup import torch if torch.cuda.is_available(): print(f"🎮 GPU: {torch.cuda.get_device_name(0)}") print(f"💾 VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB") else: print("⚠️ No CUDA GPU detected") @dataclass class AppState: """ Maintains the application state throughout user interactions. """ conversation: list = field(default_factory=list) stopped: bool = False model_outs: Any = None lyrics: str = "" genre: str = "pop" mood: str = "upbeat" theme: str = "love" def validate_api_keys(): """Validate required API keys for Spaces deployment""" required_keys = ["GROQ_API_KEY", "GEMINI_API_KEY"] missing_keys = [] for key in required_keys: if not os.getenv(key): missing_keys.append(key) if missing_keys: print(f"⚠️ Missing API keys: {missing_keys}") return False print("✅ All API keys validated") return True def validate_file_structure(): """Validate that required files and directories exist""" required_paths = [ "YuEGP/inference/infer.py", "YuEGP/inference/codecmanipulator.py", "YuEGP/inference/mmtokenizer.py", "tools/generate_lyrics.py", "tools/groq_client.py", "schemas/lyrics.py" # Required for lyrics structure models ] missing_files = [] for path in required_paths: if not os.path.exists(path): missing_files.append(path) if missing_files: print(f"⚠️ Missing required files: {missing_files}") return False print("✅ All required files found") return True @spaces.GPU(duration=1200) # H200 on ZeroGPU is free for 25mins, for compatibility on A10G large and L40s def generate_music_spaces(lyrics: str, genre: str, mood: str, progress=gr.Progress()) -> str: """ Generate music using YuE model with high-performance Spaces configuration """ if not lyrics.strip(): return "Please provide lyrics to generate music." try: progress(0.1, desc="Preparing lyrics...") # Use lyrics directly (already formatted from chat interface) formatted_lyrics = lyrics # Create temporary files with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as genre_file: genre_file.write(f"instrumental,{genre},{mood},male vocals") genre_file_path = genre_file.name # Convert lyrics format for YuEGP compatibility # YuEGP expects [VERSE], [CHORUS] format, but our AI generates **VERSE**, **CHORUS** import re # Extract only the actual lyrics content, removing AI commentary formatted_lyrics_for_yue = formatted_lyrics # Convert **VERSE 1** to [VERSE], **CHORUS** to [CHORUS], etc. formatted_lyrics_for_yue = re.sub(r'\*\*(VERSE\s*\d*)\*\*', r'[\1]', formatted_lyrics_for_yue) formatted_lyrics_for_yue = re.sub(r'\*\*(CHORUS)\*\*', r'[\1]', formatted_lyrics_for_yue) formatted_lyrics_for_yue = re.sub(r'\*\*(BRIDGE)\*\*', r'[\1]', formatted_lyrics_for_yue) formatted_lyrics_for_yue = re.sub(r'\*\*(OUTRO)\*\*', r'[\1]', formatted_lyrics_for_yue) # Remove AI commentary (lines that don't contain actual lyrics) lines = formatted_lyrics_for_yue.split('\n') clean_lines = [] in_song = False for line in lines: line = line.strip() # Start collecting from first section marker if re.match(r'\[(VERSE|CHORUS|BRIDGE|OUTRO)', line): in_song = True # Stop at AI commentary if in_song and line and not line.startswith('[') and any(phrase in line.lower() for phrase in ['how do you like', 'would you like', 'let me know', 'take a look']): break if in_song: clean_lines.append(line) formatted_lyrics_for_yue = '\n'.join(clean_lines).strip() print(f"🐛 DEBUG - Original lyrics length: {len(formatted_lyrics)}") print(f"🐛 DEBUG - Converted lyrics for YuE: '{formatted_lyrics_for_yue}'") print(f"🐛 DEBUG - Converted lyrics length: {len(formatted_lyrics_for_yue)}") with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as lyrics_file: lyrics_file.write(formatted_lyrics_for_yue) lyrics_file_path = lyrics_file.name progress(0.2, desc="Setting up generation...") # Generate music with high-performance Spaces configuration output_dir = tempfile.mkdtemp() # High-performance command based on Spaces GPU resources # In Spaces, working directory is /app infer_script_path = os.path.join(os.getcwd(), "YuEGP", "inference", "infer.py") cmd = [ sys.executable, infer_script_path, "--cuda_idx", "0", "--stage1_model", "m-a-p/YuE-s1-7B-anneal-en-cot", "--stage2_model", "m-a-p/YuE-s2-1B-general", "--genre_txt", genre_file_path, "--lyrics_txt", lyrics_file_path, "--run_n_segments", "2", # Full segments for better quality "--stage2_batch_size", "4", # Higher batch size for speed "--output_dir", output_dir, "--max_new_tokens", "3000", # Full token count "--profile", "1", # Highest performance profile "--verbose", "3", "--rescale", # Enable audio rescaling to proper volume "--prompt_start_time", "0", "--prompt_end_time", "30", # Full 30-second clips ] # Use flash attention if available, otherwise fallback if not flash_attn_available: cmd.append("--sdpa") # More detailed progress updates progress(0.1, desc="🚀 Initializing models...") progress(0.15, desc="📝 Processing lyrics...") progress(0.2, desc="🎵 Starting Stage 1 (7B model generation)...") # Extract parameters from cmd for logging run_n_segments = cmd[cmd.index("--run_n_segments") + 1] if "--run_n_segments" in cmd else "2" max_new_tokens = cmd[cmd.index("--max_new_tokens") + 1] if "--max_new_tokens" in cmd else "3000" print("🎵 Starting high-quality music generation...") print(f"📊 Generation settings: {run_n_segments} segments, {max_new_tokens} tokens, 30s audio") print(f"⏱️ Estimated time: 8-9 minutes for high-quality generation") print(f"Working directory: {os.getcwd()}") print(f"Command: {' '.join(cmd)}") # Change to YuEGP/inference directory for execution original_cwd = os.getcwd() inference_dir = os.path.join(os.getcwd(), "YuEGP", "inference") try: os.chdir(inference_dir) print(f"Changed to inference directory: {inference_dir}") cmd[1] = "infer.py" progress(0.25, desc="🔥 Stage 1: Running 7B parameter model...") # Start the subprocess import threading import time def parse_output_and_update_progress(process): """Parse subprocess output in real-time and update progress accordingly""" stage1_messages = [ "🧠 Stage 1: Generating musical concepts...", "🎼 Stage 1: Creating melody patterns...", "🎹 Stage 1: Composing harmony structure..." ] stage2_messages = [ "⚡ Starting Stage 2: Refining with 1B model...", "🎵 Stage 2: Adding musical details...", "🎶 Stage 2: Finalizing composition..." ] stage1_progress = [0.3, 0.45, 0.6] stage2_progress = [0.7, 0.8, 0.85] current_stage = 1 stage1_step = 0 stage2_step = 0 output_lines = [] try: while True: line = process.stdout.readline() if not line: break line = line.strip() output_lines.append(line) print(line) # Still print for debugging # Check for stage transitions based on actual output if "Stage 2 inference..." in line: current_stage = 2 stage2_step = 0 progress(0.7, desc=stage2_messages[0]) print(f"⏳ {stage2_messages[0]}") elif "Stage 2 DONE" in line: progress(0.9, desc="🔊 Decoding to audio format...") print("⏳ 🔊 Decoding to audio format...") # Update Stage 1 progress periodically elif current_stage == 1 and stage1_step < len(stage1_messages): # Update Stage 1 progress every 15 seconds or on specific markers if stage1_step < len(stage1_progress): progress(stage1_progress[stage1_step], desc=stage1_messages[stage1_step]) print(f"⏳ {stage1_messages[stage1_step]}") stage1_step += 1 # Update Stage 2 progress periodically elif current_stage == 2 and stage2_step < len(stage2_messages) - 1: stage2_step += 1 if stage2_step < len(stage2_progress): progress(stage2_progress[stage2_step], desc=stage2_messages[stage2_step]) print(f"⏳ {stage2_messages[stage2_step]}") except Exception as e: print(f"Progress parsing error: {e}") return '\n'.join(output_lines) print(f"🚀 Executing command: {' '.join(cmd)}") # Use Popen for real-time output processing process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True) # Parse output in real-time stdout_output = parse_output_and_update_progress(process) # Wait for process to complete and get return code return_code = process.wait() # Create result object similar to subprocess.run class Result: def __init__(self, returncode, stdout, stderr=""): self.returncode = returncode self.stdout = stdout self.stderr = stderr result = Result(return_code, stdout_output) # Print stdout and stderr for debugging if result.stdout: print(f"✅ Command output:\n{result.stdout}") if result.stderr: print(f"⚠️ Command stderr:\n{result.stderr}") print(f"📊 Return code: {result.returncode}") finally: os.chdir(original_cwd) progress(0.95, desc="🎉 Processing completed, finalizing output...") # Clean up input files os.unlink(genre_file_path) os.unlink(lyrics_file_path) if result.returncode == 0: # Find generated audio file - prioritize mixed audio from vocoder/mix directory import glob final_files = glob.glob(os.path.join(output_dir, "*_mixed.mp3")) if final_files: progress(1.0, desc="Finish music generation") print(f"✅ Found audio file at root: {final_files[0]}") return final_files[0] # First look for the final mixed audio in vocoder/mix mixed_files = glob.glob(os.path.join(output_dir, "vocoder/mix/*_mixed.mp3")) if mixed_files: progress(1.0, desc="Music generation complete!") print(f"✅ Found mixed audio file: {mixed_files[0]}") return mixed_files[0] # Fallback to any MP3 file audio_files = glob.glob(os.path.join(output_dir, "**/*.mp3"), recursive=True) if audio_files: progress(1.0, desc="Music generation complete!") print(f"✅ Found audio file: {audio_files[0]}") return audio_files[0] # Return path to generated audio else: print(f"❌ No audio files found in {output_dir}") print(f"Directory contents: {os.listdir(output_dir) if os.path.exists(output_dir) else 'Directory not found'}") return "Music generation completed but no audio file found." else: error_msg = f"Return code: {result.returncode}\n" if result.stderr: error_msg += f"Error: {result.stderr[-1000:]}\n" if result.stdout: error_msg += f"Output: {result.stdout[-1000:]}" return f"Music generation failed:\n{error_msg}" except subprocess.TimeoutExpired: return "Music generation timed out after 20 minutes. Please try again." except Exception as e: return f"Error during music generation: {str(e)}" def respond(message, state): """Enhanced response function for conversational lyrics generation""" try: # Add user message to conversation state.conversation.append({"role": "user", "content": message}) # Use conversational generation logic (same as voice input) response = generate_chat_completion(groq_client, state.conversation, state.genre, state.mood, state.theme) # Add assistant response state.conversation.append({"role": "assistant", "content": response}) # Update lyrics with improved format recognition - extract only segments if any(marker in response.lower() for marker in ["[verse", "[chorus", "[bridge", "**verse", "**chorus", "sectiontype.verse", "verse:"]): state.lyrics = extract_lyrics_segments_only(response) # Format conversation for display return "", [{"role": msg["role"], "content": msg["content"]} for msg in state.conversation], state except Exception as e: error_response = f"Sorry, I encountered an error: {str(e)}" state.conversation.append({"role": "assistant", "content": error_response}) return "", [{"role": msg["role"], "content": msg["content"]} for msg in state.conversation], state def build_interface(): """Build the Gradio interface optimized for Spaces with high performance""" with gr.Blocks( title="MiloMusic - AI Music Generation", theme=gr.themes.Soft(), css=""" .container { max-width: 1400px; margin: auto; } .performance-notice { background-color: #d4edda; padding: 15px; border-radius: 5px; margin: 10px 0; } .generation-status { background-color: #f8f9fa; padding: 10px; border-radius: 5px; } """ ) as demo: # Header gr.Markdown(""" # 🎵 MiloMusic - AI Music Generation ### Professional AI-powered music creation from natural language """) # Performance notice for Spaces gr.Markdown("""