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,29 +76,38 @@ class ElevenlabsTTS:
|
|
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 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
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)
|
|
|
5 |
from typing import List
|
6 |
import re
|
7 |
import tempfile
|
8 |
+
from flask import Flask, request, render_template, send_file, url_for
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
|
|
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 |
+
text = request.form.get('text')
|
97 |
+
voice = request.form.get('voice', 'Brian')
|
98 |
+
try:
|
99 |
+
audio_file = tts_provider.tts(text, voice)
|
100 |
+
filename = Path(audio_file).name
|
101 |
+
audio_url = url_for('serve_audio', filename=filename)
|
102 |
+
download_filename = f"{voice}_output.mp3"
|
103 |
+
except Exception as e:
|
104 |
+
error = str(e)
|
105 |
+
|
106 |
+
return render_template('index.html',
|
107 |
+
voices=tts_provider.all_voices.keys(),
|
108 |
+
audio_url=audio_url,
|
109 |
+
download_filename=download_filename,
|
110 |
+
error=error)
|
|
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
app.run(host='0.0.0.0', port=5000)
|