generated from aicoe-aiops/project-template
-
Notifications
You must be signed in to change notification settings - Fork 51
/
wsgi.py
executable file
·168 lines (126 loc) · 4.57 KB
/
wsgi.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
#!/usr/bin/env python3
#
# Copyright(C) 2020, 2021 Red Hat, Thoth Team
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Thoth AIDevSecOps Tutorial App."""
import os
import logging
import time
import json
from version import __version__
from flask_cors import CORS
from flask import Flask
from flask import request
from flask import redirect
from flask import jsonify
from prometheus_flask_exporter import PrometheusMetrics
from prometheus_client import generate_latest
_LOGGER = logging.getLogger("aidevsecops-tutorial")
_LOGGER.info("Thoth AIDevSecOps Tutorial v%s", __version__)
_REDIRECT_URL = os.getenv(
"THOTH_AIDEVSECOPS_REDIRECT_URL",
"https://github.com/thoth-station/elyra-aidevsecops-tutorial/blob/master/README.md",
)
USE_NEURAL_MAGIC = bool(int(os.getenv("TUTORIAL_USE_NEURAL_MAGIC", 0)))
USE_PYTORCH = bool(int(os.getenv("TUTORIAL_USE_PYTORCH", 0)))
application = Flask("aidevsecops-tutorial")
# Add Cross Origin Request Policy to all
CORS(application)
prometheus_metrics = PrometheusMetrics(application, group_by="endpoint")
# static information as metric
prometheus_metrics.info(
"aidevsecops_tutorial_app_info", "App version deployed", version=__version__
)
if USE_NEURAL_MAGIC:
from src.neural_magic_model import Model as NeuralMagicModel
nm_model = NeuralMagicModel()
# custom metric to expose model version
model_version_metric = prometheus_metrics.info(
"aidevsecops_tutorial_model_info",
"Model version deployed",
version=nm_model.model_version, # label
)
elif USE_PYTORCH:
from src.pytorch_model import Model as PytorchModel
pytorch_model = PytorchModel()
# custom metric to expose model version
model_version_metric = prometheus_metrics.info(
"aidevsecops_tutorial_model_info",
"Model version deployed",
version=pytorch_model.model_version, # label
)
else:
from src.model import Model as TensorflowModel
model = TensorflowModel()
# custom metric to expose model version
model_version_metric = prometheus_metrics.info(
"aidevsecops_tutorial_model_info",
"Model version deployed",
version=model.model_version, # label
)
@application.before_first_request
def before_first_request_callback():
"""Register callback, runs before first request to this service."""
model_version_metric.set(1)
_LOGGER.info("Running once before first request to expose metric.")
@application.after_request
def extend_response_headers(response):
"""Just add my signature."""
response.headers["X-Thoth-AIDevSecOps-Tutorial-Version"] = f"v{__version__}"
return response
@application.route("/")
def main():
"""Show this to humans."""
return redirect(_REDIRECT_URL, code=308)
def _healthiness():
return (
jsonify({"status": "ready", "version": __version__}),
200,
{"ContentType": "application/json"},
)
@application.route("/readiness")
def api_readiness():
"""Report readiness for OpenShift readiness probe."""
return _healthiness()
@application.route("/liveness")
def api_liveness():
"""Report liveness for OpenShift readiness probe."""
return _healthiness()
@application.route("/predict", methods=["POST"])
def predict():
"""Evaluate prediction."""
image = request.get_json()["inputs"]
start = time.monotonic()
if USE_NEURAL_MAGIC:
prediction, probability = nm_model.predict(image=image)
elif USE_PYTORCH:
prediction, probability = pytorch_model.predict(image=image)
else:
prediction, probability = model.predict(image=image)
latency = time.monotonic() - start
return json.dumps(
{
"prediction": int(prediction),
"latency": latency,
"probability": float(probability),
}
)
@application.route("/metrics")
def metrics():
"""Return the Prometheus Metrics."""
return generate_latest().decode("utf-8")
if __name__ == "__main__":
_LOGGER.debug("Debug mode is on")
application.run(host="0.0.0.0", port=8080)