Ë
    »dæj"  ã            	       óÒ  — d Z ddl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 ej(                  Z G d„ dej,                  «      Z G d„ dej,                  «      Z ej2                  d ej4                  e«       ej4                  ej6                  «      g«       ej2                  dej6                  ej8                   ej4                  e	j:                  «      g«       ej2                  dej6                  g«       ej<                  d ej4                  e	j:                  «      gd«      fZej@                   G d„ d«      «       Z! G d„ d«      Z"y)zMediaPipe text embedder task.é    N)ÚOptional)Úembedding_result)Úembedding_result_c©Úcosine_similarity©Úbase_options)Úbase_options_c)Úmediapipe_c_bindings)Úmediapipe_c_utils)Úserial_dispatcherc                   óH   — e Zd ZdZdej
                  fdej
                  fgZy)Ú_EmbedderOptionsCzC struct for embedder options.Úl2_normalizeÚquantizeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚctypesÚc_boolÚ_fields_© ó    úl/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/mediapipe/tasks/python/text/text_embedder.pyr   r      s&   „ Ù&ð �v—}‘}Ð%Ø�6—=‘=Ð!ð�(r   r   c                   ó4   — e Zd ZdZdej
                  fdefgZy)Ú_TextEmbedderOptionsCz#C struct for text embedder options.r	   Úembedder_optionsN)r   r   r   r   Úbase_options_c_moduleÚBaseOptionsCr   r   r   r   r   r   r   (   s&   „ Ù+ð Ð,×9Ñ9Ð:ØÐ,Ð-ð�(r   r   ÚMpTextEmbedderCreateÚMpTextEmbedderEmbedÚMpTextEmbedderCloseÚMpTextEmbedderCloseResultc                   ód   — e Zd ZU dZej
                  ed<   dZee	   ed<   dZ
