§
    \zniÐu  ã                   ó   — d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZ d dlmZmZmZmZ d dlmZmZmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$ d
dl%m&Z&m'Z'  e$j(        e)¦  «        Z* e"¦   «         rd dl+Z+ e!¦   «         rd dl,m-Z- dZ.d+d„Z/dZ0 G d„ d¦  «        Z1 G d„ de1¦  «        Z2 G d„ de1¦  «        Z3de1fd„Z4d„ Z5dddddddd d!d"d#œ
Z6d$„ Z7d%„ Z8d,d'„Z9d(„ Z: G d)„ d*¦  «        Z;dS )-é    N)ÚAnyÚDictÚListÚOptionalÚUnion)Úcreate_repoÚhf_hub_downloadÚmetadata_updateÚupload_folder)ÚRepositoryNotFoundErrorÚbuild_hf_headersÚget_sessioné   )Úcustom_object_saveÚget_class_from_dynamic_moduleÚget_imports)Úis_pil_image)ÚAutoProcessor)ÚCONFIG_NAMEÚcached_fileÚis_accelerate_availableÚis_torch_availableÚis_vision_availableÚloggingé   )Úhandle_agent_inputsÚhandle_agent_outputs)Úsend_to_deviceútool_config.jsonc                 óü   — |�|S 	 t          | t          fddi|¤Ž dS # t          $ rH 	 t          | t          fddi|¤Ž Y dS # t          $ r t          d| › d�¦  «        ‚t          $ r Y Y dS w xY wt          $ r Y dS w xY w)NÚ	repo_typeÚspaceÚmodelú`z9` does not seem to be a valid repo identifier on the Hub.)r	   ÚTOOL_CONFIG_FILEr   ÚEnvironmentErrorÚ	Exception)Úrepo_idr!   Ú
hub_kwargss      úw/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/transformers/tools/base.pyÚget_repo_typer+   7   sç   € ØÐØÐðÝ˜Õ!1ÐSÐS¸WÐSÈ
ÐSÐSÐSØˆwøÝ"ð ð ð ð	Ý˜GÕ%5ÐWÐWÀÐWÈJÐWÐWÐWØ�7�7øÝ&ð 	kð 	kð 	kÝ"Ð#i wÐ#iÐ#iÐ#iÑjÔjÐjÝð 	ð 	ð 	Ø�7�7�7ð	øøøåð ð ð Øˆwˆwðøøøs,   † �
A;¨A Á &A+Á&A;Á*A+Á+A;Á:A;zufrom transformers import launch_gradio_demo
from {module_name} import {class_name}

launch_gradio_demo({class_name})
c                   ó$  — e Zd ZU dZdZeed<   dZeed<   ee         ed<   ee         ed<   d„ Z	d	„ Z
d
„ Zd„ Ze	 	 	 ddedee         dee         defd„¦   «         Z	 	 	 	 ddededee         deeeef                  dedefd„Zed„ ¦   «         ZdS )ÚToolaƒ  
    A base class for the functions used by the agent. Subclass this and implement the `__call__` method as well as the
    following class attributes:

    - **description** (`str`) -- A short description of what your tool does, the inputs it expects and the output(s) it
      will return. For instance 'This is a tool that downloads a file from a `url`. It takes the `url` as input, and
      returns the text contained in the file'.
    - **name** (`str`) -- A performative name that will be used for your tool in the prompt to the agent. For instance
      `"text-classifier"` or `"image_generator"`.
    - **inputs** (`List[str]`) -- The list of modalities expected for the inputs (in the same order as in the call).
      Modalitiies should be `"text"`, `"image"` or `"audio"`. This is only used by `launch_gradio_demo` or to make a
      nice space from your tool.
    - **outputs** (`List[str]`) -- The list of modalities returned but the tool (in the same order as the return of the
      call method). Modalitiies should be `"text"`, `"image"` or `"audio"`. This is only used by `launch_gradio_demo`
      or to make a nice space from your tool.

    You can also override the method [`~Tool.setup`] if your tool as an expensive operation to perform before being
    usable (such as loading a model). [`~Tool.setup`] will be called the first time you use your tool, but not at
    instantiation.
    zThis is a tool that ...ÚdescriptionÚ ÚnameÚinputsÚoutputsc                 ó   — d| _         d S )NF©Úis_initialized©ÚselfÚargsÚkwargss      r*   Ú__init__zTool.__init__m   s   € Ø#ˆÔÐÐó    c                 ó    — t          d¦  «        S )Nz-Write this method in your subclass of `Tool`.)ÚNotImplementedr6   s      r*   Ú__call__zTool.__call__p   s   € ÝÐMÑNÔNÐNr;   c                 ó   — d| _         dS )z°
        Overwrite this method here for any operation that is expensive and needs to be executed before you start using
        your tool. Such as loading a big model.
        TNr4   )r7   s    r*   Úsetupz
Tool.setups   s   € ð
 #ˆÔÐÐr;   c                 óN  — t          j        |d¬¦  «         | j        dk    rt          d| › d|› d�¦  «        ‚t	          | |¦  «        }| j        j        }|                     d¦  «        d         }|› d| j        j        › �}t           j         	                    |d	¦  «        }t           j         
                    |¦  «        r?t          |d
