
    sgQ:                        d dl mZ d dlmZmZ d Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd;dZd Zd Zeeeeee	e
eeeeeeedZ G d d      Z eg ddgd       eg ddgddg       eddgddgd        eg d!d"gd#       eg d$ddgd%       ed&d'gd"gd(       ed)gdgd*       eg d+ddgd,       eg d-dgd.       eg d/dd0id1       ed2gd"gd3      gZd4 Zd<d6Zd=d7efd8Zd=d9Zd>d:Zy5)?   )BASE_PYTHON_TOOLS)InterpreterErrorevaluatec                     d|  d| dS )NzThis is the classification of z along . )textlabelss     U/var/www/html/venv/lib/python3.12/site-packages/transformers/agents/evaluate_agent.py
classifierr      s    +D6BB    c                     d|  d| d| dS )NzThis is the translation of  from z to r   r   )r	   src_langtgt_langs      r   
translatorr      s    (fXJd8*ANNr   c                     d|  dS )Nz!This is actually a sound reading r   r   r	   s    r   speakerr      s    .tfA66r   c                 4    d| vrt        d|  d      d|  dS )Nsoundz	`audio` (z) is not a sound.z"This is the transcribed text from r   
ValueError)audios    r   transcriberr   "   s.    e9UG+<=>>/wa88r   c                     d|  dS )Nz'This is actually an image representing r   r   )prompts    r   image_generatorr   (   s    4VHA>>r   c                 4    d| vrt        d|  d      d|  dS )Nimage	`image` () is not an image.zThis is a description of r   r   )r    s    r   image_captionerr#   ,   s.    e9UG+=>??&ugQ//r   c                 :    d| vrt        d|  d      d|  d| dS )Nr    r!   r"   zThis is a transformation of z according to r   r   r    r   s     r   image_transformerr&   2   s4    e9UG+=>??)%vhaHHr   c                     d| d|  dS )NThis is the answer to r   r   r   )r	   questions     r   question_answererr*   8   s    #H:VD6;;r   c                 :    d| vrt        d|  d      d| d|  dS )Nr    r!   r"   r(   r   r   r   r    r)   s     r   image_qar-   <   s4    e9UG+=>??#H:VE7!<<r   c                     d|  dS )NzThis is the content of r   r   )urls    r   text_downloaderr0   B   s    $SE++r   c                     d|  dS )NzThis is a summary of r   r   r   s    r   
summarizerr2   F   s    "4&**r   c                     d|  S )NzA video of r   )r   secondss     r   video_generatorr5   J   s    !!r   c                     d| d|  dS )Nr(   z from the document r   r   r,   s     r   document_qar7   N   s    #H:-@qIIr   c                     d| d|  S )NzThis is the mask of z in r   r%   s     r   image_segmenterr9   R   s    !&eW55r   )text_classifierr   text_readerr2   r   r   r#   r&   text_qar0   r-   r5   r7   r9   c                       e Zd ZdZd Zy)Problema#  
    A class regrouping all the information to solve a problem on which we will evaluate agents.

    Args:
        task (`str` ou `list[str]`):
            One or several descriptions of the task to perform. If a list, it should contain variations on the
            phrasing, but for the same task.
        inputs (`list[str]` or `dict[str, str]`):
            The inputs that will be fed to the tools. For this testing environment, only strings are accepted as
            values. Pass along a dictionary when you want to specify the values of each inputs, or just the list of
            inputs expected (the value used will be `<<input_name>>` in this case).
        answer (`str` or `list[str]`):
            The theoretical answer (or list of possible valid answers) to the problem, as code.
    c                 .    || _         || _        || _        y Ntaskinputsanswer)selfrB   rC   rD   s       r   __init__zProblem.__init__x   s    	r   N)__name__
