| | from typing import Dict, List, Any |
| | from transformers import AutoProcessor, MusicgenForConditionalGeneration |
| | import torch |
| |
|
| | class EndpointHandler: |
| | def __init__(self, path=""): |
| | |
| | self.processor = AutoProcessor.from_pretrained("facebook/musicgen-small") |
| | self.model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small").to("cuda") |
| |
|
| | def __call__(self, data: Dict[str, Any]) -> Dict[str, str]: |
| | """ |
| | Args: |
| | data (:dict:): |
| | The payload with the text prompt and generation parameters. |
| | """ |
| | |
| | inputs = data.pop("inputs", data) |
| | parameters = data.pop("parameters", None) |
| |
|
| | |
| | inputs = processor( |
| | text=inputs, |
| | padding=True, |
| | return_tensors="pt",) |
| |
|
| | |
| | if parameters is not None: |
| | outputs = self.model.generate(inputs, max_new_tokens=256, **parameters) |
| | else: |
| | outputs = self.model.generate(inputs, max_new_tokens=256) |
| |
|
| | |
| | prediction = outputs[0].numpy() |
| |
|
| | return [{"generated_audio": prediction}] |