d¬¦  «        5 }t          j        |¦  «        }ddd¦  «         n# 1 swxY w Y   ni }|| j        | j        dœ}t          |dd¬¦  «        5 }|                     t          j        |dd¬¦  «        dz   ¦  «         ddd¦  «         n# 1 swxY w Y   t           j         	                    |d¦  «        }	t          |	dd¬¦  «        5 }|                     t$                               || j        j        ¬¦  «        ¦  «         ddd¦  «         n# 1 swxY w Y   t           j         	                    |d¦  «        }
g }|D ]$}|                     t+          |¦  «        ¦  «         Œ%t-          t/          |¦  «        ¦  «        }t          |
dd¬¦  «        5 }|                     d 	                    |¦  «        dz   ¦  «         ddd¦  «         dS # 1 swxY w Y   dS )a�  
        Saves the relevant code files for your tool so it can be pushed to the Hub. This will copy the code of your
        tool in `output_dir` as well as autogenerate:

        - a config file named `tool_config.json`
        - an `app.py` file so that your tool can be converted to a space
        - a `requirements.txt` containing the names of the module used by your tool (as detected when inspecting its
          code)

        You should only use this method to save tools that are defined in a separate module (not `__main__`).

        Args:
            output_dir (`str`): The folder in which you want to save your tool.
        T)Úexist_okÚ__main__z We can't save the code defining z in z{ as it's been defined in __main__. You have to put this code in a separate module so we can include it in the saved folder.ú.éÿÿÿÿr   Úrúutf-8©ÚencodingN)Ú
tool_classr.   r0   Úwr   )ÚindentÚ	sort_keysú
zapp.py)Úmodule_nameÚ
class_namezrequirements.txt)ÚosÚmakedirsÚ
__module__Ú
ValueErrorr   Ú	__class__ÚsplitÚ__name__ÚpathÚjoinÚisfileÚopenÚjsonÚloadr.   r0   ÚwriteÚdumpsÚAPP_FILE_TEMPLATEÚformatÚextendr   ÚlistÚset)r7   Ú
output_dirÚmodule_filesrO   Úlast_moduleÚ	full_nameÚconfig_fileÚfÚtool_configÚapp_fileÚrequirements_fileÚimportsÚmodules                r*   Úsavez	Tool.savez   s¼  € õ 	Œ�J¨Ð.Ñ.Ô.Ð.àŒ?˜jÒ(Ð(Ýðg°4ð gð g¸Zð gð gð gñô ð õ *¨$°
Ñ;Ô;ˆà”nÔ/ˆØ!×'Ò'¨Ñ,Ô,¨RÔ0ˆØ"Ð>Ð> T¤^Ô%<Ð>Ð>ˆ	õ ”g—l’l :Ð/AÑBÔBˆÝŒ7�>Š>˜+Ñ&Ô&ð 	Ý�k 3°Ð9Ñ9Ô9ð +¸QÝ"œi¨™lœl�ð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +øð ˆKà%.¸tÔ?OÐY]ÔYbÐcÐcˆÝ�+˜s¨WÐ5Ñ5Ô5ð 	N¸Ø�GŠG•D”J˜{°1ÀÐEÑEÔEÈÑLÑMÔMÐMð	Nð 	Nð 	Nñ 	Nô 	Nð 	Nð 	Nð 	Nð 	Nð 	Nð 	Nøøøð 	Nð 	Nð 	Nð 	Nõ ”7—<’< 
¨HÑ5Ô5ˆÝ�(˜C¨'Ð2Ñ2Ô2ð 	k°aØ�GŠGÕ%×,Ò,¸ÐQUÔQ_ÔQhÐ,ÑiÔiÑjÔjÐjð	kð 	kð 	kñ 	kô 	kð 	kð 	kð 	kð 	kð 	kð 	køøøð 	kð 	kð 	kð 	kõ œGŸLšL¨Ð5GÑHÔHÐØˆØ"ð 	0ð 	0ˆFØ�NŠN�; vÑ.Ô.Ñ/Ô/Ð/Ð/Ý•s˜7‘|”|Ñ$Ô$ˆÝÐ# S°7Ð;Ñ;Ô;ð 	/¸qØ�GŠG�D—I’I˜gÑ&Ô&¨Ñ-Ñ.Ô.Ð.ð	/ð 	/ð 	/ñ 	/ô 	/ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/øøøð 	/ð 	/ð 	/ð 	/ð 	/ð 	/sH   ÃC2Ã2C6Ã9C6Ä".EÅE Å#E Æ:GÇG#Ç&G#É!,JÊJÊ!JNFr(   Úmodel_repo_idÚtokenÚremotec                 ó,  ‡— |r/|€-t          ¦   «         }||vrt          d|› d�¦  «        ‚||         }g d¢Šˆfd„|                     ¦   «         D ¦   «         }t          |fi |¤Ž|d<   t	          |t
          fd|i|¤ddd	œ¤Ž}|du}	|€t	          |t          fd|i|¤ddd	œ¤Ž}|€t          |› d
�¦  «        ‚t          |d¬¦  «        5 }
t          j
        |
¦  «        }ddd¦  «         n# 1 swxY w Y   |	sd|vrt          |› d�¦  «        ‚|d         }n|}|d         }t          ||fd|i|¤Ž}t          |j        ¦  «        dk    r|d         |_        |j        |d         k    r/t                               |j        › d�¦  «         |d         |_        t          |j        ¦  «        dk    r|d         |_        |j        |d         k    r/t                               |j        › d�¦  «         |d         |_        |rt%          |||¬¦  «        S  ||fd|i|¤ŽS )aw  
        Loads a tool defined on the Hub.

        Args:
            repo_id (`str`):
                The name of the repo on the Hub where your tool is defined.
            model_repo_id (`str`, *optional*):
                If your tool uses a model and you want to use a different model than the default, you can pass a second
                repo ID or an endpoint url to this argument.
            token (`str`, *optional*):
                The token to identify you on hf.co. If unset, will use the token generated when running
                `huggingface-cli login` (stored in `~/.huggingface`).
            remote (`bool`, *optional*, defaults to `False`):
                Whether to use your tool by downloading the model or (if it is available) with an inference endpoint.
            kwargs (additional keyword arguments, *optional*):
                Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as
                `cache_dir`, `revision`, `subfolder`) will be used when downloading the files for your tool, and the
                others will be passed along to its init.
        Nú'Could not infer a default endpoint for ú:, you need to pass one using the `model_repo_id` argument.)Ú	cache_dirÚforce_downloadÚresume_downloadÚproxiesÚrevisionr!   Ú	subfolderÚlocal_files_onlyc                 ó$   •— i | ]\  }}|‰v ¯	||“ŒS © r   )Ú.0ÚkÚvÚhub_kwargs_namess      €r*   ú
<dictcomp>z!Tool.from_hub.<locals>.<dictcomp>Þ   s*   ø€ ÐOÐOÐO™t˜q !¸Ð>NÐ9NÐ9N�a˜Ð9NÐ9NÐ9Nr;   r!   Úuse_auth_tokenF)Ú%_raise_exceptions_for_missing_entriesÚ'_raise_exceptions_for_connection_errorszY does not appear to provide a valid configuration in `tool_config.json` or `config.json`.rG   rH   Úcustom_toolzO does not provide a mapping to custom tools in its configuration `config.json`.rJ   r   r0   z_ implements a different name in its configuration and class. Using the tool configuration name.r.   zm implements a different description in its configuration and class. Using the tool configuration description.©rr   rJ   rr   )Úget_default_endpointsrT   Úitemsr+   r   r%   r   r&   r[   r\   r]   r   Úlenr0   ÚloggerÚwarningrW   r.   Ú
RemoteTool)Úclsr(   rq   rr   rs   r9   Ú	endpointsr)   Úresolved_config_fileÚis_tool_configÚreaderÚconfigrˆ   rJ   rƒ   s                 @r*   Úfrom_hubzTool.from_hub°   sn  ø€ ð8 ð 	/�mÐ+Ý-Ñ/Ô/ˆIØ˜iÐ'Ð'Ý ð0¸gð 0ð 0ð 0ñô ð ð & gÔ.ˆMð	
ð 	
ð 	
Ðð PÐOÐOÐO v§|¢|¡~¤~ÐOÑOÔOˆ
õ #0°Ð"FÐ"F¸:Ð"FÐ"Fˆ
�;ÑÝ*ØÝð 
ð  
ð !ð 
ð ð	 
ð
 38Ø49ð 
ð  
ð  
ð  
Ðð .°TÐ9ˆØÐ'Ý#.ØÝð$ð $ð  %ð$ð ð	$ð
 7<Ø8=ð$ð $ð $ð $Ð ð  Ð'Ý"ØÐuÐuÐuñô ð õ Ð&°Ð9Ñ9Ô9ð 	'¸VÝ”Y˜vÑ&Ô&ˆFð	'ð 	'ð 	'ñ 	'ô 	'ð 	'ð 	'ð 	'ð 	'ð 	'ð 	'øøøð 	'ð 	'ð 	'ð 	'ð ð 	!Ø FÐ*Ð*Ý&ØÐoÐoÐoñô ð ð ! Ô/ˆKˆKà ˆKà  Ô.ˆ
Ý2°:¸wÐkÐkÐW\ÐkÐ`jÐkÐkˆ
åˆzŒÑÔ 1Ò$Ð$Ø)¨&Ô1ˆJŒOØŒ?˜k¨&Ô1Ò1Ð1Ý�NŠNØÔ&ð &ð &ð &ñô ð ð *¨&Ô1ˆJŒOåˆzÔ%Ñ&Ô&¨!Ò+Ð+Ø%0°Ô%?ˆJÔ"ØÔ! [°Ô%?Ò?Ð?Ý�NŠNØÔ&ð 2ð 2ð 2ñô ð ð &1°Ô%?ˆJÔ"àð 	QÝ˜m°5ÀZÐPÑPÔPÐPØˆz˜-Ð?Ð?¨uÐ?¸Ð?Ð?Ð?s   ÃC'Ã'C+Ã.C+úUpload toolÚcommit_messageÚprivateÚ	create_prÚreturnc                 óœ  — t          |||ddd¬¦  «        }|j        }t          |ddgid¬¦  «         t          j        ¦   «         5 }|                      |¦  «         t                               d|› d	d
                     t          j
        |¦  «        ¦  «        › �¦  «         t          |||||d¬¦  «        cddd¦  «         S # 1 swxY w Y   dS )a�  
        Upload the tool to the Hub.

        Parameters:
            repo_id (`str`):
                The name of the repository you want to push your tool to. It should contain your organization name when
                pushing to a given organization.
            commit_message (`str`, *optional*, defaults to `"Upload tool"`):
                Message to commit while pushing.
            private (`bool`, *optional*):
                Whether or not the repository created should be private.
            token (`bool` or `str`, *optional*):
                The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated
                when running `huggingface-cli login` (stored in `~/.huggingface`).
            create_pr (`bool`, *optional*, defaults to `False`):
                Whether or not to create a PR with the uploaded files or directly commit.
        Tr"   Úgradio)r(   rr   r™   rB   r!   Ú	space_sdkÚtagsÚtool©r!   z!Uploading the following files to z: ú,)r(   r˜   Úfolder_pathrr   rš   r!   N)r   r(   r
   ÚtempfileÚTemporaryDirectoryrp   r�   ÚinforY   rQ   Úlistdirr   )r7   r(   r˜   r™   rr   rš   Úrepo_urlÚwork_dirs           r*   Úpush_to_hubzTool.push_to_hub  s/  € õ2 Ø 5°'ÀDÐT[Ðgoð
ñ 
ô 
ˆð Ô"ˆÝ˜ &¨6¨(Ð!3¸wÐGÑGÔGÐGåÔ(Ñ*Ô*ð 	¨hà�IŠI�hÑÔÐÝ�KŠKÐg¸GÐgÐgÀsÇxÂxÕPRÔPZÐ[cÑPdÔPdÑGeÔGeÐgÐgÑhÔhÐhÝ ØØ-Ø$ØØ#Ø!ðñ ô ð		ð 	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	s   ÁA/CÃCÃCc                 óV   —  G d„ dt           ¦  «        }| j        |_         || ¦  «        S )z8
        Creates a [`Tool`] from a gradio tool.
        c                   ó   ‡ — e Zd Zˆ fd„Zˆ xZS )ú+Tool.from_gradio.<locals>.GradioToolWrapperc                 óx   •— t          ¦   «                              ¦   «          |j        | _        |j        | _        d S ©N)Úsuperr:   r0   r.   )r7   Ú_gradio_toolrU   s     €r*   r:   z4Tool.from_gradio.<locals>.GradioToolWrapper.__init__Q  s4   ø€ Ý‘”× Ò Ñ"Ô"Ð"Ø(Ô-�”	Ø#/Ô#;�Ô Ð Ð r;   )rW   rS   Ú__qualname__r:   Ú__classcell__©rU   s   @r*   ÚGradioToolWrapperr­   P  s8   ø€ € € € € ð<ð <ð <ð <ð <ð <ð <ð <ð <r;   rµ   )r-   Úrunr>   )Úgradio_toolrµ   s     r*   Úfrom_gradiozTool.from_gradioJ  sJ   € ð	<ð 	<ð 	<ð 	<ð 	<¥ñ 	<ô 	<ð 	<ð &1¤_ÐÔ"Ø Ð  Ñ-Ô-Ð-r;   )NNF)r—   NNF)rW   rS   r²   Ú__doc__r.   ÚstrÚ__annotations__r0   r   r:   r>   r@   rp   Úclassmethodr   Úboolr–   r   rª   Ústaticmethodr¸   r   r;   r*   r-   r-   Q   sŸ  € € € € € € ðð ð* 1€K�Ð0Ð0Ñ0Ø€Dˆ#€N€N�Nà�ŒIÐÐÑØ�#ŒYÐÐÑð$ð $ð $ðOð Oð Oð#ð #ð #ð4/ð 4/ð 4/ðl ð (,Ø#Øðk@ð k@àðk@ð   ”}ðk@ð ˜Œ}ð	k@ð
 ðk@ð k@ð k@ñ „[ðk@ð` ,Ø"&Ø,0Øð*ð *àð*ð ð*ð ˜$”ð	*ð
 ˜˜d C˜iÔ(Ô)ð*ð ð*ð 