__module____qualname____doc__rF   r   r   r   r>   r>   h   s    r   r>   )z:Is the following `text` (in Spanish) positive or negative?zEIs the text in the variable `text` (in Spanish) positive or negative?z`Translate the following `text` from Spanish to English then tell me if its positive or negative.r	   zjtext_classifier(translator(text, src_lang="Spanish", tgt_lang="English"), labels=["positive", "negative"])rA   )z+Tell me out loud what the `image` contains.z(Describe the following `image` out loud.zDFind what is in the picture stored in `image` then read it out loud.r    z#text_reader(image_captioner(image))z>text_reader(image_qa(image, question='What is in the image?'))zkGenerate an image from the text given in `text_input`. Then transform it according to the text in `prompt`.zeUse the following `text_input` to generate an image, then transform it by using the text in `prompt`.
text_inputr   z6image_transformer(image_generator(text_input), prompt))zTDownload the content of `url`, summarize it then generate an image from its content.z<Use a summary of the web page at `url` to generate an image.zXSummarize the content of the web page at `url`, and use the result to generate an image.r/   z1image_generator(summarizer(text_downloader(url))))zUTransform the following `image` using the prompt in `text`. The prompt is in Spanish.zNUse the text prompt in `text` (in Spanish) to transform the following `image`.z]Translate the `text` from Spanish to English then use it to transform the picture in `image`.zRimage_transformer(image, translator(text, src_lang='Spanish', tgt_lang='English'))zHDownload the content of `url`, summarize it then read it out loud to me.z+Read me a summary of the web page at `url`.z-text_reader(summarizer(text_downloader(url)))z6Generate an image from the text given in `text_input`.zimage_generator(text_input))z2Replace the beaver in the `image` by the `prompt`.z7Transform the `image` so that it contains the `prompt`.z'Use `prompt` to transform this `image`.z image_transformer(image, prompt))zmProvide me the summary of the `text`, then read it to me before transcribing it and translating it in French.zXSummarize `text`, read it out loud then transcribe the audio and translate it in French.zURead me a summary of the `text` out loud. Transcribe this and translate it in French.z]translator(transcriber(text_reader(summarizer(text))), src_lang='English', tgt_lang='French'))z Generate a video of the `prompt`zAnimate a `prompt`z%Make me a short video using `prompt`.zA lobster swimmingz%video_generator('A lobster swimming')zDownload the following file `url`, summarize it in a few words and generate a video from it.Fetch the file at this `url`, summarize it, and create an animation out of it.z1video_generator(summarizer(text_downloader(url)))c                    t        |t              st        D ch c]	  }||v s| c}S t        | t              rAt	        ||      D ]2  \  }}|| j                         v st        D ch c]	  }||v s| c}c S  t	        ||      D ]%  \  }}| |k(  st        D ch c]	  }||v s| c}c S  t        D ch c]  }||d   v s| c}S c c}w c c}w c c}w c c}w )N    )
isinstancelist
TEST_TOOLSdictzipvalues)agent_answertheoretical_answercode_answername
one_answerone_codes         r   get_theoretical_toolsrZ      s    ($/!+Ct{/BCC,%$'(:K$H 	I J\0022)3Htx7GHH	I !$$6 D E
H:%%/DT483CDDDE (BT4;q>+ADBB D
 I EBs.   	CC'	C1C	C%C7CCNc                    t        j                         }t        j                         D ]  \  }}|| vr|||<    t	        |t
              r|j                         }n||D ci c]	  }|d| d }}||j                  |       n|}	 t        | ||      S c c}w # t        $ r}	t        |	      cY d }	~	S d }	~	wt        $ r}	|rt        |	       Y d }	~	y d }	~	ww xY w)Nz<<z>>)r   copyrP   itemsrN   rQ   updater   r   str	Exceptionprint)
coderC   stateverbosereturn_interpretor_errortoolsrW   toolinpes
             r   evaluate_coderj      s    ""$E &&( 
dtd
 &$		/56#C5|#66VeU++ 7  1v !Hs0   "B	B 	C$
B4.C4C CCrd   c                 
   |rt        |        t        t              rng| v r|rt        d       yt        | t              r0t	        fd| j                         D              r|rt        d       y|rt        d       y)NzPerfect!r   c              3   &   K   | ]  }|v  
 y wr@   r   ).0vrU   s     r   	<genexpr>zscore_code.<locals>.<genexpr>  s     /gA5G0G/gs   z Almsot perfect, result in state!g      ?z.Result is not the right one but code executed.g333333?)ra   rN   rO   rQ   anyrS   )rT   rU   rd   s    ` r   
score_coderq     s~    l.//9:Ld/S+ZlYm))*	L$	'C/gQ]QdQdQf/g,g45BCr   c                    t         D ch c]  }d| d| v s| }}t        |||      }||k(  rd}d }	n9t        ||z
        }
t        ||z
        }t        ddd|
