
    gQ:                     L   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 d0d d1id2           ed3gd#gd4          gZd5 Zd?d8Zd@d9efd:Zd@d;ZdAd=Zd6S )B   )BASE_PYTHON_TOOLS)InterpreterErrorevaluatec                     d|  d| dS )NzThis is the classification of z along . )textlabelss     ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/agents/evaluate_agent.py
classifierr      s    BDBBBB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#    NNNXNN8NNNNr   c                     d|  dS )Nz!This is actually a sound reading r   r   r	   s    r   speakerr      s    6t6666r   c                 <    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   "   s8    e=U===>>>88888r   c                     d|  dS )Nz'This is actually an image representing r   r   )prompts    r   image_generatorr   (   s    >V>>>>r   c                 <    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#   ,   s8    e>U>>>???/u////r   c                 B    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   s@    e>U>>>???H%HHvHHHHr   c                     d| d|  dS )NThis is the answer to r   r   r   )r	   questions     r   question_answererr*   8   s    ;H;;D;;;;r   c                 B    d| vrt          d|  d          d| d|  dS )Nr    r!   r"   r(   r   r   r   r    r)   s     r   image_qar-   <   s@    e>U>>>???<H<<E<<<<r   c                     d|  dS )NzThis is the content of r   r   )urls    r   text_downloaderr0   B   s    +S++++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_generatorr6   J   s    !!!!r   c                     d| d|  dS )Nr(   z from the document r   r   r,   s     r   document_qar8   N   s    IHIIIIIIr   c                     d| d|  S )NzThis is the mask of z in r   r%   s     r   image_segmenterr:   R   s    5&55e555r   )text_classifierr   text_readerr2   r   r   r#   r&   text_qar0   r-   r6   r8   r:   c                       e Zd ZdZd ZdS )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                 0    || _         || _        || _        d S Ntaskinputsanswer)selfrC   rD   rE   s       r   __init__zProblem.__init__x   s    	r   N)__name__
__module____qualname____doc__rG   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"])rB   )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fdt          D             S t          | t                    rAt	          |          D ]0\  }||                                 v rfdt          D             c S 1t	          |          D ] \  }| |k    rfdt          D             c S !fdt          D             S )Nc                     h | ]}|v |	S r   r   .0namecode_answers     r   	<setcomp>z(get_theoretical_tools.<locals>.<setcomp>   s#    CCCt{/B/B/B/B/Br   c                     h | ]}|v |	S r   r   rP   rQ   one_codes     r   rS   z(get_theoretical_tools.<locals>.<setcomp>   s#    HHHtx7G7G7G7G7Gr   c                     h | ]}|v |	S r   r   rU   s     r   rS   z(get_theoretical_tools.<locals>.<setcomp>   s#    DDDT483C3CD3C3C3Cr   c                 (    h | ]}|d          v |S )    r   rO   s     r   rS   z(get_theoretical_tools.<locals>.<setcomp>   s'    BBBT4;q>+A+AD+A+A+Ar   )
isinstancelist
TEST_TOOLSdictzipvalues)agent_answertheoretical_answerrR   
one_answerrV   s     ` @r   get_theoretical_toolsrc      s   ($// DCCCCCCCC,%% I$'(:K$H$H 	I 	I J\002222HHHHHHHHHH 3 !$$6 D D E E
H:%%DDDDZDDDDDD & CBBBZBBBBr   NFc                    t          j                    }t                                          D ]\  }}|| vr
|||<   t	          |t
                    r|                                }n|d |D             }||                    |           n|}	 t          | ||          S # t          $ r}t          |          cY d }~S d }~wt          $ r}|rt          |           Y d }~d S d }~ww xY w)Nc                     i | ]	}|d | d
S )z<<z>>r   )rP   inps     r   
<dictcomp>z!evaluate_code.<locals>.<dictcomp>   s"    666#|C|||666r   )r   copyr\   itemsrZ   r]   updater   r   str	Exceptionprint)	coderD   stateverbosereturn_interpretor_errortoolsrQ   tooles	            r   evaluate_coderu      s#   "$$E &&((  
dtd&$ 7		66v666VeU+++   1vv    	!HHHttttts*   B! !
C(+B?9C(?C(C##C(rp   c                 Z   |rt          |            t          t                    rng| v r|rt          d           dS t          | t                    r@t	          fd|                                 D                       r|rt          d           dS |rt          d           dS )NzPerfect!r   c              3       K   | ]}|v V  	d S rA   r   )rP   vra   s     r   	<genexpr>zscore_code.<locals>.<genexpr>  s)      /g/gA5G0G/g/g/g/g/g/gr   z Almsot perfect, result in state!g      ?z.Result is not the right one but code executed.g333333?)rm   rZ   r[   r]   anyr_   )r`   ra   rp   s    ` r   
score_coder{     s     0l.////9:Ld/S/Sm++ZlYm))) 	*q	L$	'	' C/g/g/g/gQ]QdQdQfQf/g/g/g,g,g  	64555t 	DBCCCsr   c                      fdt           D             }t          |||          }||k    rd}d }nEt          ||z
            }	t          ||z
            }
t          ddd|	z  z
  d|
z  z
            }||d} fdt           D             }||k    rd}d }nEt          ||z
            }	t          ||z
            }
t          ddd|	z  z
  d|
z  z
            }||d}t	          |||          }|dk     r ||d}nd }|||f|||ffS )	Nc                 $    h | ]}d | d v 
|S )`r   rP   rQ   rn   s     r   rS   z&evaluate_one_result.<locals>.<setcomp>  s+    HHHdKKKK44G4GT4G4G4Gr   g      ?rY   g      ?)selected_toolstheoretical_toolsc                     h | ]}|v |	S r   r   r   s     r   rS   z&evaluate_one_result.<locals>.<setcomp>.  s    AAAdDDLLTLLLr   rp   )code_produced
evaluationra   )r\   rc   lenmaxr{   )rn   r`   ra   rE   rp   tools_in_coder   tool_selection_scoretool_selection_errorsmissing_toolsunexpected_toolstool_used_scoretool_used_errorsscorecode_errorss   `              r   evaluate_one_resultr     s   HHHHjHHHM-l<NPVWW)))" $-=>>}/@@AA"1cD=,@&@4JZCZ&Z[[ ,!2!
 !

 BAAAjAAAM)))-=>>}/@@AAatm';!;dEU>U!UVV ,!2
 

 |%7IIIEs{{!&"4
 
  /59<QScep;qqqr      c           	      d    t           j                                                  }|t          t                    k    rDt          t                    |z
  }t          |          t          z
  }t	          d| d| d          g }g }t          t                    D ]\  }	}
