
    wi5R                    l   U 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	 ddl
mZ ddlmZmZ ddlmZ  e e	e          j        dz            5 Z ej        e          Zddd           n# 1 swxY w Y    G d	 d
          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z!i Z"e#                                D ]-Z$e$e"e$j%        <   e$#                                D ]Z&e&e"e&j%        <   .ee"d
<   ee"d<   ee"d <   i d!ed"ed#ed$ed%ed&ed'ed(ed)ed*ed+ed,e d-e d.e d/ed0ed1eee e e ee!e ee e e e eee d2Z'd3e(d4<   dS )5z:Included for backwards compatibility with 3.x spaces/apps.    )annotationsN)Path)Any)
media_datautils)FileDataz
types.jsonc                  N    e Zd Zd ZddZddZddZdd	ZdddZ	 	 	 dd dZ	dS )!Serializablec                *    |                                  S z
        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
        )api_infoselfs    i/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/gradio_client/serializing.pyserialized_infozSerializable.serialized_info   s    
 }}    returndict[str, list[str]]c                    t                      r   NotImplementedErrorr   s    r   r   zSerializable.api_info       
 "###r   dict[str, Any]c                    t                      )z
        The example inputs for this component as a dictionary whose values are example inputs compatible with this component.
        Keys of the dictionary are: raw, serialized
        r   r   s    r   example_inputszSerializable.example_inputs#   r   r   tuple[str, str]c                    |                                  }|                    d|d         d         gdz            }|d         |d         fS )Nserialized_inputinfotype   r      r   getr   r   typess      r   input_api_infozSerializable.input_api_info+   sH    ==??/(62B62J1Ka1OPPa%(##r   c                    |                                  }|                    d|d         d         gdz            }|d         |d         fS )Nserialized_outputr   r    r!   r   r"   r#   r%   s      r   output_api_infozSerializable.output_api_info1   sH    ==??08F3CF3K2Lq2PQQa%(##r    Fxr   load_dir
str | Pathallow_linksboolc                    |S )z]
        Convert data from human-readable format to serialized format for a browser.
         r   r,   r-   r/   s       r   	serializezSerializable.serialize6   s	     r   Nsave_dirstr | Path | Noneroot_url
str | Nonehf_tokenc                    |S )z]
        Convert data from serialized format for a browser to human-readable format.
        r2   r   r,   r5   r7   r9   s        r   deserializezSerializable.deserialize<   s	     r   )r   r   r   r   )r   r   r+   F)r,   r   r-   r.   r/   r0   NNN)r,   r   r5   r6   r7   r8   r9   r8   )
__name__
__module____qualname__r   r   r   r'   r*   r4   r<   r2   r   r   r
   r
      s          $ $ $ $$ $ $ $$ $ $ $$ $ $ $
     '+##
 
 
 
 
 
 
r   r
   c                  "    e Zd ZdZddZd	dZdS )
