-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_models_for_deployment.py
83 lines (66 loc) · 2.3 KB
/
export_models_for_deployment.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
"""Export fly models for deployment
Usage:
export_models_for_deployment.py --lang=<language_code>
export_models_for_deployment.py (-h | --help)
export_models_for_deployment.py --version
Options:
-h --help Show this screen.
--version Show version.
--lang=<language_code> Language of trained models.
"""
import configparser
from docopt import docopt
import joblib
import numpy as np
from glob import glob
from os.path import join, dirname
from pathlib import Path
from shutil import copy, make_archive
from fly.fly import Fly
def read_config(lang):
config_path = lang+'.hyperparameters.cfg'
config = configparser.ConfigParser()
config.read(config_path)
return config_path, config
class DeployedFly:
def __init__(self):
self.pn_size = None
self.kc_size = None
self.wta = None
self.projections = None
self.top_words = None
self.hyperparameters = None
def export_fly(fly):
dfly = DeployedFly()
dfly.kc_size = fly.kc_size
dfly.wta = fly.wta
dfly.projections = fly.projections
dfly.top_words = fly.top_words
dfly.hyperparameters = fly.hyperparameters
return dfly
args = docopt(__doc__, version='Deploying the fruit fly, ver 0.1')
lang = args['--lang']
current_path = Path().resolve()
deployment_path = join(current_path, "./pears_deployment", lang)
Path(deployment_path).mkdir(exist_ok=True, parents=True)
config_path, config = read_config(lang)
ridge_path = config['RIDGE']['path']
expander_path = join(dirname(ridge_path), lang+'wiki.expansion.m')
print(expander_path)
fly_path = config['FLY']['path']
# Copy spm model
copy(join(current_path, f'spm/{lang}/{lang}wiki.model'), deployment_path)
copy(join(current_path, f'spm/{lang}/{lang}wiki.vocab'), deployment_path)
# Copy ridge models
copy(ridge_path, deployment_path)
copy(expander_path, deployment_path)
# Prepare fly model
with open(fly_path, 'rb') as f:
fly_model = joblib.load(glob(join(f'./fly/models/flies/{lang}','*fly.m'))[0])
deployment_fly = export_fly(fly_model)
with open(join(deployment_path,"fly.m"), "wb") as f:
joblib.dump(deployment_fly, f)
# Throw in the config file
copy(join(current_path, config_path), deployment_path)
# Zip folder
make_archive(lang+'_deployment', 'zip', deployment_path)