ð*ð *ð *ð *ðX ð.ð .ñ „\ð.ð .ð .r;   r-   c                   ó,   — e Zd ZdZdd„Zd„ Zd„ Zd„ ZdS )r�   at  
    A [`Tool`] that will make requests to an inference endpoint.

    Args:
        endpoint_url (`str`, *optional*):
            The url of the endpoint to use.
        token (`str`, *optional*):
            The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when
            running `huggingface-cli login` (stored in `~/.huggingface`).
        tool_class (`type`, *optional*):
            The corresponding `tool_class` if this is a remote version of an existing tool. Will help determine when
            the output should be converted to another type (like images).
    Nc                 óN   — || _         t          ||¬¦  «        | _        || _        d S )N©rr   )Úendpoint_urlÚEndpointClientÚclientrJ   )r7   rÂ   rr   rJ   s       r*   r:   zRemoteTool.__init__i  s)   € Ø(ˆÔÝ$ \¸Ð?Ñ?Ô?ˆŒØ$ˆŒˆˆr;   c           	      óÆ  — |                      ¦   «         }t          |¦  «        dk    �rp| j        �ót          | j        t          ¦  «        r| j        j        }n| j        j        }t          j        |¦  «        j	        }d„ | 
                    ¦   «         D ¦   «         }|d         dk    r
|dd…         }t          |¦  «        t          |¦  «        k    r7t          | j        › dt          |¦  «        › dt          |¦  «        › d�¦  «        ‚t          ||¦  «        D ]
\  }}|||<   Œnvt          |¦  «        dk    rt          d	¦  «        ‚t          |¦  «        dk    rAt          |d         ¦  «        r"d
| j                             |d         ¦  «        iS d
|d         iS | 
                    ¦   «         D ]1\  }	}
t          |
¦  «        r| j                             |
¦  «        ||	<   Œ2d
|iS )aP  
        Prepare the inputs received for the HTTP client sending data to the endpoint. Positional arguments will be
        matched with the signature of the `tool_class` if it was provided at instantation. Images will be encoded into
        bytes.

        You can override this method in your custom class of [`RemoteTool`].
        r   Nc                 ój   — g | ]0\  }}|j         t          j        j        t          j        j        fv¯.|‘Œ1S r   )ÚkindÚinspectÚ_ParameterKindÚVAR_POSITIONALÚVAR_KEYWORD)r€   r�   Úps      r*   ú
