
    ;3h0                       d Z ddlmZ ddlZddlmZm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 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 ddlmZ ddlm Z  ddl!m"Z" e
rddl#m$Z$  e        G d de             Z%y)zgr.File() component    )annotationsN)CallableSequence)Path)TYPE_CHECKINGAnyLiteral)handle_file)document)processing_utils)	Component)FileData	ListFiles)Events)Error)I18nData)NamedString)Timerc                  h    e Zd ZdZej
                  ej                  ej                  ej                  ej                  ej                  gZ	 ddddddddddddddddddddd		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd
ZddZ	 	 	 	 ddZddZddZddZddZddZ xZS )Filez
    Creates a file component that allows uploading one or more generic files (when used as an input) or displaying generic files or URLs for download (as output).

        Demo: zip_files, zip_to_json
    NsinglefilepathT   valueF)
file_count
file_typestypelabeleveryinputs
show_label	containerscale	min_widthheightinteractivevisibleelem_idelem_classesrenderkeypreserved_by_keyallow_reorderingc                  g d}|| _         | j                   |vrt        d|       | j                   dv rt        | _        nt        | _        || _        |2t        |t              s"t        d|j                  j                         ddg}||vrt        d| d	|       t        | -  |||||	|
|||||||||
       || _        || _        || _        | j                   dk(  rd| _        yd| _        y)a  
        Parameters:
            value: Default file(s) to display, given as a str file path or URL, or a list of str file paths / URLs. If a function is provided, the function will be called each time the app loads to set the initial value of this 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: the label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
            every: Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
            inputs: Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
            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 size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
            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 default height of the file component when no files have been uploaded, or the maximum height of the file component when files are present. Specified in pixels if a number is passed, or in CSS units if a string is passed. 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.
            render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
            key: in a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.
            preserved_by_key: A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.
            allow_reordering: if True, will allow users to reorder uploaded files by dragging and dropping.
        )r   multiple	directoryz*Parameter file_count must be one of them: )r/   r0   Nz.Parameter file_types must be a list. Received r   binaryz$Invalid value for parameter `type`: z. Please choose from one of: )r   r   r    r!   r"   r#   r$   r&   r'   r(   r)   r*   r+   r,   r   r   za string filepathza list of string filepaths)r   
ValueErrorr   
data_modelr   r   
isinstancelist	__class____name__super__init__r   r%   r-   _value_description)selfr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   file_count_valid_typesvalid_typesr6   s                          V/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/components/file.pyr9   zFile.__init__+   sK   ^ "E$??"88<=S<TU  __ 99'DO&DO$!*Z*F@AUAUA^A^@_`  
 {"6tf<YZeYfg  	!#%- 	 	
" 	 0 (*   	 . 	    c                   |j                   }| j                  dk(  rw| j                  r8t        j                  || j                        st        d| j                         t        j                  d| j                        }||_	        t        |      S | j                  dk(  r&t        |d      5 }|j                         cd d d        S t        dt        t              z   dz         # 1 sw Y   y xY w)	Nr   zFInvalid file type. Please upload a file that is one of these formats: F)deletedirr1   rbzUnknown type: z+. Please choose from: 'filepath', 'binary'.)pathr   r   client_utilsis_valid_filer   tempfileNamedTemporaryFileGRADIO_CACHEnamer   openreadr2   str)r;   f	file_namefile	file_datas        r>   _process_single_filezFile._process_single_file   s    FF	99
"|'A'A4??( \]a]l]l\mn  ..eARARSD!DIy))YY("i& () ~~'( (  d)?@ ( (s   .C&&C/c                   |y| j                   dk(  r5t        |t              r| j                  |d         S | j                  |      S t        |t              r|D cg c]  }| j                  |       c}S | j                  |      gS c c}w )a  
        Parameters:
            payload: File information as a FileData object, or a list of FileData objects.
        Returns:
            Passes the file as a `str` or `bytes` object, or a list of `str` or list of `bytes` objects, depending on `type` and `file_count`.
        Nr   r   )r   r4   r   rR   )r;   payloadrN   s      r>   
