o
    ٷi5R                     @  s>  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eeZW d   n1 sPw   Y  G d	d
 d
ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!i Z"e# D ]Z$e$e"e$j%< e$# D ]Z&e&e"e&j%< qqe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                   @  sZ   e Zd Zdd Zd!ddZd"dd	Zd#ddZd#ddZd$d%ddZ			d&d'dd Z	dS )(Serializablec                 C     |   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self r   M/home/ubuntu/.local/lib/python3.10/site-packages/gradio_client/serializing.pyserialized_info   s   zSerializable.serialized_inforeturndict[str, list[str]]c                 C     t  r
   NotImplementedErrorr   r   r   r   r         zSerializable.api_infodict[str, Any]c                 C  r   )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   r   r   r   example_inputs#   r   zSerializable.example_inputstuple[str, str]c                 C  2   |   }|d|d d gd }|d |d fS )Nserialized_inputinfotype   r      r   getr   r   typesr   r   r   input_api_info+      zSerializable.input_api_infoc                 C  r   )Nserialized_outputr   r   r   r   r   r    r"   r   r   r   output_api_info1   r%   zSerializable.output_api_info Fxr   load_dir
str | Pathallow_linksboolc                 C     |S )z]
        Convert data from human-readable format to serialized format for a browser.
        r   r   r)   r*   r,   r   r   r   	serialize6   s   zSerializable.serializeNsave_dirstr | Path | Noneroot_url
str | Nonehf_tokenc                 C  r.   )z]
        Convert data from serialized format for a browser to human-readable format.
        r   r   r)   r1   r3   r5   r   r   r   deserialize<   s   
zSerializable.deserialize)r   r   r   r   )r   r   r(   F)r)   r   r*   r+   r,   r-   NNN)r)   r   r1   r2   r3   r4   r5   r4   )
__name__
__module____qualname__r   r   r   r$   r'   r0   r7   r   r   r   r   r      s    



	r   c                   @  $   e Zd ZdZd
ddZdddZd	S )SimpleSerializablezIGeneral class that does not perform any serialization or deserialization.r   dict[str, bool | dict]c                 C     t d ddS )Nr?   Fr   r   serializer_typesr   r   r   r   r   L      zSimpleSerializable.api_infor   c                 C  s
   d d dS )Nraw
serializedr   r   r   r   r   r   R      z!SimpleSerializable.example_inputsNr   r@   r8   r;   r<   r=   __doc__r   r   r   r   r   r   r?   I       
r?   c                   @  r>   )StringSerializablez?Expects a string as input/output but performs no serialization.r   r@   c                 C  rA   )NrN   FrB   rC   r   r   r   r   r   \   rE   zStringSerializable.api_infor   c                 C  
   dddS )NHowdy!rF   r   r   r   r   r   r   b   rI   z!StringSerializable.example_inputsNrJ   r8   rK   r   r   r   r   rN   Y   rM   rN   c                   @  r>   )ListStringSerializablezHExpects a list of strings as input/output but performs no serialization.r   r@   c                 C  rA   )NrQ   FrB   rC   r   r   r   r   r   l   rE   zListStringSerializable.api_infor   c                 C  s   ddgddgdS )NrP   MerhabarF   r   r   r   r   r   r   r   s   z%ListStringSerializable.example_inputsNrJ   r8   rK   r   r   r   r   rQ   i   rM   rQ   c                   @  r>   )BooleanSerializablez@Expects a boolean as input/output but performs no serialization.r   r@   c                 C  rA   )NrS   FrB   rC   r   r   r   r   r   |   rE   zBooleanSerializable.api_infor   c                 C  rO   )NTrF   r   r   r   r   r   r      rI   z"BooleanSerializable.example_inputsNrJ   r8   rK   r   r   r   r   rS   y   rM   rS   c                   @  r>   )NumberSerializablezKExpects a number (int/float) as input/output but performs no serialization.r   r@   c                 C  rA   )NrT   FrB   rC   r   r   r   r   r      rE   zNumberSerializable.api_infor   c                 C  rO   )N   rF   r   r   r   r   r   r      rI   z!NumberSerializable.example_inputsNrJ   r8   rK   r   r   r   r   rT      rM   rT   c                   @  sN   e Zd ZdZdd ZdddZdd	d
Z		ddddZ			d d!ddZdS )"ImgSerializablezJExpects a base64 string as input/output which is serialized to a filepath.c                 C  
   dddS )Nstringz+filepath on your computer (or URL) of imager   descriptionr   r   r   r   r   r      rI   zImgSerializable.serialized_infor   r@   c                 C  rA   )NrV   TrB   rC   r   r   r   r   r         zImgSerializable.api_infor   c                 C  s   t jddS )NPhttps://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.pngrF   r   BASE64_IMAGEr   r   r   r   r      s   zImgSerializable.example_inputsr(   Fr)   r4   r*   r+   r,   r-   c                 C  s.   |sdS t |rt |S t t|| 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/   r   r   r   r0      s
   

zImgSerializable.serializeNr1   r2   r3   r5   c                 C  s(   |du s|dkr
dS t j||d}|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)   r1   r3   r5   filer   r   r   r7      s   zImgSerializable.deserializerJ   r8   r9   )r)   r4   r*   r+   r,   r-   r   r4   r:   )
r)   r4   r1   r2   r3   r4   r5   r4   r   r4   )	r;   r<   r=   rL   r   r   r   r0   r7   r   r   r   r   rV      s    

	rV   c                      s   e Zd ZdZd6 fddZdd Zdd	 Zd
d Zdd Zdd Z	d7ddZ
d8ddZd8ddZd8ddZ		d9d:d#d$Zd%d& Z	'	'	'd;d<d,d-Z		d9d=d0d1Z	'	'	'd;d>d4d5Z  ZS )?FileSerializablezfExpects a dict with base64 representation of object as input/output which is serialized to a filepath.r   Nonec                   s   d | _ d | _t   d S N)streamstream_namesuper__init__r   	__class__r   r   rm      s   zFileSerializable.__init__c                 C  r	   ri   )_single_file_serialized_infor   r   r   r   r         z FileSerializable.serialized_infoc                 C  rA   )NSingleFileSerializableTrB   rC   r   r   r   r   _single_file_api_info   rE   z&FileSerializable._single_file_api_infoc                 C  rW   )NrX   *filepath on your computer (or URL) of filerY   r   r   r   r   r   rp      rI   z-FileSerializable._single_file_serialized_infoc                 C  s   ddddddS )Narrayz&List of filepath(s) or URL(s) to filesrX   rt   rY   )r   rZ   itemsr   r   r   r   r   _multiple_file_serialized_info   s   z/FileSerializable._multiple_file_serialized_infoc                 C  rA   )NMultipleFileSerializableTrB   rC   r   r   r   r   _multiple_file_api_info   rE   z(FileSerializable._multiple_file_api_infodict[str, dict | bool]c                 C  r	   ri   )rs   r   r   r   r   r      rq   zFileSerializable.api_infor   c                 C  r	   ri   )_single_file_example_inputsr   r   r   r   r      rq   zFileSerializable.example_inputsc                 C     dt jdddS NFis_filedatazMhttps://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdfrF   r   BASE64_FILEr   r   r   r   r{        
z,FileSerializable._single_file_example_inputsc                 C  s   dt jdgdgdS r}   r   r   r   r   r   _multiple_file_example_inputs  s   z.FileSerializable._multiple_file_example_inputsr(   Fr)   str | FileData | Noner*   r+   r,   r-   FileData | Nonec                 C  sn   |d u s	t |tr|S t|r|}d }ntt|| }t| j}|p'd |r+d nt|t|j	|dS )N)re   r   	orig_namesize)

