
    ^Nj!                        U d dl mZmZmZmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZmZ  ed	d
ddd ed	      d       eddddd ed      d       eddddd ed      d       edd
ddd ed      d       eddddd ed      d      gZee   ed <    G d! d"eee         Z G d# d$ee         Zy%)&    )AnyIterableSequenceType)
NumpyArrayDevice)
ImageInputOnnxProvider)OnnxOutputContext)define_cache_dir	normalize)ImageEmbeddingBase)ImageEmbeddingWorkerOnnxImageModel)DenseModelDescriptionModelSourcezQdrant/clip-ViT-B-32-visioni   z4Image embeddings, Multimodal (text&image), 2021 yearmitg(\?)hfz
model.onnx)modeldimdescriptionlicense
size_in_GBsources
model_filezQdrant/resnet50-onnxi   z-Image embeddings, Unimodal (image), 2016 yearz
apache-2.0g?zQdrant/Unicom-ViT-B-16i   zYImage embeddings (more detailed than Unicom-ViT-B-32), Multimodal (text&image), 2023 yearg=
ףp=?zQdrant/Unicom-ViT-B-32z4Image embeddings, Multimodal (text&image), 2023 yeargQ?zjinaai/jina-clip-v1z4Image embeddings, Multimodal (text&image), 2024 yearzonnx/vision_model.onnxsupported_onnx_modelsc                   `    e Zd Zdddej                  ddddfdededz  dedz  dee   dz  de	ez  de
e   dz  d	e	d
edz  dedz  def fdZddZede
e   fd       Z	 	 ddeee   z  dededz  dedee   f
dZeded   fd       Zdeeef   dedeeef   fdZdededee   fdZ xZS )OnnxImageEmbeddingNF
model_name	cache_dirthreads	providerscuda
device_ids	lazy_load	device_idspecific_model_pathkwargsc
                    t        |   |||fi |
 || _        || _        | j	                  |
      | _        || _        || _        d| _        ||| _        n | j                  | j                  d   | _        | j                  |      | _
        t        t        |            | _        |	| _        | j                  | j                  | j                  | j                   | j                        | _        | j                  s| j%                          yy)a  
        Args:
            model_name (str): The name of the model to use.
            cache_dir (str, optional): The path to the cache directory.
                                       Can be set using the `FASTEMBED_CACHE_PATH` env variable.
                                       Defaults to `fastembed_cache` in the system's temp directory.
            threads (int, optional): The number of threads single onnxruntime session can use. Defaults to None.
            providers (Optional[Sequence[OnnxProvider]], optional): The list of onnxruntime providers to use.
                Mutually exclusive with the `cuda` and `device_ids` arguments. Defaults to None.
            cuda (Union[bool, Device], optional): Whether to use cuda for inference. Mutually exclusive with `providers`
                Defaults to Device.AUTO.
            device_ids (Optional[list[int]], optional): The list of device ids to use for data parallel processing in
                workers. Should be used with `cuda` equals to `True`, `Device.AUTO` or `Device.CUDA`, mutually exclusive
                with `providers`. Defaults to None.
            lazy_load (bool, optional): Whether to load the model during class initialization or on demand.
                Should be set to True when using multiple-gpu and parallel encoding. Defaults to False.
            device_id (Optional[int], optional): The device id to use for loading the model in the worker process.
            specific_model_path (Optional[str], optional): The specific path to the onnx model dir if it should be imported from somewhere else

        Raises:
            ValueError: If the model_name is not in the format <org>/<model> e.g. BAAI/bge-base-en.
        Nr   )local_files_onlyr'   )super__init__r"   r%   _select_exposed_session_options_extra_session_optionsr$   r#   r&   _get_model_descriptionmodel_descriptionstrr   r    _specific_model_pathdownload_model_local_files_only
_model_dirload_onnx_model)selfr   r    r!   r"   r#   r$   r%   r&   r'   r(   	__class__s              o/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastembed/image/onnx_embedding.pyr,   zOnnxImageEmbedding.__init__?   s    J 	YB6B""&*&J&J6&R# %	 &* &DN__(!__Q/DN!%!<!<Z!H-i89$7!--""NN!33 $ 9 9	 . 
 ~~  "     returnc           	          | j                  | j                  | j                  j                  | j                  | j
                  | j                  | j                  | j                         y)z&
        Load the onnx model.
        )	model_dirr   r!   r"   r#   r&   extra_session_optionsN)	_load_onnx_modelr5   r0   r   r!   r"   r#   r&   r.   )r7   s    r9   r6   z"OnnxImageEmbedding.load_onnx_model   sR     	oo--88LLnnnn"&"="= 	 	
r:   c                     t         S )z
        Lists the supported models.

        Returns:
            list[DenseModelDescription]: A list of DenseModelDescription objects containing the model information.
        )r   clss    r9   _list_supported_modelsz)OnnxImageEmbedding._list_supported_models   s
     %$r:   images
batch_sizeparallelc              +     K    | j                   d| j                  t        | j                        |||| j                  | j
                  | j                  | j                  | j                  | j                  d|E d{    y7 w)a  
        Encode a list of images into list of embeddings.
        We use mean pooling with attention so that the model can handle variable-length inputs.

        Args:
            images: Iterator of image paths or single image path to embed
            batch_size: Batch size for encoding -- higher values will use more memory, but be faster
            parallel:
                If > 1, data-parallel encoding will be used, recommended for offline encoding of large datasets.
                If 0, use all available cores.
                If None, don't use data-parallel processing, use default onnxruntime threading instead.

        Returns:
            List of embeddings, one per document
        )r   r    rD   rE   rF   r"   r#   r$   r*   r'   r>   N )
_embed_imagesr   r1   r    r"   r#   r$   r4   r2   r.   )r7   rD   rE   rF   r(   s        r9   embedzOnnxImageEmbedding.embed   s}     . &4%% 
$..)!nn!33 $ 9 9"&"="=
 
 	
 	
s   A;B=B>Bz ImageEmbeddingWorker[NumpyArray]c                     t         S N)OnnxImageEmbeddingWorkerrA   s    r9   _get_worker_classz$OnnxImageEmbedding._get_worker_class   s    ''r:   
onnx_inputc                     |S )z,
        Preprocess the onnx input.
        rH   )r7   rO   r(   s      r9   _preprocess_onnx_inputz)OnnxImageEmbedding._preprocess_onnx_input   s
     r:   outputc                 ,    t        |j                        S rL   )r   model_output)r7   rR   r(   s      r9   _post_process_onnx_outputz,OnnxImageEmbedding._post_process_onnx_output   s     ,,--r:   )r;   N)   N)__name__
__module____qualname__r   AUTOr1   intr   r
   boollistr   r,   r6   classmethodr   rC   r	   r   r   rJ   r   rN   dictrQ   r   rU   __classcell__)r8   s   @r9   r   r   >   s   
 !%"37$kk'+ $*.@#@# :	@#
 t@# L)D0@# Vm@# I$@# @# :@# !4Z@# @#D
 %t,A'B % % #	$
Xj11$
 $
 *	$

 $
 
*	$
L ($'I"J ( (sJ/;>	c:o	.'.36.	*	.r:   r   c                   $    e Zd ZdedededefdZy)rM   r   r    r(   r;   c                      t        d||dd|S )N   )r   r    r!   rH   )r   )r7   r   r    r(   s       r9   init_embeddingz'OnnxImageEmbeddingWorker.init_embedding   s'    ! 
!
 	
 	
r:   N)rW   rX   rY   r1   r   r   rd   rH   r:   r9   rM   rM      s$    
 
 
 
Pb 
r:   rM   N)typingr   r   r   r   fastembed.common.typesr   r   fastembed.commonr	   r
   fastembed.common.onnx_modelr   fastembed.common.utilsr   r   $fastembed.image.image_embedding_baser    fastembed.image.onnx_image_modelr   r   "fastembed.common.model_descriptionr   r   r   r]   __annotations__r   rM   rH   r:   r9   <module>rn      s   0 0 6 5 9 > C Q Q +J<= $C56 &o78 &J78 #J45+K.6 t12 .bQ.+^J-G Q.h
3J? 
r:   