
    ;3hC                       d Z ddlm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	 ddl
mZ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 dd
lmZ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% ddl&m'Z' ddl(m)Z) ddl*m+Z+ erddlm,Z,  G d de,      Z-ddl.m/Z/  G d de      Z0 G d dee      Z1d Z2 G d de1e      Z3 G d de3      Z4 G d d ejj                        Z6 G d! d"ejj                        Z7d%d#Z8	 d&	 	 	 	 	 	 	 d'd$Z9y)(zContains all of the components that can be used with Gradio Interface / Blocks.
Along with the docs for each component, you can find the names of example demos that use
each component. These demos are located in the `demo` directory.    )annotationsN)ABCabstractmethod)CallableSequence)Enum)Path)TYPE_CHECKINGAny)utils)BlockBlockContext)ComponentMeta)	BaseModelDeveloperPathFileDataFileDataDictGradioDataModelMediaStreamChunk)EventListener)I18nData)Form)move_files_to_cache)	TypedDictc                  "    e Zd ZU ded<   ded<   y)DataframeDataz	list[str]headerszlist[list[str | int | bool]]dataN)__name__
__module____qualname____annotations__     V/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/components/base.pyr   r   '   s    **r$   r   Timerc                      e Zd ZdZdZy)	_KeywordsNO_VALUEFINISHED_ITERATINGN)r   r    r!   r*   r+   r#   r$   r%   r)   r)   .   s    H-r$   r)   c                      e Zd ZU g Zded<   edd       Zed        Zed        Zedd       Z	edd       Z
eddd       Zedd	       Zeedd
              Zedd       Zedd       Zy)ComponentBasezlist[EventListener | str]EVENTSc                    |S )a  
        Any preprocessing needed to be performed on function input.
        Parameters:
            payload: The input data received by the component from the frontend.
        Returns:
            The preprocessed input data sent to the user's function in the backend.
        r#   selfpayloads     r%   
preprocesszComponentBase.preprocess6   s	     r$   c                    |S )a  
        Any postprocessing needed to be performed on function output.
        Parameters:
            value: The output data received by the component from the user's function in the backend.
        Returns:
            The postprocessed output data sent to the frontend.
        r#   r1   values     r%   postprocesszComponentBase.postprocessA   s	     r$   c                     y)a?  
        Process the input data in a way that can be displayed by the examples dataset component in the front-end.

        For example, only return the name of a file as opposed to a full path. Or get the head of a dataframe.
        The return value must be able to be json-serializable to put in the config.
        Nr#   r5   s     r%   process_examplezComponentBase.process_exampleL   s     	r$   c                     y)
        The typing information for this component as a dictionary whose values are a list of 2 strings: [Python type, language-agnostic description].
        Keys of the dictionary are: raw_input, raw_output, serialized_input, serialized_output
        Nr#   r1   s    r%   api_infozComponentBase.api_infoV   s     	r$   c                     y)zW
        Deprecated and replaced by `example_payload()` and `example_value()`.
        Nr#   r<   s    r%   example_inputszComponentBase.example_inputs^       
 	r$   c                     y)s
        Write the component's value to a format that can be stored in a csv or jsonl format for flagging.
        Nr#   r1   r2   flag_dirs      r%   flagzComponentBase.flage   r@   r$   c                    |S zW
        Convert the data from the csv or jsonl file into the component state.
        r#   r0   s     r%   read_from_flagzComponentBase.read_from_flagl   s	    
 r$   c                     y)zBWhether this component should be skipped from the api return valueNr#   r<   s    r%   skip_apizComponentBase.skip_apis   s    r$   c                    || j                   v S N)r.   )clsevents     r%   	has_eventzComponentBase.has_eventx   s    

""r$   c                    | j                   }t        j                  |   j                  }t	        j
                  | j                   d| j                               j                         }|S )N_)	r    sysmodules__file__hashlibsha256r   encode	hexdigest)rM   module_namemodule_pathmodule_hashs       r%   get_component_class_idz$ComponentBase.get_component_class_id|   sZ    nnkk+.77nn||nAk]+224

)+ 	 r$   Nr2   r   returnr   )r^   zdict[str, list[str]]r^   r    )r2   zAny | GradioDataModelrD   
str | Pathr^   str)r2   r   r^   zGradioDataModel | Any)r^   bool)rN   zstr | EventListenerr^   rd   )r^   rc   )r   r    r!   r.   r"   r   r3   r7   r9   r=   r?   rE   rH   propertyrJ   classmethodrO   r\   r#   r$   r%   r-   r-   3   s    (*F%*              Q  Q # #  r$   r-   )	metaclassc                    d| _         | S )NT)_is_server_fn)fns    r%   serverrk      s    BIr$   c                  .    e Zd ZdZ	 dddddddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ ed      ZdZ fd	Ze	d
        Z