<listcomp>z-RemoteTool.prepare_inputs.<locals>.<listcomp>  sD   € ð ð ð á˜˜1Ø”v¥gÔ&<Ô&KÍWÔMcÔMoÐ%pÐpÐpð àpÐpÐpr;   r7   r   z only accepts z arguments but z were given.z4A `RemoteTool` can only accept one positional input.r1   )ÚcopyrŒ   rJ   Ú
issubclassÚPipelineToolÚencoder>   rÈ   Ú	signatureÚ
parametersr‹   rT   Úzipr   rÄ   Úencode_image)r7   r8   r9   r1   Úcall_methodrÒ   rÓ   Úargr0   ÚkeyÚvalues              r*   Úprepare_inputszRemoteTool.prepare_inputsn  sñ  € ð —’‘”ˆÝˆt‰9Œ9�qŠ=‰=ØŒÐ*å˜dœo­|Ñ<Ô<ð ;Ø"&¤/Ô"8�K�Kà"&¤/Ô":�KÝ#Ô-¨kÑ:Ô:ÔE�	ðð à )§¢Ñ 1Ô 1ðñ ô �
ð
 ˜a”= FÒ*Ð*Ø!+¨A¨B¨B¤�JÝ�t‘9”9�s :™œÒ.Ð.Ý$Øœ?ÐqÐq½#¸j¹/¼/ÐqÐqÕZ]Ð^bÑZcÔZcÐqÐqÐqñô ð õ "% T¨:Ñ!6Ô!6ð 'ð '‘I�C˜Ø#&�F˜4‘L�Lð'å�T‘”˜Q’�Ý Ð!WÑXÔXÐXÝ�T‘”˜a’�Ý  Q¤Ñ(Ô(ð IØ$ d¤k×&>Ò&>¸tÀA¼wÑ&GÔ&GÐHÐHØ  $ q¤'Ð*Ð*à Ÿ,š,™.œ.ð 	>ð 	>‰JˆC�Ý˜EÑ"Ô"ð >Ø"œk×6Ò6°uÑ=Ô=��s‘øà˜&Ð!Ð!r;   c                 ó   — |S )z¢
        You can override this method in your custom class of [`RemoteTool`] to apply some custom post-processing of the
        outputs of the endpoint.
        r   ©r7   r2   s     r*   Úextract_outputszRemoteTool.extract_outputs™  s	   € ð
 ˆr;   c                 óð  — t          |i |¤Ž\  }}| j        d uo| j        j        dgk    } | j        |i |¤Ž}t	          |t
          ¦  «        r | j        di |¤d|i¤Ž}n|                      ||¬¦  «        }t	          |t          ¦  «        r6t          |¦  «        dk    r#t	          |d         t          ¦  «        r|d         }t          || j        �| j        j        nd ¦  «        }|  
                    |¦  «        S )NÚimageÚoutput_image)rà   r   r   r   )r   rJ   r2   rÚ   Ú
isinstanceÚdictrÄ   rc   rŒ   r   rÝ   )r7   r8   r9   rà   r1   r2   s         r*   r>   zRemoteTool.__call__   s  € Ý*¨DÐ;°FÐ;Ð;‰ˆˆfà”¨dÐ2Ð[°t´Ô7NÐSZÐR[Ò7[ˆØ$�Ô$ dÐ5¨fÐ5Ð5ˆÝ�f�dÑ#Ô#ð 	EØ!�d”kÐFÐF FÐFÐF¸ÐFÐFÐFˆGˆGà—k’k &°|�kÑDÔDˆGÝ�g�tÑ$Ô$ð 	!­¨W©¬¸Ò):Ð):½zÈ'ÐRSÌ*ÕVZÑ?[Ô?[Ð):Ø˜a”jˆGå& wÈ4Ì?ÐKf°´Ô0GÐ0GÐlpÑqÔqˆà×#Ò# GÑ,Ô,Ð,r;   )NNN)rW   rS   r²   r¹   r:   rÚ   rÝ   r>   r   r;   r*   r�   r�   Z  s`   € € € € € ðð ð%ð %ð %ð %ð
)"ð )"ð )"ðVð ð ð-ð -ð -ð -ð -r;   r�   c                   ód   ‡ — e Zd ZdZeZdZeZdZ	 	 	 	 	 	 	 d	ˆ fd„	Z	ˆ fd„Z
d„ Zd„ Zd„ Zd„ Zˆ xZS )
rÐ   a0	  
    A [`Tool`] tailored towards Transformer models. On top of the class attributes of the base class [`Tool`], you will
    need to specify:

    - **model_class** (`type`) -- The class to use to load the model in this tool.
    - **default_checkpoint** (`str`) -- The default checkpoint that should be used when the user doesn't specify one.
    - **pre_processor_class** (`type`, *optional*, defaults to [`AutoProcessor`]) -- The class to use to load the
      pre-processor
    - **post_processor_class** (`type`, *optional*, defaults to [`AutoProcessor`]) -- The class to use to load the
      post-processor (when different from the pre-processor).

    Args:
        model (`str` or [`PreTrainedModel`], *optional*):
            The name of the checkpoint to use for the model, or the instantiated model. If unset, will default to the
            value of the class attribute `default_checkpoint`.
        pre_processor (`str` or `Any`, *optional*):
            The name of the checkpoint to use for the pre-processor, or the instantiated pre-processor (can be a
            tokenizer, an image processor, a feature extractor or a processor). Will default to the value of `model` if
            unset.
        post_processor (`str` or `Any`, *optional*):
            The name of the checkpoint to use for the post-processor, or the instantiated pre-processor (can be a
            tokenizer, an image processor, a feature extractor or a processor). Will default to the `pre_processor` if
            unset.
        device (`int`, `str` or `torch.device`, *optional*):
            The device on which to execute the model. Will default to any accelerator available (GPU, MPS etc...), the
            CPU otherwise.
        device_map (`str` or `dict`, *optional*):
            If passed along, will be used to instantiate the model.
        model_kwargs (`dict`, *optional*):
            Any keyword argument to send to the model instantiation.
        token (`str`, *optional*):
            The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when
            running `huggingface-cli login` (stored in `~/.huggingface`).
        hub_kwargs (additional keyword arguments, *optional*):
            Any additional keyword argument to send to the methods that will load the data from the Hub.
    Nc                 ó˜  •— t          ¦   «         st          d¦  «        ‚t          ¦   «         st          d¦  «        ‚|€| j        €t	          d¦  «        ‚| j        }|€|}|| _        || _        || _        || _        || _	        |€i n|| _
        |�
|| j
        d<   || _        || j        d<   t          ¦   «                              ¦   «          d S )Nú/Please install torch in order to use this tool.z4Please install accelerate in order to use this tool.zHThis tool does not implement a default checkpoint, you need to pass one.Ú
device_maprr   )r   ÚImportErrorr   Údefault_checkpointrT   r#   Úpre_processorÚpost_processorÚdeviceræ   Úmodel_kwargsr)   r°   r:   )
r7   r#   ré   rê   rë   ræ   rì   rr   r)   rU   s
            €r*   r:   zPipelineTool.__init__Ü  sé   ø€ õ "Ñ#Ô#ð 	QÝÐOÑPÔPÐPå&Ñ(Ô(ð 	VÝÐTÑUÔUÐUàˆ=ØÔ&Ð.Ý Ð!kÑlÔlÐlØÔ+ˆEØÐ Ø!ˆMàˆŒ