SimpleSerializablezIGeneral class that does not perform any serialization or deserialization.r   dict[str, bool | dict]c                "    t           d         ddS )NrD   Fr   r   serializer_typesr   s    r   r   zSimpleSerializable.api_infoL       $%9:$
 
 	
r   r   c                    d d dS )Nraw
serializedr2   r   s    r   r   z!SimpleSerializable.example_inputsR       
 
 	
r   Nr   rE   r=   r@   rA   rB   __doc__r   r   r2   r   r   rD   rD   I   sB        SS
 
 
 

 
 
 
 
 
r   rD   c                  "    e Zd ZdZddZd	dZdS )
StringSerializablez?Expects a string as input/output but performs no serialization.r   rE   c                "    t           d         ddS )NrT   FrG   rH   r   s    r   r   zStringSerializable.api_info\   rJ   r   r   c                    dddS )NHowdy!rL   r2   r   s    r   r   z!StringSerializable.example_inputsb   s    "
 
 	
r   NrP   r=   rQ   r2   r   r   rT   rT   Y   sB        II
 
 
 

 
 
 
 
 
r   rT   c                  "    e Zd ZdZddZd	dZdS )
ListStringSerializablezHExpects a list of strings as input/output but performs no serialization.r   rE   c                "    t           d         ddS )NrY   FrG   rH   r   s    r   r   zListStringSerializable.api_infol   s    $%=>$
 
 	
r   r   c                    ddgddgdS )NrW   MerhabarL   r2   r   s    r   r   z%ListStringSerializable.example_inputsr   s!    i(#Y/
 
 	
r   NrP   r=   rQ   r2   r   r   rY   rY   i   sB        RR
 
 
 

 
 
 
 
 
r   rY   c                  "    e Zd ZdZddZd	dZdS )
BooleanSerializablez@Expects a boolean as input/output but performs no serialization.r   rE   c                "    t           d         ddS )Nr^   FrG   rH   r   s    r   r   zBooleanSerializable.api_info|   s    $%:;$
 
 	
r   r   c                    dddS )NTrL   r2   r   s    r   r   z"BooleanSerializable.example_inputs   rO   r   NrP   r=   rQ   r2   r   r   r^   r^   y   sB        JJ
 
 
 

 
 
 
 
 
r   r^   c                  "    e Zd ZdZddZd	dZdS )
NumberSerializablezKExpects a number (int/float) as input/output but performs no serialization.r   rE   c                "    t           d         ddS )Nrb   FrG   rH   r   s    r   r   zNumberSerializable.api_info   rJ   r   r   c                    dddS )N   rL   r2   r   s    r   r   z!NumberSerializable.example_inputs   s    
 
 	
r   NrP   r=   rQ   r2   r   r   rb   rb      sB        UU
 
 
 

 
 
 
 
 
r   rb   c                  F    e Zd ZdZd ZddZddZ	 	 dddZ	 	 	 dddZdS )ImgSerializablezJExpects a base64 string as input/output which is serialized to a filepath.c                    dddS )Nstringz+filepath on your computer (or URL) of imager    descriptionr2   r   s    r   r   zImgSerializable.serialized_info   s    H
 
 	
r   r   rE   c                "    t           d         ddS )Nrg   TrG   rH   r   s    r   r   zImgSerializable.api_info   s    ():;PTUUUr   r   c                     t           j        ddS )NPhttps://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.pngrL   r   BASE64_IMAGEr   s    r   r   zImgSerializable.example_inputs   s    *l
 
 	
r   r+   Fr,   r8   r-   r.   r/   r0   c                    |sdS t          j        |          rt          j        |          S t          j        t	          |          |z            S )z
        Convert from human-friendly version of a file (string filepath) to a serialized
        representation (base64).
        Parameters:
            x: String path to file to serialize
            load_dir: Path to directory containing x
        N)r   is_http_url_likeencode_url_to_base64encode_file_to_base64r   r3   s       r   r4   zImgSerializable.serialize   sQ      	4!!$$ 	1-a000*4>>A+=>>>r   Nr5   r6   r7   r9   c                P    ||dk    rdS t          j        ||          }|j        S )a  
        Convert from serialized representation of a file (base64) to a human-friendly
        version (string filepath). Optionally, save the file to the directory specified by save_dir
        Parameters:
            x: Base64 representation of image to deserialize into a string filepath
            save_dir: Path to directory to save the deserialized image to
            root_url: Ignored
            hf_token: Ignored
        Nr+   dir)r   decode_base64_to_filename)r   r,   r5   r7   r9   files         r   r<   zImgSerializable.deserialize   s3      9R4*1(;;;yr   rP   r=   r>   )r,   r8   r-   r.   r/   r0   r   r8   r?   )
