change location models dir, install scripts for preload models
This commit is contained in:
parent
ece9957d48
commit
08cfa841bc
|
|
@ -2,8 +2,8 @@ HF_TOKEN=
|
|||
API_KEY=
|
||||
LOG_LEVEL=INFO
|
||||
# Host paths mounted into the container. Model downloads survive rebuilds.
|
||||
MODEL_STORAGE_PATH=./models
|
||||
DATA_STORAGE_PATH=./data
|
||||
MODEL_STORAGE_PATH=/srv/llmserver/models
|
||||
DATA_STORAGE_PATH=/srv/llmserver/data
|
||||
# Override only when a different CUDA 13.3 image variant is required.
|
||||
CUDA_IMAGE=nvidia/cuda:13.3.0-cudnn-runtime-ubuntu24.04
|
||||
# MODEL_SERVER_QWEN_MODEL=Qwen/Qwen2.5-7B-Instruct
|
||||
|
|
|
|||
26
README.md
26
README.md
|
|
@ -16,7 +16,8 @@ docker compose up --build
|
|||
|
||||
The Docker image uses NVIDIA CUDA 13.3.0 with cuDNN. Override `CUDA_IMAGE` in
|
||||
`.env` to select another CUDA 13.3 image variant. Model downloads are stored
|
||||
on the host in `./models` and temporary uploaded data in `./data`; set
|
||||
on the host in `/srv/llmserver/models` and temporary uploaded data in
|
||||
`/srv/llmserver/data`; set
|
||||
`MODEL_STORAGE_PATH` or `DATA_STORAGE_PATH` in `.env` to use other locations.
|
||||
These bind mounts keep model weights outside the image and preserve them across
|
||||
container rebuilds.
|
||||
|
|
@ -61,7 +62,8 @@ On an apt-based Linux host with a working NVIDIA driver, install the service
|
|||
from the repository checkout:
|
||||
|
||||
```bash
|
||||
sudo ./scripts/install-host.sh
|
||||
# Pass the token on the first run so the installer can download gated models.
|
||||
sudo env HF_TOKEN=hf_... ./scripts/install-host.sh
|
||||
sudoedit /etc/llmserver/llmserver.env
|
||||
sudo systemctl restart llmserver
|
||||
curl http://127.0.0.1:8000/healthz
|
||||
|
|
@ -69,9 +71,21 @@ curl http://127.0.0.1:8000/healthz
|
|||
|
||||
The installer creates a dedicated `llmserver` account, a virtual environment
|
||||
under `/opt/llmserver`, and a single-worker systemd service. Configuration is
|
||||
kept in `/etc/llmserver/llmserver.env`, uploads in `/var/lib/llmserver`, and the
|
||||
Hugging Face model cache in `/var/cache/llmserver`. It is safe to rerun the
|
||||
installer to update the application; those persistent directories are retained.
|
||||
kept in `/etc/llmserver/llmserver.env`, uploads in `/srv/llmserver/data`, and the
|
||||
model cache in `/srv/llmserver/models`. During installation it downloads all
|
||||
configured models, including models referenced by the Pyannote pipeline. Set
|
||||
`HF_TOKEN` and any model overrides in the configuration, then rerun the
|
||||
installer if a gated model could not be downloaded. It is safe to rerun the
|
||||
installer to update the application; persistent directories and cached files
|
||||
are retained.
|
||||
|
||||
Models can also be downloaded manually without starting the API:
|
||||
|
||||
```bash
|
||||
sudo -u llmserver /opt/llmserver/venv/bin/llmserver-download-models \
|
||||
--env-file /etc/llmserver/llmserver.env \
|
||||
--models-dir /srv/llmserver/models
|
||||
```
|
||||
|
||||
Useful service commands:
|
||||
|
||||
|
|
@ -96,7 +110,7 @@ library from the Python package, so no manual `LD_LIBRARY_PATH` is required.
|
|||
## Notes
|
||||
|
||||
- Persist `/models` so model weights are not downloaded again. The Compose file
|
||||
maps it to `${MODEL_STORAGE_PATH:-./models}` on the host.
|
||||
maps it to `${MODEL_STORAGE_PATH:-/srv/llmserver/models}` on the host.
|
||||
- The queue is in memory. Jobs do not survive process restarts and this service must be deployed as one process/replica.
|
||||
- Pyannote returns speaker time ranges. Combine its result with Whisper segments in a client if speaker-attributed transcripts are needed.
|
||||
- Qwen 7B at 4-bit leaves headroom for KV cache on 16 GB. Reduce `max_tokens` or use a smaller Qwen model for unusually long prompts.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
import argparse
|
||||
import gc
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from huggingface_hub import snapshot_download
|
||||
|
||||
from .config import Settings, configure_logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def download_models(settings: Settings) -> None:
|
||||
"""Download every configured model without moving it to the GPU."""
|
||||
huggingface_cache = settings.models_dir / "huggingface"
|
||||
faster_whisper_cache = settings.models_dir / "faster-whisper"
|
||||
huggingface_cache.mkdir(parents=True, exist_ok=True)
|
||||
faster_whisper_cache.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
logger.info("Downloading Faster-Whisper model=%s", settings.whisper_model)
|
||||
from faster_whisper import download_model
|
||||
|
||||
download_model(settings.whisper_model, cache_dir=str(faster_whisper_cache))
|
||||
|
||||
for name, model_id in (
|
||||
("Pyannote", settings.pyannote_model),
|
||||
("emotion", settings.emotion_model),
|
||||
("Qwen", settings.qwen_model),
|
||||
):
|
||||
logger.info("Downloading %s model=%s", name, model_id)
|
||||
snapshot_download(
|
||||
repo_id=model_id,
|
||||
cache_dir=huggingface_cache,
|
||||
token=settings.huggingface_token,
|
||||
)
|
||||
|
||||
# A Pyannote pipeline can reference additional repositories in config.yaml.
|
||||
# Constructing it on CPU downloads those dependencies without running inference.
|
||||
logger.info("Downloading models referenced by Pyannote pipeline=%s", settings.pyannote_model)
|
||||
from pyannote.audio import Pipeline
|
||||
|
||||
pipeline = Pipeline.from_pretrained(
|
||||
settings.pyannote_model,
|
||||
token=settings.huggingface_token,
|
||||
cache_dir=huggingface_cache,
|
||||
)
|
||||
del pipeline
|
||||
gc.collect()
|
||||
logger.info("All configured models are downloaded under %s", settings.models_dir)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Download all models used by llmserver.")
|
||||
parser.add_argument(
|
||||
"--env-file",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Read model names and HF_TOKEN from this environment file.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--models-dir",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Override the model storage directory.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
overrides = {}
|
||||
if args.models_dir is not None:
|
||||
overrides["models_dir"] = args.models_dir
|
||||
settings = Settings(_env_file=args.env_file, **overrides)
|
||||
configure_logging(settings.log_level)
|
||||
download_models(settings)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -16,8 +16,8 @@ services:
|
|||
volumes:
|
||||
# Bind mounts keep large model files and uploads outside Docker's
|
||||
# writable layer. Override either location in .env when desired.
|
||||
- ${MODEL_STORAGE_PATH:-./models}:/models
|
||||
- ${DATA_STORAGE_PATH:-./data}:/data
|
||||
- ${MODEL_STORAGE_PATH:-/srv/llmserver/models}:/models
|
||||
- ${DATA_STORAGE_PATH:-/srv/llmserver/data}:/data
|
||||
deploy:
|
||||
resources:
|
||||
reservations:
|
||||
|
|
|
|||
|
|
@ -7,26 +7,23 @@ After=network-online.target
|
|||
Type=simple
|
||||
User=llmserver
|
||||
Group=llmserver
|
||||
WorkingDirectory=/var/lib/llmserver
|
||||
WorkingDirectory=/srv/llmserver/data
|
||||
EnvironmentFile=/etc/llmserver/llmserver.env
|
||||
Environment=PYTHONUNBUFFERED=1
|
||||
Environment=HF_HOME=/var/cache/llmserver/huggingface
|
||||
Environment=MODEL_SERVER_DATA_DIR=/var/lib/llmserver/uploads
|
||||
Environment=HF_HOME=/srv/llmserver/models/huggingface
|
||||
Environment=MODEL_SERVER_DATA_DIR=/srv/llmserver/data
|
||||
Environment=MODEL_SERVER_MODELS_DIR=/srv/llmserver/models
|
||||
ExecStart=/opt/llmserver/venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 1
|
||||
Restart=on-failure
|
||||
RestartSec=5s
|
||||
TimeoutStopSec=90s
|
||||
|
||||
# systemd creates these directories and assigns them to User/Group.
|
||||
StateDirectory=llmserver
|
||||
CacheDirectory=llmserver
|
||||
|
||||
# Basic service hardening. GPU devices remain available to the process.
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectHome=true
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/var/lib/llmserver /var/cache/llmserver
|
||||
ReadWritePaths=/srv/llmserver
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ dependencies = [
|
|||
"pydantic-settings>=2.4,<3",
|
||||
"torch>=2.4,<3",
|
||||
"transformers>=4.45,<5",
|
||||
"huggingface-hub>=0.26,<1",
|
||||
"accelerate>=0.34,<2",
|
||||
"bitsandbytes>=0.43,<1; platform_system == 'Linux'",
|
||||
"faster-whisper>=1.0,<2",
|
||||
|
|
@ -22,6 +23,9 @@ dependencies = [
|
|||
"pyannote.audio>=4.0.4,<5",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
llmserver-download-models = "app.download_models:main"
|
||||
|
||||
[project.optional-dependencies]
|
||||
test = ["httpx>=0.27,<1", "pytest>=8,<9", "pytest-asyncio>=0.23,<1"]
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ if ! getent group llmserver >/dev/null; then
|
|||
groupadd --system llmserver
|
||||
fi
|
||||
if ! id -u llmserver >/dev/null 2>&1; then
|
||||
useradd --system --gid llmserver --home-dir /var/lib/llmserver \
|
||||
useradd --system --gid llmserver --home-dir /srv/llmserver \
|
||||
--shell /usr/sbin/nologin llmserver
|
||||
fi
|
||||
|
||||
|
|
@ -48,12 +48,30 @@ echo "Installing the application and Python dependencies..."
|
|||
"${INSTALL_DIR}/venv/bin/python" -m pip install --upgrade "${PROJECT_DIR}"
|
||||
|
||||
if [[ ! -e "${CONFIG_DIR}/llmserver.env" ]]; then
|
||||
install -m 0600 "${PROJECT_DIR}/deploy/llmserver.env.example" \
|
||||
install -o root -g llmserver -m 0640 \
|
||||
"${PROJECT_DIR}/deploy/llmserver.env.example" \
|
||||
"${CONFIG_DIR}/llmserver.env"
|
||||
echo "Created ${CONFIG_DIR}/llmserver.env; add HF_TOKEN before using diarization."
|
||||
else
|
||||
echo "Preserving existing ${CONFIG_DIR}/llmserver.env."
|
||||
fi
|
||||
chown root:llmserver "${CONFIG_DIR}/llmserver.env"
|
||||
chmod 0640 "${CONFIG_DIR}/llmserver.env"
|
||||
|
||||
install -d -o llmserver -g llmserver -m 0755 \
|
||||
/srv/llmserver /srv/llmserver/models /srv/llmserver/data
|
||||
chown -R llmserver:llmserver /srv/llmserver
|
||||
|
||||
echo "Downloading configured models (this can take a while)..."
|
||||
download_environment=(HF_HOME=/srv/llmserver/models/huggingface)
|
||||
if [[ -n ${HF_TOKEN:-} ]]; then
|
||||
download_environment+=("HF_TOKEN=${HF_TOKEN}")
|
||||
fi
|
||||
runuser -u llmserver -- \
|
||||
env "${download_environment[@]}" \
|
||||
"${INSTALL_DIR}/venv/bin/llmserver-download-models" \
|
||||
--env-file "${CONFIG_DIR}/llmserver.env" \
|
||||
--models-dir /srv/llmserver/models
|
||||
|
||||
install -m 0644 "${PROJECT_DIR}/deploy/llmserver.service" "${SERVICE_FILE}"
|
||||
systemctl daemon-reload
|
||||
|
|
|
|||
|
|
@ -17,6 +17,6 @@ echo "The service was disabled and its unit was archived."
|
|||
echo "Application, configuration, model cache, and data were preserved:"
|
||||
echo " /opt/llmserver"
|
||||
echo " /etc/llmserver"
|
||||
echo " /var/cache/llmserver"
|
||||
echo " /var/lib/llmserver"
|
||||
echo " /srv/llmserver/models"
|
||||
echo " /srv/llmserver/data"
|
||||
echo "Remove those directories manually only if their contents are no longer needed."
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
import sys
|
||||
|
||||
from app.config import Settings
|
||||
from app.download_models import download_models
|
||||
|
||||
|
||||
def test_downloads_all_configured_models(monkeypatch, tmp_path):
|
||||
calls = []
|
||||
|
||||
faster_whisper = ModuleType("faster_whisper")
|
||||
faster_whisper.download_model = lambda model, cache_dir: calls.append(
|
||||
("whisper", model, cache_dir)
|
||||
)
|
||||
monkeypatch.setitem(sys.modules, "faster_whisper", faster_whisper)
|
||||
|
||||
class FakePipeline:
|
||||
@classmethod
|
||||
def from_pretrained(cls, model, token, cache_dir):
|
||||
calls.append(("pipeline", model, token, cache_dir))
|
||||
return cls()
|
||||
|
||||
pyannote_audio = ModuleType("pyannote.audio")
|
||||
pyannote_audio.Pipeline = FakePipeline
|
||||
monkeypatch.setitem(sys.modules, "pyannote.audio", pyannote_audio)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.download_models.snapshot_download",
|
||||
lambda **kwargs: calls.append(("snapshot", kwargs)),
|
||||
)
|
||||
|
||||
settings = Settings(
|
||||
_env_file=None,
|
||||
models_dir=tmp_path / "models",
|
||||
whisper_model="test/whisper",
|
||||
pyannote_model="test/pyannote",
|
||||
emotion_model="test/emotion",
|
||||
qwen_model="test/qwen",
|
||||
huggingface_token="secret",
|
||||
)
|
||||
|
||||
download_models(settings)
|
||||
|
||||
hf_cache = tmp_path / "models" / "huggingface"
|
||||
assert calls == [
|
||||
("whisper", "test/whisper", str(tmp_path / "models" / "faster-whisper")),
|
||||
(
|
||||
"snapshot",
|
||||
{"repo_id": "test/pyannote", "cache_dir": hf_cache, "token": "secret"},
|
||||
),
|
||||
(
|
||||
"snapshot",
|
||||
{"repo_id": "test/emotion", "cache_dir": hf_cache, "token": "secret"},
|
||||
),
|
||||
(
|
||||
"snapshot",
|
||||
{"repo_id": "test/qwen", "cache_dir": hf_cache, "token": "secret"},
|
||||
),
|
||||
("pipeline", "test/pyannote", "secret", hf_cache),
|
||||
]
|
||||
assert hf_cache.is_dir()
|
||||
assert (tmp_path / "models" / "faster-whisper").is_dir()
|
||||
Loading…
Reference in New Issue