-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathttsRequestFramework.py
More file actions
48 lines (42 loc) · 1.58 KB
/
ttsRequestFramework.py
File metadata and controls
48 lines (42 loc) · 1.58 KB
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
import os
import requests
from dotenv import load_dotenv, dotenv_values
from playsound import playsound
class ttsRequestFramework:
api_key = None
voice_id = None
url = None
stability = None
similarityBoost = None
def convertTTS(self, text):
payload = {
"text": text,
"voice_settings": {
"stability": 0.5, # CNG to VAR
"similarity_boost": 0.6 # CNG to VAR
}
}
headers = {
"xi-api-key": ttsRequestFramework.api_key,
"Content-Type": "application/json"
}
response = requests.request("POST", ttsRequestFramework.url, json=payload, headers=headers)
# Check for success
if response.status_code == 200:
# Save the audio file
with open("static/audio/output.mp3", "wb") as file:
file.write(response.content)
playsound('static/audio/output.mp3')
print("Audio generated successfully!")
return True
else:
print("Error:", response.status_code)
print(response.text)
return False
def __init__(self, stability, similarityBoost):
load_dotenv()
ttsRequestFramework.api_key = os.environ["TTS_API_KEY"]
ttsRequestFramework.voice_id = "78pDRD8cI8CahmrAekML"
ttsRequestFramework.url = f"https://api.elevenlabs.io/v1/text-to-speech/{ttsRequestFramework.voice_id}"
ttsRequestFramework.stability = stability
ttsRequestFramework.similarityBoost = similarityBoost