o
    An0j5R                     @  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   `/home/wildlama/miniconda3/envs/lam_a2e/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 )NZserialized_inputinfotype   r      r   getr   r   typesr   r   r   input_api_info+      zSerializable.input_api_infoc                 C  r   )NZ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'   r/   r1   r3   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   r/   r0   r1   r2   r3   r2   )
__name__
__module____qualname__r   r   r   r#   r%   r.   r5   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rawZ
serializedr   r   r   r   r   r   R      z!SimpleSerializable.example_inputsNr   r>   r6   r9   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  r?   )NrK   Fr@   rA   r   r   r   r   r   \   rC   zStringSerializable.api_infor   c                 C  
   dddS )NHowdy!rD   r   r   r   r   r   r   b   rF   z!StringSerializable.example_inputsNrG   r6   rH   r   r   r   r   rK   Y   rJ   rK   c                   @  r<   )ListStringSerializablezHExpects a list of strings as input/output but performs no serialization.r   r>   c                 C  r?   )NrN   Fr@   rA   r   r   r   r   r   l   rC   zListStringSerializable.api_infor   c                 C  s   ddgddgdS )NrM   ZMerhabarD   r   r   r   r   r   r   r   s   z%ListStringSerializable.example_inputsNrG   r6   rH   r   r   r   r   rN   i   rJ   rN   c                   @  r<   )BooleanSerializablez@Expects a boolean as input/output but performs no serialization.r   r>   c                 C  r?   )NrO   Fr@   rA   r   r   r   r   r   |   rC   zBooleanSerializable.api_infor   c                 C  rL   )NTrD   r   r   r   r   r   r      rF   z"BooleanSerializable.example_inputsNrG   r6   rH   r   r   r   r   rO   y   rJ   rO   c                   @  r<   )NumberSerializablezKExpects a number (int/float) as input/output but performs no serialization.r   r>   c                 C  r?   )NrP   Fr@   rA   r   r   r   r   r      rC   zNumberSerializable.api_infor   c                 C  rL   )N   rD   r   r   r   r   r   r      rF   z!NumberSerializable.example_inputsNrG   r6   rH   r   r   r   r   rP      rJ   rP   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      rF   zImgSerializable.serialized_infor   r>   c                 C  r?   )NrR   Tr@   rA   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.pngrD   r   ZBASE64_IMAGEr   r   r   r   r      s   zImgSerializable.example_inputsr&   Fr'   r2   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_likeZencode_url_to_base64Zencode_file_to_base64r   r-   r   r   r   r.      s
   

zImgSerializable.serializeNr/   r0   r1   r3   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'   r/   r1   r3   filer   r   r   r5      s   zImgSerializable.deserializerG   r6   r7   )r'   r2   r(   r)   r*   r+   r   r2   r8   )
r'   r2   r/   r0   r1   r2   r3   r2   r   r2   )	r9   r:   r;   rI   r   r   r   r.   r5   r   r   r   r   rR      s    

	rR   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   rf      s   zFileSerializable.__init__c                 C  r	   rb   )_single_file_serialized_infor   r   r   r   r         z FileSerializable.serialized_infoc                 C  r?   )NZSingleFileSerializableTr@   rA   r   r   r   r   _single_file_api_info   rC   z&FileSerializable._single_file_api_infoc                 C  rS   )NrT   *filepath on your computer (or URL) of filerU   r   r   r   r   r   ri      rF   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 filesrT   rl   rU   )r   rV   itemsr   r   r   r   r   _multiple_file_serialized_info   s   z/FileSerializable._multiple_file_serialized_infoc                 C  r?   )NZMultipleFileSerializableTr@   rA   r   r   r   r   _multiple_file_api_info   rC   z(FileSerializable._multiple_file_api_infodict[str, dict | bool]c                 C  r	   rb   )rk   r   r   r   r   r      rj   zFileSerializable.api_infor   c                 C  r	   rb   )_single_file_example_inputsr   r   r   r   r      rj   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.pdfrD   r   ZBASE64_FILEr   r   r   r   rr        
z,FileSerializable._single_file_example_inputsc                 C  s   dt jdgdgdS rt   rx   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)r^   rw   	orig_namesize)

isinstancedictr   rZ   strr   statst_sizeZencode_url_or_file_to_base64r^   )r   r'   r(   r*   filenamer~   r   r   r   _serialize_single  s   
z"FileSerializable._serialize_singlec                 C  s   t ||S rb   )r   Zdownload_byte_stream)r   urlr3   r   r   r   _setup_stream&  s   zFileSerializable._setup_streamNr/   r2   r1   r3   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| )Nr[   rv   r^   zThe 'name' field is missing in zfile=)r3   r\   	is_streamz2name and root_url and save_dir must all be presentzstream/)r3      T)parentsexist_okr}   outputrw   zThe 'data' field is missing in zLA FileSerializable component can only deserialize a string or a dict, not a z: )r   r   r   r]   r^   r   r    
ValueErrorZdownload_tmp_copy_of_fileZcreate_tmp_copy_of_filerc   rd   r   nextr   tempfile
gettempdirsecretsZ	token_hexmkdirwrite_bytesr   )
r   r'   r/   r1   r3   	file_namefilepathchunkpathrw   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   r.   \  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 )r/   r1   r3   )r   r   r3   r1   r/   r   r   r   r     s    z0FileSerializable.deserialize.<locals>.<listcomp>r   )r   r   r   r   r   r4   r   r   r   r5   q  s   

zFileSerializable.deserialize)r   ra   r   rq   r6   r7   )r'   r{   r(   r)   r*   r+   r   r|   r8   )
r'   r{   r/   r2   r1   r2   r3   r2   r   r2   )r'   r   r(   r)   r*   r+   r   r   )
r'   r   r/   r   r1   r2   r3   r2   r   r   )r9   r:   r;   rI   rf   r   rk   ri   ro   rp   r   r   rr   rz   r   r   r   r.   r5   __classcell__r   r   rg   r   r`      s6    




