
    ;3hsB                       d 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mZmZmZ ddlmZm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 m!Z!mZm"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 erddl1m2Z2 eejf                  ejh                  jh                  e	e5f   Z6e7e6e5f   Z8 G d de'      Z9 G d de'      Z: G d de(      Z; e        G d de$             Z<y)zgr.Gallery() component.    )annotations)CallableSequence)ThreadPoolExecutor)Path)TYPE_CHECKINGAnyLiteralOptionalUnion)quoteurlparseNhandle_file)utils)document)is_http_url_like)image_utilsprocessing_utilsr   
wasm_utils)	Component)FileDataGradioModelGradioRootModel	ImageData)EventListenerEvents)Error)I18nData)Timerc                  &    e Zd ZU ded<   dZded<   y)GalleryImager   imageNOptional[str]caption__name__
__module____qualname____annotations__r%        Y/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/components/gallery.pyr"   r"   &   s    !G]!r,   r"   c                  &    e Zd ZU ded<   dZded<   y)GalleryVideor   videoNr$   r%   r&   r+   r,   r-   r/   r/   +   s    O!G]!r,   r/   c                      e Zd ZU ded<   y)GalleryDataz'list[Union[GalleryImage, GalleryVideo]]rootN)r'   r(   r)   r*   r+   r,   r-   r2   r2   0   s    
11r,   r2   c                      e Zd ZdZej
                  ej                  ej                   edd       edd      gZ	e
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 fdZ	 	 	 	 ddZ	 	 	 	 ddZedd       ZddZddZ xZS )GalleryaA  
    Creates a gallery component that allows displaying a grid of images or videos, and optionally captions. If used as an input, the user can upload images or videos to the gallery.
    If used as an output, the user can click on individual images or videos to view them at a higher resolution.

    Demos: fake_gan
    preview_closezFThis event is triggered when the Gallery preview is closed by the user)docpreview_openzFThis event is triggered when the Gallery preview is opened by the userNwebpT   value   filepath)format
file_typeslabeleveryinputs
show_label	containerscale	min_widthvisibleelem_idelem_classesrenderkeypreserved_by_keycolumnsrowsheightallow_previewpreviewselected_index
object_fitshow_share_buttonshow_download_buttoninteractivetypeshow_fullscreen_buttonc                  || _         || _        || _        || _        || _        || _        || _        |t        j                         dun|| _	        || _
        || _        || _        || _        |t        j                         dun|| _        t        | A  ||||||	|
||||||||       d|dk(  rdn|dk(  rdnd | _        y)	a  
        Parameters:
            value: List of images or videos to display in the gallery by default. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
            format: Format to save images before they are returned to the frontend, such as 'jpeg' or 'png'. This parameter only applies to images that are returned from the prediction function as numpy arrays or PIL Images. The format should be supported by the PIL library.
            file_types: List of file extensions or types of files to be uploaded (e.g. ['image', '.mp4']), when this is used as an input component. "image" allows only image files to be uploaded, "video" allows only video files to be uploaded, ".mp4" allows only mp4 files to be uploaded, etc. If None, any image and video files types are allowed.
            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.
            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.
            columns: Represents the number of images that should be shown in one row, for each of the six standard screen sizes (<576px, <768px, <992px, <1200px, <1400px, >1400px). If fewer than 6 are given then the last will be used for all subsequent breakpoints
            rows: Represents the number of rows in the image grid, for each of the six standard screen sizes (<576px, <768px, <992px, <1200px, <1400px, >1400px). If fewer than 6 are given then the last will be used for all subsequent breakpoints
            height: The height of the gallery component, specified in pixels if a number is passed, or in CSS units if a string is passed. If more images are displayed than can fit in the height, a scrollbar will appear.
            allow_preview: If True, images in the gallery will be enlarged when they are clicked. Default is True.
            preview: If True, Gallery will start in preview mode, which shows all of the images as thumbnails and allows the user to click on them to view them in full size. Only works if allow_preview is True.
            selected_index: The index of the image that should be initially selected. If None, no image will be selected at start. If provided, will set Gallery to preview mode unless allow_preview is set to False.
            object_fit: CSS object-fit property for the thumbnail images in the gallery. Can be "contain", "cover", "fill", "none", or "scale-down".
            show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
            show_download_button: If True, will show a download button in the corner of the selected image. If False, the icon does not appear. Default is True.
            interactive: If True, the gallery will be interactive, allowing the user to upload images. If False, the gallery will be static. Default is True.
            type: The format the image is converted to before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.
            show_fullscreen_button: If True, will show a fullscreen icon in the corner of the component that allows user to view the gallery in fullscreen mode. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
        N)r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r;   rV   z
