MSGEncrypted commited on
Commit
11c0ad1
·
1 Parent(s): b16996a

model config wip

Browse files
libs/inference/pyproject.toml CHANGED
@@ -8,16 +8,12 @@ authors = [
8
  ]
9
  requires-python = ">=3.12"
10
  dependencies = [
 
11
  "huggingface-hub>=0.27.0",
12
  "llama-cpp-python>=0.3.0",
13
  "pyyaml>=6.0.2",
14
- ]
15
-
16
- [project.optional-dependencies]
17
- transformers = [
18
- "accelerate>=1.2.0",
19
  "torch>=2.5.0",
20
- "transformers>=4.47.0",
21
  ]
22
 
23
  [build-system]
 
8
  ]
9
  requires-python = ">=3.12"
10
  dependencies = [
11
+ "accelerate>=1.2.0",
12
  "huggingface-hub>=0.27.0",
13
  "llama-cpp-python>=0.3.0",
14
  "pyyaml>=6.0.2",
 
 
 
 
 
15
  "torch>=2.5.0",
16
+ "transformers>=5.7.0",
17
  ]
18
 
19
  [build-system]
libs/inference/src/inference/transformers.py CHANGED
@@ -21,8 +21,8 @@ class TransformersBackend:
21
  from transformers import AutoModelForCausalLM, AutoTokenizer
22
  except ImportError as exc:
23
  raise ImportError(
24
- "transformers backend requires optional deps. "
25
- "Install with: uv sync --package inference --extra transformers"
26
  ) from exc
27
 
28
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
21
  from transformers import AutoModelForCausalLM, AutoTokenizer
22
  except ImportError as exc:
23
  raise ImportError(
24
+ "transformers backend requires torch and transformers. "
25
+ "Install with: uv sync --all-packages"
26
  ) from exc
27
 
28
  device = "cuda" if torch.cuda.is_available() else "cpu"
scripts/download_model.py CHANGED
@@ -1,16 +1,76 @@
1
  #!/usr/bin/env python3
2
- """Download a configured GGUF preset from Hugging Face Hub for offline dev."""
3
 
4
  from __future__ import annotations
5
 
6
  import argparse
7
  from pathlib import Path
8
 
9
- from huggingface_hub import hf_hub_download
10
 
11
  from inference.config import get_app_config, get_model_config
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def main() -> None:
15
  parser = argparse.ArgumentParser(description=__doc__)
16
  parser.add_argument(
@@ -22,7 +82,7 @@ def main() -> None:
22
  "--output-dir",
23
  type=Path,
24
  default=Path("models"),
25
- help="Directory to copy/symlink the downloaded model into",
26
  )
27
  args = parser.parse_args()
28
 
@@ -30,35 +90,15 @@ def main() -> None:
30
  preset_key = args.preset or app_config.active_model
31
  model = get_model_config(preset_key)
32
 
33
- if model.backend != "llama_cpp":
34
- raise SystemExit(
35
- f"Preset {preset_key!r} uses backend {model.backend!r}. "
36
- "Only llama_cpp presets with model_repo/model_file can be downloaded."
37
- )
38
-
39
- if model.model_path:
40
- path = Path(model.model_path)
41
- if not path.exists():
42
- raise SystemExit(f"Local MODEL_PATH does not exist: {model.model_path}")
43
- print(f"Preset {preset_key!r} already points to local file: {path}")
44
- print(f"Set MODEL_PATH={path} or update models.yaml model_path to use it directly.")
45
  return
46
 
47
- if not model.model_repo or not model.model_file:
48
- raise SystemExit(f"Preset {preset_key!r} is missing model_repo/model_file.")
49
-
50
- args.output_dir.mkdir(parents=True, exist_ok=True)
51
 
52
- path = hf_hub_download(
53
- repo_id=model.model_repo,
54
- filename=model.model_file,
55
- local_dir=args.output_dir,
56
- local_dir_use_symlinks=False,
57
- )
58
- print(f"Preset: {preset_key} ({model.label})")
59
- print(f"Model ready at: {path}")
60
- print("Add to models.yaml under that preset:")
61
- print(f" model_path: {Path(path).resolve()}")
62
 
63
 
64
  if __name__ == "__main__":
 
1
  #!/usr/bin/env python3
2
+ """Download a configured model preset from Hugging Face Hub for offline dev."""
3
 
4
  from __future__ import annotations
5
 
6
  import argparse
7
  from pathlib import Path
8
 
9
+ from huggingface_hub import hf_hub_download, snapshot_download
10
 
11
  from inference.config import get_app_config, get_model_config
12
 
13
 
