a
    d9                     @  s  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Z	ddl
mZ ddlmZ ddlmZmZm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 dd
lmZ ddlm Z! ddl"m#Z#mZ ddl$m%Z%m&Z& ddl'm(Z(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/ er2ddlm0Z0 G dd de0Z1ed e!2  G dd deZ3G dd de%eZ4G dd de4Z5G dd dZ6dddddZ7d%d!dd"d#d$Z8dS )&zContains all of the components that can be used with Gradio Interface / Blocks.
Along with the docs for each component, you can find the names of example demos that use
each component. These demos are located in the `demo` directory.    )annotationsN)Enum)Path)TYPE_CHECKINGAnyCallable)
UploadFile)utils)set_documentation_group)Serializable)Image)processing_utilsr	   )BlockBlockContext)warn_deprecationwarn_style_method_deprecation)EventListener)ColumnFormRow)	TypedDictc                   @  s   e Zd ZU ded< ded< dS )DataframeDataz	list[str]headerszlist[list[str | int | bool]]dataN)__name__
__module____qualname____annotations__ r   r   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio/components/base.pyr   '   s   
r   	componentc                   @  s   e Zd ZdZdZdS )	_KeywordsNO_VALUEFINISHED_ITERATINGN)r   r   r   r"   r#   r   r   r   r   r!   0   s   r!   c                      sX   e Zd ZdZdd Zdd Zdd Z fdd	Zd
d
dddZdd Z	dd Z
  ZS )	ComponentzW
    A base class for defining the methods that all gradio components should have.
    c                 O  s&   t j| g|R i | t|  d S N)r   __init__r   )selfargskwargsr   r   r   r&   :   s    zComponent.__init__c                 C  s   |   S r%   )__repr__r'   r   r   r   __str__>   s    zComponent.__str__c                 C  s
   |    S r%   )get_block_namer+   r   r   r   r*   A   s    zComponent.__repr__c                   s   d|   it  S )zr
        :return: a dictionary with context variables for the javascript file associated with the context
        name)r-   super
get_configr+   	__class__r   r   r0   D   s    zComponent.get_configr   )xreturnc                 C  s   |S )zM
        Any preprocessing needed to be performed on function input.
        r   )r'   r3   r   r   r   
preprocessM   s    zComponent.preprocessc                 C  s   |S )zO
        Any postprocessing needed to be performed on function output.
        r   )r'   yr   r   r   postprocessS   s    zComponent.postprocessc                 O  s   t   d}d|v r:td t|d ttfr0d}|d d|v rjtd t|d ttfr`d}|d d|v rtd |d |D ]}td	|  q|rt| jttfr| jj	d
krd| j_	| S )zn
        This method is deprecated. Please set these arguments in the Components constructor instead.
        FZroundedzw'rounded' styling is no longer supported. To round adjacent components together, place them in a Column(variant='box').Tmarginz'margin' styling is no longer supported. To place adjacent components together without margin, place them in a Column(variant='box').Zborderz'border' styling is no longer supported. To place adjacent components in a shared border, place them in a Column(variant='box').zUnknown style parameter: defaultcompact)
r   r   
isinstancelisttuplepopparentr   r   variant)r'   r(   r)   Zput_deprecated_params_in_boxkeyr   r   r   styleY   s>    



zComponent.style)r   r   r   __doc__r&   r,   r*   r0   r5   r7   rB   __classcell__r   r   r1   r   r$   5   s   	r$   c                      sp  e Zd ZdZdddddddddddddddddddd	d	dddd
dddddZedFddddddZedGddddddZeddddZedHddddddZ	ddd d!d"Z
d#ddd$d%d&Zddd'd(d)ZdIdddd*d+d,ZdJd.ddd/d0d1Zd2ddd3d4d5Zd2ddd6d7d8Zddd9d:d;Z fd<d=Zed>d? Zd@ddAdBdCZdDdE Z  ZS )KIOComponentzY
    A base class for defining methods that all input/output components should have.
    NT)valuelabelinfo
