Putu Widyantara Artanta Wibawa
01/12/2024, 9:06 AMmodel.py
import tensorflow as tf
import keras
import numpy as np
import bentoml
X = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
Y = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5], dtype=float)
model = keras.models.Sequential([
keras.layers.Dense(1, input_shape=(1,))
])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(X, Y, epochs=500, batch_size=1)
print(model.predict([6.0]))
saved_model = bentoml.tensorflow.save_model('linreg', model)
service.py
import numpy as np
import bentoml
from <http://bentoml.io|bentoml.io> import NumpyNdarray
runner = bentoml.tensorflow.get("linreg:latest").to_runner()
svc = bentoml.Service(name="linreg_service", runners=[runner])
@svc.api(input=NumpyNdarray(), output=NumpyNdarray())
def predict(input_series: np.ndarray):
return runner.run(input_series)