isinstancedictr   r_   strr   statst_sizeencode_url_or_file_to_base64re   )r   r)   r*   r,   filenamer   r   r   r   _serialize_single  s   
z"FileSerializable._serialize_singlec                 C  s   t ||S ri   )r   download_byte_stream)r   urlr5   r   r   r   _setup_stream&  s   zFileSerializable._setup_streamNr1   r4   r3   r5   c           
      C  s  |d u rd S t |trtj||dj}|S t |tr|drJ|d}|d u r/td| |d urAtj|d | ||d}|S tj	||d}|S |dr|d rW|rW|s[td| j
re| j|d krx| j|d	 |d  |d
| _
|d | _t| j
}t|pt td }|jddd ||dd }|| t|}|S |d}	|	d u rtd| tj|	|dj}|S tdt| d| )Nrb   r   re   zThe 'name' field is missing in zfile=)r5   rc   	is_streamz2name and root_url and save_dir must all be presentzstream/)r5      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   rd   re   r   r!   
ValueErrordownload_tmp_copy_of_filecreate_tmp_copy_of_filerj   rk   r   nextr   tempfile
gettempdirsecrets	token_hexmkdirwrite_bytesr   )
r   r)   r1   r3   r5   	file_namefilepathchunkpathr   r   r   r   _deserialize_single)  sX   
'



 





z$FileSerializable._deserialize_single3str | FileData | None | list[str | FileData | None]'FileData | None | list[FileData | None]c                   sB   |du s|dkr
dS t |tr fd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                   s   g | ]	} | qS r   )r   .0fr,   r*   r   r   r   
<listcomp>m  s    z.FileSerializable.serialize.<locals>.<listcomp>)r   listr   r/   r   r   r   r0   \  s
   
zFileSerializable.serializePath | str | Nonestr | None | list[str | None]c                   sR   |du rdS t trtt |tr  fdd|D S j| d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                   s   g | ]}j | d qS )r1   r3   r5   )r   r   r5   r3   r1   r   r   r   r     s    z0FileSerializable.deserialize.<locals>.<listcomp>r   )r   r   r   r   r   r6   r   r   r   r7   q  s   

zFileSerializable.deserialize)r   rh   r   rz   r8   r9   )r)   r   r*   r+   r,   r-   r   r   r:   )
r)   r   r1   r4   r3   r4   r5   r4   r   r4   )r)   r   r*   r+   r,   r-   r   r   )
r)   r   r1   r   r3   r4   r5   r4   r   r   )r;   r<   r=   rL   rm   r   rs   rp   rw   ry   r   r   r{   r   r   r   r   r0   r7   __classcell__r   r   rn   r   rg      s6    




6rg   c                      sT   e Zd Zdd ZdddZddd	Z	d d! fddZ			d"d# fddZ  ZS )$VideoSerializablec                 C  rW   )NrX   z0filepath on your computer (or URL) of video filerY   r   r   r   r   r   r     rI   z!VideoSerializable.serialized_infor   rz   c                 C  rA   )Nrg   TrB   rC   r   r   r   r   r     r[   zVideoSerializable.api_infor   c                 C  r|   )NFr~   zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/video_sample.mp4rF   )r   BASE64_VIDEOr   r   r   r   r     r   z VideoSerializable.example_inputsr(   Fr)   r4   r*   r+   r,   r-   tuple[FileData | None, None]c                   s   t  |||d fS ri   )rl   r0   r/   rn   r   r   r0     s   zVideoSerializable.serializeN.tuple[FileData | None, FileData | None] | Noner1   r   r3   r5   *str | tuple[str | None, str | None] | Nonec                   sp   t |ttfrt|dkrtd| |d |d g}ntd| t ||||}t |tr6|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   rl   r7   )r   r)   r1   r3   r5   	x_as_listdeserialized_filern   r   r   r7     s   
zVideoSerializable.deserializer   r8   r9   )r)   r4   r*   r+   r,   r-   r   r   r:   )
r)   r   r1   r   r3   r4   r5   r4   r   r   )	r;   r<   r=   r   r   r   r0   r7   r   r   r   rn   r   r     s    

