R-TA commited on
Commit
1a961fb
·
verified ·
1 Parent(s): 041788a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import html
 
3
  import subprocess
4
  import tempfile
5
  from typing import Optional
@@ -10,7 +11,7 @@ import gradio as gr
10
  DESCRIPTION = """
11
  Mimic 3 TTS on Hugging Face Spaces (Gradio)
12
 
13
- - Uses the Mimic 3 CLI under-the-hood and returns a WAV file.
14
  - Leave the Voice Key blank to use the default voice, or provide a specific key (e.g., `en_US/cmu-arctic_low`).
15
  - You can optionally wrap the input in SSML for rate/pitch by toggling the advanced options.
16
 
@@ -56,11 +57,33 @@ def synthesize(text: str, voice_key: str, use_ssml: bool, rate: str, pitch: str)
56
  err = proc.stderr.decode(errors="ignore")
57
  raise gr.Error(f"Mimic 3 failed (code {proc.returncode}).\n\n{err}")
58
 
59
- # Write the WAV bytes to a temp file for Gradio
60
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
61
  tmp.write(proc.stdout)
62
- tmp_path = tmp.name
63
- return tmp_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  except FileNotFoundError:
65
  # The mimic3 CLI was not found; show a helpful error in the UI
66
  raise gr.Error("mimic3 CLI not found. Ensure package 'mycroft-mimic3-tts' is installed and available in PATH.")
 
1
  import os
2
  import html
3
+ import shutil
4
  import subprocess
5
  import tempfile
6
  from typing import Optional
 
11
  DESCRIPTION = """
12
  Mimic 3 TTS on Hugging Face Spaces (Gradio)
13
 
14
+ - Uses the Mimic 3 CLI under-the-hood and returns MP3 audio (falls back to WAV if conversion fails).
15
  - Leave the Voice Key blank to use the default voice, or provide a specific key (e.g., `en_US/cmu-arctic_low`).
16
  - You can optionally wrap the input in SSML for rate/pitch by toggling the advanced options.
17
 
 
57
  err = proc.stderr.decode(errors="ignore")
58
  raise gr.Error(f"Mimic 3 failed (code {proc.returncode}).\n\n{err}")
59
 
60
+ # Write the WAV bytes to a temp file
61
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
62
  tmp.write(proc.stdout)
63
+ wav_path = tmp.name
64
+
65
+ ffmpeg_path = shutil.which("ffmpeg")
66
+ if not ffmpeg_path:
67
+ return wav_path
68
+
69
+ mp3_fd, mp3_path = tempfile.mkstemp(suffix=".mp3")
70
+ os.close(mp3_fd)
71
+
72
+ convert = subprocess.run(
73
+ [ffmpeg_path, "-y", "-i", wav_path, "-codec:a", "libmp3lame", "-qscale:a", "4", mp3_path],
74
+ stdout=subprocess.PIPE,
75
+ stderr=subprocess.PIPE,
76
+ check=False,
77
+ )
78
+
79
+ if convert.returncode == 0 and os.path.exists(mp3_path):
80
+ os.remove(wav_path)
81
+ return mp3_path
82
+
83
+ # Conversion failed; clean up mp3 placeholder and return WAV instead
84
+ if os.path.exists(mp3_path):
85
+ os.remove(mp3_path)
86
+ return wav_path
87
  except FileNotFoundError:
88
  # The mimic3 CLI was not found; show a helpful error in the UI
89
  raise gr.Error("mimic3 CLI not found. Ensure package 'mycroft-mimic3-tts' is installed and available in PATH.")