-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve_model_fastapi.py
62 lines (41 loc) · 1.32 KB
/
serve_model_fastapi.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
# Libraries:
from fastapi import FastAPI, Query, Body
import uvicorn
import joblib, os
import pickle
import numpy as np
import pandas as pd
from typing import List, Dict
from pydantic import BaseModel
import datetime
# Global variables:
MODEL_PATH = '/apps/works/Ficheros_Datos/comunes/objetos_modelo/simple_model_iris.pickle'
# # Custom data type:
# class MyModel(BaseModel):
# sepal_length : float
# sepal_width : float
# petal_length : float
# petal_width : float
# Load the model:
model_object = open(MODEL_PATH, "rb")
model = joblib.load(model_object)
# Initialize the app:
app = FastAPI(name = 'Machine Learning prediction service - experiment')
# Routes:
@app.get('/metadata/fecha_actual')
async def get_metadata():
now = datetime.datetime.now()
return {'fecha_actual':now.strftime("%Y-%m-%d %H:%M:%S")}
# ML model predictions:
@app.post("/predict")
async def predict(column_dict : Dict):
pred_proba = model.predict_proba(np.array(list(column_dict.values())).reshape(1, -1))
pred_class = np.argmax(pred_proba, axis = 1)
return {"pred_class": float(pred_class[0]),
"pred_prob": float(pred_proba[0][pred_class[0]])
}
# Create the app:
if __name__ == '__main__':
host = "10.99.191.249"
port = 8080
uvicorn.run(app, host = host, port = port)