a
    dP                      @  s  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 s0    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$< qvq`ee!d	< ee!d< ee!d< eeeeeeeeeeeeeeeeeeeeee eeeeeeeeed Z&d!e'd"< dS )#    )annotationsN)Path)Any)
media_datautils)FileDataz
types.jsonc                   @  sv   e Zd Zdd ZddddZdddd	Zd
dddZd
dddZdddddddZddddddddZ	dS )Serializablec                 C  s   |   S )
        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   b/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio_client/serializing.pyserialized_info   s    zSerializable.serialized_infozdict[str, list[str]]returnc                 C  s
   t  dS )r	   NNotImplementedErrorr   r   r   r   r
      s    zSerializable.api_infodict[str, Any]c                 C  s
   t  dS )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
        Nr   r   r   r   r   example_inputs!   s    zSerializable.example_inputsztuple[str, str]c                 C  s2   |   }|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)   s    zSerializable.input_api_infoc                 C  s2   |   }|d|d d gd }|d |d fS )NZserialized_outputr   r   r   r   r   r   r   r   r   r   output_api_info/   s    zSerializable.output_api_info Fr   
str | Pathbool)xload_dirallow_linksc                 C  s   |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str | Path | None
str | None)r#   save_dirroot_urlhf_tokenc                 C  s   |S )z]
        Convert data from serialized format for a browser to human-readable format.
        r   r   r#   r*   r+   r,   r   r   r   deserialize:   s    
zSerializable.deserialize)r    F)NNN)
__name__
__module____qualname__r   r
   r   r   r   r'   r.   r   r   r   r   r      s   	   r   c                   @  s,   e Zd ZdZddddZddddZd	S )
SimpleSerializablezIGeneral class that does not perform any serialization or deserialization.dict[str, bool | dict]r   c                 C  s   t d ddS )Nr2   Fr   r   serializer_typesr   r   r   r   r
   J   s    zSimpleSerializable.api_infor   c                 C  s
   d d dS )NrawZ
serializedr   r   r   r   r   r   P   s    z!SimpleSerializable.example_inputsNr/   r0   r1   __doc__r
   r   r   r   r   r   r2   G   s   r2   c                   @  s,   e Zd ZdZddddZddddZd	S )
StringSerializablez?Expects a string as input/output but performs no serialization.r3   r   c                 C  s   t d ddS )Nr;   Fr4   r5   r   r   r   r   r
   Z   s    zStringSerializable.api_infor   c                 C  s
   dddS )NHowdy!r7   r   r   r   r   r   r   `   s    z!StringSerializable.example_inputsNr9   r   r   r   r   r;   W   s   r;   c                   @  s,   e Zd ZdZddddZddddZd	S )
ListStringSerializablezHExpects a list of strings as input/output but performs no serialization.r3   r   c                 C  s   t d ddS )Nr=   Fr4   r5   r   r   r   r   r
   j   s    zListStringSerializable.api_infor   c                 C  s   ddgddgdS )Nr<   ZMerhabar7   r   r   r   r   r   r   p   s    z%ListStringSerializable.example_inputsNr9   r   r   r   r   r=   g   s   r=   c                   @  s,   e Zd ZdZddddZddddZd	S )
BooleanSerializablez@Expects a boolean as input/output but performs no serialization.r3   r   c                 C  s   t d ddS )Nr>   Fr4   r5   r   r   r   r   r
   z   s    zBooleanSerializable.api_infor   c                 C  s
   dddS )NTr7   r   r   r   r   r   r      s    z"BooleanSerializable.example_inputsNr9   r   r   r   r   r>   w   s   r>   c                   @  s,   e Zd ZdZddddZddddZd	S )
NumberSerializablezKExpects a number (int/float) as input/output but performs no serialization.r3   r   c                 C  s   t d ddS )Nr?   Fr4   r5   r   r   r   r   r
      s    zNumberSerializable.api_infor   c                 C  s
   dddS )N   r7   r   r   r   r   r   r      s    z!NumberSerializable.example_inputsNr9   r   r   r   r   r?      s   r?   c                   @  sb   e Zd ZdZdd ZddddZddd	d
ZddddddddZdddddddddZdS )ImgSerializablezJExpects a base64 string as input/output which is serialized to a filepath.c                 C  s
   dddS )Nstringzfilepath or URL to imager   descriptionr   r   r   r   r   r      s    zImgSerializable.serialized_infor3   r   c                 C  s   t d ddS )NrA   Tr4   r5   r   r   r   r   r
      s    zImgSerializable.api_infor   c                 C  s   t jddS )NPhttps://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.pngr7   r   ZBASE64_IMAGEr   r   r   r   r      s    zImgSerializable.example_inputsr    Fr)   r!   r"   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(   r#   r*   r+   r,   r   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*   r+   r,   filer   r   r   r.      s    zImgSerializable.deserialize)r    F)NNN)	r/   r0   r1   r:   r   r
   r   r'   r.   r   r   r   r   rA      s   	     rA   c                      s   e Zd ZdZdd fddZdd Zdd	 Zd
