Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +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__)
|
11 |
|
@@ -76,38 +76,29 @@ class ElevenlabsTTS:
|
|
76 |
f.write(combined_audio.getvalue())
|
77 |
return filename.as_posix()
|
78 |
|
79 |
-
# Serve static audio files
|
80 |
-
app.config['AUDIO_FOLDER'] = tempfile.gettempdir()
|
81 |
-
|
82 |
-
@app.route('/audio/<filename>')
|
83 |
-
def serve_audio(filename):
|
84 |
-
return send_file(Path(app.config['AUDIO_FOLDER']) / filename, mimetype='audio/mpeg')
|
85 |
-
|
86 |
# Web Interface
|
87 |
tts_provider = ElevenlabsTTS()
|
88 |
|
89 |
@app.route('/', methods=['GET', 'POST'])
|
90 |
def index():
|
91 |
-
audio_url = None
|
92 |
-
download_filename = None
|
93 |
-
error = None
|
94 |
-
|
95 |
if request.method == 'POST':
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
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__)
|
11 |
|
|
|
76 |
f.write(combined_audio.getvalue())
|
77 |
return filename.as_posix()
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Web Interface
|
80 |
tts_provider = ElevenlabsTTS()
|
81 |
|
82 |
@app.route('/', methods=['GET', 'POST'])
|
83 |
def index():
|
|
|
|
|
|
|
|
|
84 |
if request.method == 'POST':
|
85 |
+
if 'generate' in request.form:
|
86 |
+
text = request.form.get('text')
|
87 |
+
voice = request.form.get('voice', 'Brian')
|
88 |
+
try:
|
89 |
+
audio_file = tts_provider.tts(text, voice)
|
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)
|