
    Ig                        d dl mZmZmZmZmZmZmZ d dl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	d
ddddidddddddddidddddddddidddd	dddddiddgZ G d deee	j0                           Z G d de      Zy)     )AnyDictIterableListOptionalSequenceTypeN)
ImageInputOnnxProvider)OnnxOutputContext)define_cache_dir	normalize)ImageEmbeddingBase)ImageEmbeddingWorkerOnnxImageModelz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?c                   x    e Zd Z	 	 	 	 	 	 	 ddedee   dee   deee      dedee	e      dedee   f fd	Z
ddZed
e	eeef      fd       Z	 	 ddededee   d
eej&                     fdZed
ed   fd       Zdeeej&                  f   d
eeej&                  f   fdZded
eej&                     fdZ xZS )OnnxImageEmbedding
model_name	cache_dirthreads	providerscuda
device_ids	lazy_load	device_idc	                    t        
|   |||fi |	 || _        || _        || _        || _        ||| _        n(| j                  | j                  d   | _        nd| _        | j                  |      | _        t        |      | _
        | 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 (bool, optional): Whether to use cuda for inference. Mutually exclusive with `providers`
                Defaults to False.
            device_ids (Optional[List[int]], optional): The list of device ids to use for data parallel processing in
                workers. Should be used with `cuda=True`, 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.

        Raises:
            ValueError: If the model_name is not in the format <org>/<model> e.g. BAAI/bge-base-en.
        Nr   )local_files_only)super__init__r    r#   r"   r!   r$   _get_model_descriptionmodel_descriptionr   r   download_model_local_files_only
_model_dirload_onnx_model)selfr   r   r   r    r!   r"   r#   r$   kwargs	__class__s             [/var/www/html/answerous/venv/lib/python3.12/site-packages/fastembed/image/onnx_embedding.pyr(   zOnnxImageEmbedding.__init__<   s    B 	YB6B"" %	  &DN__(!__Q/DN!DN!%!<!<Z!H))4--""DNNTE[E[ . 
 ~~  "     returnc                     | j                  | j                  | j                  d   | j                  | j                  | j
                  | j                         y)z&
        Load the onnx model.
        r   )	model_dirr   r   r    r!   r$   N)_load_onnx_modelr-   r*   r   r    r!   r$   )r/   s    r2   r.   z"OnnxImageEmbedding.load_onnx_modelv   sH     	oo--l;LLnnnn 	 	
r3   c                     t         S )z
        Lists the supported models.

        Returns:
            List[Dict[str, Any]]: A list of dictionaries containing the model information.
        )supported_onnx_modelsclss    r2   list_supported_modelsz(OnnxImageEmbedding.list_supported_models   s
     %$r3   images
batch_sizeparallelc              +      K    | j                   d| j                  t        | 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   r=   r>   r?   r    r!   r"   N )_embed_imagesr   strr   r    r!   r"   )r/   r=   r>   r?   r0   s        r2   embedzOnnxImageEmbedding.embed   sb     . &4%% 

$..)!nn

 

 
	
 
	
s   AA$A"A$r   c                     t         S N)OnnxImageEmbeddingWorkerr:   s    r2   _get_worker_classz$OnnxImageEmbedding._get_worker_class   s    ''r3   
onnx_inputc                     |S )z,
        Preprocess the onnx input.
        rA   )r/   rI   r0   s      r2   _preprocess_onnx_inputz)OnnxImageEmbedding._preprocess_onnx_input   s
     r3   outputc                 f    t        |j                        j                  t        j                        S rF   )r   model_outputastypenpfloat32)r/   rL   s     r2   _post_process_onnx_outputz,OnnxImageEmbedding._post_process_onnx_output   s"    ,,-44RZZ@@r3   )NNNFNFN)r4   N)   N)__name__
__module____qualname__rC   r   intr   r   boolr   r(   r.   classmethodr   r   r<   r
   r   rP   ndarrayrD   r	   rH   rK   r   rR   __classcell__)r1   s   @r2   r   r   ;   sk    $(!%6:*.#'8#8# C=8# #	8#
 H\238# 8# T#Y'8# 8# C=8#t
 %d4S>&: % % "&	!
!
 !
 3-	!
 
"**	!
F ($'="> ( (sBJJ/	c2::o	A0A AhrzzFZ Ar3   r   c                        e Zd ZdededefdZy)rG   r   r   r4   c                      t        d||dd|S )N   )r   r   r   rA   )r   )r/   r   r   r0   s       r2   init_embeddingz'OnnxImageEmbeddingWorker.init_embedding   s'    ! 
!
 	
 	
r3   N)rT   rU   rV   rC   r   r_   rA   r3   r2   rG   rG      s    
 
 
K] 
r3   rG   )typingr   r   r   r   r   r   r	   numpyrP   fastembed.commonr
   r   fastembed.common.onnx_modelr   fastembed.common.utilsr   r   $fastembed.image.image_embedding_baser    fastembed.image.onnx_image_modelr   r   r9   rZ   r   rG   rA   r3   r2   <module>rg      s    F F F  5 9 > C Q /M/
 #
 (F(
 #
 *r*
 #
 *M*
 #
E- `CA+^BJJ-G CAL
3 
r3   