Ø*ˆÔØ,ˆÔØˆŒØ$ˆŒØ".Ð"6˜B˜B¸LˆÔØÐ!Ø.8ˆDÔ˜lÑ+Ø$ˆŒØ#(ˆŒ˜Ñ å‰Œ×ÒÑÔÐÐÐr;   c                 óâ  •— t          | j        t          ¦  «        r" | j        j        | j        fi | j        ¤Ž| _        t          | j        t          ¦  «        r) | j        j        | j        fi | j        ¤| j        ¤Ž| _        | j	        €| j        | _	        n<t          | j	        t          ¦  «        r" | j
        j        | j	        fi | j        ¤Ž| _	        | j        €Q| j        �7t          | j        j                             ¦   «         ¦  «        d         | _        nt!          ¦   «         | _        | j        €| j                             | j        ¦  «         t%          ¦   «                              ¦   «          dS )z^
        Instantiates the `pre_processor`, `model` and `post_processor` if necessary.
        Nr   )rá   ré   rº   Úpre_processor_classÚfrom_pretrainedr)   r#   Úmodel_classrì   rê   Úpost_processor_classrë   ræ   rc   Úhf_device_mapÚvaluesÚget_default_deviceÚtor°   r@   )r7   rU   s    €r*   r@   zPipelineTool.setup  sO  ø€ õ �dÔ(­#Ñ.Ô.ð 	qØ!I Ô!9Ô!IÈ$ÔJ\Ð!pÐ!pÐ`dÔ`oÐ!pÐ!pˆDÔå�d”j¥#Ñ&Ô&ð 	nØ9˜Ô)Ô9¸$¼*ÐmÐmÈÔHYÐmÐ]aÔ]lÐmÐmˆDŒJàÔÐ&Ø"&Ô"4ˆDÔÐÝ˜Ô+­SÑ1Ô1ð 	tØ"K $Ô";Ô"KÈDÔL_Ð"sÐ"sÐcgÔcrÐ"sÐ"sˆDÔàŒ;ÐØŒÐ*Ý" 4¤:Ô#;×#BÒ#BÑ#DÔ#DÑEÔEÀaÔH�”�å0Ñ2Ô2�”àŒ?Ð"ØŒJ�MŠM˜$œ+Ñ&Ô&Ð&å‰Œ�Š‰Œˆˆˆr;   c                 ó,   — |                       |¦  «        S )zQ
        Uses the `pre_processor` to prepare the inputs for the `model`.
        )ré   )r7   Ú
raw_inputss     r*   rÑ   zPipelineTool.encode  s   € ð ×!Ò! *Ñ-Ô-Ð-r;   c                 óv   — t          j        ¦   «         5   | j        di |¤Žcddd¦  «         S # 1 swxY w Y   dS )z7
        Sends the inputs through the `model`.
        Nr   )ÚtorchÚno_gradr#   )r7   r1   s     r*   ÚforwardzPipelineTool.forward!  s‰   € õ Œ]‰_Œ_ð 	(ð 	(Ø�4”:Ð'Ð' Ð'Ð'ð	(ð 	(ð 	(ð 	(ñ 	(ô 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(øøøð 	(ð 	(ð 	(ð 	(ð 	(ð 	(s   ”.®2µ2c                 ó,   — |                       |¦  «        S )zG
        Uses the `post_processor` to decode the model output.
        )rê   rÜ   s     r*   ÚdecodezPipelineTool.decode(  s   € ð ×"Ò" 7Ñ+Ô+Ð+r;   c                 ó8  — t          |i |¤Ž\  }}| j        s|                      ¦   «           | j        |i |¤Ž}t	          || j        ¦  «        }|                      |¦  «        }t	          |d¦  «        }|                      |¦  «        }t          || j	        ¦  «        S )NÚcpu)
r   r5   r@   rÑ   r   rë   rû   rý   r   r2   )r7   r8   r9   Úencoded_inputsr2   Údecoded_outputss         r*   r>   zPipelineTool.__call__.  sš   € Ý*¨DÐ;°FÐ;Ð;‰ˆˆfàÔ"ð 	Ø�JŠJ‰LŒLˆLà$˜œ dÐ5¨fÐ5Ð5ˆÝ'¨¸¼ÑDÔDˆØ—,’,˜~Ñ.Ô.ˆÝ  ¨%Ñ0Ô0ˆØŸ+š+ gÑ.Ô.ˆå# O°T´\ÑBÔBÐBr;   )NNNNNNN)rW   rS   r²   r¹   r   rî   rð   rñ   rè   r:   r@   rÑ   rû   rý   r>   r³   r´   s   @r*   rÐ   rÐ   ±  sÕ   ø€ € € € € ð#ð #ðJ (ÐØ€KØ(ÐØÐð ØØØØØØð#ð #ð #ð #ð #ð #ðJð ð ð ð ð4.ð .ð .ð(ð (ð (ð,ð ,ð ,ðCð Cð Cð Cð Cð Cð Cr;   rÐ   rJ   c                 óì   ‡— 	 ddl }n# t          $ r t          d¦  «        ‚w xY w | ¦   «         Šˆfd„}|                     || j        | j        | j        ‰j        ¬¦  «                             ¦   «          dS )zò
    Launches a gradio demo for a tool. The corresponding tool class needs to properly implement the class attributes
    `inputs` and `outputs`.

    Args:
        tool_class (`type`): The class of the tool for which to launch the demo.
    r   Nz<Gradio should be installed in order to launch a gradio demo.c                  ó   •—  ‰| i |¤ŽS r¯   r   )r8   r9   r    s     €r*   Úfnzlaunch_gradio_demo.<locals>.fnL  s   ø€ Øˆt�TÐ$˜VÐ$Ð$Ð$r;   )r  r1   r2   ÚtitleÚarticle)r�   rç   Ú	Interfacer1   r2   rW   r.   Úlaunch)rJ   Úgrr  r    s      @r*   Úlaunch_gradio_demor
  =  s°   ø€ ðZØÐÐÐÐøÝð Zð Zð ZÝÐXÑYÔYÐYðZøøøð ˆ:‰<Œ<€Dð%ð %ð %ð %ð %ð ‡L‚LØØÔ ØÔ"ØÔ!ØÔ ð ñ ô ÷ ‚f�h„h€h€h€hs   ƒ ˆ"c                  ó°  — t                                d¦  «         t          ¦   «         st          d¦  «        ‚t          j        j                             ¦   «         r7t          j        j                             ¦   «         rt	          j	        d¦  «        S t          j
                             ¦   «         rt	          j	        d¦  «        S t	          j	        d¦  «        S )Nu’   `get_default_device` is deprecated and will be replaced with `accelerate`'s `PartialState().default_device` in version 4.36 of ðŸ¤— Transformers. rå   ÚmpsÚcudarÿ   )r�   rŽ   r   rç   rù   Úbackendsr  Úis_availableÚis_builtrë   r  r   r;   r*   rô   rô   Y  s´   € Ý