show_label	containerscale	min_widthinteractivevisibleelem_idelem_classesload_fneveryr   z
str | Nonezbool | Noneboolz
int | Nonezlist[str] | str | NonezCallable | Nonezfloat | Nonec                K  s   t  | _tjdp&ttt d | _	t
j| f|
||	d| || _|| _|sd|r`td d}|d u rpd}|| _|| _|d ur|t|krtd| d || _|| _|| _d | _d | _| |\}}| jr|n| || _t|r| || d S )	NZGRADIO_TEMP_DIRgradio)rO   rP   rN   z1show_label has no effect when container is False.FTz*'scale' value should be an integer. Using z will cause issues.)set
temp_filesosenvirongetstrr   tempfile
gettempdirDEFAULT_TEMP_DIRr$   r&   rG   rH   r   rI   rJ   roundrK   rL   rM   Z
load_eventload_event_to_attachget_load_fn_and_initial_valueZ_skip_init_processingr7   rF   callableattach_load_event)r'   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r)   initial_valuer   r   r   r&      sJ    
zIOComponent.__init__   z
str | PathintrZ   )	file_pathchunk_num_blocksr4   c                   s`   t  t| d6t fdddD ]}| q*W d    n1 sN0    Y   S )Nrbc                     s     j S r%   )read
block_sizer   rg   fsha1r   r   <lambda>       z'IOComponent.hash_file.<locals>.<lambda>ro   )hashlibrm   openiterupdate	hexdigest)rf   rg   chunkr   rk   r   	hash_file   s
    *zIOComponent.hash_file)urlrg   r4   c                 C  s\   t  }tj| }d}d}|||j }|||j 7 }|rT||krHqT|| q| S )N  @r   )	rp   rm   urllibrequesturlopenri   rj   rs   rt   )rw   rg   rm   remoteZmax_file_sizeZ
