diff --git a/gradio/components/gallery.py b/gradio/components/gallery.py index 965f4fb099ae..a963276f9cf8 100644 --- a/gradio/components/gallery.py +++ b/gradio/components/gallery.py @@ -3,7 +3,7 @@ from __future__ import annotations from pathlib import Path -from typing import Any, Callable, List, Literal, Optional +from typing import Any, Callable, List, Literal, Optional, Tuple, Union from urllib.parse import urlparse import numpy as np @@ -28,6 +28,10 @@ class GalleryData(GradioRootModel): root: List[GalleryImage] +GalleryImageType = Union[np.ndarray, _Image.Image, Path, str] +CaptionedGalleryImageType = Tuple[GalleryImageType, str] + + @document() class Gallery(Component): """ @@ -126,9 +130,7 @@ def __init__( def postprocess( self, - value: list[np.ndarray | _Image.Image | str] - | list[tuple[np.ndarray | _Image.Image | str, str]] - | None, + value: list[GalleryImageType | CaptionedGalleryImageType] | None, ) -> GalleryData: """ Parameters: diff --git a/js/app/test/gallery_component_events.spec.ts b/js/app/test/gallery_component_events.spec.ts index bc675f12b30a..d848e576fc49 100644 --- a/js/app/test/gallery_component_events.spec.ts +++ b/js/app/test/gallery_component_events.spec.ts @@ -1,24 +1,27 @@ import { test, expect } from "@gradio/tootils"; +const regexLocalHostFileURL = (basename: string) => + new RegExp(`^http://localhost:7860/(\\w+/)*file=(.*\\/)?${basename}$`); + test("Gallery preview mode displays all images correctly.", async ({ page }) => { await page.getByRole("button", { name: "Run" }).click(); await page.getByLabel("Thumbnail 2 of 3").click(); - await expect( - await page.getByTestId("detailed-image").getAttribute("src") - ).toEqual("https://gradio-builds.s3.amazonaws.com/assets/lite-logo.png"); + expect(await page.getByTestId("detailed-image").getAttribute("src")).toMatch( + regexLocalHostFileURL("lite-logo.png") + ); - await expect( - await page.getByTestId("thumbnail 1").getAttribute("src") - ).toEqual("https://gradio-builds.s3.amazonaws.com/assets/cheetah-003.jpg"); + expect(await page.getByTestId("thumbnail 1").getAttribute("src")).toMatch( + regexLocalHostFileURL("cheetah-003.jpg") + ); }); test("Gallery select event returns the right value", async ({ page }) => { await page.getByRole("button", { name: "Run" }).click(); await page.getByLabel("Thumbnail 2 of 3").click(); await expect(page.getByLabel("Select Data")).toHaveValue( - "https://gradio-builds.s3.amazonaws.com/assets/lite-logo.png" + regexLocalHostFileURL("lite-logo.png") ); });