‡N‚Nð	1ñô ð õ ÑÔð MÝÐKÑLÔLÐLå„~Ô×&Ò&Ñ(Ô(ð #­U¬^Ô-?×-HÒ-HÑ-JÔ-Jð #ÝŒ|˜EÑ"Ô"Ð"Ý	Œ×	 Ò	 Ñ	"Ô	"ð #ÝŒ|˜FÑ#Ô#Ð#åŒ|˜EÑ"Ô"Ð"r;   ÚDocumentQuestionAnsweringToolÚImageCaptioningToolÚImageQuestionAnsweringToolÚImageSegmentationToolÚSpeechToTextToolÚTextSummarizationToolÚTextClassificationToolÚTextQuestionAnsweringToolÚTextToSpeechToolÚTranslationTool)
zdocument-question-answeringzimage-captioningzimage-question-answeringzimage-segmentationzspeech-to-textÚsummarizationztext-classificationztext-question-answeringztext-to-speechÚtranslationc                  ó¦   — t          ddd¬¦  «        } t          | dd¬¦  «        5 }t          j        |¦  «        }d d d ¦  «         n# 1 swxY w Y   |S )Nz#huggingface-tools/default-endpointszdefault_endpoints.jsonÚdatasetr¡   rF   rG   rH   )r   r[   r\   r]   )Úendpoints_filerj   r‘   s      r*   rŠ   rŠ   w  s“   € Ý Ð!FÐH`ÐluÐvÑvÔv€NÝ	ˆn˜c¨GÐ	4Ñ	4Ô	4ð !¸Ý”I˜a‘L”Lˆ	ð!ð !ð !ñ !ô !ð !ð !ð !ð !ð !ð !øøøð !ð !ð !ð !àÐs   ¥AÁA
ÁA
c                 ó&   — t          ¦   «         }| |v S r¯   )rŠ   )Útask_or_repo_idr‘   s     r*   Úsupports_remoter"  ~  s   € Ý%Ñ'Ô'€IØ˜iÐ'Ð'r;   Fc                 óL  — | t           v r†t           |          }t          j        d¦  «        }|j        }t	          ||¦  «        }|rA|€-t          ¦   «         }	| |	vrt          d| › d�¦  «        ‚|	|          }t          |||¬¦  «        S  ||fd|i|¤ŽS t          j	        | f|||dœ|¤ŽS )aÜ  
    Main function to quickly load a tool, be it on the Hub or in the Transformers library.

    Args:
        task_or_repo_id (`str`):
            The task for which to load the tool or a repo ID of a tool on the Hub. Tasks implemented in Transformers
            are:

            - `"document-question-answering"`
            - `"image-captioning"`
            - `"image-question-answering"`
            - `"image-segmentation"`
            - `"speech-to-text"`
            - `"summarization"`
            - `"text-classification"`
            - `"text-question-answering"`
            - `"text-to-speech"`
            - `"translation"`

        model_repo_id (`str`, *optional*):
            Use this argument to use a different model than the default one for the tool you selected.
        remote (`bool`, *optional*, defaults to `False`):
            Whether to use your tool by downloading the model or (if it is available) with an inference endpoint.
        token (`str`, *optional*):
            The token to identify you on hf.co. If unset, will use the token generated when running `huggingface-cli
            login` (stored in `~/.huggingface`).
        kwargs (additional keyword arguments, *optional*):
            Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as
            `cache_dir`, `revision`, `subfolder`) will be used when downloading the files for your tool, and the others
            will be passed along to its init.
    ÚtransformersNru   rv   r‰   rr   )rq   rr   rs   )
ÚTASK_MAPPINGÚ	importlibÚimport_moduleÚtoolsÚgetattrrŠ   rT   r�   r-   r–   )
r!  rq   rs   rr   r9   Útool_class_nameÚmain_moduleÚtools_modulerJ   r‘   s
             r*   Ú	load_toolr-  ƒ  sð   € ð@ �,Ð&Ð&Ý& Ô7ˆÝÔ-¨nÑ=Ô=ˆØ"Ô(ˆÝ˜\¨?Ñ;Ô;ˆ
