Created
January 7, 2024 23:59
-
-
Save yorrr78/011838086c59ebb15cd5bd45515d8d3a to your computer and use it in GitHub Desktop.
MediumBlogging-Vertex AI custom container deployment-obslib.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import joblib | |
import os | |
import numpy as np | |
import pandas as pd | |
import logging | |
import json | |
logging.basicConfig(level=logging.INFO) | |
cwd = os.path.abspath(os.path.dirname(__file__)) | |
def load_model(): | |
""" | |
Load the pre-trained machine learning model | |
Returns: | |
model: The loaded machine learning model | |
""" | |
given_path = "model" | |
model_path = os.path.abspath(os.path.join(cwd, given_path)) | |
model = joblib.load(os.path.abspath( | |
os.path.join(model_path, | |
'housing_price_model.joblib'))) | |
return model | |
def formatting(prediction): | |
"""Round the prediction to the nearest integer.""" | |
return np.around(prediction, 0) | |
def predict(instances): | |
"""Make predictions for a list of instances""" | |
model = load_model() | |
predictions = [] | |
for instance in instances: | |
df = pd.DataFrame.from_dict([json.loads(instance)]) | |
prediction = model.predict(df) | |
prediction = formatting(prediction) | |
predictions.append(prediction[0]) | |
return predictions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment