|
from typing import Dict, List, Any |
|
from transformers import pipeline |
|
import scipy.io.wavfile |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
self.synthesiser = pipeline("text-generation", "suno/bark") |
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: |
|
text_prompt = data.get("inputs") |
|
if not text_prompt: |
|
raise ValueError("Missing required 'inputs' field in request data.") |
|
|
|
try: |
|
speech = self.synthesiser(text_prompt, forward_params={"do_sample": True}) |
|
audio_data = speech["audio"] |
|
sampling_rate = speech["sampling_rate"] |
|
|
|
|
|
audio_bytes = audio_data.tobytes() |
|
return {"audio": audio_bytes, "sampling_rate": sampling_rate} |
|
|
|
except Exception as e: |
|
|
|
return {"error": str(e)} |
|
|