-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
persistor.py
342 lines (262 loc) 路 11.3 KB
/
persistor.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import io
import logging
import os
import shutil
import tarfile
from builtins import object
from typing import Optional, Tuple, List, Text
from rasa_nlu.config import RasaNLUModelConfig
logger = logging.getLogger(__name__)
def get_persistor(name):
# type: (Text) -> Optional[Persistor]
"""Returns an instance of the requested persistor.
Currently, `aws`, `gcs` and `azure` are supported"""
if name == 'aws':
return AWSPersistor(os.environ.get("BUCKET_NAME"),
os.environ.get("AWS_ENDPOINT_URL"))
if name == 'gcs':
return GCSPersistor(os.environ.get("BUCKET_NAME"))
if name == 'azure':
return AzurePersistor(os.environ.get("AZURE_CONTAINER"),
os.environ.get("AZURE_ACCOUNT_NAME"),
os.environ.get("AZURE_ACCOUNT_KEY"))
return None
class Persistor(object):
"""Store models in cloud and fetch them when needed"""
def persist(self, model_directory, model_name, project):
# type: (Text) -> None
"""Uploads a model persisted in the `target_dir` to cloud storage."""
if not os.path.isdir(model_directory):
raise ValueError("Target directory '{}' not "
"found.".format(model_directory))
file_key, tar_path = self._compress(
model_directory, model_name, project)
self._persist_tar(file_key, tar_path)
def retrieve(self, model_name, project, target_path):
# type: (Text) -> None
"""Downloads a model that has been persisted to cloud storage."""
tar_name = self._tar_name(model_name, project)
self._retrieve_tar(tar_name)
self._decompress(tar_name, target_path)
def list_models(self, project):
# type: (Text) -> List[Text]
"""Lists all the trained models of a project."""
raise NotImplementedError
def list_projects(self):
# type: () -> List[Text]
"""Lists all projects."""
raise NotImplementedError
def _retrieve_tar(self, filename):
# type: (Text) -> Text
"""Downloads a model previously persisted to cloud storage."""
raise NotImplementedError("")
def _persist_tar(self, filekey, tarname):
# type: (Text, Text) -> None
"""Uploads a model persisted in the `target_dir` to cloud storage."""
raise NotImplementedError("")
def _compress(self, model_directory, model_name, project):
# type: (Text) -> Tuple[Text, Text]
"""Creates a compressed archive and returns key and tar."""
import tempfile
dirpath = tempfile.mkdtemp()
base_name = self._tar_name(model_name, project, include_extension=False)
tar_name = shutil.make_archive(os.path.join(dirpath, base_name),
'gztar',
root_dir=model_directory,
base_dir=".")
file_key = os.path.basename(tar_name)
return file_key, tar_name
@staticmethod
def _project_prefix(project):
# type: (Text) -> Text
p = project or RasaNLUModelConfig.DEFAULT_PROJECT_NAME
return '{}___'.format(p)
@staticmethod
def _project_and_model_from_filename(filename):
# type: (Text) -> (Text, Text)
split = filename.split("___")
if len(split) > 1:
model_name = split[1].replace(".tar.gz", "")
return split[0], model_name
else:
return split[0], ""
@staticmethod
def _tar_name(model_name, project, include_extension=True):
# type: (Text, Text, bool) -> Text
ext = ".tar.gz" if include_extension else ""
return '{p}{m}{ext}'.format(p=Persistor._project_prefix(project),
m=model_name, ext=ext)
@staticmethod
def _decompress(compressed_path, target_path):
# type: (Text, Text) -> None
with tarfile.open(compressed_path, "r:gz") as tar:
tar.extractall(
target_path) # project dir will be created if it not exists
class AWSPersistor(Persistor):
"""Store models on S3.
Fetches them when needed, instead of storing them on the local disk."""
def __init__(self, bucket_name, endpoint_url=None):
# type: (Text, Optional[Text]) -> None
import boto3
super(AWSPersistor, self).__init__()
self.s3 = boto3.resource('s3', endpoint_url=endpoint_url)
self._ensure_bucket_exists(bucket_name)
self.bucket_name = bucket_name
self.bucket = self.s3.Bucket(bucket_name)
def list_models(self, project):
# type: (Text) -> List[Text]
try:
prefix = self._project_prefix(project)
return [self._project_and_model_from_filename(obj.key)[1]
for obj in self.bucket.objects.filter(Prefix=prefix)]
except Exception as e:
logger.warn("Failed to list models for project {} in "
"AWS. {}".format(project, e))
return []
def list_projects(self):
# type: () -> List[Text]
try:
projects_set = {self._project_and_model_from_filename(obj.key)[0]
for obj in self.bucket.objects.filter()}
return list(projects_set)
except Exception:
logger.exception("Failed to list projects in AWS bucket {}. "
"Region: {}".format(self.bucket_name,
self.aws_region))
return []
def _ensure_bucket_exists(self, bucket_name):
import boto3
import botocore
bucket_config = {
'LocationConstraint': boto3.DEFAULT_SESSION.region_name}
try:
self.s3.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration=bucket_config)
except botocore.exceptions.ClientError:
pass # bucket already exists
def _persist_tar(self, file_key, tar_path):
# type: (Text, Text) -> None
"""Uploads a model persisted in the `target_dir` to s3."""
with open(tar_path, 'rb') as f:
self.s3.Object(self.bucket_name, file_key).put(Body=f)
def _retrieve_tar(self, target_filename):
# type: (Text) -> None
"""Downloads a model that has previously been persisted to s3."""
with io.open(target_filename, 'wb') as f:
self.bucket.download_fileobj(target_filename, f)
class GCSPersistor(Persistor):
"""Store models on Google Cloud Storage.
Fetches them when needed, instead of storing them on the local disk."""
def __init__(self, bucket_name):
from google.cloud import storage
super(GCSPersistor, self).__init__()
self.storage_client = storage.Client()
self._ensure_bucket_exists(bucket_name)
self.bucket_name = bucket_name
self.bucket = self.storage_client.bucket(bucket_name)
def list_models(self, project):
# type: (Text) -> List[Text]
try:
blob_iterator = self.bucket.list_blobs(
prefix=self._project_prefix(project))
return [self._project_and_model_from_filename(b.name)[1]
for b in blob_iterator]
except Exception as e:
logger.warn("Failed to list models for project {} in "
"google cloud storage. {}".format(project, e))
return []
def list_projects(self):
# type: () -> List[Text]
try:
blob_iterator = self.bucket.list_blobs()
projects_set = {self._project_and_model_from_filename(b.name)[0]
for b in blob_iterator}
return list(projects_set)
except Exception as e:
logger.warning("Failed to list projects in "
"google cloud storage. {}".format(e))
return []
def _ensure_bucket_exists(self, bucket_name):
from google.cloud import exceptions
try:
self.storage_client.create_bucket(bucket_name)
except exceptions.Conflict:
# bucket exists
pass
def _persist_tar(self, file_key, tar_path):
# type: (Text, Text) -> None
"""Uploads a model persisted in the `target_dir` to GCS."""
blob = self.bucket.blob(file_key)
blob.upload_from_filename(tar_path)
def _retrieve_tar(self, target_filename):
# type: (Text) -> None
"""Downloads a model that has previously been persisted to GCS."""
blob = self.bucket.blob(target_filename)
blob.download_to_filename(target_filename)
class AzurePersistor(Persistor):
"""Store models on Azure"""
def __init__(self,
azure_container,
azure_account_name,
azure_account_key):
from azure.storage import blob as azureblob
from azure.storage.common import models as storageModel
super(AzurePersistor, self).__init__()
self.blob_client = azureblob.BlockBlobService(
account_name=azure_account_name,
account_key=azure_account_key,
endpoint_suffix="core.windows.net")
self._ensure_container_exists(azure_container)
self.container_name = azure_container
def _ensure_container_exists(self, container_name):
# type: (Text) -> None
exists = self.blob_client.exists(container_name)
if not exists:
self.blob_client.create_container(container_name)
def list_models(self, project):
# type: (Text) -> List[Text]
try:
blob_iterator = self.blob_client.list_blobs(
self.container_name,
prefix=self._project_prefix(project)
)
return [self._project_and_model_from_filename(b.name)[1]
for b in blob_iterator]
except Exception as e:
logger.warning("Failed to list models for project {} in "
"azure blob storage. {}".format(project, e))
return []
def list_projects(self):
# type: () -> List[Text]
try:
blob_iterator = self.blob_client.list_blobs(
self.container_name,
prefix=None
)
projects_set = {self._project_and_model_from_filename(b.name)[0]
for b in blob_iterator}
return list(projects_set)
except Exception as e:
logger.warning("Failed to list projects in "
"Azure. {}".format(e))
return []
def _persist_tar(self, file_key, tar_path):
# type: (Text, Text) -> None
"""Uploads a model persisted in the `target_dir` to Azure."""
self.blob_client.create_blob_from_path(
self.container_name,
file_key,
tar_path
)
def _retrieve_tar(self, target_filename):
# type: (Text) -> None
"""Downloads a model that has previously been persisted to Azure."""
self.blob_client.get_blob_to_path(
self.container_name,
target_filename,
target_filename
)