code: def build_model(
      dist: Union[Distribution, PixelCNN],
      input_shape: tuple = None,
      filepath: str = None,
  ) -> Tuple[tf.keras.Model, Union[Distribution, PixelCNN]]:
      """
      Create tf.keras.Model from TF distribution.
  
      Parameters
      ----------
      dist
          TensorFlow distribution.
      input_shape
          Input shape of the model.
      filepath
          File to load model weights from.
  
      Returns
      -------
      TensorFlow model.
      """
      x_in = Input(shape=input_shape)
      log_prob = dist.log_prob(x_in)
      model = Model(inputs=x_in, outputs=log_prob)
      model.add_loss(-tf.reduce_mean(log_prob))
      if isinstance(filepath, str):
          model.load_weights(filepath)
      return model, dist
  
imports: from functools import partial
  import logging
  import numpy
  import tensorflow
  from tensorflow.keras.layers import Input
  from tensorflow.keras.models import Model
  from tensorflow_probability.python.distributions.distribution import Distribution
  from typing import Callable, Dict, Tuple, Union
  from alibi_detect.models.tensorflow.pixelcnn import PixelCNN
  from alibi_detect.models.tensorflow.trainer import trainer
  from alibi_detect.base import BaseDetector, FitMixin, ThresholdMixin, outlier_prediction_dict
  from alibi_detect.utils.tensorflow.prediction import predict_batch
  from alibi_detect.utils.tensorflow.perturbation import mutate_categorical
  from alibi_detect.utils._types import OptimizerTF
label_desc: Use image node_id 1 for calling a global function with example usage: build_model(dist, input_shape, filepath) and returns: model,  dist