Repository navigation
Expand file tree
/
Copy pathmodel_source.py
More file actions
206 lines (155 loc) · 7.15 KB
/
Copy pathmodel_source.py
File metadata and controls
206 lines (155 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Where a spaCy model comes from — the project's one extension seam.
The whole point of this service is that the model does *not* live in the Lambda
package. Something has to fetch it at runtime, and which "something" is exactly the
decision most likely to change: S3 today, a mounted EFS volume when cold starts
matter more than storage cost, a baked-in directory in local development or in a
container image, a private artifact store at a company that does not allow public
model downloads.
So the fetch is a one-method protocol with a registry in front of it::
register_model_source("efs", lambda settings: LocalModelSource(Path("/mnt/models")))
Nothing above this module knows which implementation it got.
"""
from __future__ import annotations
import shutil
from collections.abc import Callable
from pathlib import Path
from typing import Protocol, runtime_checkable
from nlp_lambda.archive import find_model_dir, safe_extract
from nlp_lambda.config import Settings
from nlp_lambda.errors import ConfigError, ModelUnavailableError
GITHUB_RELEASES = "https://github.com/explosion/spacy-models/releases/download"
@runtime_checkable
class ModelSource(Protocol):
"""Fetches a named spaCy model and returns a directory ``spacy.load`` accepts."""
#: Short identifier used in logs and in ``MODEL_SOURCE``.
name: str
def fetch(self, model_name: str, dest: Path) -> Path:
"""Make ``model_name`` available under ``dest`` and return the pipeline dir."""
def size_bytes(self, model_name: str) -> int | None:
"""Compressed size of the model, or ``None`` if it cannot be known cheaply.
Used to check the download against the free space in ``/tmp`` *before*
starting it.
"""
class LocalModelSource:
"""Read a model that is already on the filesystem.
Used by the test suite, by ``docker compose`` when a model is mounted, and by
anyone putting models on EFS: the fetch is then a no-op path resolution.
"""
name = "local"
def __init__(self, root: Path) -> None:
self.root = Path(root)
def fetch(self, model_name: str, dest: Path) -> Path: # noqa: ARG002 - protocol shape
candidate = self.root / model_name
if not candidate.exists():
# Allow LOCAL_MODEL_ROOT to point straight at a pipeline directory.
candidate = self.root
if not candidate.exists():
raise ModelUnavailableError(f"no model {model_name!r} under {self.root}")
return find_model_dir(candidate)
def size_bytes(self, model_name: str) -> int | None: # noqa: ARG002 - protocol shape
return 0 # nothing is copied, so nothing is consumed
class S3ModelSource:
"""Download ``s3://<bucket>/<prefix>/<model>.tar.gz`` and unpack it.
This is the production path and the reason the project exists: the 250 MB
unzipped Lambda package limit is not big enough for ``en_core_web_md`` and its
dependencies, but S3 has no such limit and ``/tmp`` gives us 512 MB to unpack
into.
"""
name = "s3"
def __init__(
self,
bucket: str,
prefix: str = "models",
*,
endpoint_url: str | None = None,
client: object | None = None,
) -> None:
self.bucket = bucket
self.prefix = prefix.strip("/")
self._endpoint_url = endpoint_url
self._client = client
@property
def client(self):
if self._client is None:
import boto3 # imported lazily: the local source needs no AWS SDK
self._client = boto3.client("s3", endpoint_url=self._endpoint_url)
return self._client
def key_for(self, model_name: str) -> str:
return f"{self.prefix}/{model_name}.tar.gz" if self.prefix else f"{model_name}.tar.gz"
def fetch(self, model_name: str, dest: Path) -> Path:
dest = Path(dest)
dest.mkdir(parents=True, exist_ok=True)
archive = dest / f"{model_name}.tar.gz"
key = self.key_for(model_name)
try:
self.client.download_file(self.bucket, key, str(archive))
except Exception as exc: # botocore raises a family of ClientErrors
raise ModelUnavailableError(
f"could not download s3://{self.bucket}/{key}: {exc}"
) from exc
try:
safe_extract(archive, dest)
return find_model_dir(dest)
finally:
# The tarball is dead weight in a 512 MB /tmp once unpacked.
archive.unlink(missing_ok=True)
def size_bytes(self, model_name: str) -> int | None:
try:
head = self.client.head_object(Bucket=self.bucket, Key=self.key_for(model_name))
except Exception:
return None
return int(head["ContentLength"])
class GitHubModelSource:
"""Download a release tarball from ``explosion/spacy-models``.
Not used at request time — Lambda should not depend on GitHub being up. It
exists so ``python -m nlp_lambda.cli upload`` can seed the S3 bucket, and so a
developer can populate a local cache without AWS.
"""
name = "github"
def __init__(self, base_url: str = GITHUB_RELEASES, session: object | None = None) -> None:
self.base_url = base_url.rstrip("/")
self._session = session
def url_for(self, model_name: str) -> str:
return f"{self.base_url}/{model_name}/{model_name}.tar.gz"
def download_archive(self, model_name: str, dest: Path) -> Path:
import requests
dest = Path(dest)
dest.mkdir(parents=True, exist_ok=True)
archive = dest / f"{model_name}.tar.gz"
session = self._session or requests
with session.get(self.url_for(model_name), stream=True, timeout=60) as response:
response.raise_for_status()
with open(archive, "wb") as handle:
shutil.copyfileobj(response.raw, handle)
return archive
def fetch(self, model_name: str, dest: Path) -> Path:
archive = self.download_archive(model_name, dest)
try:
safe_extract(archive, Path(dest))
return find_model_dir(Path(dest))
finally:
archive.unlink(missing_ok=True)
def size_bytes(self, model_name: str) -> int | None: # noqa: ARG002 - protocol shape
return None
SourceFactory = Callable[[Settings], ModelSource]
_REGISTRY: dict[str, SourceFactory] = {
"s3": lambda s: S3ModelSource(
s.require_s3_bucket(), s.s3_prefix, endpoint_url=s.s3_endpoint_url
),
"local": lambda s: LocalModelSource(s.require_local_model_root()),
"github": lambda _settings: GitHubModelSource(),
}
def register_model_source(name: str, factory: SourceFactory) -> None:
"""Add or replace a model source. The seam a future developer reaches for."""
_REGISTRY[name.strip().lower()] = factory
def available_model_sources() -> tuple[str, ...]:
return tuple(sorted(_REGISTRY))
def build_model_source(settings: Settings) -> ModelSource:
try:
factory = _REGISTRY[settings.model_source]
except KeyError as exc:
raise ConfigError(
f"unknown MODEL_SOURCE={settings.model_source!r}; "
f"known sources: {', '.join(available_model_sources())}"
) from exc
return factory(settings)