o
    eP                     @  s8  U 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 sNw   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d/ed0eeeeee eeeeeeeeed1Z&d2e'd3< dS )4    )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   ID:\Projects\ConvertPro\env\Lib\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_info/   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   	serialize4   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   J      zSimpleSerializable.api_infor   c                 C  s
   d d dS )NrawZ
serializedr   r   r   r   r   r   P      z!SimpleSerializable.example_inputsNr   r>   r6   r9   r:   r;   __doc__r   r   r   r   r   r   r=   G       
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   Z   rC   zStringSerializable.api_infor   c                 C  
   dddS )NHowdy!rD   r   r   r   r   r   r   `   rF   z!StringSerializable.example_inputsNrG   r6   rH   r   r   r   r   rK   W   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   j   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   p   s   z%ListStringSerializable.example_inputsNrG   r6   rH   r   r   r   r   rN   g   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   z   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   w   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 or URL to imager   descriptionr   r   r   r   r   r         
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   rg      s   zFileSerializable.__init__c                 C  r	   rc   )_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 or URL to filerU   r   r   r   r   r   rj      rW   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   rm   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	   rc   )rl   r   r   r   r   r      rk   zFileSerializable.api_infor   c                 C  r	   rc   )_single_file_example_inputsr   r   r   r   r      rk   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   rs         
z,FileSerializable._single_file_example_inputsc                 C  s   dt jdgdgdS ru   ry   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  sl   |d u s	t |tr|S t|r|}d }ntt|| }t| j}||r)d nt|t|j	||dS )N)r_   rx   	orig_namerw   size)

isinstancedictr   r[   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 rc   )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s/J d| |d urAtj|d | ||d}|S tj||d}|S |dr|d rW|rW|sYJ | j	rc| j
|d krv| j|d |d  |d	| _	|d | _
t| j	}t|pt td
 }|jddd ||dd }|| t|}|S |d}	|	d usJ d| tj|	|dj}|S tdt| d| )Nr\   rw   r_   zThe 'name' field is missing in zfile=)r3   r]   	is_streamzstream/)r3      T)parentsexist_okr~   outputrx   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    Zdownload_tmp_copy_of_fileZcreate_tmp_copy_of_filerd   re   r   nextr   tempfile
gettempdirsecretsZ	token_hexmkdirwrite_bytes
ValueErrorr   )
r   r'   r/   r1   r3   	file_namefilepathchunkpathrx   r   r   r   _deserialize_single  sN   
"







	
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>^  s    z.FileSerializable.serialize.<locals>.<listcomp>)r   listr   r-   r   r   r   r.   M  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   w  s    z0FileSerializable.deserialize.<locals>.<listcomp>r   )r   r   r   r   r   r4   r   r   r   r5   b  s   

zFileSerializable.deserialize)r   rb   r   rr   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   rg   r   rl   rj   rp   rq   r   r   rs   r{   r   r   r   r.   r5   __classcell__r   r   rh   r   ra      s6    



1ra   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   zfilepath or URL to video filerU   r   r   r   r   r   r     rW   z!VideoSerializable.serialized_infor   rr   c                 C  r?   )Nra   Tr@   rA   r   r   r   r   r     rX   zVideoSerializable.api_infor   c                 C  rt   )NFrv   zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/video_sample.mp4rD   )r   ZBASE64_VIDEOr   r   r   r   r     rz   z VideoSerializable.example_inputsr&   Fr'   r2   r(   r)   r*   r+   tuple[FileData | None, None]c                   s   t  |||d fS rc   )rf   r.   r-   rh   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sJ 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   rf   r5   )r   r'   r/   r1   r3   Z	x_as_listZdeserialized_filerh   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   rh   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     rW   z JSONSerializable.serialized_infor   rr   c                 C  r?   )Nr   Tr@   rA   r   r   r   r   r     rX   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   rr   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   rY   rD   rZ   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loadro   ra   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   ra   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
timeseriesstateZbuttonZuploadbuttonZcolorpicker)labelZhighlightedtextr   htmlZgalleryZchatbotZmodel3dZplotZbarplotZlineplotZscatterplotmarkdowncodeZannotatedimagezdict[str, type]COMPONENT_MAPPING)(
__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   ra   r   r   r   ZSERIALIZER_MAPPING__subclasses__clsr9   subclsr   __annotations__r   r   r   r   <module>   s    58 5'3?
	
