-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodel_deploy.py
More file actions
116 lines (97 loc) · 4.7 KB
/
model_deploy.py
File metadata and controls
116 lines (97 loc) · 4.7 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
import Algorithmia
from datetime import datetime
from git import Repo
from os import environ
from shutil import copyfile
from six.moves.urllib.parse import quote_plus
from tempfile import mkdtemp
from time import sleep
## CONFIGURABLE SETTINGS:
# using a rotating algo name for demo, but this can be any name you prefer; it will be created under https://algorithmia.com/algorithms/[YOUR_USERNAME]
ALGO_NAME = 'digit_recognition_'+datetime.now().strftime('%Y%m%d%H%M%S')
# pick any collection name you prefer; it will be created for you in https://algorithmia.com/data/hosted/
COLLECTION_NAME = 'digit_recognition'
# config your Algorithm details/settings as per https://docs.algorithmia.com/#create-an-algorithm
ALGORITHM_DETAILS = {
'label': 'Digit Recognition',
'tagline': 'Simple digit recognition model, for demo purposes only',
'summary': 'Demo only: Provide an image URL as input, and this model will attempt to identify the number shown.',
}
ALGORITHM_SETTINGS = {
'language': 'python3-1',
'source_visibility': 'closed',
'license': 'apl',
'network_access': 'full',
'pipeline_enabled': True,
'environment': 'cpu'
}
# config your publish settings as per https://docs.algorithmia.com/#publish-an-algorithm
ALGORITHM_VERSION_INFO = {
'sample_input': 'https://github.com/algorithmiaio/sample-apps/blob/master/algo-dev-demo/digit_recognition/images/digit_1.png?raw=true'
}
# path within this repo where the algo.py, requirements.txt, and model file are located
ALGO_TEMPLATE_PATH = 'jenkins_deploy_algorithmia/algorithm_template/'
# name of the model file to be uploaded to Hosted Data
MODEL_FILE = 'digits_classifier.pkl'
# if you need to update the contents of algo.py during deployment, do so here
def UPDATE_ALGORITHM_TEMPLATE(file_contents):
return file_contents.replace('data://username/demo/'+MODEL_FILE, data_path+'/'+MODEL_FILE)
## DEPLOYMENT SCRIPT:
# verify that environment keys are set
api_key = environ.get('ALGORITHMIA_MANAGEMENT_API_KEY')
username = environ.get('ALGORITHMIA_USERNAME')
if not api_key:
raise SystemExit('Please set the environment variable ALGORITHMIA_MANAGEMENT_API_KEY (key must have permission to manage algorithms)')
if not username:
raise SystemExit('Please set the environment variable ALGORITHMIA_USERNAME')
# set up Algorithmia client and path names
algo_full_name = username+'/'+ALGO_NAME
data_path = 'data://'+username+'/'+COLLECTION_NAME
client = Algorithmia.client(api_key)
algo = client.algo(algo_full_name)
# create Hosted Data collection
print('CREATING '+data_path)
if not client.dir(data_path).exists():
client.dir(data_path).create()
# upload the model file
print('UPLOADING model to '+data_path+'/'+MODEL_FILE)
client.file(data_path+'/'+MODEL_FILE).putFile(ALGO_TEMPLATE_PATH+MODEL_FILE)
# create the Algorithm
print('CREATING '+algo_full_name)
try:
print(algo.create(details=ALGORITHM_DETAILS, settings=ALGORITHM_SETTINGS))
except Exception as x:
raise SystemExit('ERROR: cannot create {}: if the Algorithm already exists and you wish to overwrite it, remove/ignore this step\n{}'.format(algo_full_name, x))
# git clone the created algorithm's repo into a temp directory
print('CLONING repo')
tmpdir = mkdtemp()
encoded_api_key = quote_plus(api_key)
algo_repo = 'https://{}:{}@git.algorithmia.com/git/{}.git'.format(username, encoded_api_key, algo_full_name)
cloned_repo = Repo.clone_from(algo_repo, tmpdir)
# add algo.py (updating as needed) and requirements.txt into repo
print('ADDING algorithm files')
algorithm_file_name='{}.py'.format(algo_full_name.split('/')[1])
copyfile(ALGO_TEMPLATE_PATH+'requirements.txt', tmpdir+'/requirements.txt')
with open(ALGO_TEMPLATE_PATH+'algo.py', 'r+') as file_in:
with open(tmpdir+'/src/'+algorithm_file_name, 'w+') as file_out:
file_out.write(UPDATE_ALGORITHM_TEMPLATE(file_in.read()))
cloned_repo.git.add(update=True)
cloned_repo.index.commit('Add algorithm files')
# push changes (implicitly causes Algorithm to recompile on server)
print('PUSHING repo')
origin = cloned_repo.remote(name='origin')
origin.push()
# wait for compile to complete, then publish the Algorithm
print('PUBLISHING '+algo_full_name)
sleep(15)
try:
results = algo.publish(version_info=ALGORITHM_VERSION_INFO)
except:
print('RETRYING: if this occurs repeatedly, increase the sleep() time before the PUBLISH step to allow for compilation time')
try:
sleep(60)
results = algo.publish(version_info=ALGORITHM_VERSION_INFO)
except Exception as x:
raise SystemExit('ERROR: unable to publish Algorithm: code will not compile, or compile takes too long\n{}'.format(x))
print(results)
print('DEPLOYED version {} to https://algorithmia.com/algorithms/{}'.format(results.version_info.semantic_version,algo_full_name))