ee	   ed<   defd„Zy)Ú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	   Nr   r   Úreturnc                 óÊ   — | j                   j                  «       }t        | j                  �| j                  nd| j                  �| j                  nd¬«      }t        ||¬«      S )z/Generates a ctypes TextEmbedderOptionsC object.F)r   r   )r	   r   )r	   Ú	to_ctypesr   r   r   r   )Úselfr
   Úembedder_options_cs      r   r)   zTextEmbedderOptions.to_ctypesb   sb   € à×&Ñ&×0Ñ0Ó2€NÜ*à×ÑÐ(ð ×&Ò&àØ"&§-¡-Ð";�—’Àô	Ðô !Ø#Ð6Hôð r   )r   r   r   r   Úbase_options_moduleÚBaseOptionsÚ__annotations__r   r   Úboolr   r   r)   r   r   r   r&   r&   O   s>   … ñð $×/Ñ/Ó/Ø!%€,�˜‘Ó%Ø!€(ˆH�T‰NÓ!ðÐ.ô r   r&   c                   ó<  — e Zd ZU dZej
                  ed<   ej                  ed<   dej
                  dej                  fd„Z	e
dedd fd	„«       Ze
d
edd fd„«       Zdedej                   fd„Ze
dej$                  dej$                  defd„«       Zd„ Zd„ Zd„ Zd„ Zy)Ú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]`.
  Ú_libÚ_handleÚlibÚhandlec                 ó    — || _         || _        y ©N)r2   Ú_embedder_handle)r*   r4   r5   s      r   Ú__init__zTextEmbedder.__init__‰   s   € ð
 €D„IØ"€DÕr   Ú
model_pathr'   c                 óh   — t        j                  |¬«      }t        |¬«      }| j                  |«      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-   r&   Úcreate_from_options)Úclsr:   r	   Úoptionss       r   Úcreate_from_model_pathz#TextEmbedder.create_from_model_path‘   s/   € ô  '×2Ñ2ÀJÔO€LÜ!¨|Ô<€GØ×"Ñ" 7Ó+Ð+r   r?   c                 ó  — t        j                  t        «      }|j                  «       }t	        j
                  «       }|j                  t	        j                  |«      t	        j                  |«      «       t        ||¬«      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.
    )r4   r5   )	r   Úload_shared_libraryÚ_CTYPES_SIGNATURESr)   r   Úc_void_pr!   Úbyrefr1   )r>   r?   r4   Úctypes_optionsÚembedder_handles        r   r=   z TextEmbedder.create_from_options¥   sc   € ô ×
2Ñ
2Ô3EÓ
F€CØ×&Ñ&Ó(€NÜ—o‘oÓ'€OØ×ÑÜ�‰�^Ó$¤f§l¡l°?Ó&Côô ˜C¨Ô8Ð8r   Útextc                 óZ  — t        j                  «       }| j                  j                  | j                  |j                  d«      t        j                  |«      «       t        j                  j                  |«      }| j                  j                  t        j                  |«      «       |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.
    zutf-8)Úembedding_result_c_moduleÚEmbeddingResultCr2   r"   r8   Úencoder   rE   Úembedding_result_moduleÚEmbeddingResultÚfrom_ctypesr$   )r*   rH   Úctypes_resultÚpython_results       r   ÚembedzTextEmbedder.embed¼   s†   € ô  .×>Ñ>Ó@€MØ‡I�I×!Ñ!Ø×ÑØ�‰�GÓÜ�‰�]Ó#ôô 	 ×/Ñ/×;Ñ;Øó	
ð ð
 	‡I�I×'Ñ'¬¯©°]Ó(CÔDØÐr   Ú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   )r>   rS   rT   s      r   r   zTextEmbedder.cosine_similarityÚ   s   € ô0 ×.Ñ.¨q°!Ó4Ð4r   c                 óª   — | j                   rG| j                  j                  | j                   «       d| _         | j                  j                  «        yy)z'Shuts down the MediaPipe task instance.N)r8   r2   r#   Úclose©r*   s    r   rW   zTextEmbedder.closeô   s>   € à×ÒØ
‡i�i×#Ñ# D×$9Ñ$9Ô:Ø"€dÔØ
‡i�i‡o�oÕð r   c                 ó   — | S )z1Returns `self` upon entering the runtime context.r   rX   s    r   Ú	__enter__zTextEmbedder.__enter__û   s   € à€Kr   c                 ó$   — | j                  «        y)zFShuts down the MediaPipe task instance on exit of the context manager.N©rW   )r*   Úunused_exc_typeÚunused_exc_valueÚunused_tracebacks       r   Ú__exit__zTextEmbedder.__exit__ÿ   s   € à‡J�J…Lr   c                 ó$   — | j                  «        y r7   r\   rX   s    r   Ú__del__zTextEmbedder.__del__  s   € Ø‡J�J…Lr   N)r   r   r   r   r   ÚSerialDispatcherr.   r   rD   r9   ÚclassmethodÚstrr@   r&   r=   rM   rN   rR   Ú	EmbeddingÚfloatr   rW   rZ   r`   rb   r   r   r   r1   r1   p   sò   … ñð* 	×*Ñ*Ó*Ø�?‰?Óð#à×-Ñ-ð#ð �o‰oó#ð ð,¨cð ,°nò ,ó ð,ð& ð9Ð(;ð 9Àò 9ó ð9ð,àðð ×.Ñ.óð< ð5à	 ×	*Ñ	*ð5ð 
!×	*Ñ	*ð5ð ò	5ó ð5ò2òòór   r1   )#r   r   ÚdataclassesÚtypingr   Ú,mediapipe.tasks.python.components.containersr   rM   r   rJ   Ú'mediapipe.tasks.python.components.utilsr   Úmediapipe.tasks.python.corer	   r,   r
   r   r   r   r   rN   ÚTextEmbedderResultÚ	Structurer   r   ÚCStatusFunctionÚPOINTERrD   Úc_char_prK   Ú	CFunctionrC   Ú	dataclassr&   r1   r   r   r   ú<module>rt      s\  ðñ $Û Û Ý å dÝ hÝ EÝ KÝ OÝ <Ý 9Ý 9à,×<Ñ<Ð ô˜×(Ñ(ô ô˜F×,Ñ,ô ð &Ð×%Ñ%ØàˆF�N‰NÐ0Ó1ØˆF�N‰N˜6Ÿ?™?Ó+ð	
óð &Ð×%Ñ%Øà�O‰OØ�O‰OØˆF�N‰NÐ4×EÑEÓFð	
óð &Ð×%Ñ%Øà�O‰Oð	
óð  Ð×ÑØ#Ø	ˆ�‰Ð1×BÑBÓ	CÐDØóð-Ð ð< ×Ñ÷ð ó ð÷@Tò Tr   