Niansuh commited on
Commit
e65b5bc
·
verified ·
1 Parent(s): 30f5ae5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -27
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, url_for
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
- 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)
 
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)