edd       Z	 d	 	 	 	 	 ddZd Zd ZddZddZddZddZddZddZdddZddZ xZS )	ComponentzY
    A base class for defining methods that all input/output components should have.
    NTr6   )labelinfo
show_label	containerscale	min_widthinteractivevisibleelem_idelem_classesrenderkeypreserved_by_keyload_fneveryinputsc          	     H   t        | j                        D cg c]:  }t        t        | |            r#t        t        | |      dd      rt        | |      < c}| _        |sg }d| _        t        | d      sd | _        t        j                  | |
||	|||       t        | t              r| j                          || _        || _        |s|rt        j                   d       d}|d}|| _        || _        |'|t'        |      k7  rt        j                   d| d       || _        || _        || _        d | _        d | _        | j3                  |      \  }}| j5                  |      }t        |t6              r|j9                         }t;        || dd	      | _        t?        j@                  | j<                        r(| jB                  jE                  | j<                  d
          t        |      r| jG                  |||       | j                  jI                         | _%        y c c}w )Nri   F
data_model)rv   rw   ru   rx   ry   rz   z1show_label has no effect when container is False.Tz*'scale' value should be an integer. Using z will cause issues.)r7   keep_in_cachepath)&dir	__class__callablegetattr
server_fns_selectablehasattrr   r   __init__
isinstanceStreamingInputcheck_streamablern   ro   warningswarnrp   rq   roundrr   rs   rt   
load_eventload_event_to_attachget_load_fn_and_initial_valuer7   r   
model_dumpr   r6   client_utilsis_file_objr   addattach_load_eventr\   component_class_id)r1   r6   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   initial_values                      r%   r   zComponent.__init__   s   . T^^,
e,-e,ouE D% 
 L !t\*<@DO%-	
 dN+!!#
	QRJJ$"%,!6MM<UGCVW 
"& 26  	! "&!C!CE6!R((7 mY/)446M(	

 ##DJJ/""4::f#56G""7E6:"&.."G"G"I[
s   ?Hz./templates/z../../frontend/c                    t         |          }| j                  r| j                  |d<   t        | j                        r'| j                  D cg c]  }|j
                   c}|d<   |j                  dd        |S c c}w )Nro   r   rx   )super
get_configro   lenr   r   pop)r1   configrj   r   s      r%   r   zComponent.get_config   sf    #%99!YYF6Nt:>//#JBBKK#JF< 

8T" $Ks   A;c                     yNFr#   r<   s    r%   rJ   zComponent.skip_api   s    r$   c                J    d }t        |       r|s |        }| }||fS | }d }||fS rL   )r   )r6   r}   r   r{   s       r%   r   z'Component.get_load_fn_and_initial_value  sA    E? %G %% "MG%%r$   c                    t        |t              r|g}|r|D cg c]  }t        |d      s|df c}ng }t        |t        t        f      rddlm}  ||      }|r|j                  |df       |||f| _        yc c}w )zOAdd an event that runs `callable`, optionally at interval specified by `every`.changer   r&   tickN)	r   rm   r   intfloatgradio.componentsr'   appendr   )r1   r   r|   r}   ichangeable_eventsr'   s          r%   r   zComponent.attach_load_event  s     fi(XFGMFCqga.Ba]CSU 	 ec5\*/%LE$$eV_5%
! Ds
   A;A;c                $    | j                  |      S )a  
        Process the input data in a way that can be displayed by the examples dataset component in the front-end.
        By default, this calls the `.postprocess()` method of the component. However, if the `.postprocess()` method is
        computationally intensive, or returns a large payload, a custom implementation may be appropriate.

        For example,  the `process_example()` method of the `gr.Audio()` component only returns the name of the file, not
        the processed audio file. The `.process_example()` method of the `gr.Dataframe()` returns the head of a dataframe
        instead of the full dataframe.

        The return value of this method must be json-serializable to put in the config.
        )r7   r5   s     r%   r9   zComponent.process_example(  s     &&r$   c                $    | j                  |      S )z/Deprecated and replaced by `process_example()`.)r9   r5   s     r%   