r,   r8   r5   r6   r7   r8   r9   r8   r   r8   )	r@   rA   rB   rR   r   r   r   r4   r<   r2   r   r   rg   rg      s        TT
 
 
V V V V
 
 
 
  "!	? ? ? ? ?, '+##      r   rg   c                       e Zd ZdZd' fdZd Zd Zd Zd Zd	 Z	d(dZ
d)dZd)dZd)dZ	 	 d*d+dZd Z	 	 	 d,d-d Z	 	 d*d.d#Z	 	 	 d,d/d&Z xZS )0FileSerializablezfExpects a dict with base64 representation of object as input/output which is serialized to a filepath.r   Nonec                d    d | _         d | _        t                                                       d S N)streamstream_namesuper__init__)r   	__class__s    r   r   zFileSerializable.__init__   s.    r   c                *    |                                  S r   )_single_file_serialized_infor   s    r   r   z FileSerializable.serialized_info   s    00222r   c                "    t           d         ddS )NSingleFileSerializableTrG   rH   r   s    r   _single_file_api_infoz&FileSerializable._single_file_api_info   s    $%=>#
 
 	
r   c                    dddS )Nri   *filepath on your computer (or URL) of filerj   r2   r   s    r   r   z-FileSerializable._single_file_serialized_info   s    G
 
 	
r   c                    ddddddS )Narrayz&List of filepath(s) or URL(s) to filesri   r   rj   )r    rk   itemsr2   r   s    r   _multiple_file_serialized_infoz/FileSerializable._multiple_file_serialized_info   s'    C K 
 
 	
r   c                "    t           d         ddS )NMultipleFileSerializableTrG   rH   r   s    r   _multiple_file_api_infoz(FileSerializable._multiple_file_api_info   s    $%?@#
 
 	
r   dict[str, dict | bool]c                *    |                                  S r   )r   r   s    r   r   zFileSerializable.api_info   s    ))+++r   r   c                *    |                                  S r   )_single_file_example_inputsr   s    r   r   zFileSerializable.example_inputs   s    //111r   c                &    dt           j        dddS NFis_filedatazMhttps://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdfrL   r   BASE64_FILEr   s    r   r   z,FileSerializable._single_file_example_inputs  s"    $j.DEEi
 
 	
r   c                *    dt           j        dgdgdS r   r   r   s    r   _multiple_file_example_inputsz.FileSerializable._multiple_file_example_inputs  s*     %z/EFFG_
 
 	
r   r+   Fr,   str | FileData | Noner-   r.   r/   r0   FileData | Nonec                R   |t          |t                    r|S t          j        |          r|}d }nEt	          t          |          |z            }t          |                                          j        }|pd |rd nt          j        |          t          |          j	        |dS )N)ry   r   	orig_namesize)

isinstancedictr   rr   strr   statst_sizeencode_url_or_file_to_base64ry   )r   r,   r-   r/   filenamer   s         r   _serialize_singlez"FileSerializable._serialize_single  s     9
1d++9H!!$$ 	1HDD4>>A-..H>>&&((0D$>DD3H==h,
 
 	
r   c                ,    t          j        ||          S r   )r   download_byte_stream)r   urlr9   s      r   _setup_streamzFileSerializable._setup_stream&  s    )#x888r   Nr5   r8   r7   r9   c                   |d S t          |t                    rt          j        ||          j        }nt          |t
                    r|                    d          rb|                    d          }|t          d|           |t          j        |dz   |z   ||          }nt          j	        ||          }n|                    d          r|d         r|r|st          d          | j
        r| j        |d         k    r5|                     |d	z   |d         z   |
          | _
        |d         | _        t          | j
                  }t          |pt          j                              t#          j        d          z  }|                    dd           ||                    dd          z  }|                    |           t          |          }ng|                    d          }	|	t          d|           t          j        |	|          j        }n"t          dt+          |           d|           |S )Nrv   r   ry   zThe 'name' field is missing in zfile=)r9   rw   	is_streamz2name and root_url and save_dir must all be presentzstream/)r9      T)parentsexist_okr   outputr   zThe 'data' field is missing in zLA FileSerializable component can only deserialize a string or a dict, not a z: )r   r   r   rx   ry   r   r$   
