-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepgram_synthesis.py
44 lines (34 loc) · 1.12 KB
/
deepgram_synthesis.py
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
import asyncio
from dotenv import load_dotenv
import logging
from deepgram.utils import verboselogs
from pydub import AudioSegment
from pydub.playback import play
import io
import os
from deepgram import (
DeepgramClient,
DeepgramClientOptions,
SpeakOptions,
)
load_dotenv()
# SPEAK_TEXT = {"text": "My name is Bolarinwa, how are you today?"}
async def text_to_speech(SPEAK_TEXT):
deepgram_api_key = os.getenv("DEEPGRAM_API_KEY")
config: DeepgramClientOptions = DeepgramClientOptions(
options={"keepalive": "true"}
)
deepgram: DeepgramClient = DeepgramClient(deepgram_api_key, config)
options = SpeakOptions(
model="aura-asteria-en",
)
response = await deepgram.speak.asyncrest.v("1").stream_raw(SPEAK_TEXT, options)
print(f"Response: {response}")
for header in response.headers:
print(f"{header}: {response.headers[header]}")
audio_data = bytearray()
async for data in response.aiter_bytes():
audio_data.extend(data)
await response.aclose()
audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format="mp3")
play(audio_segment)