code: def specs_from_metrics(
      metrics: Optional[_MetricsOrLosses] = None,
      unweighted_metrics: Optional[_MetricsOrLosses] = None,
      model_names: Optional[List[str]] = None,
      output_names: Optional[List[str]] = None,
      output_weights: Optional[Dict[str, float]] = None,
      binarize: Optional[config_pb2.BinarizationOptions] = None,
      aggregate: Optional[config_pb2.AggregationOptions] = None,
      query_key: Optional[str] = None,
      include_example_count: Optional[bool] = None,
      include_weighted_example_count: Optional[bool] = None,
  ) -> List[config_pb2.MetricsSpec]:
      """Returns specs for tf.keras.metrics/losses or tfma.metrics classes.
  
      Examples:
  
        metrics_specs = specs_from_metrics(
          [
              tf.keras.metrics.BinaryAccuracy(),
              tfma.metrics.AUC(),
              tfma.metrics.MeanLabel(),
              tfma.metrics.MeanPrediction()
              ...
          ],
          unweighted=[
              tfma.metrics.Precision(),
              tfma.metrics.Recall()
          ])
  
        metrics_specs = specs_from_metrics({
          'output1': [
              tf.keras.metrics.BinaryAccuracy(),
              tfma.metrics.AUC(),
              tfma.metrics.MeanLabel(),
              tfma.metrics.MeanPrediction()
              ...
          ],
          'output2': [
              tfma.metrics.Precision(),
              tfma.metrics.Recall(),
          ]
        })
  
      Args:
        metrics: List of tfma.metrics.Metric, tf.keras.metrics.Metric, or
          tf.keras.losses.Loss. For multi-output models a dict of dicts may be
          passed where the first dict is indexed by the output_name. Whether these
          metrics are weighted or not will be determined based on whether the
          ModelSpec associated with the metrics contains example weight key settings
          or not.
        unweighted_metrics: Same as metrics only these metrics will not be weighted
          by example_weight regardless of the example weight key settings.
        model_names: Optional model names (if multi-model evaluation).
        output_names: Optional output names (if multi-output models). If the metrics
          are a dict this should not be set.
        output_weights: Optional output weights for creating overall metric
          aggregated across outputs (if multi-output model). If a weight is not
          provided for an output, it's weight defaults to 0.0 (i.e. output ignored).
        binarize: Optional settings for binarizing multi-class/multi-label metrics.
        aggregate: Optional settings for aggregating multi-class/multi-label
          metrics.
        query_key: Optional query key for query/ranking based metrics.
        include_example_count: True to add example_count metric. Default is True.
        include_weighted_example_count: True to add weighted example_count metric.
          Default is True. A weighted example count will be added per output for
          multi-output models.
  
      Returns:
        MetricsSpecs based on options provided. A separate spec is returned for
        weighted vs unweighted metrics. A separate spec is also returned for each
        output if a dict of metrics per output is passed.
      """
      if isinstance(metrics, dict) and output_names:
          raise ValueError(
              "metrics cannot be a dict when output_names is used: "
              "metrics={}, output_names={}".format(
                  metrics, output_names
              )
          )
      if (
          metrics
          and unweighted_metrics
          and isinstance(metrics, dict)
          != isinstance(unweighted_metrics, dict)
      ):
          raise ValueError(
              "metrics and unweighted_metrics must both be either dicts or lists: "
              f"metrics={metrics}, unweighted_metrics={unweighted_metrics}"
          )
  
      if isinstance(metrics, dict) or isinstance(
          unweighted_metrics, dict
      ):
          metrics_dict = metrics if isinstance(metrics, dict) else {}
          unweighted_metrics_dict = (
              unweighted_metrics
              if isinstance(unweighted_metrics, dict)
              else {}
          )
          specs = []
          output_names = set(metrics_dict) | set(
              unweighted_metrics_dict
          )
          for output_name in sorted(output_names):
              specs.extend(
                  specs_from_metrics(
                      metrics_dict.get(output_name),
                      unweighted_metrics=unweighted_metrics_dict.get(
                          output_name
                      ),
                      model_names=model_names,
                      output_names=[output_name],
                      binarize=binarize,
                      aggregate=aggregate,
                      include_example_count=include_example_count,
                      include_weighted_example_count=include_weighted_example_count,
                  )
              )
              include_example_count = False
          return specs
  
      if include_example_count is None:
          include_example_count = True
      if include_weighted_example_count is None:
          include_weighted_example_count = True
  
      # Add the computations for the example counts and weights since they are
      # independent of the model and class ID.
      specs = example_count_specs(
          model_names=model_names,
          output_names=output_names,
          output_weights=output_weights,
          include_example_count=include_example_count,
          include_weighted_example_count=include_weighted_example_count,
      )
  
      if metrics:
          specs.append(
              config_pb2.MetricsSpec(
                  metrics=[
                      config_from_metric(metric) for metric in metrics
                  ],
                  model_names=model_names,
                  output_names=output_names,
                  output_weights=output_weights,
                  binarize=binarize,
                  aggregate=aggregate,
                  example_weights=None,
                  query_key=query_key,
              )
          )
      if unweighted_metrics:
          specs.append(
              config_pb2.MetricsSpec(
                  metrics=[
                      config_from_metric(metric)
                      for metric in unweighted_metrics
                  ],
                  model_names=model_names,
                  output_names=output_names,
                  output_weights=output_weights,
                  binarize=binarize,
                  aggregate=aggregate,
                  example_weights=config_pb2.ExampleWeightOptions(
                      unweighted=True
                  ),
                  query_key=query_key,
              )
          )
  
      return specs
  
imports: import collections
  import importlib
  import json
  import re
  from typing import Any, Dict, FrozenSet, Iterable, Iterator, List, NamedTuple, Optional, Tuple, Type, Union
  import tensorflow
  from tensorflow_model_analysis.metrics import aggregation
  from tensorflow_model_analysis.metrics import binary_confusion_matrices
  from tensorflow_model_analysis.metrics import calibration
  from tensorflow_model_analysis.metrics import calibration_plot
  from tensorflow_model_analysis.metrics import confusion_matrix_metrics
  from tensorflow_model_analysis.metrics import confusion_matrix_plot
  from tensorflow_model_analysis.metrics import example_count
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.metrics import multi_class_confusion_matrix_plot
  from tensorflow_model_analysis.metrics import tf_metric_wrapper
  from tensorflow_model_analysis.metrics import weighted_example_count
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.slicer import slicer_lib
  from tensorflow_model_analysis.utils import model_util
  from tensorflow_metadata.proto.v0 import schema_pb2
label_desc: Use image node_id 4 for calling a global function with example usage: specs_from_metrics(metrics, unweighted_metrics, model_names, output_names, output_weights, binarize, aggregate, query_key, include_example_count, include_weighted_example_count) and returns: specs, specs