d Zdd Zdd Z	ddddZ
ddddZddddZddddZd2ddddd d!d"Zd#d$ Zd3dd&d&d&d&d'd(d)Zd4d*ddd+d d,d-Zd5d*d.d&d&d/d'd0d1Z  ZS )6FileSerializablezfExpects a dict with base64 representation of object as input/output which is serialized to a filepath.Noner   c                   s   d | _ d | _t   d S N)streamstream_namesuper__init__r   	__class__r   r   rU      s    zFileSerializable.__init__c                 C  s   |   S rQ   )_single_file_serialized_infor   r   r   r   r      s    z FileSerializable.serialized_infoc                 C  s   t d ddS )NZSingleFileSerializableTr4   r5   r   r   r   r   _single_file_api_info   s    z&FileSerializable._single_file_api_infoc                 C  s
   dddS )NrB   filepath or URL to filerC   r   r   r   r   r   rX      s    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 filesrB   rZ   rC   )r   rD   itemsr   r   r   r   r   _multiple_file_serialized_info   s    z/FileSerializable._multiple_file_serialized_infoc                 C  s   t d ddS )NZMultipleFileSerializableTr4   r5   r   r   r   r   _multiple_file_api_info   s    z(FileSerializable._multiple_file_api_infodict[str, dict | bool]c                 C  s   |   S rQ   )rY   r   r   r   r   r
      s    zFileSerializable.api_infor   c                 C  s   |   S rQ   )_single_file_example_inputsr   r   r   r   r      s    zFileSerializable.example_inputsc                 C  s   dt jdddS NFis_filedatazMhttps://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdfr7   r   ZBASE64_FILEr   r   r   r   r`      s    
z,FileSerializable._single_file_example_inputsc                 C  s   dt jdgdgdS ra   re   r   r   r   r   _multiple_file_example_inputs   s    z.FileSerializable._multiple_file_example_inputsr    Fzstr | FileData | Noner!   r"   zFileData | NonerG   c                 C  sl   |d u st |tr|S t|r*|}d }ntt|| }t| j}||rRd nt|t|j	||dS )N)rM   rd   	orig_namerc   size)

isinstancedictr   rH   strr   statst_sizeZencode_url_or_file_to_base64rM   )r   r#   r$   r%   filenamerh   r   r   r   _serialize_single  s    
z"FileSerializable._serialize_singlec                 C  s   t ||S rQ   )r   Zdownload_byte_stream)r   urlr,   r   r   r   _setup_stream  s    zFileSerializable._setup_streamNr)   rI   c           
      C  s  |d u rd S t |tr*tj||dj}nft |trx|dr|d}|d us`J d| |d urtj|d | ||d}ntj||d}n|drD|d r|r|sJ | j	r| j
|d kr| j|d |d  |d	| _	|d | _
t| j	}t|pt td
 }|jddd ||dd }|| t|}n2|d}	|	d usfJ d| tj|	|dj}ntdt| d| |S )NrJ   rc   rM   zThe 'name' field is missing in zfile=)r,   rK   	is_streamzstream/)r,      T)parentsexist_okrg   outputrd   zThe 'data' field is missing in zLA FileSerializable component can only deserialize a string or a dict, not a z: )ri   rk   r   rL   rM   rj   r   Zdownload_tmp_copy_of_fileZcreate_tmp_copy_of_filerR   rS   rq   nextr   tempfile
gettempdirsecretsZ	token_hexmkdirwrite_bytes
ValueErrorr   )
r   r#   r*   r+   r,   	file_namefilepathchunkpathrd   r   r   r   _deserialize_single  sF    








z$FileSerializable._deserialize_singlez3str | FileData | None | list[str | FileData | None]z'FileData | None | list[FileData | None]c                   sF   |du s|dkrdS t |tr4 fdd|D S | S d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   )ro   .0fr%   r$   r   r   r   
<listcomp>^      z.FileSerializable.serialize.<locals>.<listcomp>)ri   listro   r&   r   r   r   r'   M  s
    
zFileSerializable.serializePath | str | Nonezstr | None | list[str | None]c                   sV   |du rdS t trtt |tr@ fdd|D S j| dS 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*   r+   r,   )r   r   r,   r+   r*   r   r   r   r   w  s   z0FileSerializable.deserialize.<locals>.<listcomp>r   )ri   r   rk   r   r   r-   r   r   r   r.   b  s    

zFileSerializable.deserialize)r    F)NNN)r    F)NNN)r/   r0   r1   r:   rU   r   rY   rX   r]   r^   r
   r   r`   rf   ro   rq   r   r'   r.   __classcell__r   r   rV   r   rO      s4        1     rO   c                      sj   e Zd Zdd ZddddZdddd	Zdddddd fddZddddddd fddZ  ZS )VideoSerializablec                 C  s
   dddS )NrB   zfilepath or URL to video filerC   r   r   r   r   r   r     s    z!VideoSerializable.serialized_infor_   r   c                 C  s   t d ddS )NrO   Tr4   r5   r   r   r   r   r
     s    zVideoSerializable.api_infor   c                 C  s   dt jdddS )NFrb   zNhttps://github.com/gradio-app/gradio/raw/main/test/test_files/video_sample.mp4r7   )r   ZBASE64_VIDEOr   r   r   r   r     s    
