WillemVH commited on
Commit
cd25ab6
·
verified ·
1 Parent(s): c0338e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -28
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, send_file, request, render_template_string
2
  import subprocess
3
  import os
4
  import uuid
@@ -7,42 +7,36 @@ app = Flask(__name__)
7
  AUDIO_DIR = "/tmp/tts_audio"
8
  os.makedirs(AUDIO_DIR, exist_ok=True)
9
 
10
- # Root route (homepage)
11
- @app.route('/')
12
- def home():
13
- return render_template_string('''
14
- <!DOCTYPE html>
15
- <html>
16
- <head>
17
- <title>eSpeak NG TTS API</title>
18
- <style>
19
- body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
20
- code { background: #f4f4f4; padding: 2px 5px; }
21
- </style>
22
- </head>
23
- <body>
24
- <h1>eSpeak NG Text-to-Speech API</h1>
25
- <p>Use the endpoint below to generate speech:</p>
26
- <code>GET willemvh-espeakngapi.hf.space/speak?input=Your+Text+Here</code>
27
- <p>Example:</p>
28
- <a href="/speak?input=Hello+World" target="_blank">willemvh-espeakngapi.hf.space/speak?input=Hello+World</a>
29
- <p>Returns: Raw MP3 audio.</p>
30
- </body>
31
- </html>
32
- ''')
33
-
34
- # TTS API endpoint (unchanged)
35
  @app.route('/speak')
36
  def speak():
37
  text = request.args.get('input', '')
 
 
38
  if not text:
39
  return "Error: Add ?input=Your+Text to the URL", 400
40
 
41
  wav_path = os.path.join(AUDIO_DIR, f"{uuid.uuid4()}.wav")
42
  mp3_path = wav_path.replace('.wav', '.mp3')
43
 
44
- subprocess.run(['espeak-ng', '-v', 'en-us', '-s', '150', '-w', wav_path, text], check=True)
45
- subprocess.run(['ffmpeg', '-i', wav_path, '-vn', '-ar', '44100', '-ac', '2', '-b:a', '192k', mp3_path], check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  return send_file(mp3_path, mimetype="audio/mpeg")
48
 
 
1
+ from flask import Flask, send_file, request
2
  import subprocess
3
  import os
4
  import uuid
 
7
  AUDIO_DIR = "/tmp/tts_audio"
8
  os.makedirs(AUDIO_DIR, exist_ok=True)
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  @app.route('/speak')
11
  def speak():
12
  text = request.args.get('input', '')
13
+ voice = request.args.get('voice', 'mb-en1') # Default: MBROLA English
14
+
15
  if not text:
16
  return "Error: Add ?input=Your+Text to the URL", 400
17
 
18
  wav_path = os.path.join(AUDIO_DIR, f"{uuid.uuid4()}.wav")
19
  mp3_path = wav_path.replace('.wav', '.mp3')
20
 
21
+ # Generate speech with MBROLA voice
22
+ subprocess.run([
23
+ 'espeak-ng',
24
+ '-v', voice,
25
+ '-s', '150',
26
+ '-w', wav_path,
27
+ text
28
+ ], check=True)
29
+
30
+ # Convert to MP3
31
+ subprocess.run([
32
+ 'ffmpeg',
33
+ '-i', wav_path,
34
+ '-vn',
35
+ '-ar', '44100',
36
+ '-ac', '2',
37
+ '-b:a', '192k',
38
+ mp3_path
39
+ ], check=True)
40
 
41
  return send_file(mp3_path, mimetype="audio/mpeg")
42