Eyob-Sol commited on
Commit
a38f382
·
verified ·
1 Parent(s): 2fc7902

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -26
app.py CHANGED
@@ -1,23 +1,36 @@
1
  # app.py
2
- from __future__ import annotations
3
- import os, glob, shutil
4
  from app.gradio_app import build_demo
5
- from models.tts_router import cleanup_old_audio, ensure_runtime_audio_dir
 
 
6
  import huggingface_hub
 
 
 
 
7
 
 
 
 
8
  def ensure_model():
 
 
 
 
 
9
  model_path = os.getenv("LLAMACPP_MODEL_PATH")
10
- repo_id = os.getenv("HF_MODEL_REPO")
11
- filename = os.getenv("HF_MODEL_FILE") or (os.path.basename(model_path) if model_path else None)
12
 
13
  if not model_path or not repo_id or not filename:
14
- raise RuntimeError("Missing config: set LLAMACPP_MODEL_PATH and HF_MODEL_REPO (optionally HF_MODEL_FILE) in .env")
15
 
16
  if os.path.exists(model_path):
17
- print(f"[MODEL] Found at {model_path}")
18
  return model_path
19
 
20
  os.makedirs(os.path.dirname(model_path), exist_ok=True)
 
21
  print(f"[MODEL] Downloading {filename} from {repo_id} …")
22
  local_path = huggingface_hub.hf_hub_download(
23
  repo_id=repo_id,
@@ -28,31 +41,70 @@ def ensure_model():
28
  print(f"[MODEL] Saved at {local_path}")
29
  return local_path
30
 
31
- def _dump_audio_dir(tag: str):
32
- audio_dir = ensure_runtime_audio_dir()
33
- files = sorted(glob.glob(os.path.join(audio_dir, "*")))
34
- print(f"[AUDIO:{tag}] dir={audio_dir} count={len(files)}")
35
- for f in files[:8]:
36
- print(" ", os.path.basename(f))
37
- if len(files) > 8:
38
- print(" ...")
39
 
40
- def main():
41
- # Show env so we can verify on HF:
42
- print("[ENV] TTS_ENGINE =", os.getenv("TTS_ENGINE"))
43
- print("[ENV] PIPER_MODEL=", os.getenv("PIPER_MODEL"))
44
- print("[ENV] PIPER_BIN =", os.getenv("PIPER_BIN"))
 
 
 
 
 
 
 
 
45
 
46
- # Ensure GGUF present
47
- ensure_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Clean up old TTS on boot (and prove it with logs)
50
- _dump_audio_dir("BEFORE_CLEAN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  cleanup_old_audio(keep_latest=None)
52
- _dump_audio_dir("AFTER_CLEAN")
53
 
 
 
 
 
 
54
  demo = build_demo()
55
- demo.launch(share=True)
 
56
 
57
  if __name__ == "__main__":
58
  main()
 
1
  # app.py
 
 
2
  from app.gradio_app import build_demo
3
+ from models.tts_router import cleanup_old_audio
4
+
5
+ import os
6
  import huggingface_hub
7
+ import urllib.request
8
+ import tarfile
9
+ import stat
10
+
11
 
12
+ # ---------------------------------------------------------------------
13
+ # Model helper
14
+ # ---------------------------------------------------------------------
15
  def ensure_model():
16
+ """
17
+ Downloads the GGUF model into ./models if not already present.
18
+ - LLAMACPP_MODEL_PATH must include the filename.
19
+ - HF_MODEL_REPO must point to the Hugging Face repo.
20
+ """
21
  model_path = os.getenv("LLAMACPP_MODEL_PATH")
22
+ repo_id = os.getenv("HF_MODEL_REPO")
23
+ filename = os.getenv("HF_MODEL_FILE") or (os.path.basename(model_path) if model_path else None)
24
 
25
  if not model_path or not repo_id or not filename:
26
+ raise RuntimeError("Missing config: set LLAMACPP_MODEL_PATH, HF_MODEL_REPO, HF_MODEL_FILE in .env")
27
 
28
  if os.path.exists(model_path):
29
+ print(f"[MODEL] Found existing model at {model_path}")
30
  return model_path
31
 
32
  os.makedirs(os.path.dirname(model_path), exist_ok=True)
33
+
34
  print(f"[MODEL] Downloading {filename} from {repo_id} …")
35
  local_path = huggingface_hub.hf_hub_download(
36
  repo_id=repo_id,
 
41
  print(f"[MODEL] Saved at {local_path}")
42
  return local_path
43
 
 
 
 
 
 
 
 
 
44
 
45
+ # ---------------------------------------------------------------------
46
+ # Piper helper
47
+ # ---------------------------------------------------------------------
48
+ def ensure_piper():
49
+ """
50
+ Ensure Piper binary and model are available.
51
+ - Downloads a prebuilt Piper binary if not present.
52
+ - Expects PIPER_MODEL already downloaded into ./models/piper/.
53
+ """
54
+ piper_bin = os.getenv("PIPER_BIN", "piper")
55
+ if os.path.isabs(piper_bin) and os.path.exists(piper_bin):
56
+ print(f"[PIPER] Using existing binary at {piper_bin}")
57
+ return piper_bin
58
 
59
+ # place inside models/piper
60
+ bin_dir = os.path.join("models", "piper")
61
+ os.makedirs(bin_dir, exist_ok=True)
62
+ desired_bin = os.path.join(bin_dir, "piper")
63
+
64
+ if os.path.exists(desired_bin):
65
+ print(f"[PIPER] Found at {desired_bin}")
66
+ return desired_bin
67
+
68
+ # download Linux prebuilt
69
+ url = "https://github.com/rhasspy/piper/releases/download/v1.2.0/piper_linux_x86_64.tar.gz"
70
+ tgz = os.path.join(bin_dir, "piper.tar.gz")
71
+ print(f"[PIPER] Downloading from {url} …")
72
+ urllib.request.urlretrieve(url, tgz)
73
+
74
+ with tarfile.open(tgz, "r:gz") as tar:
75
+ tar.extractall(bin_dir)
76
+ os.remove(tgz)
77
 
78
+ # find extracted binary
79
+ for root, _, files in os.walk(bin_dir):
80
+ if "piper" in files:
81
+ candidate = os.path.join(root, "piper")
82
+ os.chmod(candidate, os.stat(candidate).st_mode | stat.S_IEXEC)
83
+ os.rename(candidate, desired_bin)
84
+ break
85
+
86
+ if not os.path.exists(desired_bin):
87
+ raise RuntimeError("Piper binary not found after extraction")
88
+
89
+ print(f"[PIPER] Installed at {desired_bin}")
90
+ return desired_bin
91
+
92
+
93
+ # ---------------------------------------------------------------------
94
+ # Main
95
+ # ---------------------------------------------------------------------
96
+ def main():
97
+ # Clean up any old TTS files
98
  cleanup_old_audio(keep_latest=None)
 
99
 
100
+ # Ensure model + piper
101
+ ensure_model()
102
+ ensure_piper()
103
+
104
+ # Launch Gradio demo
105
  demo = build_demo()
106
+ demo.launch(share=True) # On HF, share=True is ignored, but safe locally
107
+
108
 
109
  if __name__ == "__main__":
110
  main()