|
from typing import Any, Dict |
|
|
|
import torch |
|
from transformers import AutoModel, AutoProcessor |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.processor = AutoProcessor.from_pretrained("suno/bark-small") |
|
self.model = AutoModel.from_pretrained( |
|
"suno/bark-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. |
|
""" |
|
|
|
text = data.pop("inputs", data) |
|
parameters = data.get("parameters", None) |
|
if parameters is None: |
|
parameters = {} |
|
|
|
inputs = self.processor( |
|
text=[text], |
|
return_tensors="pt", |
|
voice_preset=parameters.get("voice_preset", None), |
|
).to("cuda") |
|
|
|
with torch.autocast("cuda"): |
|
outputs = self.model.generate(**inputs) |
|
|
|
|
|
prediction = outputs.cpu().numpy().tolist() |
|
|
|
return [{"generated_audio": prediction}] |
|
|