class_docstr: A base class for all interpreters that use the Saliency library.
class_docstr_tok: ["A","base","class","for","all","interpreters","that","use","the","Saliency","library","."]
docstr: Runs the component, given a model and input(s).
code: def run(
      self,
      inputs: list[JsonDict],
      model: lit_model.Model,
      dataset: lit_dataset.Dataset,
      model_outputs: Optional[list[JsonDict]] = None,
      config: Optional[JsonDict] = None,
  ) -> Optional[list[JsonDict]]:
      """Runs the component, given a model and input(s)."""
      input_spec = model.input_spec()
      output_spec = model.output_spec()
      config = config or {}
  
      if not inputs:
          return []
  
      # Find all fields required for the interpretation.
      supported_fields = find_supported_fields(input_spec, output_spec)
      if supported_fields is None:
          return None
  
      grad_field_key = supported_fields.grad_field_key
      image_field_key = supported_fields.image_field_key
      grad_target_field_key = supported_fields.grad_target_field_key
  
      if target_config := config.get(TARGET_INFO_KEY):
          preds_field_key = target_config["field"]
      else:
          preds_field_key = supported_fields.preds_field_key
  
      preds_field_spec = output_spec[preds_field_key]
      if not isinstance(preds_field_spec, _SUPPORTED_PRED_TYPES):
          logging.warning(
              "Image Salience is not compatible with field '%s'",
              preds_field_key,
          )
          return None
  
      # Determine the shape of gradients by calling the model with a single input
      # and extracting the shape from the gradient output.
      first_example_preds = list(model.predict([inputs[0]]))[0]
      grad_shape = first_example_preds[grad_field_key].shape
  
      # If it is a multiclass model, find the labels with respect to which we
      # should compute the gradients.
      if isinstance(preds_field_spec, types.MulticlassPreds):
          # Get class labels.
          label_vocab = list(preds_field_spec.vocab)
  
          if (target_config := config.get(TARGET_INFO_KEY)) and (
              target_class := target_config.get("index")
          ):
              grad_target_labels = [
                  label_vocab[target_class] for _ in inputs
              ]
          else:
              # Run the model in order to find the gradient target labels.
              outputs = list(model.predict(inputs))
              grad_target_labels = []
              for model_input, model_output in zip(inputs, outputs):
                  if model_input.get(grad_target_field_key) is not None:
                      grad_target_labels.append(
                          model_input[grad_target_field_key]
                      )
                  else:
                      max_idx = int(
                          np.argmax(model_output[preds_field_key])
                      )
                      grad_target_labels.append(label_vocab[max_idx])
      else:  # RegressionScore
          grad_target_labels = [None] * len(inputs)
  
      saliency_object = (
          self.get_saliency_object()
      )  # get this on class init?
      extra_saliency_params = self.get_extra_saliency_params(config)
      all_results = []
      for example, grad_target_label in zip(inputs, grad_target_labels):
          result = {}
          image_str = example[image_field_key]
          saliency_input = image_utils.convert_image_str_to_array(
              image_str=image_str, shape=grad_shape
          )
          call_model_func = get_call_model_func(
              model=model,
              model_input=example,
              image_field_key=image_field_key,
              grad_field_key=grad_field_key,
              grad_target_field_key=grad_target_field_key,
              grad_target_label=grad_target_label,
          )
          attribution = self.make_saliency_call(
              saliency_object=saliency_object,
              x_value=saliency_input,
              call_model_function=call_model_func,
              extra_saliency_params=extra_saliency_params,
          )
          if attribution.ndim == 3:
              attribution = attribution.sum(axis=2)
          viz_params = self.get_visualization_params()
          overlaid_image = image_utils.overlay_pixel_saliency(
              image_str, attribution, **viz_params
          )
          result[grad_field_key] = image_utils.convert_pil_to_image_str(
              overlaid_image
          )
          all_results.append(result)
      return all_results
  
imports: import abc
  from collections.abc import Callable
  from typing import Any, NamedTuple, Optional, cast
  from absl import logging
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.lib import image_utils
  from lit_nlp.lib import utils
  import numpy
  import saliency
label_desc: Use image node_id 1 for calling the SaliencyLibInterpreter obj's underlying member method code with example usage: obj.run(inputs, model, dataset, model_outputs, config) and returns: all_results, list, None, None