àð 	DØÐ$Ý1Ñ3Ô3�	Ø"¨)Ð3Ð3Ý$ð4À/ð 4ð 4ð 4ñô ð ð !*¨/Ô :�Ý˜m°5ÀZÐPÑPÔPÐPà�:˜mÐCÐC°5ÐC¸FÐCÐCÐCåŒ}˜_Ðp¸MÐQVÐ_eÐpÐpÐioÐpÐpÐpr;   c                 ó   ‡ — ˆ fd„}|S )z<
    A decorator that adds a description to a function.
    c                 ó.   •— ‰| _         | j        | _        | S r¯   )r.   rW   r0   )Úfuncr.   s    €r*   Úinnerzadd_description.<locals>.inner¾  s   ø€ Ø&ˆÔØ”MˆŒ	Øˆr;   r   )r.   r1  s   ` r*   Úadd_descriptionr2  ¹  s#   ø€ ð
ð ð ð ð ð
 €Lr;   c                   óÚ   — e Zd Zddedee         fd„Zed„ ¦   «         Zed„ ¦   «         Z	 	 	 	 ddee	ee
ee         eee                  f                  d	ee
         d
ee         dedef
d„ZdS )rÃ   NrÂ   rr   c                 óJ   — i t          |¬¦  «        ¥ddi¥| _        || _        d S )NrÁ   zContent-Typezapplication/json)r   ÚheadersrÂ   )r7   rÂ   rr   s      r*   r:   zEndpointClient.__init__È  s3   € Ø\Õ*°Ð7Ñ7Ô7Ð\¸ÐI[Ð\Ð\ˆŒØ(ˆÔÐÐr;   c                 óÌ   — t          j        ¦   «         }|                      |d¬¦  «         t          j        |                     ¦   «         ¦  «        }|                     d¦  «        S )NÚPNG)ra   rG   )ÚioÚBytesIOrp   Úbase64Ú	b64encodeÚgetvaluerý   )rß   Ú_bytesÚb64s      r*   rÕ   zEndpointClient.encode_imageÌ  sO   € å”‘”ˆØ�
Š
�6 %ˆ
Ñ(Ô(Ð(ÝÔ˜vŸšÑ0Ô0Ñ1Ô1ˆØ�zŠz˜'Ñ"Ô"Ð"r;   c                 óÂ   — t          ¦   «         st          d¦  «        ‚ddlm} t	          j        | ¦  «        }t          j        |¦  «        }|                     |¦  «        S )NzbThis tool returned an image but Pillow is not installed. Please install it (`pip install Pillow`).r   )ÚImage)	r   rç   ÚPILr@  r:  Ú	b64decoder8  r9  r[   )Ú	raw_imager@  r>  r=  s       r*   Údecode_imagezEndpointClient.decode_imageÓ  sh   € å"Ñ$Ô$ð 	ÝØtñô ð ð 	ÐÐÐÐÐåÔ˜yÑ)Ô)ˆÝ”˜C‘”ˆØ�zŠz˜&Ñ!Ô!Ð!r;   Fr1   ÚparamsÚdatarà   r›   c                 óà   — i }|r||d<   |r||d<   t          ¦   «                              | j        | j        ||¬¦  «        }|r|                      |j        ¦  «        S |                     ¦   «         S )Nr1   rÓ   )r5  r\   rF  )r   ÚpostrÂ   r5  rD  Úcontentr\   )r7   r1   rE  rF  rà   ÚpayloadÚresponses          r*   r>   zEndpointClient.__call__à  s…   € ð ˆØð 	'Ø &ˆG�HÑØð 	+Ø$*ˆG�LÑ!õ ‘=”=×%Ò% dÔ&7ÀÄÐT[ÐbfÐ%ÑgÔgˆð ð 	#Ø×$Ò$ XÔ%5Ñ6Ô6Ð6à—=’=‘?”?Ð"r;   r¯   )NNNF)rW   rS   r²   rº   r   r:   r¾   rÕ   rD  r   r   r   Úbytesr½   r   r>   r   r;   r*   rÃ   rÃ   Ç  sõ   € € € € € ð)ð ) Sð )°¸#´ð )ð )ð )ð )ð ð#ð #ñ „\ð#ð ð
"ð 
"ñ „\ð
"ð JNØ!%Ø $Ø"ð#ð #à˜˜s D¨$¨s¬)°T¸$¸s¼)´_ÐDÔEÔFð#ð ˜”ð#ð �uŒoð	#ð
 ð#ð 
ð#ð #ð #ð #ð #ð #r;   rÃ   r¯   )NFN)<r:  r&  rÈ   r8  r\   rQ   r¤   Útypingr   r   r   r   r   Úhuggingface_hubr   r	   r
   r   Úhuggingface_hub.utilsr   r   r   Údynamic_module_utilsr   r   r   Úimage_utilsr   Úmodels.autor   Úutilsr   r   r   r   r   r   Úagent_typesr   r   Ú
get_loggerrW   r�   rù   Úaccelerate.utilsr   r%   r+   r`   r-   r�   rÐ   r
  rô   r%  rŠ   r"  r-  r2  rÃ   r   r;   r*   ú<module>rW     sE  ðð" €€€Ø Ð Ð Ð Ø €€€Ø 	€	€	€	Ø €€€Ø 	€	€	€	Ø €€€Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3à XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ Xà aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aØ &Ð &Ð &Ð &Ð &Ð &Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð CÐ BÐ BÐ BÐ BÐ BÐ BÐ Bð 
ˆÔ	˜HÑ	%Ô	%€àÐÑÔð Ø€L€L€LàÐÑÔð 0Ø/Ð/Ð/Ð/Ð/Ð/ð &Ð ðð ð ð ð&Ð ðF.ð F.ð F.ð F.ð F.ñ F.ô F.ð F.ðRT-ð T-ð T-ð T-ð T-�ñ T-ô T-ð T-ðnICð ICð ICð ICð IC�4ñ ICô ICð ICðX 4ð ð ð ð ð8#ð #ð #ð" $CØ-Ø <Ø1Ø(Ø,Ø3Ø:Ø(Ø$ðð €ðð ð ð(ð (ð (ð
3qð 3qð 3qð 3qðl
ð 
ð 
ð.#ð .#ð .#ð .#ð .#ñ .#ô .#ð .#ð .#ð .#r;   