-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtts.py
More file actions
41 lines (35 loc) · 1.28 KB
/
tts.py
File metadata and controls
41 lines (35 loc) · 1.28 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
import os
from dotenv import load_dotenv
import requests
#Texto a voz. Esta impl utiliza ElevenLabs
class TTS():
def __init__(self):
load_dotenv()
self.key = os.getenv('ELEVENLABS_API_KEY')
def process(self, text):
CHUNK_SIZE = 1024
#Utiliza la voz especifica de Bella
#Me robe este codigo de su pagina hoh
url = "https://api.elevenlabs.io/v1/text-to-speech/EXAVITQu4vr4xnSDxMaL"
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": self.key
}
data = {
"text": text,
"model_id": "eleven_multilingual_v1",
"voice_settings": {
"stability": 0.55,
"similarity_boost": 0.55
}
}
#Lo guarda en static/response.mp3 para que el sitio web
#pueda leerlo y reproducirlo en el explorador
file_name = "response.mp3"
response = requests.post(url, json=data, headers=headers)
with open("static/" + file_name, 'wb') as f:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
f.write(chunk)
return file_name