from flask import Flask, send_file, request, render_template_string import subprocess import os import uuid app = Flask(__name__) AUDIO_DIR = "/tmp/tts_audio" os.makedirs(AUDIO_DIR, exist_ok=True) @app.route('/') def home(): return render_template_string('''
Use the endpoint below to generate speech:
GET /speak?input=Your+Text+Here
Example:
/speak?input=Hello+WorldReturns: Raw MP3 audio.
''') @app.route('/speak') def speak(): text = request.args.get('input', '') voice = request.args.get('voice', 'mb-en1') # Default: MBROLA English if not text: return "Error: Add ?input=Your+Text to the URL", 400 wav_path = os.path.join(AUDIO_DIR, f"{uuid.uuid4()}.wav") mp3_path = wav_path.replace('.wav', '.mp3') # Generate speech with MBROLA voice subprocess.run([ 'espeak-ng', '-v', voice, '-s', '150', '-w', wav_path, text ], check=True) # Convert to MP3 subprocess.run([ 'ffmpeg', '-i', wav_path, '-vn', '-ar', '44100', '-ac', '2', '-b:a', '192k', mp3_path ], check=True) return send_file(mp3_path, mimetype="audio/mpeg") if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)