z VideoSerializable.example_inputsr    Fr)   r!   r"   ztuple[FileData | None, None]rG   c                   s   t  |||d fS rQ   )rT   r'   r&   rV   r   r   r'     s    zVideoSerializable.serializeNz.tuple[FileData | None, FileData | None] | Noner   z*str | tuple[str | None, str | None] | NonerI   c                   sp   t |ttfr:t|dks(J d| |d |d g}ntd| t ||||}t |trl|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)ri   tupler   lenr}   rT   r.   )r   r#   r*   r+   r,   Z	x_as_listZdeserialized_filerV   r   r   r.     s    
zVideoSerializable.deserialize)r    F)NNN)	r/   r0   r1   r   r
   r   r'   r.   r   r   r   rV   r   r     s       r   c                   @  s^   e Zd Zdd ZddddZdddd	ZddddddddZdddddddddZdS )JSONSerializablec                 C  s
   dddS )NrB   zfilepath to JSON filerC   r   r   r   r   r   r     s    z JSONSerializable.serialized_infor_   r   c                 C  s   t d ddS )Nr   Tr4   r5   r   r   r   r   r
     s    zJSONSerializable.api_infor   c                 C  s   dddd dS )Nr   r   )abr7   r   r   r   r   r   r     s    zJSONSerializable.example_inputsr    Fr)   r!   r"   zdict | list | NonerG   c                 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.serializeNzstr | dict | listr(   rI   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
        NrJ   )r   Zdict_or_str_to_json_filerM   r-   r   r   r   r.     s    zJSONSerializable.deserialize)r    F)NNNr/   r0   r1   r   r
   r   r'   r.   r   r   r   r   r     s   	     r   c                   @  s^   e Zd Zdd ZddddZdddd	ZddddddddZdddddddddZdS )GallerySerializablec                 C  s
   dddS )NrB   z^path to directory with images and a file associating images with captions called captions.jsonrC   r   r   r   r   r   r     s    z#GallerySerializable.serialized_infor_   r   c                 C  s   t d ddS )Nr   Tr4   r5   r   r   r   r   r
     s    zGallerySerializable.api_infor   c                 C  s   t jgd dgd dS )Nr   rE   r7   rF   r   r   r   r   r     s    
z"GallerySerializable.example_inputsr    Fr)   r!   r"   zlist[list[str | None]] | NonerG   c                 C  s   |d u s|dkrd S g }t |d }|d}t|}W d    n1 sN0    Y  | D ]&\}}	t j||d}
||
|	g q`|S )Nr    captions.jsonr)r%   )r   openjsonloadr\   rO   r'   append)r   r#   r$   r%   filescaptions_filecaptions_jsoncaptionsr~   captionimgr   r   r   r'     s    (zGallerySerializable.serializeNrk   z
None | strrI   c              	   C  s   |d u rd S t |tt  }|jddd i }|D ]z}t|ttfrR|\}}nd }t j	||||d}	|||	< |d }
|

d}t|| W d    q61 s0    Y  q6tj|S )NT)ru   rt   )r+   r,   r   w)r   rk   uuiduuid4r{   ri   r   r   rO   r.   r   r   dumposr   abspath)r   r#   r*   r+   r,   Zgallery_pathr   Zimg_datar   rM   r   r   r   r   r   r.     s"    
,zGallerySerializable.deserialize)r    F)r    NNr   r   r   r   r   r     s   
    r   FileZUploadButton)ZtextboxnumberZsliderZcheckboxZcheckboxgroupZradioZdropdownimagevideoaudiorN   Z	dataframeZ
timeseriesstatebuttonZuploadbuttonZcolorpickerlabelZhighlightedtextr   htmlZgalleryZchatbotZmodel3dZplotZbarplotZlineplotZscatterplotZmarkdowncodeZannotatedimagezdict[str, type]COMPONENT_MAPPING)(
__future__r   r   r   rz   rx   r   pathlibr   typingr   Zgradio_clientr   r   Zgradio_client.data_classesr   r   __file__parentr   r   r6   r   r2   r;   r=   r>   r?   rA   rO   r   r   r   ZSERIALIZER_MAPPING__subclasses__clsr/   subclsr   __annotations__r   r   r   r   <module>   s~   (58 5'3?