6r`   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  rS   )NrT   z0filepath on your computer (or URL) of video filerU   r   r   r   r   r   r     rF   z!VideoSerializable.serialized_infor   rq   c                 C  r?   )Nr`   Tr@   rA   r   r   r   r   r     rW   zVideoSerializable.api_infor   c                 C  rs   )NFru   zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/video_sample.mp4rD   )r   ZBASE64_VIDEOr   r   r   r   r     ry   z VideoSerializable.example_inputsr&   Fr'   r2   r(   r)   r*   r+   tuple[FileData | None, None]c                   s   t  |||d fS rb   )re   r.   r-   rg   r   r   r.     s   zVideoSerializable.serializeN.tuple[FileData | None, FileData | None] | Noner/   r   r1   r3   *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   re   r5   )r   r'   r/   r1   r3   Z	x_as_listZdeserialized_filerg   r   r   r5     s   
zVideoSerializable.deserializer   r6   r7   )r'   r2   r(   r)   r*   r+   r   r   r8   )
r'   r   r/   r   r1   r2   r3   r2   r   r   )	r9   r:   r;   r   r   r   r.   r5   r   r   r   rg   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  rS   )NrT   zfilepath to JSON filerU   r   r   r   r   r   r     s   
z JSONSerializable.serialized_infor   rq   c                 C  r?   )Nr   Tr@   rA   r   r   r   r   r     rW   zJSONSerializable.api_infor   c                 C  s   dddd dS )Nr   r   )abrD   r   r   r   r   r   r     s   zJSONSerializable.example_inputsr&   Fr'   r2   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   Zfile_to_jsonr   r-   r   r   r   r.     s   zJSONSerializable.serializeNstr | dict | listr/   r0   r1   r3   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
        Nr[   )r   Zdict_or_str_to_json_filer^   r4   r   r   r   r5     s   zJSONSerializable.deserializer   r6   r7   )r'   r2   r(   r)   r*   r+   r   r   r8   )
r'   r   r/   r0   r1   r2   r3   r2   r   r2   r9   r:   r;   r   r   r   r.   r5   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  rS   )NrT   z^path to directory with images and a file associating images with captions called captions.jsonrU   r   r   r   r   r   r     rF   z#GallerySerializable.serialized_infor   rq   c                 C  r?   )Nr   Tr@   rA   r   r   r   r   r     rC   zGallerySerializable.api_infor   c                 C  s   t jgd dgd dS )Nr   rX   rD   rY   r   r   r   r   r     s   
z"GallerySerializable.example_inputsr&   Fr'   r2   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loadrn   r`   r.   append)r   r'   r(   r*   filescaptions_filecaptions_jsoncaptionsr   captionZimgr   r   r   r.     s   zGallerySerializable.serializeNr/   r   r1   r3   
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   )r1   r3   r   w)r   r   uuiduuid4r   r   r   r   r`   r5   r   r   dumposr   abspath)r   r'   r/   r1   r3   Zgallery_pathr   Zimg_datar   r^   r   r   r   r   r   r5     s&   
zGallerySerializable.deserializer   r6   r7   )r'   r2   r(   r)   r*   r+   r   r   )r&   NN)
r'   r   r/   r   r1   r2   r3   r2   r   r   r   r   r   r   r   r     s    


r   FileZUploadButtonZtextboxnumberZsliderZcheckboxZcheckboxgroupradioZdropdownimageZvideoZaudior_   Z	dataframeZ
timeseriesZfileexplorerstateZbuttonZuploadbutton)ZcolorpickerlabelZhighlightedtextr   htmlZgalleryZchatbotZmodel3dZplotZbarplotZlineplotZscatterplotmarkdowncodeZannotatedimagezdict[str, type]COMPONENT_MAPPING))rI   
__future__r   r   r   r   r   r   pathlibr   typingr   Zgradio_clientr   r   Zgradio_client.data_classesr   r   __file__parentr   r   rB   r   r=   rK   rN   rO   rP   rR   r`   r   r   r   ZSERIALIZER_MAPPING__subclasses__clsr9   subclsr   __annotations__r   r   r   r   <module>   s    5; ?+3?
	
