Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from pathlib import Path
|
|
5 |
from typing import List
|
6 |
import re
|
7 |
import tempfile
|
|
|
8 |
from flask import Flask, request, render_template, send_file
|
9 |
|
10 |
app = Flask(__name__)
|
@@ -33,7 +34,10 @@ class ElevenlabsTTS:
|
|
33 |
|
34 |
def __init__(self):
|
35 |
self.session = requests.Session()
|
36 |
-
self.session.headers.update({
|
|
|
|
|
|
|
37 |
self.cache_dir = Path(tempfile.gettempdir())
|
38 |
self.all_voices = {
|
39 |
"Brian": "nPczCjzI2devNBz1zQrb", "Alice": "Xb7hH8MSUJpSbSDYk0k2",
|
@@ -46,8 +50,10 @@ class ElevenlabsTTS:
|
|
46 |
"Matilda": "XrExE9yKIg1WjnnlVkGX", "Sarah": "EXAVITQu4vr4xnSDxMaL",
|
47 |
"Will": "bIHbv24MWmeRgasZH58o", "Neal": "Zp1aWhL05Pi5BkhizFC3"
|
48 |
}
|
49 |
-
self.params = {'allow_unauthenticated': '1'}
|
50 |
self.preview_text = "Hello, this is a sample of my voice."
|
|
|
|
|
|
|
51 |
|
52 |
def tts(self, text: str, voice: str = "Brian") -> str:
|
53 |
if voice not in self.all_voices:
|
@@ -61,7 +67,6 @@ class ElevenlabsTTS:
|
|
61 |
json_data = {'text': sentence, 'model_id': 'eleven_multilingual_v2'}
|
62 |
response = self.session.post(
|
63 |
f'https://api.elevenlabs.io/v1/text-to-speech/{self.all_voices[voice]}',
|
64 |
-
params=self.params,
|
65 |
json=json_data,
|
66 |
timeout=20
|
67 |
)
|
@@ -96,7 +101,6 @@ def index():
|
|
96 |
return send_file(audio_file, mimetype='audio/mpeg', as_attachment=True, download_name=f"{voice}_output.mp3")
|
97 |
except Exception as e:
|
98 |
return render_template('index.html', error=str(e), voices=tts_provider.all_voices.keys())
|
99 |
-
# Generate previews on startup or first visit
|
100 |
previews = {voice: tts_provider.generate_preview(voice) for voice in tts_provider.all_voices.keys()}
|
101 |
return render_template('index.html', voices=tts_provider.all_voices.keys(), previews=previews)
|
102 |
|
|
|
5 |
from typing import List
|
6 |
import re
|
7 |
import tempfile
|
8 |
+
import os
|
9 |
from flask import Flask, request, render_template, send_file
|
10 |
|
11 |
app = Flask(__name__)
|
|
|
34 |
|
35 |
def __init__(self):
|
36 |
self.session = requests.Session()
|
37 |
+
self.session.headers.update({
|
38 |
+
"User-Agent": "Mozilla/5.0",
|
39 |
+
"xi-api-key": os.getenv("ELEVENLABS_API_KEY") # Get API key from environment
|
40 |
+
})
|
41 |
self.cache_dir = Path(tempfile.gettempdir())
|
42 |
self.all_voices = {
|
43 |
"Brian": "nPczCjzI2devNBz1zQrb", "Alice": "Xb7hH8MSUJpSbSDYk0k2",
|
|
|
50 |
"Matilda": "XrExE9yKIg1WjnnlVkGX", "Sarah": "EXAVITQu4vr4xnSDxMaL",
|
51 |
"Will": "bIHbv24MWmeRgasZH58o", "Neal": "Zp1aWhL05Pi5BkhizFC3"
|
52 |
}
|
|
|
53 |
self.preview_text = "Hello, this is a sample of my voice."
|
54 |
+
# Check if API key is provided
|
55 |
+
if not os.getenv("ELEVENLABS_API_KEY"):
|
56 |
+
raise ValueError("ELEVENLABS_API_KEY environment variable is not set")
|
57 |
|
58 |
def tts(self, text: str, voice: str = "Brian") -> str:
|
59 |
if voice not in self.all_voices:
|
|
|
67 |
json_data = {'text': sentence, 'model_id': 'eleven_multilingual_v2'}
|
68 |
response = self.session.post(
|
69 |
f'https://api.elevenlabs.io/v1/text-to-speech/{self.all_voices[voice]}',
|
|
|
70 |
json=json_data,
|
71 |
timeout=20
|
72 |
)
|
|
|
101 |
return send_file(audio_file, mimetype='audio/mpeg', as_attachment=True, download_name=f"{voice}_output.mp3")
|
102 |
except Exception as e:
|
103 |
return render_template('index.html', error=str(e), voices=tts_provider.all_voices.keys())
|
|
|
104 |
previews = {voice: tts_provider.generate_preview(voice) for voice in tts_provider.all_voices.keys()}
|
105 |
return render_template('index.html', voices=tts_provider.all_voices.keys(), previews=previews)
|
106 |
|