AKg1ddlZddlZddlZddlZddlmZddlmZmZm Z m Z ddl m Z m Z ddlmZddlmZdefdZ dd ed ed eefd ZGd dZddgZy)N)Optional) AgentAudio AgentImage AgentTexthandle_agent_output_types) ActionStepMultiStepAgent) MemoryStep)_is_package_availablestep_logc#Kddl}t|tr|jd|jnd}|j dd|dt |dr|j |j j}tjd d |}tjd d |}tjd d |}|j}|j d|t |d r|j|jd}|jdk(}dt|j}|j}t|tr%t|j!dt|}nt|j}|rUtjdd|}tjdd|}|j}|j#dsd|d}|j d|d|j|dd} | t |drt|j$h|j$jrN|j$j} | r2tjdd| } |j d| d|ddt |dr9|j&-|j dt|j&d |ddd| j(d!<nCt |dr7|j&+|j dt|j&d"d i|} t |d#r/t |d$r#d%|j*d&d'|j,d&} | | z } t |d(r5|j.r"d)t1t3|j.d*nd} | | z } d+| d,} |j d| |j dd-yyw).z@Extract ChatMessage objects from agent steps with proper nestingrNzStep assistantz**rolecontent model_outputz```\s*z```z\s*```z```\s*\n\s* tool_callspython_interpretercall_answerz```.*?\nz\s*\s*z ```pythonz ```python z ```u🛠️ Used tool pending)titleidstatus)rrmetadata observationsz^Execution logs:\s*u📝 Execution Logsdone)r parent_idrerroru 💥 Errorrrinput_token_countoutput_token_countz | Input-tokens:,z | Output-tokens:durationz | Duration: z/z z-----)gradio isinstancer step_number ChatMessagehasattrrstripresubrnamelen argumentsdictstrget startswithrr rr!r"r$roundfloat)r grr(rfirst_tool_call used_coderargsrparent_message_tool log_content step_footnote token_str step_durations =C:\mat-dir\playground\huggingface-stuff\hf-agent\Gradio_UI.pypull_messages_from_steprAs(J'8@8L8L8Xh2234^` nn+K=7KnLL 8^ ,1F1F1R#00668L66"5ulKL66"5ulKL66":E<PL'--/L..k<.H H 8\ *x/B/B/N&11!4O',,0DDIH$7$7 89:I#,,D$%dhhxT;<d)//+&&b':&&!4b'B!--/))+6 +G9E:G"$.. 1/2F2F1GH#'#1# & %x0%%1h6K6K6Q6Q6S&3399; "$&&)?["QK..(#.-+@ydj!k)x)hnn.Hnn$/'3)W]^%6<  ( ( 2Xw 'HNN,F..k3x~~;NZacoYp.q q'- 80 1ghH\6]"8#=#=a"@@QRZRmRmnoQpq  Y &M 8Z (T\TeTemE%8I8I2JA,N+OPkoM ] *MKM?Zbe nn+-nJJnn+wn??s(sOOtaskreset_agent_memoryadditional_argsc#hKtds tdddl}d}d}|j|d||D]}t |j drx||j j z }||j jz }t|tr6|j j |_ |j j|_ t|D]}|} t| } t| tr(|jdd | j!d  yt| t"r'|jd| j!d d  yt| t$r'|jd| j!dd  y|jddt'|  yw)zaRuns an agent with the given task and streams the messages from the agent as gradio ChatMessages.r&UPlease install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`rNT)streamresetrDlast_input_token_countrz**Final answer:**  rz image/png)path mime_typez audio/wavz**Final answer:** )r ModuleNotFoundErrorr&runr*modelrIlast_output_token_countr'rr!r"rArrr) to_stringrrr2) agentrBrCrDr7total_input_tokenstotal_output_tokensr message final_answers r@stream_to_gradiorW~s ! *! c  IId47I[jIk 5;; 8 9 %++"D"D D  5;;#F#F F (J/-2[[-O-O*.3kk.Q.Q+.  GM lL,\:L, *nn),*@*@*B)C2F   L* -nn)335KP   L* -nn)335KP  nn+9KCP\L]K^7_n``sF0F2cHeZdZdZd dededzfdZdZgdfdZd Z d Z y) GradioUIz3A one-line interface to launch your agent in GradioNrRfile_upload_folderctds td||_||_|j6tj j |st j|yyy)Nr&rF)r rMrRrZosrKexistsmkdir)selfrRrZs r@__init__zGradioUI.__init__s_$X.%g  "4  " " .77>>"45+,6 /c#Kddl}|j|jd||t|j|dD]}|j|||yw)NruserrF)rBrC)r&appendr)rWrR)r_promptmessagesr7msgs r@interact_with_agentzGradioUI.interact_with_agentsXFFCD#DJJVPUVC OOC NWsA"A$)zapplication/pdfzGapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentz text/plaincnddl}||jdd|fS tj|j\}}||vr|jdd|fSt jj|j}tjdd |} i} tjjD]\} } | | vs | | | <| jd dd } | jd | |zd j| } t jj|j t jj| } t#j$|j| |jd | d|| gzfS#t $r"}|jd|d|fcYd}~Sd}~wwxYw)zV Handle file uploads, default allowed types are .pdf, .docx, and .txt rNzNo file uploadedT)visiblezError: zFile type disallowedz[^\w\-.]_.rzFile uploaded: )r&Textbox mimetypes guess_typer. Exceptionr\rKbasenamer,r- types_mapitemssplitrdjoinrZshutilcopy)r_filefile_uploads_logallowed_file_typesr7rLrke original_namesanitized_name type_to_extextt file_paths r@ upload_filezGradioUI.upload_files  <::0$:?AQQ Q M$// :LIq . .::4d:CEUU U((3  m  ))//1FC #!$ A2 (--c23B7b;y#99:0GGLL!8!8"'':J:J>:Z[  DIIy)zzOI;7zFHX\e[fHfff5 M::smT::|t|dkDr d|zdfSdzdfS)NrzI You have been provided with these files, which might be helpful or not: r)r/)r_ text_inputrzs r@log_user_messagezGradioUI.log_user_messagesQ '(1,]]m\no         rac >ddl}|jd5}|jg}|jg}|jddddd}|jG|j d }|j d d d }|j|j||g||g|j dd} | j|j| |g|| gj|j||g|gdddjdddd|y#1swYxYw)NrT) fill_heightAgentrf)Nzdhttps://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png)labeltype avatar_images resizeablescalez Upload a file)rz Upload StatusF)r interactiverjz Chat Message)linesr)debugshare)r&BlocksStateChatbotrZFilernchangersubmitrthenrhlaunch) r_kwargsr7demostored_messagesrzchatbotr upload_statusrs r@rzGradioUI.launchs3 YY4Y (D hhrlO!xx| jj ! G&&2 ggOg< " e]b c ""$$ "23"$45 !>BJ   %%-. *- d4++ow-G'S7):  5$d5f5;) (s CDD)N) __name__ __module__ __qualname____doc__r r2r`rhrrrrrar@rYrYs7= -n -#* - .g`   6rarYrW)FN)ror\r,rwtypingrsmolagents.agent_typesrrrrsmolagents.agentsrr smolagents.memoryr smolagents.utilsr rAr2boolr1rWrY__all__rrar@rs  __8(2_@_@J %&* 1a 1a1ad^ 1ahs6s6l z *ra