
    ;3h5R                    2   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        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jG                         D ]5  Z$e$e"e$jJ                  <   e$jG                         D ]  Z&e&e"e&jJ                  <    7 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<   y# 1 sw Y   (xY w)5z:Included for backwards compatibility with 3.x spaces/apps.    )annotationsN)Path)Any)
media_datautils)FileDataz
types.jsonc                  Z    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	y)Serializablec                "    | j                         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    Y/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio_client/serializing.pyserialized_infozSerializable.serialized_info   s    
 }}    c                    t               r   NotImplementedErrorr   s    r   r   zSerializable.api_info       
 "##r   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   c                n    | j                         }|j                  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+   sC    ==?/(62B62J1Ka1OPa%(##r   c                n    | j                         }|j                  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   sC    ==?08F3CF3K2Lq2PQa%(##r   c                    |S )z]
        Convert data from human-readable format to serialized format for a browser.
         r   xload_dirallow_linkss       r   	serializezSerializable.serialize6   s	     r   Nc                    |S )z]
        Convert data from serialized format for a browser to human-readable format.
        r(   r   r*   save_dirroot_urlhf_tokens        r   deserializezSerializable.deserialize<   s	     r   )returnzdict[str, list[str]]r4   zdict[str, Any])r4   ztuple[str, str] F)r*   r   r+   
str | Pathr,   boolNNN)r*   r   r0   str | Path | Noner1   
str | Noner2   r<   )
__name__
__module____qualname__r   r   r   r#   r&   r-   r3   r(   r   r   r
   r
      sV    $$$$
 '+##

 $
 	

 
r   r
   c                       e Zd ZdZddZddZy)SimpleSerializablezIGeneral class that does not perform any serialization or deserialization.c                    t         d   ddS )NrA   Fr   r   serializer_typesr   s    r   r   zSimpleSerializable.api_infoL       $%9:$
 	
r   c                    d d dS )Nraw
serializedr(   r   s    r   r   z!SimpleSerializable.example_inputsR       
 	
r   Nr4   zdict[str, bool | dict]r5   r=   r>   r?   __doc__r   r   r(   r   r   rA   rA   I   s    S

r   rA   c                       e Zd ZdZddZddZy)StringSerializablez?Expects a string as input/output but performs no serialization.c                    t         d   ddS )NrP   FrC   rD   r   s    r   r   zStringSerializable.api_info\   rF   r   c                    dddS )NHowdy!rH   r(   r   s    r   r   z!StringSerializable.example_inputsb   s    "
 	
r   NrL   r5   rM   r(   r   r   rP   rP   Y   s    I

r   rP   c                       e Zd ZdZddZddZy)ListStringSerializablezHExpects a list of strings as input/output but performs no serialization.c                    t         d   ddS )NrU   FrC   rD   r   s    r   r   zListStringSerializable.api_infol   s    $%=>$
 	
r   c                    ddgddgdS )NrS   MerhabarH   r(   r   s    r   r   z%ListStringSerializable.example_inputsr   s    i(#Y/
 	
r   NrL   r5   rM   r(   r   r   rU   rU   i   s    R

r   rU   c                       e Zd ZdZddZddZy)BooleanSerializablez@Expects a boolean as input/output but performs no serialization.c                    t         d   ddS )NrZ   FrC   rD   r   s    r   r   zBooleanSerializable.api_info|   s    $%:;$
 	
r   c                    dddS )NTrH   r(   r   s    r   r   z"BooleanSerializable.example_inputs   rK   r   NrL   r5   rM   r(   r   r   rZ   rZ   y   s    J

r   rZ   c                       e Zd ZdZddZddZy)NumberSerializablezKExpects a number (int/float) as input/output but performs no serialization.c                    t         d   ddS )Nr^   FrC   rD   r   s    r   r   zNumberSerializable.api_info   rF   r   c                    dddS )N   rH   r(   r   s    r   r   z!NumberSerializable.example_inputs   s    
 	
r   NrL   r5   rM   r(   r   r   r^   r^      s    U

r   r^   c                  d    e Zd ZdZd ZddZd	dZ	 	 d
	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZy)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   descriptionr(   r   s    r   r   zImgSerializable.serialized_info   s    H
 	
r   c                    t         d   ddS )Nrc   TrC   rD   r   s    r   r   zImgSerializable.api_info   s    ():;PTUUr   c                (    t         j                  ddS )NPhttps://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.pngrH   r   BASE64_IMAGEr   s    r   r   zImgSerializable.example_inputs   s    **l
 	
r   c                    |sy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   r)   s       r   r-   zImgSerializable.serialize   sC     !!!$--a00**4>A+=>>r   Nc                X    ||dk(  ry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
        Nr7   dir)r   decode_base64_to_filename)r   r*   r0   r1   r2   files         r   r3   zImgSerializable.deserialize   s.      9R**1(;yyr   rL   r5   r6   )r*   r<   r+   r8   r,   r9   r4   r<   r:   )