r   c                   @  sJ   e Zd Zdd ZdddZddd	Z	
	dd ddZ			d!d"ddZdS )#JSONSerializablec                 C  rW   )NrX   zfilepath to JSON filerY   r   r   r   r   r   r     s   
z JSONSerializable.serialized_infor   rz   c                 C  rA   )Nr   TrB   rC   r   r   r   r   r     r[   zJSONSerializable.api_infor   c                 C  s   dddd dS )Nr   r   )abrF   r   r   r   r   r   r     s   zJSONSerializable.example_inputsr(   Fr)   r4   r*   r+   r,   r-   dict | list | Nonec                 C  s&   |du s|dkr
dS t t|| 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   r/   r   r   r   r0     s   zJSONSerializable.serializeNstr | dict | listr1   r2   r3   r5   c                 C  s   |du rdS t j||d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
        Nrb   )r   dict_or_str_to_json_filere   r6   r   r   r   r7     s   zJSONSerializable.deserializer   r8   r9   )r)   r4   r*   r+   r,   r-   r   r   r:   )
r)   r   r1   r2   r3   r4   r5   r4   r   r4   r;   r<   r=   r   r   r   r0   r7   r   r   r   r   r     s    

	r   c                   @  sH   e Zd Zdd ZdddZddd	Z	dd ddZ	
		d!d"ddZdS )#GallerySerializablec                 C  rW   )NrX   z^path to directory with images and a file associating images with captions called captions.jsonrY   r   r   r   r   r   r     rI   z#GallerySerializable.serialized_infor   rz   c                 C  rA   )Nr   TrB   rC   r   r   r   r   r     rE   zGallerySerializable.api_infor   c                 C  s   t jgd dgd dS )Nr   r\   rF   r]   r   r   r   r   r     s   
z"GallerySerializable.example_inputsr(   Fr)   r4   r*   r+   r,   r-   list[list[str | None]] | Nonec                 C  s   |d u s|dkr
d S g }t |d }|d}t|}W d    n1 s'w   Y  | D ]\}}	t j||d}
||
|	g q0|S )Nr(   captions.jsonr)r,   )r   openjsonloadrv   rg   r0   append)r   r)   r*   r,   filescaptions_filecaptions_jsoncaptionsr   captionimgr   r   r   r0     s   zGallerySerializable.serializeNr1   r   r3   r5   
None | strc              	   C  s   |d u rd S t |tt  }|jddd i }|D ]=}t|ttfr)|\}}nd }t j	||||d}	|||	< |d }
|

d}t|| W d    n1 sSw   Y  qtj|S )NT)r   r   )r3   r5   r   w)r   r   uuiduuid4r   r   r   r   rg   r7   r   r   dumposr   abspath)r   r)   r1   r3   r5   gallery_pathr   img_datar   re   r   r   r   r   r   r7     s&   
zGallerySerializable.deserializer   r8   r9   )r)   r4   r*   r+   r,   r-   r   r   )r(   NN)
r)   r   r1   r   r3   r4   r5   r4   r   r   r   r   r   r   r   r     s    


r   FileUploadButtontextboxnumberslidercheckboxcheckboxgroupradiodropdownimagevideoaudiorf   	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))rL   
__future__r   r   r   r   r   r   pathlibr   typingr   gradio_clientr   r   gradio_client.data_classesr   r   __file__parentr   r   rD   r   r?   rN   rQ   rS   rT   rV   rg   r   r   r   SERIALIZER_MAPPING__subclasses__clsr;   subclsr   __annotations__r   r   r   r   <module>   s    5; ?+3?
	
