Takosaga commited on
Commit
74f118e
Β·
1 Parent(s): 480dac5

feat: downloading only needed models

Browse files
Files changed (2) hide show
  1. app.py +28 -0
  2. models/download_models.py +22 -7
app.py CHANGED
@@ -83,6 +83,34 @@ from pathlib import Path
83
 
84
  logger = logging.getLogger(__name__)
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  # ─── Phase State ────────────────────────────────────────────────────
87
 
88
  _phase1_texts: list[str] = [] # English texts from Phase 1, passed to Phase 2
 
83
 
84
  logger = logging.getLogger(__name__)
85
 
86
+ # ─── Auto-download models on first run ────────────────────────
87
+ # Ensures the app works out-of-the-box (e.g. HF Spaces) without
88
+ # baking 26 GB of model weights into git.
89
+ def _auto_download_models():
90
+ """Download all models if none are present yet."""
91
+ models_dir = Path(
92
+ os.environ.get("EUROPALEX_MODELS_DIR", ".local/models")
93
+ )
94
+ # Check for GGUF files (llama-cpp-python models). If missing, download
95
+ # everything β€” this covers the fresh-install case. Flux safetensors are
96
+ # not checked separately because if GGUF is missing but safetensors exist,
97
+ # the user still needs MiniCPM/tiny-aya and a full download is correct.
98
+ if not any(models_dir.rglob("*.gguf")):
99
+ print(f"No models found in {models_dir}. Downloading...")
100
+ try:
101
+ from models.download_models import download_all
102
+ download_all(output_dir=str(models_dir))
103
+ except Exception as e:
104
+ logger.error("Auto-download failed: %s", e)
105
+ raise RuntimeError(
106
+ f"Model download failed. Please run:\n"
107
+ f" python -m models.download_models\n"
108
+ f"Or set EUROPALEX_MODELS_DIR to a directory with model files."
109
+ ) from e
110
+
111
+
112
+ _auto_download_models()
113
+
114
  # ─── Phase State ────────────────────────────────────────────────────
115
 
116
  _phase1_texts: list[str] = [] # English texts from Phase 1, passed to Phase 2
models/download_models.py CHANGED
@@ -2,13 +2,16 @@
2
 
3
  Usage:
4
  python -m models.download_models # Download all models
5
- python -m models.download_models minicpm tiny_aya # Download specific models
6
 
7
  Models:
8
  minicpm β€” MiniCPM5-1B Q8_0 (llama-cpp-python)
9
  tiny_aya β€” tiny-aya-water Q4_K_M (llama-cpp-python)
10
- omnivoice β€” OmniVoice Q8_0 TTS (omnivoice.cpp, requires base + tokenizer)
11
  flux β€” FLUX.2-klein 4B image gen (diffusers)
 
 
 
 
12
  """
13
 
14
  import argparse
@@ -28,11 +31,6 @@ MODELS = {
28
  "files": ["tiny-aya-water-q4_k_m.gguf"],
29
  "description": "tiny-aya-water q4_k_m translation (llama-cpp-python)",
30
  },
31
- "omnivoice": {
32
- "repo": "Serveurperso/OmniVoice-GGUF",
33
- "files": ["omnivoice-base-Q8_0.gguf", "omnivoice-tokenizer-Q8_0.gguf"],
34
- "description": "OmniVoice Q8_0 TTS (base + tokenizer, omnivoice.cpp)",
35
- },
36
  "flux": {
37
  "repo": "black-forest-labs/FLUX.2-klein-4B",
38
  "files": None, # Download all files β€” safetensors weights + configs (~10–12 GB)
@@ -66,6 +64,23 @@ def download_model(name: str, target_dir: Path) -> None:
66
  print(f" βœ“ Done β€” {output_dir}\n")
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def main():
70
  parser = argparse.ArgumentParser(description="Download models from HF Hub")
71
  parser.add_argument(
 
2
 
3
  Usage:
4
  python -m models.download_models # Download all models
5
+ python -m models.download_models minicpm tiny_aya flux # Download specific models
6
 
7
  Models:
8
  minicpm β€” MiniCPM5-1B Q8_0 (llama-cpp-python)
9
  tiny_aya β€” tiny-aya-water Q4_K_M (llama-cpp-python)
 
10
  flux β€” FLUX.2-klein 4B image gen (diffusers)
11
+
12
+ Note: OmniVoice TTS is loaded at runtime via
13
+ omnivoice.OmniVoice.from_pretrained("k2-fsa/OmniVoice")
14
+ and cached in ~/.cache/huggingface/ β€” no manual download needed.
15
  """
16
 
17
  import argparse
 
31
  "files": ["tiny-aya-water-q4_k_m.gguf"],
32
  "description": "tiny-aya-water q4_k_m translation (llama-cpp-python)",
33
  },
 
 
 
 
 
34
  "flux": {
35
  "repo": "black-forest-labs/FLUX.2-klein-4B",
36
  "files": None, # Download all files β€” safetensors weights + configs (~10–12 GB)
 
64
  print(f" βœ“ Done β€” {output_dir}\n")
65
 
66
 
67
+ def download_all(output_dir: Path | str = ".local/models") -> None:
68
+ """Download all models from HF Hub. Convenience wrapper around download_model."""
69
+ output_dir = Path(output_dir)
70
+ output_dir.mkdir(parents=True, exist_ok=True)
71
+
72
+ errors = []
73
+ for name in MODELS:
74
+ try:
75
+ download_model(name, output_dir)
76
+ except Exception as e:
77
+ errors.append((name, str(e)))
78
+ print(f" βœ— {name} failed: {e}\n")
79
+
80
+ if errors:
81
+ raise RuntimeError(f"{len(errors)} model(s) failed to download: {[n for n, _ in errors]}")
82
+
83
+
84
  def main():
85
  parser = argparse.ArgumentParser(description="Download models from HF Hub")
86
  parser.add_argument(