human
4. Deployment
4.1. Prepare custom model prediction"
Please write code following the instructions in jupyter notebook style.
assistant
#Here is code to 4.

%%writefile custom_prediction.py

import os
import pickle
import numpy as np

from datetime import date
from google.cloud import logging

import tensorflow.keras as keras

class CustomModelPrediction(object):   
    def __init__(self, model, processor): 
        self._model = model
        self._processor = processor       
    
    def _postprocess(self, predictions):
        labels = ['negative', 'positive']
        return [
            {
                "label":labels[int(np.round(prediction))],
                "score":float(np.round(prediction, 4))
            } for prediction in predictions]
    
    def predict(self, instances, **kwargs):       
        preprocessed_data = self._processor.transform(instances)
        predictions =  self._model.predict(preprocessed_data)
        labels = self._postprocess(predictions)
        return labels

    @classmethod
    def from_path(cls, model_dir):       
        model = keras.models.load_model(
          os.path.join(model_dir,'keras_saved_model.h5'))
        with open(os.path.join(model_dir, 'processor_state.pkl'), 'rb') as f:
            processor = pickle.load(f)           
        return cls(model, processor)