-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (44 loc) · 1.44 KB
/
main.py
File metadata and controls
60 lines (44 loc) · 1.44 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
from fastapi import FastAPI, BackgroundTasks
import pathlib
import glob
from joblib import load, dump
from schema import IrisPredict, IrisTrain
import numpy as np
from sklearn.linear_model import LogisticRegression
from datetime import datetime
app = FastAPI(
title="My title",
description="My description",
version="0.0.1",
)
global clf
CLASSES = ["setosa", "versicolor", "virginica"]
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.on_event("startup")
async def load_model():
global clf
all_model_paths = glob.glob("./data/iris_*")
last_model = sorted(all_model_paths, reverse=True)[0]
clf = load(last_model)
@app.post("/iris/predict")
async def predict_iris(iris: IrisPredict):
return {
"predicted_classes": clf.predict(np.asarray([iris.data])).tolist(),
"predicted_probas": clf.predict_proba(np.asarray([iris.data])).tolist(),
"classes": CLASSES,
}
def retrain_model(X, y):
logreg = LogisticRegression()
logreg.fit(X, y)
dump(logreg, f'./data/iris_{datetime.now().strftime("%Y_%m_%d_%H_%M_%S")}')
@app.post("/iris/train")
async def train_iris_model(iris: IrisTrain, background_tasks: BackgroundTasks):
X = np.asarray(iris.data)
y = np.asarray(iris.targets)
background_tasks.add_task(retrain_model, X=X, y=y)
return {"message": "Notification sent in the background"}
@app.get("/iris/classes")
async def create_post(iris: IrisPredict):
return CLASSES