a list of r=   zstring filepathsnumpyznumpy arraysz
PIL images)r>   rM   rN   rO   rQ   rS   rP   r   	get_spacerU   rR   rW   rX   r?   rT   super__init___value_description)selfr;   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   	__class__s                                r-   r]   zGallery.__init__M   s   J 	$* $+ __d*% 	!
 -	&<#$ !( __d*" 	
 	!%-# 	 	
" %/TZEW/Aosw~o~]k  EQ  /R  #Sr,   c                <   ||j                   syg }|j                   D ]  }t        |t              r|j                  j                  }n|j
                  j                  xs d}| j                  r8t        j                  || j                        st        d| j                         t        |      t        u r|j                  j                  n/| j                  |j
                  j                  | j                        }|j                  ||j                  f        |S )a  
        Parameters:
            payload: a list of images or videos, or list of (media, caption) tuples
        Returns:
            Passes the list of images or videos as a list of (media, caption) tuples, or a list of (media, None) tuples if no captions are provided (which is usually the case). Images can be a `str` file path, a `numpy` array, or a `PIL.Image` object depending on `type`.  Videos are always `str` file path.
        N zFInvalid file type. Please upload a file that is one of these formats: )r3   
isinstancer/   r0   pathr#   r?   client_utilsis_valid_filer   rW   convert_to_typeappendr%   )r_   payloaddatagallery_element	file_pathmedias         r-   
preprocesszGallery.preprocess   s     ?',,&|| 	>O/<8+1166	+1166<"	|'A'A4??( \]a]l]l\mn  _-= $))..--o.C.C.H.H$))T 
 UO$;$;<=#	>$ r,   c                j    |t        g       S t        |t              rt        d      g } fd}t        j
                  r|D ]  }|j                   ||              n<t               5 }|j                  ||      D ]  }|j                  |        	 ddd       t        |      S # 1 sw Y   xY w)ai  
        Parameters:
            value: Expects the function to return a `list` of images or videos, or `list` of (media, `str` caption) tuples. Each image can be a `str` file path, a `numpy` array, or a `PIL.Image` object. Each video can be a `str` file path.
        Returns:
            a list of images or videos, or list of (media, caption) tuples
        N)r3   zlThe `value` passed into `gr.Gallery` must be a list of images or videos, or list of (media, caption) tuples.c                   d }d }d }d }t        | t        t        f      r| \  } }t        | t        j                        rLt        j                  | j                  j                        }t        t        j                  |            }nt        | t        j                  j                        rLt        j                  | j                  j                        }t        t        j                  |            }n%t        | t              rt        j                   |       }| j#                         j%                  d      r;t'        j(                  |       }t+        |       j,                  }dt/        |       }d }nt1        |       r-| }t+        t3        |       j4                        j,                  }| }nnd }t+        |       j,                  }| }nTt        | t*              r-t        |       }| j,                  }t        j                   |      }nt7        dt9        |              |d|v rt;        t=        ||||      |      S t?        tA        ||||      |      S )	N)	cache_dirr>   z.svgzdata:image/svg+xml,zCannot process type as image: r0   )rd   url	orig_name	mime_type)r0   r%   )r#   r%   )!rc   tuplelistnpndarrayr   save_img_array_to_cacheGRADIO_CACHEr>   strr   abspathPILImagesave_pil_to_cachere   get_mimetypelowerendswithr   extract_svg_contentr   namer   r   r   rd   
ValueErrorrW   r/   r   r"   r   )	imgrr   r%   rs   rt   filerl   svg_contentr_   s	           r-   _savez"Gallery.postprocess.<locals>._save   s   CGII#t}-"W#rzz*'??4#4#4T[[  d 34	C1'994#4#4T[[  d 34	C%(55c:	99;''/"-"A"A#"FK $S	I/k0B/CDC $I%c*C $Xc]%7%7 8 = =I #IC $S	I #IC&H	HH	(55i@	 #A$s)!MNN$I)=#"&"+"+	 $  $#&"+"+	 $ r,   )	r2   rc   r{   r   r   IS_WASMrh   r   map)r_   r;   outputr   r   executoros   `      r-   postprocesszGallery.postprocess   s     =B''eS!~  9	v  *eCj)* $% %!eU3 %AMM!$%% ''% %s   +)B))B2c                    |dk(  r| S t         j                  j                  |       }|dk(  rt        j                  |      }|S )Nr=   rZ   )r}   r~   openrw   array)r   rW   converted_images      r-   rg   zGallery.convert_to_type6  s<    :J!iinnS1Ow"$((?";""r,   c                    dt        d      igS )Nr#   Phttps://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.pngr   r_   s    r-   example_payloadzGallery.example_payload@  s      f
 	
r,   c                    dgS )Nr   r+   r   s    r-   example_valuezGallery.example_valueI  s    ^
 	
r,   )N)8r;   zMSequence[np.ndarray | PIL.Image.Image | str | Path | tuple] | Callable | Noner>   r{   r?   zlist[str] | Noner@   zstr | I18nData | NonerA   zTimer | float | NonerB   z7Component | Sequence[Component] | set[Component] | NonerC   bool | NonerD   boolrE   
int | NonerF   intrG   r   rH   z
str | NonerI   list[str] | str | NonerJ   r   rK   z(int | str | tuple[int | str, ...] | NonerL   r   rM   z(int | list[int] | tuple[int, ...] | NonerN   zint | list[int] | NonerO   zint | float | str | NonerP   r   rQ   r   rR   r   rS   z@Literal['contain', 'cover', 'fill', 'none', 'scale-down'] | NonerT   r   rU   r   rV   r   rW   z#Literal['numpy', 'pil', 'filepath']rX   r   )ri   zGalleryData | Nonereturnztlist[tuple[str, str | None]] | list[tuple[PIL.Image.Image, str | None]] | list[tuple[np.ndarray, str | None]] | None)r;   z9list[GalleryMediaType | CaptionedGalleryMediaType] | Noner   r2   )r   r{   rW   z#Literal['filepath', 'numpy', 'pil'])r   r	   )r'   r(   r)   __doc__r   selectuploadchanger   EVENTSr2   
data_modelr]   rn   r   staticmethodrg   r   r   __classcell__)r`   s   @r-   r5   r5   4   sb    	X	
 	X	
F J lS '+'+&*JN"& "/38<3:<='++/"#%) )-,0#'4>'+IlSlS lS %lS %lS $lS HlS  lS lS lS  !lS" #lS$ %lS& -'lS( )lS* 6+lS, 1-lS. :/lS0 %1lS2 )3lS4 5lS6 7lS8 #9lS< M=lS@ 'AlSB *ClSD !ElSF 2GlSH !%IlS\#)#	#JT(HT( 
T(l # #

r,   r5   )=r   
__future__r   collections.abcr   r   concurrent.futuresr   pathlibr   typingr   r	   r
   r   r   urllib.parser   r   rZ   rw   	PIL.Imager}   gradio_clientr   r   re   gradio_client.documentationr   gradio_client.utilsr   gradior   r   r   gradio.components.baser   gradio.data_classesr   r   r   r   gradio.eventsr   r   gradio.exceptionsr   gradio.i18nr   gradio.componentsr    rx   r~   r{   GalleryMediaTyperu   CaptionedGalleryMediaTyper"   r/   r2   r5   r+   r,   r-   <module>r      s     " . 1   )   % / 0 0 C C , Q Q / #  'SYY__dC?@ !"2C"78 "; "
"; "
2/ 2 
W
i W
 W
r,   