r*   r<   r0   r;   r1   r<   r2   r<   r4   r<   )	r=   r>   r?   rN   r   r   r   r-   r3   r(   r   r   rc   rc      s    T
V
  "!	?? ? 	?
 
?, '+## $ 	
  
r   rc   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 )FileSerializablezfExpects a dict with base64 representation of object as input/output which is serialized to a filepath.c                >    d | _         d | _        t        |           y N)streamstream_namesuper__init__)r   	__class__s    r   r~   zFileSerializable.__init__   s    r   c                "    | j                         S rz   )_single_file_serialized_infor   s    r   r   z FileSerializable.serialized_info   s    0022r   c                    t         d   ddS )NSingleFileSerializableTrC   rD   r   s    r   _single_file_api_infoz&FileSerializable._single_file_api_info   s    $%=>#
 	
r   c                    dddS )Nre   *filepath on your computer (or URL) of filerf   r(   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 filesre   r   rf   )r   rg   itemsr(   r   s    r   _multiple_file_serialized_infoz/FileSerializable._multiple_file_serialized_info   s    C K
 	
r   c                    t         d   ddS )NMultipleFileSerializableTrC   rD   r   s    r   _multiple_file_api_infoz(FileSerializable._multiple_file_api_info   s    $%?@#
 	
r   c                "    | j                         S rz   )r   r   s    r   r   zFileSerializable.api_info   s    ))++r   c                "    | j                         S rz   )_single_file_example_inputsr   s    r   r   zFileSerializable.example_inputs   s    //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.pdfrH   r   BASE64_FILEr   s    r   r   z,FileSerializable._single_file_example_inputs  s    $j.D.DEi
 	
r   c                2    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/EFG_
 	
r   c                <   |t        |t              r|S t        j                  |      r|}d }n:t	        t        |      |z        }t        |      j                         j                  }|xs d |rd nt        j                  |      t        |      j                  |dS )N)ru   r   	orig_namesize)

isinstancedictr   rn   strr   statst_sizeencode_url_or_file_to_base64ru   )r   r*   r+   r,   filenamer   s         r   _serialize_singlez"FileSerializable._serialize_single  s     9
1d+H!!!$HD4>A-.H>&&(00D$ 33H=h,,
 	
r   c                .    t        j                  ||      S rz   )r   download_byte_stream)r   urlr2   s      r   _setup_streamzFileSerializable._setup_stream&  s    ))#x88r   c                &   |y t        |t              r#t        j                  ||      j                  }|S t        |t
              r|j                  d      r\|j                  d      }|t        d|       | t        j                  |dz   |z   ||      }|S t        j                  ||      }|S |j                  d      r|d   r|r|st        d      | j                  r| j                  |d   k7  r+| j                  |d	z   |d   z   |
      | _
        |d   | _        t        | j                        }t        |xs t        j                                t#        j$                  d      z  }|j'                  dd       ||j                  dd      z  }|j)                  |       t        |      }|S |j                  d      }	|	t        d|       t        j                  |	|      j                  }|S t        dt+        |       d|       )Nrr   r   ru   zThe 'name' field is missing in zfile=)r2   rs   	is_streamz2name and root_url and save_dir must all be presentzstream/)r2      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   rt   ru   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*   r0   r1   r2   	file_namefilepathchunkpathr   s
             r   _deserialize_singlez$FileSerializable._deserialize_single)  s+    9a33A8DIIIN M 4 uuY55=#$'Fqc%JKK' % ? ? 7*X5!)$!I@ 5 !& = =hH UI4 3 {#&	h8$L  {{d&6&6!F)&C"&"4"4 9,qy88 #5 #DK ()yD$T[[)H=(;(;(=>ARARSUAVV

