Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from pathlib import Path
|
|
| 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__)
|
|
@@ -22,6 +21,7 @@ class SentenceTokenizer:
|
|
| 22 |
def tokenize(self, text: str) -> List[str]:
|
| 23 |
if not text or not text.strip():
|
| 24 |
return []
|
|
|
|
| 25 |
sentences = self.SENTENCE_END.split(text.strip())
|
| 26 |
return [s.strip() for s in sentences if s.strip()]
|
| 27 |
|
|
@@ -34,32 +34,26 @@ class ElevenlabsTTS:
|
|
| 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",
|
| 44 |
-
"Bill": "pqHfZKP75CvOlQylNhV4", "Callum": "N2lVS1w4EtoT3dr4eOWO",
|
| 45 |
-
"Charlie": "IKne3meq5aSn9XLyUdCD", "Charlotte": "XB0fDUnXU5powFXDhCwa",
|
| 46 |
-
"Chris": "iP95p4xoKVk53GoZ742B", "Daniel": "onwK4e9ZLuTAKqWW03F9",
|
| 47 |
-
"Eric": "cjVigY5qzO86Huf0OWal", "George": "JBFqnCBsd6RMkjVDRZzb",
|
| 48 |
-
"Jessica": "cgSgspJ2msm6clMCkdW9", "Laura": "FGY2WhTYpPnrIDTdsKH5",
|
| 49 |
-
"Liam": "TX3LPaxmHKxFdv7VOQHJ", "Lily": "pFZP5JQG7iQjIQuC4Bku",
|
| 50 |
-
"Matilda": "XrExE9yKIg1WjnnlVkGX", "Sarah": "EXAVITQu4vr4xnSDxMaL",
|
| 51 |
"Will": "bIHbv24MWmeRgasZH58o", "Neal": "Zp1aWhL05Pi5BkhizFC3"
|
| 52 |
}
|
| 53 |
-
self.
|
| 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:
|
| 60 |
raise ValueError(f"Voice '{voice}' not available")
|
| 61 |
|
| 62 |
-
filename = self.cache_dir / f"tts_{
|
| 63 |
sentences = split_sentences(text)
|
| 64 |
|
| 65 |
audio_chunks = {}
|
|
@@ -67,6 +61,7 @@ class ElevenlabsTTS:
|
|
| 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 |
)
|
|
@@ -81,12 +76,6 @@ class ElevenlabsTTS:
|
|
| 81 |
f.write(combined_audio.getvalue())
|
| 82 |
return filename.as_posix()
|
| 83 |
|
| 84 |
-
def generate_preview(self, voice: str) -> str:
|
| 85 |
-
preview_file = self.cache_dir / f"preview_{voice}.mp3"
|
| 86 |
-
if not preview_file.exists():
|
| 87 |
-
return self.tts(self.preview_text, voice)
|
| 88 |
-
return preview_file.as_posix()
|
| 89 |
-
|
| 90 |
# Web Interface
|
| 91 |
tts_provider = ElevenlabsTTS()
|
| 92 |
|
|
@@ -101,16 +90,15 @@ def index():
|
|
| 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 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
return str(e), 500
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
app.run(host='0.0.0.0', port=5000)
|
|
|
|
| 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__)
|
|
|
|
| 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 |
|
|
|
|
| 34 |
|
| 35 |
def __init__(self):
|
| 36 |
self.session = requests.Session()
|
| 37 |
+
self.session.headers.update({"User-Agent": "Mozilla/5.0"})
|
|
|
|
|
|
|
|
|
|
| 38 |
self.cache_dir = Path(tempfile.gettempdir())
|
| 39 |
self.all_voices = {
|
| 40 |
+
"Brian": "nPczCjzI2devNBz1zQrb", "Alice": "Xb7hH8MSUJpSbSDYk0k2",
|
| 41 |
+
"Bill": "pqHfZKP75CvOlQylNhV4", "Callum": "N2lVS1w4EtoT3dr4eOWO",
|
| 42 |
+
"Charlie": "IKne3meq5aSn9XLyUdCD", "Charlotte": "XB0fDUnXU5powFXDhCwa",
|
| 43 |
+
"Chris": "iP95p4xoKVk53GoZ742B", "Daniel": "onwK4e9ZLuTAKqWW03F9",
|
| 44 |
+
"Eric": "cjVigY5qzO86Huf0OWal", "George": "JBFqnCBsd6RMkjVDRZzb",
|
| 45 |
+
"Jessica": "cgSgspJ2msm6clMCkdW9", "Laura": "FGY2WhTYpPnrIDTdsKH5",
|
| 46 |
+
"Liam": "TX3LPaxmHKxFdv7VOQHJ", "Lily": "pFZP5JQG7iQjIQuC4Bku",
|
| 47 |
+
"Matilda": "XrExE9yKIg1WjnnlVkGX", "Sarah": "EXAVITQu4vr4xnSDxMaL",
|
| 48 |
"Will": "bIHbv24MWmeRgasZH58o", "Neal": "Zp1aWhL05Pi5BkhizFC3"
|
| 49 |
}
|
| 50 |
+
self.params = {'allow_unauthenticated': '1'}
|
|
|
|
|
|
|
|
|
|
| 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_{int(time.time())}.mp3"
|
| 57 |
sentences = split_sentences(text)
|
| 58 |
|
| 59 |
audio_chunks = {}
|
|
|
|
| 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 |
)
|
|
|
|
| 76 |
f.write(combined_audio.getvalue())
|
| 77 |
return filename.as_posix()
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# Web Interface
|
| 80 |
tts_provider = ElevenlabsTTS()
|
| 81 |
|
|
|
|
| 90 |
return send_file(audio_file, mimetype='audio/mpeg', as_attachment=True, download_name=f"{voice}_output.mp3")
|
| 91 |
except Exception as e:
|
| 92 |
return render_template('index.html', error=str(e), voices=tts_provider.all_voices.keys())
|
| 93 |
+
elif 'preview' in request.form:
|
| 94 |
+
voice = request.form.get('preview_voice')
|
| 95 |
+
try:
|
| 96 |
+
preview_text = f"Hello, this is {voice}'s voice sample."
|
| 97 |
+
audio_file = tts_provider.tts(preview_text, voice)
|
| 98 |
+
return send_file(audio_file, mimetype='audio/mpeg', as_attachment=False)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
return render_template('index.html', error=str(e), voices=tts_provider.all_voices.keys())
|
| 101 |
+
return render_template('index.html', voices=tts_provider.all_voices.keys())
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
app.run(host='0.0.0.0', port=5000)
|