14
+ def _is_local_path(value: str) -> bool:
15
+ return value.startswith(("./", "../")) or Path(value).is_absolute()
16
+
17
+
18
+ def _download_llama_cpp(model, preset_key: str, output_dir: Path) -> Path:
19
+ if model.model_path:
20
+ path = Path(model.model_path)
21
+ if not path.exists():
22
+ raise SystemExit(f"Local MODEL_PATH does not exist: {model.model_path}")
23
+ print(f"Preset {preset_key!r} already points to local file: {path}")
24
+ print(f"Set MODEL_PATH={path} or update models.yaml model_path to use it directly.")
25
+ return path.resolve()
26
+
27
+ if not model.model_repo or not model.model_file:
28
+ raise SystemExit(f"Preset {preset_key!r} is missing model_repo/model_file.")
29
+
30
+ output_dir.mkdir(parents=True, exist_ok=True)
31
+ path = Path(
32
+ hf_hub_download(
33
+ repo_id=model.model_repo,
34
+ filename=model.model_file,
35
+ local_dir=output_dir,
36
+ local_dir_use_symlinks=False,
37
+ )
38
+ )
39
+ print(f"Preset: {preset_key} ({model.label})")
40
+ print(f"Model ready at: {path}")
41
+ print("Add to models.yaml under that preset:")
42
+ print(f" model_path: {path.resolve()}")
43
+ return path.resolve()
44
+
45
+
46
+ def _download_transformers(model, preset_key: str, output_dir: Path) -> Path:
47
+ if not model.model_id:
48
+ raise SystemExit(f"Preset {preset_key!r} is missing model_id.")
49
+
50
+ if _is_local_path(model.model_id):
51
+ path = Path(model.model_id).resolve()
52
+ if not path.exists():
53
+ raise SystemExit(f"Local model_id does not exist: {model.model_id}")
54
+ print(f"Preset {preset_key!r} already points to local path: {path}")
55
+ print(f"Set MODEL_ID={path} or update models.yaml model_id to use it directly.")
56
+ return path
57
+
58
+ output_dir.mkdir(parents=True, exist_ok=True)
59
+ dest = output_dir / model.model_id
60
+ path = Path(
61
+ snapshot_download(
62
+ repo_id=model.model_id,
63
+ local_dir=dest,
64
+ local_dir_use_symlinks=False,
65
+ )
66
+ )
67
+ print(f"Preset: {preset_key} ({model.label})")
68
+ print(f"Model ready at: {path}")
69
+ print("Add to models.yaml under that preset:")
70
+ print(f" model_id: ./{path.relative_to(Path.cwd())}")
71
+ return path.resolve()
72
+
73
+
74
  def main() -> None:
75
  parser = argparse.ArgumentParser(description=__doc__)
76
  parser.add_argument(
 
82
  "--output-dir",
83
  type=Path,
84
  default=Path("models"),
85
+ help="Directory to download the model into",
86
  )
87
  args = parser.parse_args()
88
 
 
90
  preset_key = args.preset or app_config.active_model
91
  model = get_model_config(preset_key)
92
 
93
+ if model.backend == "llama_cpp":
94
+ _download_llama_cpp(model, preset_key, args.output_dir)
 
 
 
 
 
 
 
 
 
 
95
  return
96
 
97
+ if model.backend == "transformers":
98
+ _download_transformers(model, preset_key, args.output_dir)
99
+ return
 
100
 
101
+ raise SystemExit(f"Preset {preset_key!r} uses unsupported backend {model.backend!r}.")
 
 
 
 
 
 
 
 
 
102
 
103
 
104
  if __name__ == "__main__":
uv.lock CHANGED
@@ -498,28 +498,23 @@ name = "inference"
498
  version = "0.1.0"
499
  source = { editable = "libs/inference" }
500
  dependencies = [
 
501
  { name = "huggingface-hub" },
502
  { name = "llama-cpp-python" },
503
  { name = "pyyaml" },
504
- ]
505
-
506
- [package.optional-dependencies]
507
- transformers = [
508
- { name = "accelerate" },
509
  { name = "torch" },
510
  { name = "transformers" },
511
  ]
512
 
513
  [package.metadata]
514
  requires-dist = [
515
- { name = "accelerate", marker = "extra == 'transformers'", specifier = ">=1.2.0" },
516
  { name = "huggingface-hub", specifier = ">=0.27.0" },
517
  { name = "llama-cpp-python", specifier = ">=0.3.0" },
518
  { name = "pyyaml", specifier = ">=6.0.2" },
519
- { name = "torch", marker = "extra == 'transformers'", specifier = ">=2.5.0" },
520
- { name = "transformers", marker = "extra == 'transformers'", specifier = ">=4.47.0" },
521
  ]
522
- provides-extras = ["transformers"]
523
 
524
  [[package]]
525
  name = "iniconfig"
 
498
  version = "0.1.0"
499
  source = { editable = "libs/inference" }
500
  dependencies = [
501
+ { name = "accelerate" },
502
  { name = "huggingface-hub" },
503
  { name = "llama-cpp-python" },
504
  { name = "pyyaml" },
 
 
 
 
 
505
  { name = "torch" },
506
  { name = "transformers" },
507
  ]
508
 
509
  [package.metadata]
510
  requires-dist = [
511
+ { name = "accelerate", specifier = ">=1.2.0" },
512
  { name = "huggingface-hub", specifier = ">=0.27.0" },
513
  { name = "llama-cpp-python", specifier = ">=0.3.0" },
514
  { name = "pyyaml", specifier = ">=6.0.2" },
515
+ { name = "torch", specifier = ">=2.5.0" },
516
+ { name = "transformers", specifier = ">=5.7.0" },
517
  ]
 
518
 
519
  [[package]]
520
  name = "iniconfig"