as_examplezComponent.as_example6  s    ##E**r$   c                "    | j                         S )zEDeprecated and replaced by `example_payload()` and `example_value()`.)example_payloadr<   s    r%   r?   zComponent.example_inputs:  s    ##%%r$   c                    t               )z
        An example input data for this component, e.g. what is passed to this component's preprocess() method.
        This is used to generate the docs for the View API page for Gradio apps using this component.
        NotImplementedErrorr<   s    r%   r   zComponent.example_payload>      
 "##r$   c                    t               )z
        An example output data for this component, e.g. what is passed to this component's postprocess() method.
        This is used to generate an example value if this component is used as a template for a custom component.
        r   r<   s    r%   example_valuezComponent.example_valueE  r   r$   c                    | j                   3| j                   j                         }|j                  dd      }||d<   |S t        d| j	                                )r;   Ndescriptionadditional_descriptionz1The api_info method has not been implemented for )r   model_json_schemar   r   get_block_name)r1   schemadescs      r%   r=   zComponent.api_infoL  sa    
 ??&__668F::mT2D/3F+,M!?@S@S@U?VW
 	
r$   c                "    | j                         S rL   r=   r<   s    r%   api_info_as_inputzComponent.api_info_as_inputZ      }}r$   c                "    | j                         S rL   r   r<   s    r%   api_info_as_outputzComponent.api_info_as_output]  r   r$   c                R   | j                   rU| j                   j                  |      }t        |      j                  d       |j	                  |      j                         }t        |t              r|j                         }t        |t              st        j                  |      }|S )rB   T)exist_ok)r   	from_jsonr	   mkdircopy_to_dirr   r   r   rc   jsondumpsrC   s      r%   rE   zComponent.flag`  s     ??oo//8GN  $ /))(3>>@Ggy)((*G'3'jj)Gr$   c                z    | j                   r.| j                   j                  t        j                  |            S |S rG   )r   r   r   loadsr0   s     r%   rH   zComponent.read_from_flagn  s.     ????,,TZZ-@AAr$   rL   )"r6   r   rn   str | I18nData | Nonero   r   rp   bool | Nonerq   rd   rr   
int | Noners   r   rt   r   ru   rd   rv   
str | Nonerw   list[str] | str | Nonerx   rd   ry   z(int | str | tuple[int | str, ...] | Nonerz   r   r{   zCallable | Noner|   Timer | float | Noner}   7Component | Sequence[Component] | set[Component] | None)r   r   r|   r   r}   r   r_   )r^   zdict[str, Any]r`   )r2   r   rD   rb   r^   rc   )r2   r   )r   r    r!   __doc__r   r   TEMPLATE_DIRFRONTEND_DIRr   re   rJ   staticmethodr   r   r9   r   r?   r   r   r=   r   r   rE   rH   __classcell__r   s   @r%   rm   rm      s    bJ (,&*"&  $#'"/38<3:#'&*JN'bJbJ %	bJ
 $bJ  bJ bJ bJ bJ !bJ bJ bJ -bJ bJ 6bJ  1!bJ" !#bJ$ $%bJ& H'bJH !0L$L   	& 	& KO	

 $
 H	
0'+&$$
r$   rm   c                  &    e Zd ZdZddZddZd Zy)FormComponenta  
    A base class for components that are typically used in forms (e.g. Textbox, Dropdown). These
    components will be grouped together in the UI to provide a more condensed layout. Components
    that are not rendered in the UI (e.g. State) should also inherit from this class, as it will
    prevent them from breaking the grouping, see: https://github.com/gradio-app/gradio/issues/10330
    c                .    t        | dd       du ry t        S )Nrq   F)r   r   r<   s    r%   get_expected_parentz!FormComponent.get_expected_parent  s    4d+u4r$   c                    |S rL   r#   r0   s     r%   r3   zFormComponent.preprocess  s    r$   c                    |S rL   r#   r5   s     r%   r7   zFormComponent.postprocess  s    r$   N)r^   ztype[Form] | Noner]   )r   r    r!   r   r   r3   r7   r#   r$   r%   r   r   w  s    
