
    _j>                         d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZmZmZmZmZmZ d dlZd dlZd dlZd dlmZ d dlZd dlZ G d de	e                   ZdS )    validate_config_schema)	Documents
EmbeddingsImagesis_documentis_image
EmbeddableEmbeddingFunctionSpace)ListDictAnyUnioncastOptionalN)BytesIOc            	       >   e Zd ZdZ	 	 	 ddee         dededdfd	Zd
edefdZ	e
defd            ZdefdZdee         fdZe
deeef         ddfd            Zdeeef         fdZdeeef         deeef         ddfdZe
deeef         ddfd            ZdS )RoboflowEmbeddingFunctionzi
    This class is used to generate embeddings for a list of texts or images using the Roboflow API.
    Nhttps://infer.roboflow.comCHROMA_ROBOFLOW_API_KEYapi_keyapi_urlapi_key_env_varreturnc                    |t          j        dt                     t          j        d          d| _        n|| _        |pt          j        | j                  | _        | j        st          d| j         d          || _        	 t          j
        d          | _        n# t          $ r t          d          w xY wt          j
        d          | _        dS )	ao  
        Create a RoboflowEmbeddingFunction.

        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the Roboflow API.
                Defaults to "CHROMA_ROBOFLOW_API_KEY".
            api_url (str, optional): The URL of the Roboflow API.
                Defaults to "https://infer.roboflow.com".
        NzDirect api_key configuration will not be persisted. Please use environment variables via api_key_env_var for persistent storage.ROBOFLOW_API_KEYzThe z! environment variable is not set.z	PIL.ImagezTThe PIL python package is not installed. Please install it with `pip install pillow`httpx)warningswarnDeprecationWarningosgetenvr   r   
ValueErrorr   	importlibimport_module	_PILImageImportError_httpx)selfr   r   r   s       {/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/utils/embedding_functions/roboflow_embedding_function.py__init__z"RoboflowEmbeddingFunction.__init__   s      M_"  
 9'((4#5D  #2D A")D,@"A"A| 	Nt+NNN   	&4[AADNN 	 	 	f  	
  -g66s   B! !B;inputc                 |   g }|D ]}t          |          r| j                            |          }t                      }|                    |d           t          j        |                                                              d          }dd|di}| j	        
                    | j         d| j         |          }|                                d	         }	|                    t          j        |	d
         t          j                             t%          |          rd|i}
| j	        
                    | j         d| j         |
          }|                                d	         }	|                    t          j        |	d
         t          j                             t'          t(          |          S )z
        Generate embeddings for the given documents or images.

        Args:
            input: Documents or images to generate embeddings for.

        Returns:
            Embeddings for the documents or images.
        JPEG)formatzutf-8imagebase64)typevaluez/clip/embed_image?api_key=)json
embeddingsr   )dtypetextz/clip/embed_text?api_key=)r	   r'   	fromarrayr   saver2   	b64encodegetvaluedecoder)   postr   r   r5   appendnparrayfloat32r   r   r   )r*   r-   r6   itemr1   bufferbase64_imageinfer_clip_payload_imageresresultinfer_clip_payload_texts              r+   __call__z"RoboflowEmbeddingFunction.__call__F   s    
 "	I "	ID~~ !I0066 

6&
111%/0A0ABBII'RR  (!- ,( k&&|MMt|MM1 '  
 L1!!"(6!9BJ"G"G"GHHHHT"" ID+' k&&|LLdlLL0 '  
 L1!!"(6!9BJ"G"G"GHHH J
+++    c                      dS )Nroboflow rN   rK   r+   namezRoboflowEmbeddingFunction.namey   s    zrK   c                     dS )NcosinerN   r*   s    r+   default_spacez'RoboflowEmbeddingFunction.default_space}   s    xrK   c                 
    g dS )N)rQ   l2iprN   rR   s    r+   supported_spacesz*RoboflowEmbeddingFunction.supported_spaces   s    %%%%rK   configz+EmbeddingFunction[Union[Documents, Images]]c                     |                      d          }|                      d          }||
J d            t          ||          S )Nr   r   FzThis code should not be reachedr   r   )getr   )rX   r   r   s      r+   build_from_configz+RoboflowEmbeddingFunction.build_from_config   s\     !**%677**Y''"go;;;;;(+W
 
 
 	
rK   c                      | j         | j        dS )NrZ   rZ   rR   s    r+   
get_configz$RoboflowEmbeddingFunction.get_config   s    #'#7DLQQQrK   
old_config
new_configc                     d S )NrN   )r*   r_   r`   s      r+   validate_config_updatez0RoboflowEmbeddingFunction.validate_config_update   s	     	rK   c                 &    t          | d           dS )z
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        rM   Nr   )rX   s    r+   validate_configz)RoboflowEmbeddingFunction.validate_config   s     	vz22222rK   )Nr   r   )__name__
__module____qualname____doc__r   strr,   r
   r   rJ   staticmethodrO   r   rS   r   rW   r   r   r\   r^   rb   rd   rN   rK   r+   r   r      s         "&38	*7 *7#*7 *7 	*7
 
*7 *7 *7 *7X1,j 1,Z 1, 1, 1, 1,f #    \u    &$u+ & & & & 
S#X
	6
 
 
 \
RDcN R R R RsCx.6:38n	    
3S#X 
34 
3 
3 
3 \
3 
3 
3rK   r   )*chromadb.utils.embedding_functions.schemasr   chromadb.api.typesr   r   r   r   r	   r
   r   r   typingr   r   r   r   r   r   r"   r%   r2   ior   numpyr@   r   r   rN   rK   r+   <module>rp      s6   M M M M M M	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 : 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 				                P3 P3 P3 P3 P3 1* = P3 P3 P3 P3 P3rK   