ValueErrordownload_tmp_copy_of_filecreate_tmp_copy_of_filer   r   r   nextr   tempfile
gettempdirsecrets	token_hexmkdirwrite_bytesr    )
r   r,   r5   r7   r9   	file_namefilepathchunkpathr   s
             r   _deserialize_singlez$FileSerializable._deserialize_single)  s}    94a '	3A8DDDIII4   %	uuY  Q55==#$%Jq%J%JKKK' % ? 7*X5!)$! ! !II !& =hH U U UII{## Q&	 h 8 $L   { 1d&6!F)&C&C"&"4"4 9,qy88 #5 # #DK ()yD$T[))H=(;(=(=>>ARSUAVAVV

4$
777aeeK:::  '''II		uuV}}<$%Jq%J%JKKK!7(KKKP		m_cde_f_fmmjkmm   r   3str | FileData | None | list[str | FileData | None]'FileData | None | list[FileData | None]c                     ||dk    rdS t          |t                    r fd|D             S                      |          S )aG  
        Convert from human-friendly version of a file (string filepath) to a
        serialized representation (base64)
        Parameters:
            x: String path to file to serialize
            load_dir: Path to directory containing x
            allow_links: Will allow path returns instead of raw file content
        Nr+   c                >    g | ]}                     |          S r2   )r   ).0fr/   r-   r   s     r   
<listcomp>z.FileSerializable.serialize.<locals>.<listcomp>m  s+    PPPD**1hDDPPPr   )r   listr   r3   s   ` ``r   r4   zFileSerializable.serialize\  sd     9R4a 	DPPPPPPaPPPP))!X{CCCr   Path | str | Nonestr | None | list[str | None]c                     |dS t          t                    rt                    t          |t                    r fd|D             S                      |          S )aV  
        Convert from serialized representation of a file (base64) to a human-friendly
        version (string filepath). Optionally, save the file to the directory specified by `save_dir`
        Parameters:
            x: Base64 representation of file to deserialize into a string filepath
            save_dir: Path to directory to save the deserialized file to
            root_url: If this component is loaded from an external Space, this is the URL of the Space.
            hf_token: If this component is loaded from an external private Space, this is the access token for the Space
        Nc                B    g | ]}                     |           S )r5   r7   r9   )r   )r   r   r9   r7   r5   r   s     r   r   z0FileSerializable.deserialize.<locals>.<listcomp>  sI         ((8h )    r   r   )r   r   r   r   r   r;   s   ` ```r   r<   zFileSerializable.deserializeq  s      94h%% 	%8}}Ha 
	       	    ++Hx( ,   r   )r   r}   r   r   r=   r>   )r,   r   r-   r.   r/   r0   r   r   r?   )
r,   r   r5   r8   r7   r8   r9   r8   r   r8   )r,   r   r-   r.   r/   r0   r   r   )
r,   r   r5   r   r7   r8   r9   r8   r   r   )r@   rA   rB   rR   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r<   __classcell__r   s   @r   r|   r|      s}       pp     
3 3 3
 
 

 
 

 
 

 
 
, , , ,2 2 2 2
 
 
 

 
 
 
  "!	
 
 
 
 
.9 9 9  $##1 1 1 1 1l  "!	D D D D D0 '+##        r   r|   c                  N     e Zd Zd ZddZddZ	 dd fdZ	 	 	 dd fdZ xZS )VideoSerializablec                    dddS )Nri   z0filepath on your computer (or URL) of video filerj   r2   r   s    r   r   z!VideoSerializable.serialized_info  s    M
 
 	
r   r   r   c                "    t           d         ddS )Nr|   TrG   rH   r   s    r   r   zVideoSerializable.api_info      ();<QUVVVr   r   c                &    dt           j        dddS )NFr   zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/video_sample.mp4rL   )r   BASE64_VIDEOr   s    r   r   z VideoSerializable.example_inputs  s"    $j.EFFj
 
 	
r   r+   Fr,   r8   r-   r.   r/   r0   tuple[FileData | None, None]c                N    t                                          |||          d fS r   )r   r4   )r   r,   r-   r/   r   s       r   r4   zVideoSerializable.serialize  s&     !!!X{;;TBBr   N.tuple[FileData | None, FileData | None] | Noner5   r   r7   r9   *str | tuple[str | None, str | None] | Nonec                R   t          |t          t          f          r6t          |          dk    rt	          d|           |d         |d         g}nt	          d|           t                                          ||||          }t          |t                    r|d         S dS )z
        Convert from serialized representation of a file (base64) to a human-friendly
        version (string filepath). Optionally, save the file to the directory specified by `save_dir`
        r!   z&Expected tuple of length 2. Received: r   r"   N)r   tupler   lenr   r   r<   )r   r,   r5   r7   r9   	x_as_listdeserialized_filer   s          r   r<   zVideoSerializable.deserialize  s     a%'' 	K1vv{{ !M!!M!MNNN1qtIIIaIIJJJ!GG//	8XxXX'.. 	($Q''	( 	(r   r   r=   r>   )r,   r8   r-   r.   r/   r0   r   r   r?   )
r,   r   r5   r   r7   r8   r9   r8   r   r   )	r@   rA   rB   r   r   r   r4   r<   r   r   s   @r   r   r     s        
 
 
W W W W
 
 
 
 MRC C C C C C C '+##( ( ( ( ( ( ( ( ( ( (r   r   c                  B    e Zd Zd ZddZddZ	 	 dddZ	 	 	 dddZdS )JSONSerializablec                    dddS )Nri   zfilepath to JSON filerj   r2   r   s    r   r   z JSONSerializable.serialized_info  s     1HIIIr   r   r   c                "    t           d         ddS )Nr   TrG   rH   r   s    r   r   zJSONSerializable.api_info  r   r   r   c                    dddd dS )Nr"   r!   )abrL   r2   r   s    r   r   zJSONSerializable.example_inputs  s    ##
 
 	
r   r+   Fr,   r8   r-   r.   r/   r0   dict | list | Nonec                ^    ||dk    rdS t          j        t          |          |z            S )a  
        Convert from a a human-friendly version (string path to json file) to a
        serialized representation (json string)
        Parameters:
            x: String path to json file to read to get json string
            load_dir: Path to directory containing x
        Nr+   )r   file_to_jsonr   r3   s       r   r4   zJSONSerializable.serialize  s2     9R4!$x..1"4555r   Nstr | dict | listr5   r6   r7   r9   c                @    |dS t          j        ||          j        S )av  
        Convert from serialized representation (json string) to a human-friendly
        version (string path to json file).  Optionally, save the file to the directory specified by `save_dir`
        Parameters:
            x: Json string
            save_dir: Path to save the deserialized json file to
            root_url: Ignored
            hf_token: Ignored
        Nrv   )r   dict_or_str_to_json_filery   r;   s        r   r<   zJSONSerializable.deserialize  s'      94-aX>>>CCr   r   r=   r>   )r,   r8   r-   r.   r/   r0   r   r   r?   )
r,   r   r5   r6   r7   r8   r9   r8   r   r8   r@   rA   rB   r   r   r   r4   r<   r2   r   r   r   r     s        J J JW W W W
 
 
 
  "!	6 6 6 6 6( '+##D D D D D D Dr   r   c                  @    e Zd Zd ZddZddZ	 dddZ	 	 	 dddZdS )GallerySerializablec                    dddS )Nri   z^path to directory with images and a file associating images with captions called captions.jsonrj   r2   r   s    r   r   z#GallerySerializable.serialized_info  s    {
 
 	
r   r   r   c                "    t           d         ddS )Nr   TrG   rH   r   s    r   r   zGallerySerializable.api_info  s    $%:;#
 
 	
r   r   c                0    t           j        gdz  dgdz  dS )Nr!   rn   rL   ro   r   s    r   r   z"GallerySerializable.example_inputs  s1    +,q0b 
 
 	
r   r+   Fr,   r8   r-   r.   r/   r0   list[list[str | None]] | Nonec                l   ||dk    rd S g }t          |          dz  }|                    d          5 }t          j        |          }d d d            n# 1 swxY w Y   |                                D ]?\  }}	t                                          ||          }
|                    |
|	g           @|S )Nr+   captions.jsonr)r/   )r   openjsonloadr   r|   r4   append)r   r,   r-   r/   filescaptions_filecaptions_jsoncaptionsr   captionimgs              r   r4   zGallerySerializable.serialize  s     9R4Q/1$$ 	0y//H	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0"*.."2"2 	) 	)Iw"$$..yk.RRCLL#w((((s   AAANr5   r   r7   r9   
None | strc                   |d S t          |          t          t          j                              z  }|                    dd           i }|D ]}t          |t          t          f          r|\  }}nd }t                      	                    ||||          }	|||	<   |dz  }
|

                    d          5 }t          j        ||           d d d            n# 1 swxY w Y   t          j                            |          S )NT)r   r   )r7   r9   r   w)r   r   uuiduuid4r   r   r   r   r|   r<   r   r   dumposr   abspath)r   r,   r5   r7   r9   gallery_pathr  img_datar  ry   r  r  s               r   r<   zGallerySerializable.deserialize  sK    94H~~DJLL(9(99D$777 	3 	3H(T5M22 $,!''#%%11,H 2  D %HTN(?:M##C(( 3M	(M2223 3 3 3 3 3 3 3 3 3 3 3 3 3 3w|,,,s   :CC 	#C 	r   r=   r>   )r,   r8   r-   r.   r/   r0   r   r   )r+   NN)
r,   r   r5   r   r7   r8   r9   r8   r   r  r   r2   r   r   r   r     s        
 
 

 
 
 

 
 
 
 MR    " ##- - - - - - -r   r   FileUploadButtontextboxnumberslidercheckboxcheckboxgroupradiodropdownimagevideoaudiorz   	dataframe
timeseriesfileexplorerstatebuttonuploadbutton)colorpickerlabelhighlightedtextr   htmlgallerychatbotmodel3dplotbarplotlineplotscatterplotmarkdowncodeannotatedimagezdict[str, type]COMPONENT_MAPPING))rR   
__future__r   r   r  r   r   r
  pathlibr   typingr   gradio_clientr   r   gradio_client.data_classesr   r   __file__parentr   r   rI   r
   rD   rT   rY   r^   rb   rg   r|   r   r   r   SERIALIZER_MAPPING__subclasses__clsr@   subclsr1  __annotations__r2   r   r   <module>r>     s   @ @ @ " " " " " "  				                + + + + + + + + / / / / / /	T$$x..
,
.// $1 ty||$ $ $ $ $ $ $ $ $ $ $ $ $ $ $2 2 2 2 2 2 2 2j
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
\ 
 
 
 
 
 
 
 
, 
 
 
 
 
 
 
 
 
 
 
 8 8 8 8 8l 8 8 8v{ { { { {| { { {|(( (( (( (( ((( (( (( ((V0D 0D 0D 0D 0D| 0D 0D 0Df<- <- <- <- <-, <- <- <-~  &&(( 5 5C'*s|$$$&& 5 5.46?++5 &8 > "- 6 %5 > "!&!!& !&  !& #	!&
 +!& !& "!& _!& !& !& !& !!& "!& $!& !&   !!&" $#!&$ &'" #"&A!& !& !&  ! ! ! ! ! !s   A11A58A5