U
    di                  4   @   s\  d dl 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
mZ ddlmZ dd	lmZmZ dd
lmZmZmZ dd Zeeeeeeeeeeee ej!ej"ej#ej$ej%ej&ej'ej(ej)ej*ej+ej,ej-ej.ee/e0e1e2e3e4e5e6e7e8e9e:e;e<e=e>e?e@eAeBeCeDeEeFd3ZGeG dd dZHddgZIdddZJdd ZKG dd deZLG dd deZMdS )    N)	dataclass)sqrt)Dict)hf_hub_downloadlist_spaces   )is_offline_mode   )LIST_SAFE_MODULESevaluate_python_code)TOOL_CONFIG_FILETOOL_MAPPINGToolc                  G   s   d S N )argsr   r   E/tmp/pip-unpacked-wheel-bm_b0l5e/transformers/agents/default_tools.pycustom_print   s    r   )3print
isinstancerangefloatintboolstrsetlistdicttupleroundceilfloorlogexpsincostanasinacosatanatan2degreesradianspowr   lensummaxminabs	enumeratezipreversedsortedallanymapfilterordchrnextiterdivmodcallablegetattrhasattrsetattr
issubclasstypec                   @   sF   e Zd ZU eed< eeef ed< eed< eed< eed< eed< dS )PreToolnameinputsoutput_typetaskdescriptionrepo_idN)__name__
__module____qualname__r   __annotations__r   rE   r   r   r   r   rF   Z   s   
rF   zimage-transformationztext-to-imagehuggingface-toolsc           
   
   C   s   t  r| d i S t|d}i }|D ]p}|j}t|tdd}t|dd}t|}W 5 Q R X |	dd }	t
|	|d	 ||	|d
 |d d||d < q&|S )Nz;You are in offline mode, so remote tools are not available.)authorspace)Z	repo_typezutf-8)encoding/rK   rH   rI   )rJ   rK   rL   rG   rH   rI   rG   )r   infor   idr   r   openjsonloadsplitrF   )
loggerZorganizationspacestoolsZ
space_inforL   Zresolved_config_filereaderconfigrJ   r   r   r   get_remote_toolsj   s(    

	rb   c              	   C   s\   i }t d}|j}t D ]:\}}t||}| }t|j|j|j	||j
d d||j< q|S )NZtransformers)rG   rH   rI   rJ   rK   rL   )	importlibimport_moduleZagentsr   itemsrA   rF   rG   rH   rI   rK   )r]   Zdefault_toolsZmain_moduleZtools_moduleZ	task_nameZtool_class_nameZ
tool_classZtool_instancer   r   r   setup_default_tools   s    

	rf   c                       s6   e Zd ZdZdZdZdd fdd
Zdd	 Z  ZS )
PythonInterpreterToolpython_interpreterzRThis is a tool that evaluates python code. It can be used to perform calculations.stringN)authorized_importsc                   sX   |d krt tt| _nt ttt|B | _ddd| ddi| _t j|| d S )Ncoderi   zThe code snippet to evaluate. All variables used in this snippet must be defined in this same snippet, else you will get an error. This code can only import the following python libraries: .rE   rK   )r   r   r
   rj   rH   super__init__)selfrj   r   kwargs	__class__r   r   ro      s    
	zPythonInterpreterTool.__init__c                 C   s   t t|t| jd}|S )N)Zstatic_toolsrj   )r   r   BASE_PYTHON_TOOLSrj   )rp   rk   outputr   r   r   forward   s    zPythonInterpreterTool.forward)	rM   rN   rO   rG   rK   rI   ro   rv   __classcell__r   r   rr   r   rg      s
   rg   c                   @   s.   e Zd ZdZdZddddiZdZdd Zd	S )
FinalAnswerToolZfinal_answerz-Provides a final answer to the given problem.answerr8   zThe final answer to the problemrm   c                 C   s   |S r   r   )rp   ry   r   r   r   rv      s    zFinalAnswerTool.forwardN)rM   rN   rO   rG   rK   rH   rI   rv   r   r   r   r   rx      s
   rx   )rQ   )Nimportlib.utilrc   rZ   mathZdataclassesr   r   typingr   Zhuggingface_hubr   r   utilsr   rh   r
   r   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rt   rF   Z"HUGGINGFACE_DEFAULT_TOOLS_FROM_HUBrb   rf   rg   rx   r   r   r   r   <module>   s   7

