File size: 1,090 Bytes
3e55f57
a1e58ca
4f9c202
3e55f57
f067669
 
3e55f57
4f9c202
3e55f57
4f9c202
 
 
 
6eea13c
4f9c202
30769ce
4f9c202
30769ce
4f9c202
 
62f977c
4f9c202
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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")  # Attempt to create pipeline

    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:
            print(self.synthesiser)
            speech = self.synthesiser(text_prompt, forward_params={"do_sample": True})
            print(speech)
            audio_data = speech["audio"]  # Assuming audio is in a NumPy array
            sampling_rate = speech["sampling_rate"]

            # Return audio data as a byte string (adjust format as needed)
            audio_bytes = audio_data.tobytes()
            return {"audio": audio_bytes, "sampling_rate": sampling_rate}

        except Exception as e:
            # Handle potential errors with model loading or usage
            return {"error": str(e)}