Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import time
|
|
| 7 |
import tempfile
|
| 8 |
import numpy as np
|
| 9 |
from scipy.io.wavfile import write
|
|
|
|
| 10 |
|
| 11 |
# Custom theme for music maker
|
| 12 |
custom_theme = gr.themes.Soft(
|
|
@@ -26,33 +27,40 @@ custom_theme = gr.themes.Soft(
|
|
| 26 |
# Model configuration
|
| 27 |
MODEL_NAME = "facebook/musicgen-small"
|
| 28 |
MODEL_CACHE_DIR = Path.home() / ".cache" / "huggingface" / "musicgen"
|
| 29 |
-
MAX_NEW_TOKENS =
|
| 30 |
-
AUDIO_DURATION =
|
| 31 |
|
| 32 |
-
# Initialize model
|
| 33 |
def load_model():
|
| 34 |
-
"""Load the MusicGen model with caching"""
|
| 35 |
if not os.path.exists(MODEL_CACHE_DIR):
|
| 36 |
os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
|
| 37 |
|
| 38 |
-
print("Loading MusicGen model...")
|
| 39 |
start_time = time.time()
|
| 40 |
|
| 41 |
-
# Load processor
|
| 42 |
processor = AutoProcessor.from_pretrained(
|
| 43 |
MODEL_NAME,
|
| 44 |
cache_dir=MODEL_CACHE_DIR
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# Load model -
|
| 48 |
model = MusicgenForConditionalGeneration.from_pretrained(
|
| 49 |
MODEL_NAME,
|
| 50 |
cache_dir=MODEL_CACHE_DIR,
|
| 51 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
if torch.cuda.is_available():
|
| 55 |
model = model.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
load_time = time.time() - start_time
|
| 58 |
print(f"Model loaded in {load_time:.2f} seconds")
|
|
@@ -67,7 +75,7 @@ def generate_music(prompt, duration, temperature, top_k):
|
|
| 67 |
|
| 68 |
Args:
|
| 69 |
prompt: Text description of the music
|
| 70 |
-
duration: Duration in seconds
|
| 71 |
temperature: Creativity parameter
|
| 72 |
top_k: Sampling parameter
|
| 73 |
|
|
@@ -75,6 +83,11 @@ def generate_music(prompt, duration, temperature, top_k):
|
|
| 75 |
Generated audio file path
|
| 76 |
"""
|
| 77 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Generate music using MusicGen
|
| 79 |
inputs = processor(
|
| 80 |
text=[prompt],
|
|
@@ -82,40 +95,35 @@ def generate_music(prompt, duration, temperature, top_k):
|
|
| 82 |
return_tensors="pt"
|
| 83 |
).to(model.device)
|
| 84 |
|
| 85 |
-
# Generate audio
|
| 86 |
audio_values = model.generate(
|
| 87 |
**inputs,
|
| 88 |
-
max_new_tokens=
|
| 89 |
do_sample=True,
|
| 90 |
temperature=temperature,
|
| 91 |
-
top_k=top_k
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Get sampling rate from processor
|
| 95 |
-
# MusicGen uses Encodec for audio processing
|
| 96 |
sampling_rate = processor.feature_extractor.sampling_rate
|
| 97 |
|
| 98 |
# Convert audio tensor to numpy array
|
| 99 |
-
# MusicGen outputs audio in stereo (2 channels)
|
| 100 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 101 |
|
| 102 |
-
#
|
| 103 |
if len(audio_data.shape) == 1:
|
| 104 |
-
# Mono to stereo conversion
|
| 105 |
audio_data = np.stack([audio_data, audio_data], axis=0)
|
| 106 |
elif audio_data.shape[0] == 1:
|
| 107 |
-
# Single channel to stereo
|
| 108 |
audio_data = np.concatenate([audio_data, audio_data], axis=0)
|
| 109 |
|
| 110 |
-
# Normalize
|
| 111 |
audio_data = audio_data / np.max(np.abs(audio_data)) * 0.9
|
| 112 |
audio_data = (audio_data * 32767).astype(np.int16)
|
| 113 |
|
| 114 |
# Create temporary file
|
| 115 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 116 |
-
|
| 117 |
-
write(temp_file.name, sampling_rate, audio_data.T) # Transpose for stereo format
|
| 118 |
-
|
| 119 |
return temp_file.name
|
| 120 |
|
| 121 |
except Exception as e:
|
|
@@ -129,13 +137,13 @@ def music_maker_interface(prompt, duration, temperature, top_k):
|
|
| 129 |
if not prompt.strip():
|
| 130 |
raise gr.Error("Please enter a music description")
|
| 131 |
|
| 132 |
-
if duration < 5 or duration >
|
| 133 |
-
raise gr.Error("Duration must be between 5 and
|
| 134 |
|
| 135 |
# Show loading state
|
| 136 |
progress = gr.Progress()
|
| 137 |
-
for i in progress.tqdm(range(10), desc="Generating music..."):
|
| 138 |
-
time.sleep(0.
|
| 139 |
|
| 140 |
# Generate music
|
| 141 |
audio_file = generate_music(prompt, duration, temperature, top_k)
|
|
@@ -145,9 +153,9 @@ def music_maker_interface(prompt, duration, temperature, top_k):
|
|
| 145 |
# Create Gradio interface
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
gr.Markdown("""
|
| 148 |
-
# 🎵 AI Music Maker
|
| 149 |
|
| 150 |
-
Create original music from text descriptions using AI!
|
| 151 |
|
| 152 |
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 153 |
""")
|
|
@@ -163,10 +171,10 @@ with gr.Blocks() as demo:
|
|
| 163 |
|
| 164 |
duration = gr.Slider(
|
| 165 |
minimum=5,
|
| 166 |
-
maximum=
|
| 167 |
-
value=
|
| 168 |
-
step=
|
| 169 |
-
label="Duration (seconds)"
|
| 170 |
)
|
| 171 |
|
| 172 |
with gr.Accordion("Advanced Settings", open=False):
|
|
@@ -214,10 +222,11 @@ with gr.Blocks() as demo:
|
|
| 214 |
status = gr.Markdown("Enter a description and click 'Generate Music' to create your track!")
|
| 215 |
model_info = gr.Markdown(f"""
|
| 216 |
### Model Info
|
| 217 |
-
- **Model**: MusicGen Small
|
| 218 |
- **Cache Location**: `{MODEL_CACHE_DIR}`
|
| 219 |
- **Device**: {'CUDA' if torch.cuda.is_available() else 'CPU'}
|
| 220 |
-
- **Max Duration**: {AUDIO_DURATION}s
|
|
|
|
| 221 |
""")
|
| 222 |
|
| 223 |
# Event handlers
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import numpy as np
|
| 9 |
from scipy.io.wavfile import write
|
| 10 |
+
from bitsandbytes import nn as bnb_nn
|
| 11 |
|
| 12 |
# Custom theme for music maker
|
| 13 |
custom_theme = gr.themes.Soft(
|
|
|
|
| 27 |
# Model configuration
|
| 28 |
MODEL_NAME = "facebook/musicgen-small"
|
| 29 |
MODEL_CACHE_DIR = Path.home() / ".cache" / "huggingface" / "musicgen"
|
| 30 |
+
MAX_NEW_TOKENS = 500 # Increased for longer generation
|
| 31 |
+
AUDIO_DURATION = 240 # 4 minutes max
|
| 32 |
|
| 33 |
+
# Initialize model with 4-bit quantization for faster generation
|
| 34 |
def load_model():
|
| 35 |
+
"""Load the MusicGen model with 4-bit quantization and caching"""
|
| 36 |
if not os.path.exists(MODEL_CACHE_DIR):
|
| 37 |
os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
|
| 38 |
|
| 39 |
+
print("Loading MusicGen model with 4-bit quantization...")
|
| 40 |
start_time = time.time()
|
| 41 |
|
| 42 |
+
# Load processor
|
| 43 |
processor = AutoProcessor.from_pretrained(
|
| 44 |
MODEL_NAME,
|
| 45 |
cache_dir=MODEL_CACHE_DIR
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Load model with 4-bit quantization for faster generation
|
| 49 |
model = MusicgenForConditionalGeneration.from_pretrained(
|
| 50 |
MODEL_NAME,
|
| 51 |
cache_dir=MODEL_CACHE_DIR,
|
| 52 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 53 |
+
load_in_4bit=True, # Enable 4-bit quantization
|
| 54 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Optimize for inference
|
| 58 |
if torch.cuda.is_available():
|
| 59 |
model = model.to("cuda")
|
| 60 |
+
# Replace linear layers with 4-bit versions
|
| 61 |
+
for name, module in model.named_modules():
|
| 62 |
+
if isinstance(module, torch.nn.Linear):
|
| 63 |
+
module.__class__ = bnb_nn.Linear4bit
|
| 64 |
|
| 65 |
load_time = time.time() - start_time
|
| 66 |
print(f"Model loaded in {load_time:.2f} seconds")
|
|
|
|
| 75 |
|
| 76 |
Args:
|
| 77 |
prompt: Text description of the music
|
| 78 |
+
duration: Duration in seconds (5-240)
|
| 79 |
temperature: Creativity parameter
|
| 80 |
top_k: Sampling parameter
|
| 81 |
|
|
|
|
| 83 |
Generated audio file path
|
| 84 |
"""
|
| 85 |
try:
|
| 86 |
+
# Calculate tokens needed for the requested duration
|
| 87 |
+
# MusicGen generates at ~50 tokens per second
|
| 88 |
+
tokens_per_second = 50
|
| 89 |
+
max_new_tokens = int(duration * tokens_per_second)
|
| 90 |
+
|
| 91 |
# Generate music using MusicGen
|
| 92 |
inputs = processor(
|
| 93 |
text=[prompt],
|
|
|
|
| 95 |
return_tensors="pt"
|
| 96 |
).to(model.device)
|
| 97 |
|
| 98 |
+
# Generate audio with optimized settings
|
| 99 |
audio_values = model.generate(
|
| 100 |
**inputs,
|
| 101 |
+
max_new_tokens=max_new_tokens,
|
| 102 |
do_sample=True,
|
| 103 |
temperature=temperature,
|
| 104 |
+
top_k=top_k,
|
| 105 |
+
use_cache=True # Enable caching for faster generation
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# Get sampling rate from processor
|
|
|
|
| 109 |
sampling_rate = processor.feature_extractor.sampling_rate
|
| 110 |
|
| 111 |
# Convert audio tensor to numpy array
|
|
|
|
| 112 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 113 |
|
| 114 |
+
# Ensure stereo format
|
| 115 |
if len(audio_data.shape) == 1:
|
|
|
|
| 116 |
audio_data = np.stack([audio_data, audio_data], axis=0)
|
| 117 |
elif audio_data.shape[0] == 1:
|
|
|
|
| 118 |
audio_data = np.concatenate([audio_data, audio_data], axis=0)
|
| 119 |
|
| 120 |
+
# Normalize and convert to 16-bit
|
| 121 |
audio_data = audio_data / np.max(np.abs(audio_data)) * 0.9
|
| 122 |
audio_data = (audio_data * 32767).astype(np.int16)
|
| 123 |
|
| 124 |
# Create temporary file
|
| 125 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 126 |
+
write(temp_file.name, sampling_rate, audio_data.T)
|
|
|
|
|
|
|
| 127 |
return temp_file.name
|
| 128 |
|
| 129 |
except Exception as e:
|
|
|
|
| 137 |
if not prompt.strip():
|
| 138 |
raise gr.Error("Please enter a music description")
|
| 139 |
|
| 140 |
+
if duration < 5 or duration > 240:
|
| 141 |
+
raise gr.Error("Duration must be between 5 and 240 seconds (4 minutes)")
|
| 142 |
|
| 143 |
# Show loading state
|
| 144 |
progress = gr.Progress()
|
| 145 |
+
for i in progress.tqdm(range(10), desc=f"Generating {duration} second music..."):
|
| 146 |
+
time.sleep(0.2) # Faster progress for 4-bit model
|
| 147 |
|
| 148 |
# Generate music
|
| 149 |
audio_file = generate_music(prompt, duration, temperature, top_k)
|
|
|
|
| 153 |
# Create Gradio interface
|
| 154 |
with gr.Blocks() as demo:
|
| 155 |
gr.Markdown("""
|
| 156 |
+
# 🎵 AI Music Maker - Extended Edition
|
| 157 |
|
| 158 |
+
Create original music from text descriptions using AI! Now with 4-bit quantization for faster generation and support for songs up to 4 minutes long.
|
| 159 |
|
| 160 |
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 161 |
""")
|
|
|
|
| 171 |
|
| 172 |
duration = gr.Slider(
|
| 173 |
minimum=5,
|
| 174 |
+
maximum=240,
|
| 175 |
+
value=30,
|
| 176 |
+
step=5,
|
| 177 |
+
label="Duration (seconds) - Up to 4 minutes!"
|
| 178 |
)
|
| 179 |
|
| 180 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 222 |
status = gr.Markdown("Enter a description and click 'Generate Music' to create your track!")
|
| 223 |
model_info = gr.Markdown(f"""
|
| 224 |
### Model Info
|
| 225 |
+
- **Model**: MusicGen Small (4-bit quantized)
|
| 226 |
- **Cache Location**: `{MODEL_CACHE_DIR}`
|
| 227 |
- **Device**: {'CUDA' if torch.cuda.is_available() else 'CPU'}
|
| 228 |
+
- **Max Duration**: {AUDIO_DURATION}s (4 minutes)
|
| 229 |
+
- **Generation Speed**: ~2x faster with 4-bit quantization
|
| 230 |
""")
|
| 231 |
|
| 232 |
# Event handlers
|