o
    e-.                  
   @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZmZ ed e G dd deeeeeeZdS )zgr.File() component    )annotationsN)Path)AnyCallableLiteral)utils)documentset_documentation_group)FileSerializable)IOComponent	_Keywords)warn_deprecation)
Changeable	ClearableEventListenerMethod
Selectable
Uploadable	componentc                   @  s   e Zd ZdZ	dBddddddddddddddddCd#d$Zd%d& Zeejddddddddf	dDd(d)Z	dEd.d/Z
dFd3d4ZdGd8d9ZdHd;d<Zd=d> ZdId@dAZdS )JFilea4  
    Creates a file component that allows uploading generic file (when used as an input) and or displaying generic files (output).
    Preprocessing: passes the uploaded file as a {tempfile._TemporaryFileWrapper} or {List[tempfile._TemporaryFileWrapper]} depending on `file_count` (or a {bytes}/{List{bytes}} depending on `type`)
    Postprocessing: expects function to return a {str} path to a file, or {List[str]} consisting of paths to files.
    Examples-format: a {str} path to a local file that populates the component.
    Demos: zip_to_json, zip_files
    NsinglefileT   )
file_count
file_typestypelabelevery
show_label	containerscale	min_widthheightinteractivevisibleelem_idelem_classesvalue!str | list[str] | Callable | Noner   *Literal['single', 'multiple', 'directory']r   list[str] | Noner   Literal['file', 'binary']r   
str | Noner   float | Noner   bool | Noner   boolr   
int | Noner    intr!   int | float | Noner"   r#   r$   r%   list[str] | str | Nonec                K  s   || _ || _|durt|tstd|jj g d}||vr*td| d| |dkr2td |dkr?|dur?t	d	 || _
|| _|  	 tj| f|||||	|
|||||d
| dS )aq
  
        Parameters:
            value: Default file to display, given as str file path. If callable, the function will be called whenever the app loads to set the initial value of the component.
            file_count: if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
            file_types: List of file extensions or types of files to be uploaded (e.g. ['image', '.json', '.mp4']). "file" allows any file to be uploaded, "image" allows only image files to be uploaded, "audio" allows only audio files to be uploaded, "video" allows only video files to be uploaded, "text" allows only text files to be uploaded.
            type: Type of value to be returned by component. "file" returns a temporary file object with the same base name as the uploaded file, whose full path can be retrieved by file_obj.name, "binary" returns an bytes object.
            label: component name in interface.
            every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
            show_label: if True, will display label.
            container: If True, will place the component in a container - providing some extra padding around the border.
            scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
            min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
            height: The maximum height of the file component, in pixels. If more files are uploaded than can fit in the height, a scrollbar will appear.
            interactive: if True, will allow users to upload a file; if False, can only be used to display files. If not provided, this is inferred based on whether the component is used as an input or output.
            visible: If False, component will be hidden.
            elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
            elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
        Nz.Parameter file_types must be a list. Received )r   binarybytesz$Invalid value for parameter `type`: z. Please choose from one of: r4   zYThe `bytes` type is deprecated and may not work as expected. Please use `binary` instead.	directoryzGThe `file_types` parameter is ignored when `file_count` is 'directory'.)r   r   r   r   r   r    r"   r#   r$   r%   r&   )r   r   
isinstancelist
ValueError	__class____name__r   warningswarnr   r!   r   __init__)selfr&   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   kwargsZvalid_types r@   FD:\Projects\ConvertPro\env\Lib\site-packages\gradio/components/file.pyr=   -   sP   &
zFile.__init__c                 C  s$   | j | j| j| j| jdt| S )N)r   r   r&   
selectabler!   )r   r   r&   rB   r!   r   
get_configr>   r@   r@   rA   rC      s   zFile.get_config(Any | Literal[_Keywords.NO_VALUE] | Nonec	           	      C  s   ||||||||| dd
S )Nupdate)
r   r   r   r   r    r!   r"   r#   r&   Z__type__r@   )	r&   r   r   r   r   r    r!   r"   r#   r@   r@   rA   rF      s   zFile.updatexlist[dict[str, Any]] | Nonereturn\bytes | tempfile._TemporaryFileWrapper | list[bytes | tempfile._TemporaryFileWrapper] | Nonec                   sf   |du rdS d
fdd j dkr!t|tr |d S  |S t|tr/ fdd	|D S  |S )z
        Parameters:
            x: List of JSON objects with filename as 'name' property and base64 data as 'data' property
        Returns:
            File objects in requested format
        NrI   &bytes | tempfile._TemporaryFileWrapperc                   s   | d | d |  dd}}} jdkrH|r |}nt|\}} j||d}tt|} j	