preprocesszFile.preprocess   s     ???h&'9-00<<,,W55gy):ABQD--a0BB))'233 Cs   B	c                Z   g }t        |t              ra|D ]Z  }t        j                  |      r2t	        j
                  || j                        }|j                  |       J|j                  |       \ |S t        j                  |      r"t	        j
                  || j                        }|S |S N)r4   r5   rE   is_http_url_liker   save_url_to_cacherI   append)r;   r   downloaded_filesrP   downloaded_files        r>   _download_fileszFile._download_files   s    eT" 2006&6&H&Hd//'O %++O<$++D12 $#((/.@@t((O #"Lr?   c                   |y| j                  |      }t        |t              rZt        |D cg c]D  }t	        |t        |      j                  t        |      j                         j                        F c}      S t	        |t        |      j                  t        |      j                         j                        S c c}w )z
        Parameters:
            value: Expects a `str` filepath or URL, or a `list[str]` of filepaths/URLs.
        Returns:
            File information as a FileData object, or a list of FileData objects.
        N)rD   	orig_namesize)root)	r]   r4   r5   r   r   r   rJ   statst_sizer;   r   rP   s      r>   postprocesszFile.postprocess   s     =$$U+eT" !&  !"&t*//!$Z__.66	 	 u+**%[%%'// s   A	Cc                    |yt        |t              r2dj                  |D cg c]  }t        |      j                   c}      S t        |      j                  S c c}w )N z, )r4   r5   joinr   rJ   rd   s      r>   process_examplezFile.process_example   sL    =t$99%@$d4joo@AA;### As   Ac                N    | j                   dk(  rt        d      S t        d      gS Nr   zMhttps://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdf)r   r
   r;   s    r>   example_payloadzFile.example_payload   s5    ??h&_ 
 c r?   c                (    | j                   dk(  rydgS rk   )r   rl   s    r>   example_valuezFile.example_value   s!    ??h&b ` r?   rW   )(r   z!str | list[str] | Callable | Noner   z*Literal['single', 'multiple', 'directory']r   zlist[str] | Noner   zLiteral['filepath', 'binary']r   zstr | I18nData | Noner   zTimer | float | Noner    z7Component | Sequence[Component] | set[Component] | Noner!   bool | Noner"   boolr#   z
int | Noner$   intr%   zint | str | float | Noner&   rp   r'   rq   r(   z
str | Noner)   list[str] | str | Noner*   rq   r+   z(int | str | tuple[int | str, ...] | Noner,   rs   r-   rq   )rN   r   returnzNamedString | bytes)rT   ListFiles | FileData | Nonert   z,bytes | str | list[bytes] | list[str] | None)r   str | list[str]rt   rv   )r   zstr | list[str] | Nonert   ru   )r   zstr | list | Nonert   rM   )rt   r   )r7   
__module____qualname____doc__r   changeselectclearuploadrA   downloadEVENTSr9   rR   rU   r]   re   ri   rm   ro   __classcell__)r6   s   @r>   r   r      s    	F 48_
 BJ'+.8'+&*JN"& +/#'"/38<3:!&-_
0_
 ?	_

 %_
 ,_
 %_
 $_
 H_
  _
 _
 _
 _
 )_
 !_
  !_
" #_
$ -%_
& '_
( 6)_
* 1+_
, -_
B,424	54((8$
r?   r   )&ry   
__future__r   rG   collections.abcr   r   pathlibr   typingr   r   r	   gradio_client.utilsutilsrE   gradio_clientr
   gradio_client.documentationr   gradior   gradio.components.baser   gradio.data_classesr   r   gradio.eventsr   gradio.exceptionsr   gradio.i18nr   gradio.utilsr   gradio.componentsr   r    r?   r>   <module>r      s^     "  .  . . * % 0 # , 3   #   $' 
e9 e er?   