
    Wpf                     V   d Z ddl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 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 embedder task.    N)Optional)packet_creator)packet_getter)embeddings_pb2)embedder_options_pb2)text_embedder_graph_options_pb2)embedding_resultcosine_similaritybase_options)	task_info)doc_controls)base_text_task_apiembeddings_out
EMBEDDINGStext_inTEXTz4mediapipe.tasks.text.text_embedder.TextEmbedderGraphc                   x    e Zd ZU dZeed<   dZee         ed<   dZ	ee         ed<   e
j        defd            ZdS )TextEmbedderOptionsa  Options for the text embedder task.

  Attributes:
    base_options: Base options for the text embedder task.
    l2_normalize: Whether to normalize the returned feature vector with L2 norm.
      Use this option only if the model does not already contain a native
      L2_NORMALIZATION TF Lite Op. In most cases, this is already the case and
      L2 norm is thus achieved through TF Lite inference.
    quantize: Whether the returned embedding should be quantized to bytes via
      scalar quantization. Embeddings are implicitly assumed to be unit-norm and
      therefore any dimension is guaranteed to have a value in [-1.0, 1.0]. Use
      the l2_normalize option if this is not the case.
  r   Nl2_normalizequantizereturnc                     | j                                         }t          | j        | j                  }t          ||          S )z1Generates an TextEmbedderOptions protobuf object.)r   r   )r   embedder_options)r   to_pb2_EmbedderOptionsProtor   r   _TextEmbedderGraphOptionsProto)selfbase_options_protoembedder_options_protos      i/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/tasks/python/text/text_embedder.pyr   zTextEmbedderOptions.to_pb2?   sZ     *11332&@ @ @ *'/1 1 1 1    )__name__
__module____qualname____doc___BaseOptions__annotations__r   r   boolr   r   do_not_generate_docsr   r    r#   r"   r   r   ,   s           !%,%%%!(HTN!!!$14 1 1 1 %$1 1 1r#   r   c                       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
ed	ej        d
ej        defd            ZdS )TextEmbedderao  Class that performs embedding extraction on text.

  This API expects a TFLite model with TFLite Model Metadata that contains the
  mandatory (described below) input tensors and output tensors. Metadata should
  contain the input process unit for the model's Tokenizer as well as input /
  output tensor metadata.

  Input tensors:
    (kTfLiteInt32)
    - 3 input tensors of size `[batch_size x bert_max_seq_len]` with names
      "ids", "mask", and "segment_ids" representing the input ids, mask ids, and
      segment ids respectively.
    - or 1 input tensor of size `[batch_size x max_seq_len]` representing the
      input ids.

  At least one output tensor with:
    (kTfLiteFloat32)
    - `N` components corresponding to the `N` dimensions of the returned
      feature vector for this output layer.
    - Either 2 or 4 dimensions, i.e. `[1 x N]` or `[1 x 1 x 1 x N]`.
  
model_pathr   c                 l    t          |          }t          |          }|                     |          S )a  Creates an `TextEmbedder` object from a TensorFlow Lite model and the default `TextEmbedderOptions`.

    Args:
      model_path: Path to the model.

    Returns:
      `TextEmbedder` object that's created from the model file and the default
      `TextEmbedderOptions`.

    Raises:
      ValueError: If failed to create `TextEmbedder` 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)clsr/   r   optionss       r"   create_from_model_pathz#TextEmbedder.create_from_model_pathb   s8       <<<L!|<<<G""7+++r#   r4   c                     t          t          d                    t          t          g          gd                    t
          t          g          g|          } | |                                          S )a  Creates the `TextEmbedder` object from text embedder options.

    Args:
      options: Options for the text embedder task.

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

    Raises:
      ValueError: If failed to create `TextEmbedder` object from
        `TextEmbedderOptions` 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_EMBEDDINGS_TAG_EMBEDDINGS_OUT_STREAM_NAMEgenerate_graph_config)r3   r4   r   s      r"   r2   z TextEmbedder.create_from_optionsv   sp     #xx,@ ABBCHHo'BCDD
   I 3y..00111r#   textc                 &   | j                             t          t          j        |          i          }t          j                    }|                    t          j	        |t                                        t                              |          S )a4  Performs text embedding extraction on the provided text.

    Args:
      text: The input text.

    Returns:
      An embedding result object that contains a list of embeddings.

    Raises:
      ValueError: If any of the input arguments is invalid.
      RuntimeError: If text embedder failed to run.
    )_runnerprocessr@   r   create_stringr   EmbeddingResultCopyFromr   	get_protorB   TextEmbedderResultcreate_from_pb2)r   rD   output_packetsembedding_result_protos       r"   embedzTextEmbedder.embed   s      \))	~;DAABD DN ,;==##/J KLLN N N --.DEEEr#   uvc                 ,    t          j         ||          S )a?  Utility function to compute cosine similarity between two embedding entries.

    May return an InvalidArgumentError if e.g. the feature vectors are
    of different types (quantized vs. float), have different sizes, or have a
    an L2-norm of 0.

    Args:
      u: An embedding entry.
      v: An embedding entry.

    Returns:
      The cosine similarity for the two embeddings.

    Raises:
      ValueError: May return an error if e.g. the feature vectors are of
        different types (quantized vs. float), have different sizes, or have
        an L2-norm of 0.
    r
   )r3   rQ   rR   s      r"   r   zTextEmbedder.cosine_similarity   s    * .q!444r#   N)r$   r%   r&   r'   classmethodstrr5   r   r2   rL   rP   embedding_result_module	Embeddingfloatr   r,   r#   r"   r.   r.   K   s         , ,c ,n , , , ;,& 2(; 2 2 2 2 ;2.FF F F F F2 5 7 A 52<5AF5 5 5 ;5 5 5r#   r.   ).r'   dataclassestypingr   mediapipe.pythonr   r   .mediapipe.tasks.cc.components.containers.protor   .mediapipe.tasks.cc.components.processors.protor   +mediapipe.tasks.cc.text.text_embedder.protor   ,mediapipe.tasks.python.components.containersr	   rV   'mediapipe.tasks.python.components.utilsr   mediapipe.tasks.python.corer   base_options_moduler   task_info_module1mediapipe.tasks.python.core.optional_dependenciesr    mediapipe.tasks.python.text.corer   rI   rL   BaseOptionsr(   TextEmbedderGraphOptionsr   EmbedderOptionsr   TaskInfor<   rB   rA   r@   r?   r=   	dataclassr   BaseTextTaskApir.   r,   r#   r"   <module>rl      s   $ #           + + + + + + * * * * * * I I I I I I O O O O O O W W W W W W d d d d d d E E E E E E K K K K K K E E E E E E J J J J J J ? ? ? ? ? ?,< ".!@!Y ,< %	.   	I  1 1 1 1 1 1 1 1<q5 q5 q5 q5 q5%5 q5 q5 q5 q5 q5r#   