Eyob-Sol commited on
Commit
a96e484
·
verified ·
1 Parent(s): 71aed78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -1,49 +1,75 @@
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
 
8
- def ensure_model():
 
9
  """
10
- Downloads the GGUF model into ./models if not already present.
11
- - LLAMACPP_MODEL_PATH must include the filename.
12
- - HF_MODEL_REPO must point to the Hugging Face repo.
 
 
13
  """
14
- model_path = os.getenv("LLAMACPP_MODEL_PATH")
15
- repo_id = os.getenv("HF_MODEL_REPO")
 
 
16
 
17
  if not model_path or not repo_id:
18
- raise RuntimeError("Both LLAMACPP_MODEL_PATH and HF_MODEL_REPO must be set in .env")
 
 
 
 
 
 
 
19
 
 
20
  if os.path.exists(model_path):
21
  print(f"[MODEL] Found existing model at {model_path}")
22
  return model_path
23
 
 
24
  os.makedirs(os.path.dirname(model_path), exist_ok=True)
25
 
26
- filename = os.path.basename(model_path)
27
-
28
- print(f"[MODEL] Downloading {filename} from {repo_id} …")
29
- local_path = huggingface_hub.hf_hub_download(
30
  repo_id=repo_id,
31
- filename=filename,
32
- local_dir=os.path.dirname(model_path),
33
- local_dir_use_symlinks=False,
34
  )
35
- print(f"[MODEL] Saved at {local_path}")
36
- return local_path
 
 
 
 
 
 
 
 
37
 
38
  def main():
39
- # Clean up old TTS files on boot
40
  cleanup_old_audio(keep_latest=None)
41
- # dowload model at startup
42
  ensure_model()
43
 
44
  demo = build_demo()
45
- # Don’t set server_name/server_port; HF will handle it.
46
  demo.launch(share=True)
47
 
 
48
  if __name__ == "__main__":
49
  main()
 
1
  # app.py
2
+ from __future__ import annotations
3
+ import os
4
+ from typing import Optional
5
+
6
  from app.gradio_app import build_demo
7
  from models.tts_router import cleanup_old_audio
8
+ from utils.config import get_settings
9
 
 
10
  import huggingface_hub
11
 
12
+
13
+ def ensure_model() -> str:
14
  """
15
+ Ensure a local GGUF exists:
16
+ - Reads settings from .env via pydantic (get_settings()).
17
+ - If the file at LLAMACPP_MODEL_PATH doesn't exist, download from HF_MODEL_REPO/HF_MODEL_FILE.
18
+ - HF_MODEL_FILE is optional; if missing, we use basename(LLAMACPP_MODEL_PATH).
19
+ - The file is saved exactly at LLAMACPP_MODEL_PATH (e.g., models/<file>.gguf).
20
  """
21
+ s = get_settings() # <- loads .env
22
+ model_path: Optional[str] = getattr(s, "LLAMACPP_MODEL_PATH", None) or os.getenv("LLAMACPP_MODEL_PATH")
23
+ repo_id: Optional[str] = getattr(s, "HF_MODEL_REPO", None) or os.getenv("HF_MODEL_REPO")
24
+ file_name: Optional[str] = getattr(s, "HF_MODEL_FILE", None) or os.getenv("HF_MODEL_FILE")
25
 
26
  if not model_path or not repo_id:
27
+ raise RuntimeError(
28
+ "Missing config: set LLAMACPP_MODEL_PATH and HF_MODEL_REPO in .env "
29
+ "(optionally HF_MODEL_FILE)."
30
+ )
31
+
32
+ # Where/what to download
33
+ if not file_name:
34
+ file_name = os.path.basename(model_path)
35
 
36
+ # Already present?
37
  if os.path.exists(model_path):
38
  print(f"[MODEL] Found existing model at {model_path}")
39
  return model_path
40
 
41
+ # Make sure models/ exists
42
  os.makedirs(os.path.dirname(model_path), exist_ok=True)
43
 
44
+ print(f"[MODEL] Downloading {file_name} from {repo_id} → {model_path}")
45
+ # Download into a temp cache, then copy/symlink into our desired path
46
+ local_cached = huggingface_hub.hf_hub_download(
 
47
  repo_id=repo_id,
48
+ filename=file_name,
49
+ local_dir=os.path.dirname(model_path), # ensure it lands under models/
50
+ local_dir_use_symlinks=False
51
  )
52
+
53
+ # hf_hub_download already placed it in local_dir; ensure final path matches model_path
54
+ if os.path.abspath(local_cached) != os.path.abspath(model_path):
55
+ # If HF wrote it to a different name/path, copy/rename
56
+ import shutil
57
+ shutil.move(local_cached, model_path)
58
+
59
+ print(f"[MODEL] Ready at {model_path}")
60
+ return model_path
61
+
62
 
63
  def main():
64
+ # Clean old TTS files
65
  cleanup_old_audio(keep_latest=None)
66
+ # Ensure the local model is present (downloads if missing)
67
  ensure_model()
68
 
69
  demo = build_demo()
70
+ # On HF Spaces, share=True is fine; server host/port are managed by Spaces
71
  demo.launch(share=True)
72
 
73
+
74
  if __name__ == "__main__":
75
  main()