| tjd jd}||_||_|S  jdksR jd	krt|rmt|d
}| W  d    S 1 shw   Y  t|d S tdt j d )Nnamedatais_fileFr   )	file_name)deletedirr3   r4   rbr   zUnknown type: z&. Please choose from: 'file', 'bytes'.)getr   make_temp_copy_if_neededclient_utilsZdecode_base64_to_binaryZfile_bytes_to_filestrr   abspathZ
temp_filesaddtempfileNamedTemporaryFileZDEFAULT_TEMP_DIRrL   	orig_nameopenreadr8   )frO   rM   rN   path_r   	file_datarD   r@   rA   process_single_file   s<   


 z,File.preprocess.<locals>.process_single_filer   r   c                   s   g | ]} |qS r@   r@   ).0r^   )rb   r@   rA   
<listcomp>   s    z#File.preprocess.<locals>.<listcomp>)rI   rK   )r   r6   r7   )r>   rG   r@   )rb   r>   rA   
preprocess   s   
$

zFile.preprocessystr | list[str] | None,dict[str, Any] | list[dict[str, Any]] | Nonec                   sR   |du rdS t |tr fdd|D S t|j |t| jddd}|S )z
        Parameters:
            y: file path
        Returns:
            JSON object with key 'name' for filename, 'data' for base64 url, and 'size' for filesize in bytes
        Nc                   s2   g | ]}t |j |t | jd ddqS )NTr[   rL   sizerM   rN   )r   rL   rT   statst_sizerc   r   rD   r@   rA   rd      s    z$File.postprocess.<locals>.<listcomp>Tri   )r6   r7   r   rL   rT   rk   rl   )r>   rf   dr@   rD   rA   postprocess   s   	

zFile.postprocess
input_datastr | list | NonerV   c                 C  s4   |d u rdS t |trddd |D S t|jS )N z, c                 S  s   g | ]}t |jqS r@   )r   rL   rm   r@   r@   rA   rd   	  s    z#File.as_example.<locals>.<listcomp>)r6   r7   joinr   rL   )r>   rp   r@   r@   rA   
as_example  s
   

zFile.as_exampledict[str, dict | bool]c                 C     | j dkr	|  S |  S Nr   )r   Z_single_file_api_infoZ_multiple_file_api_inforD   r@   r@   rA   api_info     
zFile.api_infoc                 C  rv   rw   )r   Z_single_file_serialized_infoZ_multiple_file_serialized_inforD   r@   r@   rA   serialized_info  ry   zFile.serialized_infodict[str, Any]c                 C  rv   rw   )r   Z_single_file_example_inputsZ_multiple_file_example_inputsrD   r@   r@   rA   example_inputs  ry   zFile.example_inputs)N)r&   r'   r   r(   r   r)   r   r*   r   r+   r   r,   r   r-   r   r.   r   r/   r    r0   r!   r1   r"   r-   r#   r.   r$   r+   r%   r2   )r&   rE   r   r+   r   r-   r   r-   r   r/   r    r/   r!   r1   r"   r-   r#   r-   )rG   rH   rI   rJ   )rf   rg   rI   rh   )rp   rq   rI   rV   )rI   ru   )rI   r{   )r:   
__module____qualname____doc__r=   rC   staticmethodr   ZNO_VALUErF   re   ro   rt   rx   rz   r|   r@   r@   r@   rA   r      sJ    	
U


@
 
r   ) r   
__future__r   rY   r;   pathlibr   typingr   r   r   Zgradio_clientr   rU   Zgradio_client.documentationr   r	   Zgradio_client.serializingr
   ZgradioZgradio.components.baser   r   Zgradio.deprecationr   Zgradio.eventsr   r   r   r   r   r   r@   r@   r@   rA   <module>   s.    

