o
    e<                     @  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rd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Z7	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   FD:\Projects\ConvertPro\env\Lib\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                      sR   e Zd ZdZdd Zdd Zdd Z fdd	Z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_configxr   returnc                 C     |S )zM
        Any preprocessing needed to be performed on function input.
        r   )r'   r3   r   r   r   
preprocessM      zComponent.preprocessc                 C  r5   )zO
        Any postprocessing needed to be performed on function output.
        r   )r'   yr   r   r   postprocessS   r7   zComponent.postprocessc                 O  s   t   d}d|v rtd t|d ttfrd}|d d|v r5td t|d ttfr0d}|d d|v rBtd |d |D ]	}td	|  qD|rbt| jttfrb| jj	d
krbd| 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').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)r3   r   r4   r   )r   r   r   __doc__r&   r,   r*   r0   r6   r9   rE   __classcell__r   r   r1   r   r$   5   s    
	r$   c                      s   e Zd ZdZdddddddddddddddUddZedVdWd#d$ZedVdXd&d'ZedYd)d*ZedVdZd,d-Z	d[d.d/Z
d\d3d4Zd]d5d6Z	d^d_d8d9Zd`dad>d?ZdbdBdCZdcdGdHZdddIdJZ fdKdLZedMdN ZdedQdRZdSdT Z  ZS )f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everyrI   r   rJ   
str | NonerK   rL   bool | NonerM   boolrN   
int | NonerO   rP   rQ   rR   rS   list[str] | str | NonerT   Callable | NonerU   float | Nonec                K  s   t  | _tjdpttt d | _	t
j| f|
||	d| || _|| _|s2|r0td d}|d u r8d}|| _|| _|d urP|t|krPtd| d || _|| _|| _d | _d | _| |\}}| jrk|n| || _t|r}| || d S d S )	NZGRADIO_TEMP_DIRgradio)rR   rS   rQ   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&   rJ   rK   r   rL   rM   roundrN   rO   rP   Z
load_eventload_event_to_attachget_load_fn_and_initial_valueZ_skip_init_processingr9   rI   callableattach_load_event)r'   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   r)   initial_valuer   r   r   r&      sL   
zIOComponent.__init__   	file_path
str | Pathchunk_num_blocksintr4   rc   c                   sf   t  t| dt fdddD ]}| qW d     S 1 s*w   Y   S )Nrbc                     s     j S r%   )read
block_sizer   rp   fsha1r   r   <lambda>   s    z'IOComponent.hash_file.<locals>.<lambda>    )hashlibrw   openiterupdate	hexdigest)rn   rp   chunkr   ru   r   	hash_file   s   
zIOComponent.hash_fileurlc                 C  s^   t  }tj| }d}d}	 |||j }|||j 7 }|r$||kr)	 | S || q)N  @r   )	rz   rw   urllibrequesturlopenrs   rt   r}   r~   )r   rp   rw   remoteZmax_file_sizeZ
total_readr   r   r   r   hash_url   s   
zIOComponent.hash_urlbytesc                 C  s   t  }||  | S r%   )rz   rw   r}   r~   )r   rw   r   r   r   
hash_bytes   s   
zIOComponent.hash_bytesbase64_encodingc                 C  sR   t  }tdt| ||j D ]}| ||||j   }||d q| S )Nr   zutf-8)rz   rw   rangelenrt   r}   encoder~   )r   rp   rw   ir   r   r   r   hash_base64   s
   zIOComponent.hash_base64c                 C  sp   |  |}t| j| }|jddd tt|j}tt	|| }t|
 s0t|| | 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)r   r   rf   mkdirclient_utils!strip_invalid_filename_charactersr.   rc   r	   abspathexistsshutilcopy2r_   add)r'   rn   temp_dirr.   full_temp_file_pathr   r   r   make_temp_copy_if_needed   s   
z$IOComponent.make_temp_copy_if_neededfiler   
upload_dirc           	   	     s   t d}t|| }|jddd |jr"t|jj}t|}ndt d }tt	
|| }t|d4 I d H !}	 |dI d H }|sJn	||I d H  q?W d   I d H  |S 1 I d H sdw   Y  |S )N   Tr   tmp   wbr   )secretsZ	token_hexr   r   filenamer.   r   r   rc   r	   r   aiofilesr{   rs   write)	r'   r   r   r   	file_namer.   r   Zoutput_filecontentr   r   r   save_uploaded_file   s,   zIOComponent.save_uploaded_filec              	   C  s   |  |}t| j| }|jddd tt|j}tt	|| }t|
 s]tj|dd$}t|d}t|j| W d   n1 sIw   Y  W d   n1 sXw   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   rf   r   r   r   r.   rc   r	   r   r   requestsrb   r{   r   copyfileobjrawr_   r   )r'   r   r   r.   r   rrv   r   r   r   download_temp_copy_if_needed  s    
 z(IOComponent.download_temp_copy_if_neededr   c           	      C  s   |  |}t| j| }|jddd t|}|r t|}n
|r(d| }nd}tt	|| }t|
 sZt|\}}t|d}|| W d   n1 sUw   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   rf   r   r   get_extensionr   rc   r	   r   r   Zdecode_base64_to_binaryr{   r   r_   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pngimg_Image.Imagedirc                 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   rc   saveZget_pil_metadata)r'   r   r   format
bytes_datar   r   r   r   r   pil_to_temp_file6  s   zIOComponent.pil_to_temp_filearr
np.ndarrayc                 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_filer   sample_rater   c                 C  sN   t | j| |  }|jddd t|d|  }tj||||d |S )NTr   zaudio.r   )r   rf   r   tobytesr   rc   r   Zaudio_to_file)r'   r   r   r   r   r   r   r   r   audio_to_temp_fileD  s
   zIOComponent.audio_to_temp_filec                 C  s>   t | j| | }|jddd |t |j }|| |S )NTr   )r   rf   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r| j|d< |S )N)rJ   rL   rM   rN   rO   rP   rK   )	rJ   rL   rM   rN   rO   rP   r/   r0   rK   )r'   configr1   r   r   r0   R  s   	
zIOComponent.get_configc                 C  s*   t | r|  }| }||fS | }d }||fS r%   )rj   )rI   rl   rT   r   r   r   ri   `  s   z)IOComponent.get_load_fn_and_initial_valuerj   r   c                 C  s   ||f| _ dS )zHAdd a load event that runs `callable`, optionally every `every` seconds.N)rh   )r'   rj   rU   r   r   r   rk   j  s   zIOComponent.attach_load_eventc                 C  r5   )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)rI   r   rJ   rV   rK   rV   rL   rW   rM   rX   rN   rY   rO   rY   rP   rW   rQ   rX   rR   rV   rS   rZ   rT   r[   rU   r\   )rm   )rn   ro   rp   rq   r4   rc   )r   rc   rp   rq   r4   rc   )r   r   )r   rc   rp   rq   r4   rc   )rn   ro   r4   rc   )r   r   r   rc   r4   rc   )r   rc   r4   rc   r%   )r   rc   r   rV   r4   rc   )r   )r   r   r   rc   r4   rc   )r   r   r   rc   r4   rc   )r   r   r   rq   r   rc   )r   r   r   rc   )rj   r   rU   r\   )r   r   r   rF   r&   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r0   ri   rk   r   rG   r   r   r1   r   rH   }   sL    9







	rH   c                   @  s   e Zd ZdddZdS )FormComponentr4   type[Form] | Nonec                 C  s   t | dd du r