total_readr   r   r   r   hash_url   s    zIOComponent.hash_urlbytes)r~   c                 C  s   t  }||  | S r%   )rp   rm   rs   rt   )r~   rm   r   r   r   
hash_bytes   s    
zIOComponent.hash_bytes)base64_encodingrg   r4   c                 C  sR   t  }tdt| ||j D ]*}| ||||j   }||d q| S )Nr   zutf-8)rp   rm   rangelenrj   rs   encodert   )r   rg   rm   ir   r   r   r   hash_base64   s
    zIOComponent.hash_base64)rf   r4   c                 C  sp   |  |}t| j| }|jddd tt|j}tt	|| }t|
 s`t|| | j| |S )zReturns a temporary file path for a copy of the given file path if it does
        not already exist. Otherwise returns the path to the existing temp file.Texist_okparents)rv   r   r]   mkdirclient_utils!strip_invalid_filename_charactersr.   rZ   r	   abspathexistsshutilcopy2rV   add)r'   rf   temp_dirr.   full_temp_file_pathr   r   r   make_temp_copy_if_needed   s    
z$IOComponent.make_temp_copy_if_neededr   )file
upload_dirr4   c           	   	     s   t d}t|| }|jddd |jrBt|jj}t|}ndt d }tt	
|| }t|d4 I d H >}|dI d H }|sq||I d H  qzW d   I d H  q1 I d H s0    Y  |S )N   Tr   tmp   wbrx   )secretsZ	token_hexr   r   filenamer.   r   r   rZ   r	   r   aiofilesrq   ri   write)	r'   r   r   r   	file_namer.   r   Zoutput_filecontentr   r   r   save_uploaded_file   s     <zIOComponent.save_uploaded_file)rw   r4   c              	   C  s   |  |}t| j| }|jddd tt|j}tt	|| }t|
 stj|ddH}t|d}t|j| W d   n1 s0    Y  W d   n1 s0    Y  | j| |S )zDownloads a file and makes a temporary file path for a copy if does not already
        exist. Otherwise returns the path to the existing temp file.Tr   )streamr   N)r}   r   r]   r   r   r   r.   rZ   r	   r   r   requestsrY   rq   r   copyfileobjrawrV   r   )r'   rw   r   r.   r   rrl   r   r   r   download_temp_copy_if_needed  s    
Jz(IOComponent.download_temp_copy_if_needed)r   r   r4   c           	      C  s   |  |}t| j| }|jddd t|}|r@t|}n|rPd| }nd}tt	|| }t|
 st|\}}t|d}|| W d   n1 s0    Y  | j| |S )zConverts a base64 encoding to a file and returns the path to the file if
        the file doesn't already exist. Otherwise returns the path to the existing file.
        Tr   zfile.r   r   N)r   r   r]   r   r   get_extensionr   rZ   r	   r   r   Zdecode_base64_to_binaryrq   r   rV   r   )	r'   r   r   r   guess_extensionr   r   _Zfbr   r   r   base64_to_temp_file_if_needed  s     

(z)IOComponent.base64_to_temp_file_if_neededpngz_Image.Image)imgdirr4   c                 C  sV   t ||}t|| | }|jddd t|d|  }|j|t |d |S )NTr   zimage.)Zpnginfo)r   Zencode_pil_to_bytesr   r   r   rZ   saveZget_pil_metadata)r'   r   r   format
bytes_datar   r   r   r   r   pil_to_temp_file6  s    zIOComponent.pil_to_temp_filez
np.ndarray)arrr   r4   c                 C  s(   t tj|tjdd}| j||ddS )NF)Z
force_copyr   r   )_ImageZ	fromarrayr   _convertnpZuint8r   )r'   r   r   Z	pil_imager   r   r   img_array_to_temp_file>  s    z"IOComponent.img_array_to_temp_file)r   sample_rater   c                 C  sN   t | j| |  }|jddd t|d|  }tj||||d |S )NTr   zaudio.r   )r   r]   r   tobytesr   rZ   r   Zaudio_to_file)r'   r   r   r   r   r   r   r   r   audio_to_temp_fileD  s
    zIOComponent.audio_to_temp_file)r   r   c                 C  s>   t | j| | }|jddd |t |j }|| |S )NTr   )r   r]   r   r   r.   write_bytes)r'   r   r   pathr   r   r   file_bytes_to_fileK  s
    
zIOComponent.file_bytes_to_filec                   s<   | j | j| j| j| j| jdt  }| jr8| j|d< |S )N)rG   rI   rJ   rK   rL   rM   rH   )	rG   rI   rJ   rK   rL   rM   r/   r0   rH   )r'   configr1   r   r   r0   R  s    	
zIOComponent.get_configc                 C  s$   t | r|  }| }n| }d }||fS r%   )ra   )rF   rc   rQ   r   r   r   r`   `  s    z)IOComponent.get_load_fn_and_initial_valuer   )ra   rR   c                 C  s   ||f| _ dS )zHAdd a load event that runs `callable`, optionally every `every` seconds.N)r_   )r'   ra   rR   r   r   r   rb   j  s    zIOComponent.attach_load_eventc                 C  s   |S )zhReturn the input data in a way that can be displayed by the examples dataset component in the front-end.r   )r'   Z
input_datar   r   r   
as_examplen  s    zIOComponent.as_example)rd   )rd   )rd   )N)r   )r   r   r   rC   r&   staticmethodrv   r}   r   r   r   r   r   r   r   r   r   r   r0   r`   rb   r   rD   r   r   r1   r   rE   }   sJ   *9 
	rE   c                   @  s   e Zd ZddddZdS )FormComponentztype[Form] | None)r4   c                 C  s   t | dd du rd S tS )NrJ   F)getattrr   r+   r   r   r   get_expected_parentt  s    z!FormComponent.get_expected_parentN)r   r   r   r   r   r   r   r   r   s  s   r   rZ   )cls_namer4   c                 C  s*   t |  }t|tr&td|j |S )NInvalid component: )r	   component_or_layout_classr;   r   
ValueErrorr2   )r   objr   r   r   r    z  s    
Tzstr | dict | Component)compr4   c                 C  s   t | tr"t| }|s|  |S t | trv| d}t|}|f i | }t |trft	d| |sr|  |S t | t
r| S t	d|  d S )Nr.   r   zCComponent must provided as a `str` or `dict` or `Component` but is )r;   rZ   r    Zunrenderdictr>   r	   r   r   r   r$   )r   renderZcomponent_objr.   Zcomponent_clsr   r   r   get_component_instance  s&    





r   )T)9rC   
__future__r   rp   rW   r   r   r[   urllib.requestry   enumr   pathlibr   typingr   r   r   r   numpyr   r   Zfastapir   Zgradio_clientr	   r   Zgradio_client.documentationr
   Zgradio_client.serializingr   ZPILr   r   rT   r   Zgradio.blocksr   r   Zgradio.deprecationr   r   Zgradio.eventsr   Zgradio.layoutsr   r   r   r   r   initr!   r$   rE   r   r    r   r   r   r   r   <module>   sF   H w