Skip to content

Instantly share code, notes, and snippets.

@yorrr78
Created January 7, 2024 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorrr78/011838086c59ebb15cd5bd45515d8d3a to your computer and use it in GitHub Desktop.
Save yorrr78/011838086c59ebb15cd5bd45515d8d3a to your computer and use it in GitHub Desktop.
MediumBlogging-Vertex AI custom container deployment-obslib.py
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