Instructions to use mmomeni/audiogen-medium with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mmomeni/audiogen-medium with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-to-audio", model="mmomeni/audiogen-medium")# Load model directly from transformers import AutoProcessor, AutoModelForTextToWaveform processor = AutoProcessor.from_pretrained("mmomeni/audiogen-medium") model = AutoModelForTextToWaveform.from_pretrained("mmomeni/audiogen-medium", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 1,548 Bytes
93bba1b cf32753 6777b26 93bba1b 1b7c652 7139a45 93bba1b cf32753 93bba1b cf32753 93bba1b cf32753 93bba1b cf32753 93bba1b cf32753 93bba1b cf32753 93bba1b | 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 | from typing import Dict, Any
from audiocraft.models import AudioGen
# from audiocraft.data.audio import audio_write
class EndpointHandler:
def __init__(self, path = ""):
# Load the AudioGen model
# self.model = AudioGen.get_pretrained('facebook/audiogen-medium')
self.model = AudioGen.get_pretrained(path)
self.model.set_generation_params(duration=5) # Set default duration to 5 seconds
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Args:
data (:dict:):
The payload with the text prompt and generation parameters.
"""
# process input
inputs = data.pop("inputs", data)
parameters = data.pop("parameters", {})
# Update generation parameters if provided
if 'duration' in parameters:
self.model.set_generation_params(duration=parameters['duration'])
# Generate audio from descriptions
descriptions = [inputs]
wav = self.model.generate(descriptions)
# Convert the generated audio to a list format for JSON serialization
predictions = []
for idx, one_wav in enumerate(wav):
# Save the audio to a file (optional)
# audio_write(f'{idx}', one_wav.cpu(), self.model.sample_rate, strategy="loudness", loudness_compressor=True)
# Convert the tensor to a list
prediction = one_wav.cpu().numpy().tolist()
predictions.append(prediction)
return {"generated_audio": predictions}
|