
    Wpf                     N   d Z ddlZddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ej        Zej        Zej        Zej         Z!ej"        Z#dZ$dZ%dZ&dZ'dZ(ej)         G d d                      Z* G d dej+                  Z,dS )zMediaPipe text classifier task.    N)OptionalList)packet_creator)packet_getter)classifications_pb2)classifier_options_pb2)!text_classifier_graph_options_pb2)classification_resultbase_options)	task_info)doc_controls)base_text_task_apiclassifications_outCLASSIFICATIONStext_inTEXTz8mediapipe.tasks.text.text_classifier.TextClassifierGraphc                       e Zd ZU dZeed<   dZee         ed<   dZ	ee
         ed<   dZee         ed<   dZeee                  ed<   dZeee                  ed<   ej        d	efd
            ZdS )TextClassifierOptionsa  Options for the text classifier task.

  Attributes:
    base_options: Base options for the text classifier task.
    display_names_locale: The locale to use for display names specified through
      the TFLite Model Metadata.
    max_results: The maximum number of top-scored classification results to
      return.
    score_threshold: Overrides the ones provided in the model metadata. Results
      below this value are rejected.
    category_allowlist: Allowlist of category names. If non-empty,
      classification results whose category name is not in this set will be
      filtered out. Duplicate or unknown category names are ignored. Mutually
      exclusive with `category_denylist`.
    category_denylist: Denylist of category names. If non-empty, classification
      results whose category name is in this set will be filtered out. Duplicate
      or unknown category names are ignored. Mutually exclusive with
      `category_allowlist`.
  r   Ndisplay_names_localemax_resultsscore_thresholdcategory_allowlistcategory_denylistreturnc                     | j                                         }t          | j        | j        | j        | j        | j                  }t          ||          S )z3Generates an TextClassifierOptions protobuf object.)r   r   r   r   r   )r   classifier_options)	r   to_pb2_ClassifierOptionsProtor   r   r   r   r    _TextClassifierGraphOptionsProto)selfbase_options_protoclassifier_options_protos      k/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/tasks/python/text/text_classifier.pyr   zTextClassifierOptions.to_pb2G   sk     *11336,20!6$ &  &  & ,'35 5 5 5    )__name__
__module____qualname____doc___BaseOptions__annotations__r   r   strr   intr   floatr   r   r   r   do_not_generate_docsr    r    r%   r$   r   r   +   s          & (,,,,#+x}###%)/8E?))),0htCy)000+/Xd3i(///$56 5 5 5 %$5 5 5r%   r   c                   b    e Zd ZdZededd fd            Zededd fd            Zdede	fdZ
d	S )
TextClassifiera7  Class that performs classification on text.

  This API expects a TFLite model with (optional) TFLite Model Metadata that
  contains the mandatory (described below) input tensors, output tensor,
  and the optional (but recommended) category labels as AssociatedFiles with
  type
  TENSOR_AXIS_LABELS per output classification tensor. Metadata is required for
  models with int32 input tensors because it contains the input process unit
  for the model's Tokenizer. No metadata is required for models with string
  input tensors.

  Input tensors:
    (kTfLiteInt32)
    - 3 input tensors of size `[batch_size x bert_max_seq_len]` representing
      the input ids, segment ids, and mask ids
    - or 1 input tensor of size `[batch_size x max_seq_len]` representing the
      input ids
    or (kTfLiteString)
    - 1 input tensor that is shapeless or has shape [1] containing the input
      string
  At least one output tensor with:
    (kTfLiteFloat32/kBool)
    - `[1 x N]` array with `N` represents the number of categories.
    - optional (but recommended) category labels as AssociatedFiles with type
      TENSOR_AXIS_LABELS, containing one label per line. The first such
      AssociatedFile (if any) is used to fill the `category_name` field of the
      results. The `display_name` field is filled from the AssociatedFile (if
      any) whose locale matches the `display_names_locale` field of the
      `TextClassifierOptions` used at creation time ("en" by default, i.e.
      English). If none of these are available, only the `index` field of the
      results will be filled.
  
model_pathr   c                 l    t          |          }t          |          }|                     |          S )a  Creates an `TextClassifier` object from a TensorFlow Lite model and the default `TextClassifierOptions`.

    Args:
      model_path: Path to the model.

    Returns:
      `TextClassifier` object that's created from the model file and the
      default `TextClassifierOptions`.

    Raises:
      ValueError: If failed to create `TextClassifier` object from the provided
        file such as invalid file path.
      RuntimeError: If other types of error occurred.
    )model_asset_pathr   )r*   r   create_from_options)clsr3   r   optionss       r$   create_from_model_pathz%TextClassifier.create_from_model_pathy   s8       <<<L#>>>G""7+++r%   r8   c                     t          t          d                    t          t          g          gd                    t
          t          g          g|          } | |                                          S )a  Creates the `TextClassifier` object from text classifier options.

    Args:
      options: Options for the text classifier task.

    Returns:
      `TextClassifier` object that's created from `options`.

    Raises:
      ValueError: If failed to create `TextClassifier` object from
        `TextClassifierOptions` such as missing the model.
      RuntimeError: If other types of error occurred.
    :)
task_graphinput_streamsoutput_streamstask_options)	_TaskInfo_TASK_GRAPH_NAMEjoin	_TEXT_TAG_TEXT_IN_STREAM_NAME_CLASSIFICATIONS_TAG_CLASSIFICATIONS_STREAM_NAMEgenerate_graph_config)r7   r8   r   s      r$   r6   z"TextClassifier.create_from_options   sq      #xx,@ ABBCHH*,HIJJ
   I 3y..00111r%   textc                 &   | j                             t          t          j        |          i          }t          j                    }|                    t          j	        |t                                        t                              |          S )aC  Performs classification on the input `text`.

    Args:
      text: The input text.

    Returns:
      A `TextClassifierResult` object that contains a list of text
      classifications.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If text classification failed to run.
    )_runnerprocessrD   r   create_stringr   ClassificationResultCopyFromr   	get_protorF   TextClassifierResultcreate_from_pb2)r!   rH   output_packetsclassification_result_protos       r$   classifyzTextClassifier.classify   s     \))	~;DAABD DN #6"J"L"L((/K LMMO O O  //0KLLLr%   N)r&   r'   r(   r)   classmethodr,   r9   r   r6   rP   rT   r0   r%   r$   r2   r2   W   s         B ,c ,6F , , , ;,& 2#82=M2 2 2 ;20M3 M#7 M M M M M Mr%   r2   )-r)   dataclassestypingr   r   mediapipe.pythonr   r   .mediapipe.tasks.cc.components.containers.protor   .mediapipe.tasks.cc.components.processors.protor   -mediapipe.tasks.cc.text.text_classifier.protor	   ,mediapipe.tasks.python.components.containersr
   classification_result_modulemediapipe.tasks.python.corer   base_options_moduler   task_info_module1mediapipe.tasks.python.core.optional_dependenciesr    mediapipe.tasks.python.text.corer   rM   rP   BaseOptionsr*   TextClassifierGraphOptionsr    ClassifierOptionsr   TaskInfor@   rF   rE   rD   rC   rA   	dataclassr   BaseTextTaskApir2   r0   r%   r$   <module>ri      s   & %     ! ! ! ! ! ! ! ! + + + + + + * * * * * * N N N N N N Q Q Q Q Q Q [ [ [ [ [ [ n n n n n n K K K K K K E E E E E E J J J J J J ? ? ? ? ? ?3H ".#D#_  0B %	4 (   	M  (5 (5 (5 (5 (5 (5 (5 (5VdM dM dM dM dM'7 dM dM dM dM dMr%   