human
2. Implement Predictor Interface
Interface Spec: https://cloud.google.com/ml-engine/docs/tensorflow/custom-prediction-routines#predictor-class
This tells AI Platform how to load the model artifacts, and is where we specify our custom prediction code.
Note: the correct PROJECT_ID will automatically be inserted using the bash sed command in the subsequent cell."
Please write code following the instructions in jupyter notebook style.
 ... What is a code to 2?
assistant
#Here is code to 2.

%%writefile predictor.py
import tensorflow as tf
from google.cloud import bigquery

PROJECT_ID = 'will_be_replaced'

class TaxifarePredictor(object):
    def __init__(self, predict_fn):
      self.predict_fn = predict_fn   
    
    def predict(self, instances, **kwargs):
        bq = bigquery.Client(PROJECT_ID)
        query_string = """
        SELECT
          *
        FROM
          `taxifare.traffic_realtime`
        ORDER BY
          time DESC
        LIMIT 1
        """
        trips = bq.query(query_string).to_dataframe()['trips_last_5min'][0]
        instances['trips_last_5min'] = [trips for _ in range(len(list(instances.items())[0][1]))]
        predictions = self.predict_fn(instances)
        return predictions['predictions'].tolist() # convert to list so it is JSON serialiable (requirement)
    

    @classmethod
    def from_path(cls, model_dir):
        predict_fn = tf.contrib.predictor.from_saved_model(model_dir,'predict')
        return cls(predict_fn)

!sed -i -e 's/will_be_replaced/{PROJECT_ID}/g' predictor.py