d S tS )NrM   F)getattrr   r+   r   r   r   get_expected_parentt  s   z!FormComponent.get_expected_parentN)r4   r   )r   r   r   r   r   r   r   r   r   s  s    r   cls_namerc   r4   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   
compstr | dict | ComponentrenderrW   c                 C  s   t | tr
t| }n2t | tr-| d}t|}|di | }t |tr,td| nt | t	r5| }ntd|  |rG|j
sG|  |S |du rR|j
rR|  |S )a~  
    Returns a component instance from a string, dict, or Component object.
    Parameters:
        comp: the component to instantiate. If a string, must be the name of a component, e.g. "dropdown". If a dict, must have a "name" key, e.g. {"name": "dropdown", "choices": ["a", "b"]}. If a Component object, will be returned as is.
        render: whether to render the component. If True, renders the component (if not already rendered). If False, *unrenders* the component (if already rendered) -- this is useful when constructing an Interface or ChatInterface inside of a Blocks. If None, does not render or unrender the component.
    r.   r   zCComponent must provided as a `str` or `dict` or `Component` but is FNr   )r>   rc   r    dictrA   r	   r   r   r   r$   Zis_renderedr   Zunrender)r   r   Zcomponent_objr.   Zcomponent_clsr   r   r   get_component_instance  s(   
	






r   )r   rc   r4   r$   r%   )r   r   r   rW   r4   r$   )9rF   
__future__r   rz   r`   r   r   rd   urllib.requestr   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   r]   r   Zgradio.blocksr   r   Zgradio.deprecationr   r   Zgradio.eventsr   Zgradio.layoutsr   r   r   r   r   initr!   r$   rH   r   r    r   r   r   r   r   <module>   sJ    H w