z  z
  d|z  z
        }||d}	t         D ch c]	  }|| v s| }}||k(  rd}d }n9t        ||z
        }
t        ||z
        }t        ddd|
z  z
  d|z  z
        }||d}t	        |||      }|dk  r| ||d}nd }|||f|	||ffS c c}w c c}w )N`g      ?rM   g      ?)selected_toolstheoretical_toolsrd   )code_produced
evaluationrU   )rP   rZ   lenmaxrq   )rb   rT   rU   rD   rd   rW   tools_in_coderu   tool_selection_scoretool_selection_errorsmissing_toolsunexpected_toolstool_used_scoretool_used_errorsscorecode_errorss                   r   evaluate_one_resultr     sv   &0HdavQK44GTHMH-l<NPVW))" $-=>}/@@A"1cD=,@&@4JZCZ&Z[ ,!2!

 '1AdDDLTAMA))-=>}/@@Aatm';!;dEU>U!UV ,!2

 |%7IEs{!&"4
  /59<QScep;qqqO I Bs   C.C.5	C3?C3c           	         t        | j                  j                               }|t        t              k7  r6t        t              |z
  }t        |      t        z
  }t	        d| d| d      g }g }t        t              D ]  \  }	}
t        |
j                  t              rD|j                  |
j                         |j                  |	gt        |
j                        z         d|j                  |
j                         |j                  |	        d}d}d}|ri }i }i }t        dt        |      |      D ]{  }t        ||z   t        |            }||| }|D cg c]  }| j                  |d       }}t        |      D ],  \  }	}t        |||	z         }|rt!        d||	z    d||	    d	       | j#                  |d
      }t%        ||j&                  |      }t        |j(                  t              r.|j(                  D cg c]  }t%        ||j&                         }}n t%        |j(                  |j&                        }t+        ||||j(                  |      \  }}||d   z  }||d   z  }||d   z  }|s|d   |d   ||	   <   |d   |d   ||	   <   |d   "|d   ||	   <   / ~ d|t        |      z  z  d|t        |      z  z  d|t        |      z  z  d}|r|fS |S c c}w c c}w )z
    Evaluates a new agent on all `EVALUATION_TASKS`.

    Example:

    ```py
    agent = NewOpenAiAgent(model="text-davinci-003", api_key=your_api_key)
    bads = new_evaluate_agent(agent)
    for bad in bads:
        print(bad)
    ```
    z?Fix the test tools in the evaluate_agent module. Tools mising: z. Extra tools: r   rM   T)return_generated_codez	====Task z====

zAnswer:)split_tokenrv   r      d   )ztool selection scoreztool used scorez
code score)settoolboxkeysrP   r   	enumerateEVALUATION_TASKSrN   rB   rO   extendry   appendrangeminrunra   extract_actionrj   rC   rD   r   )agent
batch_sizerd   return_errorsagent_toolsr~   r   
eval_taskseval_idxidxpbr|   r   
code_scorer}   r   r   	start_idxend_idxbatch_tasksrB   resultsresultproblemrb   rT   rD   rU   scoreserrorss                                 r   evaluate_agentr   I  s9    emm((*+Kc*o%J+5{+j8Mm_\kl|k}}~
 	
 JH-. !Rbggt$bgg&OOSECL01bgg&OOC ! OJ "1c*oz: !>	i*,c*o> 73KVW4599T9>WW$W- 	>KC&x	C'@AG	)c/!2&S9I8J"MN''I'FD )w~~wOL'..$/ZaZhZh%iPVmFGNN&K%i"%i%27>>7>>%R"0l$6PWNFF !F1I- vay(O&)#J!9(>DQi)+c*:;!9(9?$[%56!9(4:1IKC 017	>!>H !$';c*o'M N/C
O"CDZ#j/9:F ,.>KKQ X &js   K1K6)r   )NNFF)F)   FF)agentsr   python_interpreterr   r   r   r   r   r   r   r#   r&   r*   r-   r0   r2   r5   r7   r9   rP   r>   r   rZ   rj   boolrq   r   r   r   r   r   <module>r      s  " & :CO79?0I<=,+"J6
 "&&* &&&
$ 0 

 x 

 y1L
 ys
 h'G 

 wB 

  c V9
 w> D
 ~, 

 "1 

 xn p./6
 ]
 wBm^ BC 6$ &(rVUr   