Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,6 @@ class SentenceTokenizer:
|
|
21 |
def tokenize(self, text: str) -> List[str]:
|
22 |
if not text or not text.strip():
|
23 |
return []
|
24 |
-
# Simple sentence splitting
|
25 |
sentences = self.SENTENCE_END.split(text.strip())
|
26 |
return [s.strip() for s in sentences if s.strip()]
|
27 |
|
@@ -35,21 +34,26 @@ class ElevenlabsTTS:
|
|
35 |
def __init__(self):
|
36 |
self.session = requests.Session()
|
37 |
self.session.headers.update({"User-Agent": "Mozilla/5.0"})
|
38 |
-
# Use temporary directory instead of fixed cache_dir
|
39 |
self.cache_dir = Path(tempfile.gettempdir())
|
40 |
self.all_voices = {
|
41 |
-
"Brian": "nPczCjzI2devNBz1zQrb",
|
42 |
-
"
|
43 |
-
"
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
45 |
}
|
46 |
self.params = {'allow_unauthenticated': '1'}
|
|
|
47 |
|
48 |
def tts(self, text: str, voice: str = "Brian") -> str:
|
49 |
if voice not in self.all_voices:
|
50 |
raise ValueError(f"Voice '{voice}' not available")
|
51 |
|
52 |
-
filename = self.cache_dir / f"tts_{int(time.time())}.mp3"
|
53 |
sentences = split_sentences(text)
|
54 |
|
55 |
audio_chunks = {}
|
@@ -64,7 +68,6 @@ class ElevenlabsTTS:
|
|
64 |
response.raise_for_status()
|
65 |
audio_chunks[i] = response.content
|
66 |
|
67 |
-
# No need to create directory as tempfile.gettempdir() already exists
|
68 |
combined_audio = BytesIO()
|
69 |
for i in sorted(audio_chunks.keys()):
|
70 |
combined_audio.write(audio_chunks[i])
|
@@ -73,20 +76,37 @@ class ElevenlabsTTS:
|
|
73 |
f.write(combined_audio.getvalue())
|
74 |
return filename.as_posix()
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Web Interface
|
77 |
tts_provider = ElevenlabsTTS()
|
78 |
|
79 |
@app.route('/', methods=['GET', 'POST'])
|
80 |
def index():
|
81 |
if request.method == 'POST':
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
app.run(host='0.0.0.0', port=5000)
|
|
|
21 |
def tokenize(self, text: str) -> List[str]:
|
22 |
if not text or not text.strip():
|
23 |
return []
|
|
|
24 |
sentences = self.SENTENCE_END.split(text.strip())
|
25 |
return [s.strip() for s in sentences if s.strip()]
|
26 |
|
|
|
34 |
def __init__(self):
|
35 |
self.session = requests.Session()
|
36 |
self.session.headers.update({"User-Agent": "Mozilla/5.0"})
|
|
|
37 |
self.cache_dir = Path(tempfile.gettempdir())
|
38 |
self.all_voices = {
|
39 |
+
"Brian": "nPczCjzI2devNBz1zQrb", "Alice": "Xb7hH8MSUJpSbSDYk0k2",
|
40 |
+
"Bill": "pqHfZKP75CvOlQylNhV4", "Callum": "N2lVS1w4EtoT3dr4eOWO",
|
41 |
+
"Charlie": "IKne3meq5aSn9XLyUdCD", "Charlotte": "XB0fDUnXU5powFXDhCwa",
|
42 |
+
"Chris": "iP95p4xoKVk53GoZ742B", "Daniel": "onwK4e9ZLuTAKqWW03F9",
|
43 |
+
"Eric": "cjVigY5qzO86Huf0OWal", "George": "JBFqnCBsd6RMkjVDRZzb",
|
44 |
+
"Jessica": "cgSgspJ2msm6clMCkdW9", "Laura": "FGY2WhTYpPnrIDTdsKH5",
|
45 |
+
"Liam": "TX3LPaxmHKxFdv7VOQHJ", "Lily": "pFZP5JQG7iQjIQuC4Bku",
|
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:
|
54 |
raise ValueError(f"Voice '{voice}' not available")
|
55 |
|
56 |
+
filename = self.cache_dir / f"tts_{voice}_{int(time.time())}.mp3"
|
57 |
sentences = split_sentences(text)
|
58 |
|
59 |
audio_chunks = {}
|
|
|
68 |
response.raise_for_status()
|
69 |
audio_chunks[i] = response.content
|
70 |
|
|
|
71 |
combined_audio = BytesIO()
|
72 |
for i in sorted(audio_chunks.keys()):
|
73 |
combined_audio.write(audio_chunks[i])
|
|
|
76 |
f.write(combined_audio.getvalue())
|
77 |
return filename.as_posix()
|
78 |
|
79 |
+
def generate_preview(self, voice: str) -> str:
|
80 |
+
preview_file = self.cache_dir / f"preview_{voice}.mp3"
|
81 |
+
if not preview_file.exists():
|
82 |
+
return self.tts(self.preview_text, voice)
|
83 |
+
return preview_file.as_posix()
|
84 |
+
|
85 |
# Web Interface
|
86 |
tts_provider = ElevenlabsTTS()
|
87 |
|
88 |
@app.route('/', methods=['GET', 'POST'])
|
89 |
def index():
|
90 |
if request.method == 'POST':
|
91 |
+
if 'generate' in request.form:
|
92 |
+
text = request.form.get('text')
|
93 |
+
voice = request.form.get('voice', 'Brian')
|
94 |
+
try:
|
95 |
+
audio_file = tts_provider.tts(text, voice)
|
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 |
+
|
103 |
+
@app.route('/preview/<voice>')
|
104 |
+
def preview(voice):
|
105 |
+
try:
|
106 |
+
audio_file = tts_provider.generate_preview(voice)
|
107 |
+
return send_file(audio_file, mimetype='audio/mpeg')
|
108 |
+
except Exception as e:
|
109 |
+
return str(e), 500
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
app.run(host='0.0.0.0', port=5000)
|