/
create-keras-dnn.py
executable file
·127 lines (107 loc) · 3.15 KB
/
create-keras-dnn.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
#!/usr/bin/env python
import os
import sys
import json
import argparse
import requests
from spylunking.log.setup_logging import build_colorized_logger
from antinex_utils.utils import ppj
name = 'create-keras-dnn'
log = build_colorized_logger(
name=name)
parser = argparse.ArgumentParser(
description=("Train a Deep Neural Network "
"with Django + Keras + Tensorflow + Celery + Redis"))
parser.add_argument(
"-f",
help="file to use default ./test-keras-dnn.json",
required=False,
dest="data_file")
args = parser.parse_args()
test_data_file = os.getenv(
"TEST_DATA",
"./test-keras-dnn.json")
if args.data_file:
if os.path.exists(args.data_file):
test_data_file = args.data_file
else:
log.error("Missing data_file: -f {}".format(
args.data_file))
sys.exit(1)
# end of assigning data file
url = os.getenv(
"ANTINEX_URL",
"http://localhost:8010")
username = os.getenv(
"API_USER",
"root")
password = os.getenv(
"API_PASS",
"123321")
if not os.path.exists(test_data_file):
log.error(("Failed to find test_data_file={}")
.format(test_data_file))
sys.exit(1)
# end of checking the path to the test json file
test_data = json.loads(open(test_data_file, "r").read())
auth_url = "{}/api-token-auth/".format(url)
resource_url = "{}/ml/".format(url)
use_headers = {
"Content-type": "application/json"
}
login_data = {
"username": username,
"password": password
}
# Login as the user:
log.info(("Logging in user url={}")
.format(
auth_url))
post_response = requests.post(
auth_url,
data=json.dumps(login_data),
headers=use_headers)
user_token = ""
if post_response.status_code == 200:
user_token = json.loads(post_response.text)["token"]
if user_token == "":
log.error(("Failed logging in as user={} - stopping"
"post_response={}")
.format(username,
post_response.text))
sys.exit(1)
else:
log.info(("logged in user={} token={}")
.format(username,
user_token))
# end if/else
log.info("building post data")
use_headers = {
"Content-type": "application/json",
"Authorization": "JWT {}".format(user_token)
}
log.info(("Running ML Job url={} "
"test_data={}")
.format(resource_url,
test_data))
post_response = requests.post(resource_url,
data=json.dumps(test_data),
headers=use_headers)
if post_response.status_code != 201 \
and post_response.status_code != 200:
log.error(("Failed with Post response status={} reason={}")
.format(post_response.status_code,
post_response.reason))
log.error("Details:\n{}".format(post_response.text))
sys.exit(1)
else:
log.info(("SUCCESS - Post Response status={} reason={}")
.format(post_response.status_code,
post_response.reason))
as_json = True
record = {}
if as_json:
record = json.loads(post_response.text)
log.info(ppj(record))
# end of post for running an ML Job
sys.exit(0)