-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
35 lines (24 loc) · 1 KB
/
predict.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
# In this script we use the transformed input data and the model to make predicictions.
import os
# import pickle
import numpy as np
import mlflow
from loguru import logger
from fastapi import FastAPI
RUN_ID = os.getenv('RUN_ID', 'f0c63f97bbc74a75aa796be9d729df58')
logged_model = f's3://mlflow-semicon-clf/{RUN_ID}/artifacts/artifacts/'
# Load model as a PyFuncModel.
loaded_model = mlflow.pyfunc.load_model(logged_model)
app = FastAPI()
@app.post("/predict")
async def predict(row: dict):
row = row.values()
row = np.array(list(row)).reshape(1, -1)
pred = int(loaded_model.predict(row))
# have to convert to int because numpy is not supported by fastapi : https://github.com/tiangolo/fastapi/issues/2293
return {'prediction': pred, 'model_version': RUN_ID}
if __name__ == "__main__":
# Use this for debugging perpuses
logger.debug("Running in developement mode. Do not run like this in production")
import uvicorn
uvicorn.run(app, host="localhost", port=8001, log_level="debug")