-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathold_application.py
More file actions
48 lines (37 loc) · 1.29 KB
/
old_application.py
File metadata and controls
48 lines (37 loc) · 1.29 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
import os
import pickle
from flask import Flask, jsonify, request
import numpy as np
app = Flask(__name__)
with open(os.getenv("MODEL_PATH"), "rb") as rf:
clf = pickle.load(rf)
@app.route("/healthcheck", methods=["GET"])
def healthcheck():
msg = (
"this sentence is already halfway over, "
"and still hasn't said anything at all"
)
return jsonify({"message": msg})
@app.route("/predict", methods=["POST"])
def predict():
samples = request.get_json()["samples"]
data = np.array([sample["text"] for sample in samples])
probas = clf.predict_proba(data)
predictions = probas.argmax(axis=1)
return jsonify(
{
"predictions": (
np.tile(clf.classes_, (len(predictions), 1))[
np.arange(len(predictions)), predictions
].tolist()
),
"probabilities": probas[np.arange(len(predictions)), predictions].tolist()
}
)
@app.route("/predict/<label>", methods=["POST"])
def predict_label(label):
samples = request.get_json()["samples"]
data = np.array([sample["text"] for sample in samples])
probas = clf.predict_proba(data)
target_idx = clf.classes_.tolist().index(label)
return jsonify({"label": label, "probabilities": probas[:, target_idx].tolist()})