Instructions to use KlangGames/musicgen-medium with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use KlangGames/musicgen-medium with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-to-audio", model="KlangGames/musicgen-medium")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForTextToWaveform processor = AutoProcessor.from_pretrained("KlangGames/musicgen-medium") model = AutoModelForTextToWaveform.from_pretrained("KlangGames/musicgen-medium", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from KlangGames/musicgen-medium: direct link, hf CLI and curl.
- Browser
- Download file 918 Bytes
-
https://huggingface.co/KlangGames/musicgen-medium/resolve/main/handler.py
- Command line
-
hf download hf://KlangGames/musicgen-medium/handler.py
-
curl -L -o handler.py https://huggingface.co/KlangGames/musicgen-medium/resolve/main/handler.py
918 Bytes
| from audiocraft.data.audio_utils import convert_audio | |
| from audiocraft.data.audio import audio_write | |
| from audiocraft.models import MusicGen | |
| from typing import Dict, List, Any | |
| import logging | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| self.model = MusicGen.get_pretrained("musicgen-medium") | |
| def __call__(self, data: Dict[str, Any]) -> Dict[str, str]: | |
| """ | |
| Args: | |
| data (:dict:): | |
| The payload with the text prompt and generation parameters. | |
| """ | |
| # process input | |
| logging.info(f"data: {data}") | |
| inputs = data.pop("inputs", data) | |
| parameters = data.pop("parameters", None) | |
| self.model.set_generation_params(**parameters) | |
| outputs = self.model.generate(**inputs) | |
| # postprocess the prediction | |
| prediction = outputs[0].cpu().numpy().tolist() | |
| return [{"generated_audio": prediction}] | |