t          |
j        t                    rF|
                    |
j                   |
                    |	gt          |
j                  z             e|                    |
j                   |                    |	           d}d}d}|ri }i }i }t          dt          |          |          D ]}t          ||z   t          |                    }|||         } fd|D             }t          |          D ]=\  }	}t          |||	z                     |rt          d||	z    d||	          d                                |d	
          }t#          |j        |          }t          j        t                    rfdj        D             }nt#          j        j                  }t)          |||j        |          \  }}||d         z  }||d         z  }||d         z  }|rK|d         |d         |||	         <   |d         |d         |||	         <   |d         |d         |||	         <   ?d|t          |          z  z  d|t          |          z  z  d|t          |          z  z  d}|r||||fS |S )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   rY   c                 >    g | ]}                     |d           S )T)return_generated_code)run)rP   rC   agents     r   
<listcomp>z"evaluate_agent.<locals>.<listcomp>v  s)    WWW4599T9>>WWWr   z	====Task z====

zAnswer:)split_tokenr   c                 :    g | ]}t          |j                  S r   )ru   rD   )rP   rE   problems     r   r   z"evaluate_agent.<locals>.<listcomp>  s&    %i%i%iPVmFGN&K&K%i%i%ir   r   r3   Nd   )ztool selection scoreztool used scorez
code score)settoolboxkeysr\   r   	enumerateEVALUATION_TASKSrZ   rC   r[   extendr   appendrangeminrm   extract_actionru   rD   rE   r   )r   
batch_sizerp   return_errorsagent_toolsr   r   
eval_taskseval_idxidxpbr   r   
code_scorer   r   r   	start_idxend_idxbatch_tasksresultsresultrn   r`   ra   scoreserrorsr   s   `                          @r   evaluate_agentr   I  s    em((**++Kc*oo%%J+5{++j8ml|
 
 	
 JH-.. ! !Rbgt$$ 	!bg&&&OOSECLL01111bg&&&OOC    OJ  "1c*ooz:: !> !>	i*,c*oo>> 7!23WWWW;WWW$W-- 	> 	>KC&x	C'@AG OM)c/MMS9IMMMNNN''I'FFD )w~wOOOL'.$// S%i%i%i%iZaZh%i%i%i""%27>7>%R%R"0l$6PW  NFF !F1I- vay(O&)#J >!9(>DQi)+c*:;!9(9?$[%56!9(4:1IKC 017	>< !$';c*oo'M N/C
OO"CDZ#j//9: F  ,.>KKr   )r3   )NNFF)F)r   FF)agentsr   python_interpreterr   r   r   r   r   r   r   r#   r&   r*   r-   r0   r2   r6   r8   r:   r\   r?   r   rc   ru   boolr{   r   r   r   r   r   <module>r      s  " & % % % % % : : : : : : : :C C CO O O7 7 79 9 9? ? ?0 0 0I I I< < <= = =, , ,+ + +" " " "J J J6 6 6
 "&&* &&& 
$       0 G
 
 

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

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

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

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

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

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