File size: 1,680 Bytes
b36a86c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from os import getenv

import requests

from utils.functions import play, stream, save

ELEVEN_API_KEY = getenv("ELEVEN_API_KEY")
CHUNK_SIZE = 1024
ELEVENLABS_STREAM_ENDPOINT = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream?optimize_streaming_latency=3"
ELEVENLABS_ENDPOINT = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"


voices = {
    "Bella": "EXAVITQu4vr4xnSDxMaL",
    "Dorothy": "ThT5KcBeYPX3keUQqHPh",
    "Male": "onwK4e9ZLuTAKqWW03F9",
    "Chimamanda": "QSKN4kAq766BnZ0ilL0L",
    "Ruth": "o9iLaGDMP3YCJcZevdfB",
    "Ifeanyi": "iQe5hWADpVlprlflH1k8",
}


class TTS:
    def __init__(self, voice_id):
        self.voice_id = voice_id
        self.headers = {
            "Accept": "audio/mpeg",
            "Content-Type": "application/json",
            "xi-api-key": ELEVEN_API_KEY,
        }

    def generate(self, text, stream_: bool = False, model="eleven_monolingual_v1"):
        data = {
            "text": text,
            "model_id": model,
            "voice_settings": {"stability": 0.5, "similarity_boost": 0.0},
        }

        url = (
            ELEVENLABS_STREAM_ENDPOINT.format(voice_id=self.voice_id)
            if stream_
            else ELEVENLABS_STREAM_ENDPOINT.format(voice_id=self.voice_id)
        )
        response = requests.post(
            url,
            json=data,
            headers=self.headers,
            stream=stream_,
        )

        if stream_:
            audio_stream = (
                chunk for chunk in response.iter_content(chunk_size=CHUNK_SIZE) if chunk
            )
            return audio_stream
        else:
            audio = response.content
            return audio