-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTS.py
60 lines (50 loc) · 1.95 KB
/
TTS.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# THIS IS FOR TESTING PURPOSES
# The 'requests' and 'json' libraries are imported.
# 'requests' is used to send HTTP requests, while 'json' is used for parsing the JSON data that we receive from the API.
import requests
import json
import os
# play sound
from playsound import playsound
# .env
from dotenv import load_dotenv
load_dotenv()
# Define constants for the script
CHUNK_SIZE = 1024 # Size of chunks to read/write at a time
XI_API_KEY = os.getenv('ELEVEN_LABS_KEY') # Your API key for authentication
VOICE_ID = "VkRQXxkfeZ8MQzwDOLue" # ID of the voice model to use
TEXT_TO_SPEAK = "You're such a good boy" # Text you want to convert to speech
OUTPUT_PATH = "./audio/output.mp3" # Path to save the output audio file
# Construct the URL for the Text-to-Speech API request
tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
# Set up headers for the API request, including the API key for authentication
headers = {
"Accept": "application/json",
"xi-api-key": XI_API_KEY
}
# Set up the data payload for the API request, including the text and voice settings
data = {
"text": TEXT_TO_SPEAK,
"model_id": "eleven_multilingual_v2",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.8,
"style": 0.0,
"use_speaker_boost": True
}
}
# Make the POST request to the TTS API with headers and data, enabling streaming response
response = requests.post(tts_url, headers=headers, json=data, stream=True)
# Check if the request was successful
if response.ok:
# Open the output file in write-binary mode
with open(OUTPUT_PATH, "wb") as f:
# Read the response in chunks and write to the file
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
f.write(chunk)
# Inform the user of success
print("Audio stream saved successfully.")
playsound(OUTPUT_PATH)
else:
# Print the error message if the request was not successful
print(response.text)