4$
7aeeK::  'I	  uuV}<$'Fqc%JKK!77(KPP	
  ^_cde_f^ggijkilm r   c                    ||dk(  ryt        |t              r!|D cg c]  }| j                  |||       c}S | j                  |||      S c c}w )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
        Nr7   )r   listr   )r   r*   r+   r,   fs        r   r-   zFileSerializable.serialize\  sW     9RaNOPD**1hDPP))!X{CC Qs   Ac           	         |yt        |t              rt        |      }t        |t              r#|D cg c]  }| j	                  ||||       c}S | j	                  ||||      S c c}w )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
        N)r0   r1   r2   )r   r   r   r   r   )r   r*   r0   r1   r2   r   s         r   r3   zFileSerializable.deserializeq  s      9h%8}Ha
 	  ((8h )   ++Hx( ,  s   A')r4   Noner4   zdict[str, dict | bool]r5   r6   )r*   str | FileData | Noner+   r8   r,   r9   r4   zFileData | Noner:   )
r*   r   r0   r<   r1   r<   r2   r<   r4   r<   )r*   3str | FileData | None | list[str | FileData | None]r+   r8   r,   r9   r4   z'FileData | None | list[FileData | None])
r*   r   r0   Path | str | Noner1   r<   r2   r<   r4   zstr | None | list[str | None])r=   r>   r?   rN   r~   r   r   r   r   r   r   r   r   r   r   r   r   r-   r3   __classcell__r   s   @r   rx   rx      s)   p
3



,2

  "!	
 
 
 	

 

.9  $##1 1 1 	1
 1 
1l  "!	D>D D 	D
 
1D0 '+##> $ 	
  
'r   rx   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 )Nre   z0filepath on your computer (or URL) of video filerf   r(   r   s    r   r   z!VideoSerializable.serialized_info  s    M
 	
r   c                    t         d   ddS )Nrx   TrC   rD   r   s    r   r   zVideoSerializable.api_info      ();<QUVVr   c                .    dt         j                  dddS )NFr   zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/video_sample.mp4rH   )r   BASE64_VIDEOr   s    r   r   z VideoSerializable.example_inputs  s    $j.E.EFj
 	
r   c                *    t         |   |||      d fS rz   )r}   r-   )r   r*   r+   r,   r   s       r   r-   zVideoSerializable.serialize  s     !!X{;TBBr   c                    t        |t        t        f      r't        |      dk7  rt	        d|       |d   |d   g}nt	        d|       t
        |   ||||      }t        |t              r|d   S y)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}   r3   )r   r*   r0   r1   r2   	x_as_listdeserialized_filer   s          r   r3   zVideoSerializable.deserialize  s     a%'1v{ #I!!MNN1qtIEaSIJJ!G/	8XxX'.$Q'' /r   r   r5   r6   )r*   r<   r+   r8   r,   r9   r4   ztuple[FileData | None, None]r:   )
