File size: 1,425 Bytes
e990dfa | 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 | import torch
from transformers import AutoProcessor, MusicgenForConditionalGeneration
import soundfile as sf
import uuid
MODEL_NAME = "facebook/musicgen-small"
processor = None
model = None
device = "cuda" if torch.cuda.is_available() else "cpu"
model_status = "not_loaded"
model_error = None
def _get_model():
global processor, model, model_status, model_error
if processor is None or model is None:
try:
model_status = "loading"
model_error = None
print("Loading MusicGen model...")
processor = AutoProcessor.from_pretrained(MODEL_NAME)
model = MusicgenForConditionalGeneration.from_pretrained(MODEL_NAME)
model.to(device)
model_status = "ready"
except Exception as exc:
model_status = "failed"
model_error = str(exc)
raise
return processor, model
def generate_music(prompt, duration):
processor, model = _get_model()
inputs = processor(
text=[prompt],
padding=True,
return_tensors="pt"
).to(device)
audio_values = model.generate(
**inputs,
max_new_tokens=int(duration * 50)
)
filename = f"/tmp/{uuid.uuid4()}.wav"
sampling_rate = model.config.audio_encoder.sampling_rate
sf.write(
filename,
audio_values[0, 0].cpu().numpy(),
sampling_rate
)
return filename
|