File size: 2,121 Bytes
1a63d97
 
9202468
 
 
 
87dcd10
9202468
 
1a63d97
b49fa2f
 
9202468
 
69f88db
 
9202468
 
69f88db
 
 
 
 
 
 
 
 
 
 
 
9202468
bd435b3
 
 
 
 
9202468
 
 
4385b66
 
 
 
 
730fe87
 
4385b66
 
 
87dcd10
 
1a63d97
 
 
 
 
 
 
 
 
 
 
87dcd10
1a63d97
 
87dcd10
 
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
60
61
62
63
64
65
66
67
68
import asyncio
import itertools
import os
from elevenlabs import generate, play
from elevenlabs import set_api_key
from elevenlabs import generate, stream
from agent_response import AgentResponse


class TextToSpeechService:
    def __init__(self, voice_id="Bella", model_id="eleven_monolingual_v1"):
    # def __init__(self, voice_id="Bella", model_id="eleven_english_v2"):
        account_sid = os.environ["ELEVENLABS_API_KEY"]
        set_api_key(account_sid)
        self._voice_id = voice_id
        self._model_id = model_id


    # def print_models(self):
    #     models = generate()
    #     for model in models:
    #         print (model["id"], model["name"])


    def print_voices(self):
        from elevenlabs.api import Voices
        voices = Voices.from_api()
        for voice in voices:
            print (voice)

    def speak(self, prompt):
        audio = generate(
        text=prompt,
        voice=self._voice_id,
        model=self._model_id,
        )
        play(audio)
        return
    
    def stream(self, prompt):
        audio_stream = generate(
            text=prompt,
            voice=self._voice_id,
            model=self._model_id,
            stream_chunk_size=2048,
            stream=True,
            )
        return audio_stream
    
    async def get_speech_chunks_async(self, sentence_response:AgentResponse, cancel_event):
        text_to_speak = sentence_response['llm_sentence']
        stream = self.stream(text_to_speak)
        stream, stream_backup = itertools.tee(stream)
        while True:
            # Check if there's a next item in the stream
            next_item = next(stream_backup, None)
            if next_item is None:
                # Stream is exhausted, exit the loop
                break

            # Run next(stream) in a separate thread to avoid blocking the event loop
            chunk = await asyncio.to_thread(next, stream)
            sentence_response['tts_raw_chunk'] = chunk
            if cancel_event.is_set():
                return
            yield sentence_response
            sentence_response['tts_raw_chunk_id'] += 1