Eyob-Sol commited on
Commit
3a90167
·
verified ·
1 Parent(s): e9182c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -49,36 +49,39 @@ def ensure_model() -> str:
49
  # ---------------------------------------
50
  # Piper: rely on the PyPI wheel (piper-tts)
51
  # ---------------------------------------
52
- def ensure_piper_bin() -> str | None:
53
- """
54
- Make sure a usable Piper binary is available.
55
 
56
- Priority:
57
- 1) If PIPER_BIN is an absolute path and executable → use it
58
- 2) If 'piper' is in PATH (installed by pip: piper-tts) → use that
59
- 3) Otherwise, return None (TTS will fall back to 'say' on macOS or be disabled)
60
  """
61
- desired = os.getenv("PIPER_BIN")
62
- if desired:
63
- # absolute path wins if it exists
64
- if os.path.isabs(desired) and os.path.exists(desired):
65
- st = os.stat(desired)
66
- if not (st.st_mode & stat.S_IXUSR):
67
- os.chmod(desired, st.st_mode | stat.S_IXUSR)
68
- print(f"[PIPER] Using PIPER_BIN at {desired}")
69
- return desired
70
- # if user set just 'piper', let which() resolve it
71
- if desired.strip() == "piper":
72
- resolved = which("piper")
73
- if resolved:
74
- print(f"[PIPER] Found in PATH: {resolved}")
75
- return resolved
76
- else:
77
- print("[PIPER] 'piper' not found in PATH despite PIPER_BIN=piper")
78
- return None
79
- # user gave a relative path that doesn't exist
80
- print(f"[PIPER] PIPER_BIN set but not valid: {desired}")
81
- return None
 
 
 
82
 
83
  # No PIPER_BIN provided → try PATH
84
  resolved = which("piper")
@@ -109,11 +112,7 @@ def main():
109
  # Ensure model exists locally
110
  ensure_model()
111
 
112
- # Make sure Piper binary is discoverable (no download at runtime; rely on piper-tts)
113
- piper_bin = ensure_piper_bin()
114
- if piper_bin:
115
- # Export to env so tts_router can pick it up
116
- os.environ["PIPER_BIN"] = piper_bin
117
 
118
  # Launch Gradio
119
  demo = build_demo()
 
49
  # ---------------------------------------
50
  # Piper: rely on the PyPI wheel (piper-tts)
51
  # ---------------------------------------
52
+ # --- keep your existing imports ---
53
+ import os
54
+ from huggingface_hub import hf_hub_download
55
 
56
+ def ensure_piper_voice():
57
+ """
58
+ Make sure the Piper voice model exists at PIPER_MODEL.
59
+ Defaults to Amy-medium English if none specified.
60
  """
61
+ target_path = os.getenv("PIPER_MODEL", "models/piper/en_US-amy-medium.onnx")
62
+ repo_id = os.getenv("PIPER_VOICE_REPO", "rhasspy/piper-voices")
63
+ hf_file = os.getenv("PIPER_VOICE_FILE",
64
+ "en/en_US/en_US-amy-medium.onnx") # path inside the repo
65
+
66
+ # If already present, we're done.
67
+ if os.path.exists(target_path):
68
+ print(f"[PIPER] Voice model present: {target_path}")
69
+ return target_path
70
+
71
+ os.makedirs(os.path.dirname(target_path), exist_ok=True)
72
+ print(f"[PIPER] Downloading voice {hf_file} from {repo_id} …")
73
+ local = hf_hub_download(
74
+ repo_id=repo_id,
75
+ filename=hf_file,
76
+ local_dir=os.path.dirname(target_path),
77
+ local_dir_use_symlinks=False,
78
+ )
79
+ # Move/rename to the exact target filename if the name differs
80
+ if os.path.abspath(local) != os.path.abspath(target_path):
81
+ import shutil
82
+ shutil.copyfile(local, target_path)
83
+ print(f"[PIPER] Voice ready at: {target_path}")
84
+ return target_path
85
 
86
  # No PIPER_BIN provided → try PATH
87
  resolved = which("piper")
 
112
  # Ensure model exists locally
113
  ensure_model()
114
 
115
+ ensure_piper_voice()
 
 
 
 
116
 
117
  # Launch Gradio
118
  demo = build_demo()