-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmusicgen_api.py
More file actions
271 lines (220 loc) · 8.95 KB
/
musicgen_api.py
File metadata and controls
271 lines (220 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
MusicGen API Integration Module
This module provides integration with Meta's MusicGen model for music generation.
"""
import os
import time
import torch
import scipy
import numpy as np
from datetime import datetime
from transformers import MusicgenForConditionalGeneration, AutoProcessor
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Global variable to cache the model
_cached_model = None
_cached_processor = None
def load_musicgen_model(model_size='small', device=None):
"""
Load and cache the MusicGen model.
Args:
model_size (str): Model size - 'small', 'medium', or 'large'
device (str): Device to use ('cuda', 'cpu', or None for auto-detect)
Returns:
tuple: (model, processor, device)
"""
global _cached_model, _cached_processor
if _cached_model is None or _cached_processor is None:
logger.info(f"Loading MusicGen model (size: {model_size})...")
# Model name mapping
model_names = {
'small': 'facebook/musicgen-small',
'medium': 'facebook/musicgen-medium',
'large': 'facebook/musicgen-large',
'melody': 'facebook/musicgen-melody'
}
model_name = model_names.get(model_size, 'facebook/musicgen-small')
try:
# Load model and processor
_cached_model = MusicgenForConditionalGeneration.from_pretrained(model_name)
_cached_processor = AutoProcessor.from_pretrained(model_name)
# Determine device
if device is None:
device = "cuda:0" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
# Move model to device
_cached_model.to(device)
logger.info(f"Model loaded successfully on {device}")
except Exception as e:
logger.error(f"Failed to load model: {str(e)}")
raise
return _cached_model, _cached_processor, device
def generate_music_with_musicgen(prompt, duration=10, temperature=1.0, top_k=250, top_p=0.9,
guidance_scale=3.0, model_size='small'):
"""
Generate music using Meta's MusicGen model.
Args:
prompt (str): Text description of the desired music
duration (float): Duration of the generated music in seconds
temperature (float): Sampling temperature (higher = more random)
top_k (int): Top-k sampling parameter
top_p (float): Top-p (nucleus) sampling parameter
guidance_scale (float): Classifier-free guidance scale
model_size (str): Model size to use ('small', 'medium', 'large')
Returns:
dict: Contains 'filename', 'filepath', and generation metadata
"""
try:
# Load model and processor
model, processor, device = load_musicgen_model(model_size)
logger.info(f"Generating music with prompt: '{prompt}'")
logger.info(f"Parameters: duration={duration}s, temperature={temperature}, guidance_scale={guidance_scale}")
# Calculate max_new_tokens based on duration
# MusicGen uses ~50 tokens per second of audio
max_new_tokens = int(duration * 50)
# Prepare inputs
inputs = processor(
text=[prompt],
padding=True,
return_tensors="pt",
)
# Move inputs to device
if device != "cpu":
inputs = inputs.to(device)
# Generate audio
with torch.no_grad():
audio_values = model.generate(
**inputs,
do_sample=True,
guidance_scale=guidance_scale,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_k=top_k,
top_p=top_p
)
# Get sampling rate
sampling_rate = model.config.audio_encoder.sampling_rate
# Convert to numpy and move to CPU
audio_array = audio_values[0, 0].cpu().numpy()
# Generate filename with timestamp
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"generated_{timestamp}.wav"
filepath = os.path.join('music', filename)
# Ensure music directory exists
os.makedirs('music', exist_ok=True)
# Save audio file
scipy.io.wavfile.write(filepath, rate=sampling_rate, data=audio_array)
logger.info(f"Music generated successfully: {filename}")
return {
'filename': filename,
'filepath': filepath,
'sample_rate': sampling_rate,
'duration': duration,
'prompt': prompt,
'model_size': model_size,
'temperature': temperature,
'guidance_scale': guidance_scale
}
except Exception as e:
logger.error(f"Generation failed: {str(e)}")
raise Exception(f"Music generation failed: {str(e)}")
def generate_with_audio_prompt(audio_array, sampling_rate, text_prompt=None,
duration=10, guidance_scale=3.0, model_size='small'):
"""
Generate music continuation based on an audio prompt.
Args:
audio_array (np.ndarray): Input audio array
sampling_rate (int): Sampling rate of the input audio
text_prompt (str): Optional text prompt to guide generation
duration (float): Duration of additional music to generate
guidance_scale (float): Classifier-free guidance scale
model_size (str): Model size to use
Returns:
dict: Contains generated audio information
"""
try:
# Load model and processor
model, processor, device = load_musicgen_model(model_size)
logger.info("Generating music with audio prompt")
# Calculate max_new_tokens
max_new_tokens = int(duration * 50)
# Prepare inputs
if text_prompt:
inputs = processor(
audio=audio_array,
sampling_rate=sampling_rate,
text=[text_prompt],
padding=True,
return_tensors="pt",
)
else:
inputs = processor(
audio=audio_array,
sampling_rate=sampling_rate,
padding=True,
return_tensors="pt",
)
# Move inputs to device
if device != "cpu":
inputs = inputs.to(device)
# Generate audio
with torch.no_grad():
audio_values = model.generate(
**inputs,
do_sample=True,
guidance_scale=guidance_scale,
max_new_tokens=max_new_tokens
)
# Process output
if hasattr(processor, 'batch_decode'):
audio_values = processor.batch_decode(audio_values, padding_mask=inputs.padding_mask)
audio_array = audio_values[0]
else:
audio_array = audio_values[0, 0].cpu().numpy()
# Save audio file
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"continued_{timestamp}.wav"
filepath = os.path.join('music', filename)
scipy.io.wavfile.write(filepath, rate=model.config.audio_encoder.sampling_rate, data=audio_array)
return {
'filename': filename,
'filepath': filepath,
'sample_rate': model.config.audio_encoder.sampling_rate,
'duration': duration,
'text_prompt': text_prompt,
'type': 'audio_continuation'
}
except Exception as e:
logger.error(f"Audio continuation failed: {str(e)}")
raise
def get_available_models():
"""
Return list of available MusicGen models.
"""
return [
{'name': 'musicgen-small', 'size': '300M', 'description': 'Fast generation, good quality'},
{'name': 'musicgen-medium', 'size': '1.5GB', 'description': 'Balanced speed and quality'},
{'name': 'musicgen-large', 'size': '3.3GB', 'description': 'Best quality, slower generation'},
{'name': 'musicgen-melody', 'size': '1.5GB', 'description': 'Melody-conditioned generation'}
]
def test_musicgen():
"""
Test function to verify MusicGen is working.
"""
try:
logger.info("Testing MusicGen installation...")
# Test with a simple prompt
result = generate_music_with_musicgen(
prompt="A peaceful piano melody",
duration=5,
model_size='small'
)
logger.info(f"Test successful! Generated: {result['filename']}")
return True
except Exception as e:
logger.error(f"Test failed: {str(e)}")
return False
if __name__ == "__main__":
# Run test when module is executed directly
test_musicgen()