r*   z.tuple[FileData | None, FileData | None] | Noner0   r   r1   r<   r2   r<   r4   z*str | tuple[str | None, str | None] | None)	r=   r>   r?   r   r   r   r-   r3   r   r   s   @r   r   r     s    
W
 MRCC'1CEIC	%C '+##(9( $( 	(
 ( 
4( (r   r   c                  `    e Zd Zd ZddZddZ	 	 d		 	 	 	 	 	 	 d
dZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZy)JSONSerializablec                    dddS )Nre   zfilepath to JSON filerf   r(   r   s    r   r   z JSONSerializable.serialized_info  s     1HIIr   c                    t         d   ddS )Nr   TrC   rD   r   s    r   r   zJSONSerializable.api_info  r   r   c                    dddd dS )Nr   r   )abrH   r(   r   s    r   r   zJSONSerializable.example_inputs  s    #
 	
r   c                T    ||dk(  ry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
        Nr7   )r   file_to_jsonr   r)   s       r   r-   zJSONSerializable.serialize  s,     9R!!$x.1"455r   Nc                J    |y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
        Nrr   )r   dict_or_str_to_json_fileru   r/   s        r   r3   zJSONSerializable.deserialize  s%      9--aX>CCCr   r   r5   r6   )r*   r<   r+   r8   r,   r9   r4   zdict | list | Noner:   )
r*   zstr | dict | listr0   r;   r1   r<   r2   r<   r4   r<   r=   r>   r?   r   r   r   r-   r3   r(   r   r   r   r     s    JW
  "!	66 6 	6
 
6( '+##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y)GallerySerializablec                    dddS )Nre   z^path to directory with images and a file associating images with captions called captions.jsonrf   r(   r   s    r   r   z#GallerySerializable.serialized_info  s    {
 	
r   c                    t         d   ddS )Nr   TrC   rD   r   s    r   r   zGallerySerializable.api_info  s    $%:;#
 	
r   c                8    t         j                  gdz  dgdz  dS )Nr   rj   rH   rk   r   s    r   r   z"GallerySerializable.example_inputs  s/    ++,q0b 
 	
r   c                8   ||dk(  ry g }t        |      dz  }|j                  d      5 }t        j                  |      }d d d        j	                         D ]3  \  }}	t               j                  ||      }
|j                  |
|	g       5 |S # 1 sw Y   QxY w)Nr7   captions.jsonr)r,   )r   openjsonloadr   rx   r-   append)r   r*   r+   r,   filescaptions_filecaptions_jsoncaptionsr   captionimgs              r   r-   zGallerySerializable.serialize  s     9RQ/1$ 	0yy/H	0"*.."2 	)Iw"$..yk.RCLL#w(	) 	0 	0s   BBNc                   |y t        |      t        t        j                               z  }|j	                  dd       i }|D ]w  }t        |t        t        f      r|\  }}nd }t               j                  ||||      }	|||	<   |dz  }
|
j                  d      5 }t        j                  ||       d d d        y t        j                  j                  |      S # 1 sw Y   xY w)NT)r   r   )r1   r2   r   w)r   r   uuiduuid4r   r   r   r   rx   r3   r   r   dumposr   abspath)r   r*   r0   r1   r2   gallery_pathr   img_datar   ru   r   r   s               r   r3   zGallerySerializable.deserialize  s     9H~DJJL(99D$7 	3H(T5M2$,!'#%11,H 2 D %HTN(?:M##C( 3M		(M23 3	3 ww|,,3 3s   CC&	r   r5   r6   )r*   r<   r+   r8   r,   r9   r4   list[list[str | None]] | None)r7   NN)
r*   r   r0   r   r1   r<   r2   r<   r4   z
None | strr   r(   r   r   r   r     s}    


 MR'1EI	&" ##-(- - 	-
 - 
-r   r   FileUploadButtontextboxnumberslidercheckboxcheckboxgroupradiodropdownimagevideoaudiorv   	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))rN   
__future__r   r   r   r   r   r   pathlibr   typingr   gradio_clientr   r   gradio_client.data_classesr   r   __file__parentr   r   rE   r
   rA   rP   rU   rZ   r^   rc   rx   r   r   r   SERIALIZER_MAPPING__subclasses__clsr=   subclsr  __annotations__r(   r   r   <module>r+     s   @ "  	      + /	$x.

,
./ $1 tyy|$2 2j
 
 
 
 
\ 
 
, 
 
 
 8l 8v{| {|((( ((V0D| 0Df<-, <-~  &&( 5C'*s||$$$& 5.46??+55
 &8 > "- 6 %5 > "!&!!& !&  !& #	!&
 +!& !& "!& _!& !& !& !& !!& "!& $!& !&   !!&" $#!&$ &'" #"&A!& ? !S$ $s   FF