r$   r   c                       e Zd Zd fdZej
                  	 	 	 	 	 	 dd       Zej
                  	 	 d	 	 	 	 	 dd       Z xZS )StreamingOutputc                (    t        |   |i | |  y rL   r   r   r1   argskwargsr   s      r%   r   zStreamingOutput.__init__  s    $)&)r$   c                   K   y wrL   r#   )r1   r6   	output_idfirst_chunks       r%   stream_outputzStreamingOutput.stream_output  s      	   c                   K   yw)a  Combine all of the stream chunks into a single file.

        This is needed for downloading the stream and for caching examples.
        If `only_file` is True, only the FileData corresponding to the file should be returned (needed for downloading the stream).
        The desired_output_format optionally converts the combined file. Should only be used for cached examples.
        Nr#   )r1   streamdesired_output_format	only_files       r%   combine_streamzStreamingOutput.combine_stream  s      	r   r^   None)r   rc   r   rd   r^   z3tuple[MediaStreamChunk | None, FileDataDict | dict]r   )r   zlist[bytes]r   r   r^   zGradioDataModel | FileData)	r   r    r!   r   abcr   r   r   r   r   s   @r%   r   r     sx     	 #26	< 
 	 -1	  *
 
$ r$   r   c                  D     e Zd Zd fdZej
                  d        Z xZS )r   c                $    t        |   |i | y rL   r   r   s      r%   r   zStreamingInput.__init__  s    $)&)r$   c                     y)z8Used to check if streaming is supported given the input.Nr#   r<   s    r%   r   zStreamingInput.check_streamable  s     	r$   r   )r   r    r!   r   r   r   r   r   r   s   @r%   r   r     s"    * 	 r$   r   c                     t        j                  |       |      }t        |t              rt	        d|j
                         t        |t              st        d|j
                         |S )Nrx   Invalid component: 'Expected a Component instance, but got )r   component_or_layout_classr   r   
ValueErrorr   rm   	TypeError)cls_namerx   objs      r%   	componentr     s`    
3%
)
)(
36
BC#|$.s}}o>??c9%A#--QRRJr$   c                   t        | t              rt        | |      }nt        | t              rP| j	                  d      }t        j                  |      } |di | d|i}t        |t              r/t        d|       t        | t              r| }nt        d|        |r|j                  s|j                          n|r|j                  r|j                          t        |t              st        d|j                         |S )a  
    Returns a component instance from a string, dict, or Component object.
    Parameters:
        comp: the component to instantiate. If a string, must be the name of a component, e.g. "dropdown". If a dict, must have a "name" key, e.g. {"name": "dropdown", "choices": ["a", "b"]}. If a Component object, will be returned as is.
        render: whether to render the component. If True, renders the component (if not already rendered). If False, does not do anything.
        unrender: whether to unrender the component. If True, unrenders the the component (if already rendered) -- this is useful when constructing an Interface or ChatInterface inside of a Blocks. If False, does not do anything.
    r   namerx   r   zFComponent must be provided as a `str` or `dict` or `Component` but is r   r#   )r   rc   r   dictr   r   r   r   r   rm   is_renderedrx   unrenderr   r   )comprx   r  component_objr  component_clss         r%   get_component_instancer	    s     $!$v6	D$	xx77=%<<V<m\224&9::	D)	$TUYTZ[
 	
 m//	m// mY/5m6M6M5NO
 	
 r$   )r   rc   rx   rd   r^   rm   )FF)r  zstr | dict | Componentrx   rd   r  rd   r^   rm   ):r   
__future__r   r   rU   r   rR   r   r   r   collections.abcr   r   enumr   pathlibr	   typingr
   r   gradio_client.utilsr   r   gradiogradio.blocksr   r   gradio.component_metar   gradio.data_classesr   r   r   r   r   r   gradio.eventsr   gradio.i18nr   gradio.layoutsr   gradio.processing_utilsr   r   r   r   r'   r)   r-   rk   rm   r   ABCMetar   r   r   r	  r#   r$   r%   <module>r     s   D # 
   
  # .   % *  - /  (    7 +	 + (. .
PC= Pf
iu iXI ( 6s{{  JO!
 !*.!BF!!r$   