Instructions to use derrickdso/samplegen-small with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use derrickdso/samplegen-small with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-to-audio", model="derrickdso/samplegen-small")# Load model directly from transformers import AutoTokenizer, AutoModelForTextToWaveform tokenizer = AutoTokenizer.from_pretrained("derrickdso/samplegen-small") model = AutoModelForTextToWaveform.from_pretrained("derrickdso/samplegen-small") - Notebooks
- Google Colab
- Kaggle
| from typing import Dict, List, Any | |
| from transformers import AutoProcessor, MusicgenForConditionalGeneration | |
| import torch | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| # load model and processor from path | |
| self.processor = AutoProcessor.from_pretrained(path) | |
| self.model = MusicgenForConditionalGeneration.from_pretrained(path, torch_dtype=torch.float16).to("cuda") | |
| def __call__(self, data: Dict[str, Any]) -> Dict[str, str]: | |
| """ | |
| Args: | |
| data (:dict:): | |
| The payload with the text prompt and generation parameters. | |
| """ | |
| # process input | |
| inputs = data.pop("inputs", data) | |
| parameters = data.pop("parameters", None) | |
| # preprocess | |
| inputs = self.processor( | |
| text=[inputs], | |
| padding=True, | |
| return_tensors="pt",).to("cuda") | |
| # pass inputs with all kwargs in data | |
| if parameters is not None: | |
| with torch.autocast("cuda"): | |
| outputs = self.model.generate(**inputs, **parameters) | |
| else: | |
| with torch.autocast("cuda"): | |
| outputs = self.model.generate(**inputs,) | |
| # postprocess the prediction | |
| prediction = outputs[0].cpu().numpy().tolist() | |
| return [{"generated_audio": prediction}] |