gradio-pr-bot commited on
Commit
21d7b09
·
verified ·
1 Parent(s): 1ad9a0b

Upload folder using huggingface_hub

Browse files
6.0.2/gallery/Example.svelte ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { GalleryImage, GalleryVideo } from "./types";
3
+
4
+ export let value: (GalleryImage | GalleryVideo)[] | null;
5
+ export let type: "gallery" | "table";
6
+ export let selected = false;
7
+ </script>
8
+
9
+ <div
10
+ class="container"
11
+ class:table={type === "table"}
12
+ class:gallery={type === "gallery"}
13
+ class:selected
14
+ >
15
+ {#if value && value.length > 0}
16
+ <div class="images-wrapper">
17
+ {#each value.slice(0, 3) as item}
18
+ {#if "image" in item && item.image}
19
+ <div class="image-container">
20
+ <img src={item.image.url} alt={item.caption || ""} />
21
+ {#if item.caption}
22
+ <span class="caption">{item.caption}</span>
23
+ {/if}
24
+ </div>
25
+ {:else if "video" in item && item.video}
26
+ <div class="image-container">
27
+ <video
28
+ src={item.video.url}
29
+ controls={false}
30
+ muted
31
+ preload="metadata"
32
+ />
33
+ {#if item.caption}
34
+ <span class="caption">{item.caption}</span>
35
+ {/if}
36
+ </div>
37
+ {/if}
38
+ {/each}
39
+ {#if value.length > 3}
40
+ <div class="more-indicator">…</div>
41
+ {/if}
42
+ </div>
43
+ {/if}
44
+ </div>
45
+
46
+ <style>
47
+ .container {
48
+ border-radius: var(--radius-lg);
49
+ overflow: hidden;
50
+ }
51
+
52
+ .container.selected {
53
+ border: 2px solid var(--border-color-accent);
54
+ }
55
+
56
+ .images-wrapper {
57
+ display: flex;
58
+ gap: var(--spacing-sm);
59
+ }
60
+
61
+ .container.table .images-wrapper {
62
+ flex-direction: row;
63
+ align-items: center;
64
+ padding: var(--spacing-sm);
65
+ border: 1px solid var(--border-color-primary);
66
+ border-radius: var(--radius-lg);
67
+ background: var(--background-fill-secondary);
68
+ }
69
+
70
+ .container.gallery .images-wrapper {
71
+ flex-direction: row;
72
+ gap: 0;
73
+ }
74
+
75
+ .image-container {
76
+ position: relative;
77
+ flex-shrink: 0;
78
+ }
79
+
80
+ .container.table .image-container {
81
+ width: var(--size-12);
82
+ height: var(--size-12);
83
+ }
84
+
85
+ .container.gallery .image-container {
86
+ width: var(--size-20);
87
+ height: var(--size-20);
88
+ margin-left: calc(-1 * var(--size-8));
89
+ }
90
+
91
+ .container.gallery .image-container:first-child {
92
+ margin-left: 0;
93
+ }
94
+
95
+ .more-indicator {
96
+ display: flex;
97
+ align-items: center;
98
+ justify-content: center;
99
+ font-size: var(--text-lg);
100
+ font-weight: bold;
101
+ color: var(--border-color-primary);
102
+ }
103
+
104
+ .container.table .more-indicator {
105
+ width: var(--size-12);
106
+ height: var(--size-12);
107
+ }
108
+
109
+ .container.gallery .more-indicator {
110
+ width: var(--size-20);
111
+ height: var(--size-20);
112
+ margin-left: calc(-1 * var(--size-8));
113
+ margin-right: calc(-1 * var(--size-6));
114
+ }
115
+
116
+ .image-container img,
117
+ .image-container video {
118
+ width: 100%;
119
+ height: 100%;
120
+ object-fit: cover;
121
+ border-radius: var(--radius-md);
122
+ }
123
+
124
+ .caption {
125
+ position: absolute;
126
+ bottom: 0;
127
+ left: 0;
128
+ right: 0;
129
+ background: rgba(0, 0, 0, 0.7);
130
+ color: white;
131
+ padding: var(--spacing-xs);
132
+ font-size: var(--text-xs);
133
+ text-align: center;
134
+ border-radius: 0 0 var(--radius-md) var(--radius-md);
135
+ overflow: hidden;
136
+ text-overflow: ellipsis;
137
+ white-space: nowrap;
138
+ }
139
+
140
+ .container.table .caption {
141
+ display: none;
142
+ }
143
+ </style>
6.0.2/gallery/Index.svelte ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseGallery } from "./shared/Gallery.svelte";
3
+ export { default as BaseExample } from "./Example.svelte";
4
+ </script>
5
+
6
+ <script lang="ts">
7
+ import { tick } from "svelte";
8
+ import type { FileData } from "@gradio/client";
9
+ import { Block, UploadText } from "@gradio/atoms";
10
+ import Gallery from "./shared/Gallery.svelte";
11
+ import { StatusTracker } from "@gradio/statustracker";
12
+ import { Gradio } from "@gradio/utils";
13
+ import { BaseFileUpload } from "@gradio/file";
14
+ import type { GalleryProps, GalleryEvents, GalleryData } from "./types";
15
+
16
+ let upload_promise = $state<Promise<any>>();
17
+
18
+ class GalleryGradio extends Gradio<GalleryEvents, GalleryProps> {
19
+ async get_data() {
20
+ if (upload_promise) {
21
+ await upload_promise;
22
+ await tick();
23
+ }
24
+ const data = await super.get_data();
25
+
26
+ return data;
27
+ }
28
+ }
29
+
30
+ const props = $props();
31
+ const gradio = new GalleryGradio<GalleryEvents, GalleryProps>(props);
32
+
33
+ let fullscreen = $state(false);
34
+
35
+ let no_value = $derived(
36
+ gradio.props.value === null ? true : gradio.props.value.length === 0
37
+ );
38
+
39
+ function handle_delete(
40
+ event: CustomEvent<{ file: FileData; index: number }>
41
+ ): void {
42
+ if (!gradio.props.value) return;
43
+ const { index } = event.detail;
44
+ gradio.dispatch("delete", event.detail);
45
+ gradio.props.value = gradio.props.value.filter((_, i) => i !== index);
46
+ gradio.dispatch("change", gradio.props.value);
47
+ }
48
+
49
+ async function process_upload_files(
50
+ files: FileData[]
51
+ ): Promise<GalleryData[]> {
52
+ const processed_files = await Promise.all(
53
+ files.map(async (x) => {
54
+ if (x.path?.toLowerCase().endsWith(".svg") && x.url) {
55
+ const response = await fetch(x.url);
56
+ const svgContent = await response.text();
57
+ return {
58
+ ...x,
59
+ url: `data:image/svg+xml,${encodeURIComponent(svgContent)}`
60
+ };
61
+ }
62
+ return x;
63
+ })
64
+ );
65
+
66
+ return processed_files.map((x) =>
67
+ x.mime_type?.includes("video")
68
+ ? { video: x, caption: null }
69
+ : { image: x, caption: null }
70
+ );
71
+ }
72
+ </script>
73
+
74
+ <Block
75
+ visible={gradio.shared.visible}
76
+ variant="solid"
77
+ padding={false}
78
+ elem_id={gradio.shared.elem_id}
79
+ elem_classes={gradio.shared.elem_classes}
80
+ container={gradio.shared.container}
81
+ scale={gradio.shared.scale}
82
+ min_width={gradio.shared.min_width}
83
+ allow_overflow={false}
84
+ height={typeof gradio.props.height === "number"
85
+ ? gradio.props.height
86
+ : undefined}
87
+ bind:fullscreen
88
+ >
89
+ <StatusTracker
90
+ autoscroll={gradio.shared.autoscroll}
91
+ i18n={gradio.i18n}
92
+ {...gradio.shared.loading_status}
93
+ on_clear_status={() =>
94
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
95
+ />
96
+ {#if gradio.shared.interactive && no_value}
97
+ <BaseFileUpload
98
+ bind:upload_promise
99
+ value={null}
100
+ root={gradio.shared.root}
101
+ label={gradio.shared.label}
102
+ max_file_size={gradio.shared.max_file_size}
103
+ file_count={"multiple"}
104
+ file_types={gradio.props.file_types}
105
+ i18n={gradio.i18n}
106
+ upload={(...args) => gradio.shared.client.upload(...args)}
107
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
108
+ on:upload={async (e) => {
109
+ const files = Array.isArray(e.detail) ? e.detail : [e.detail];
110
+ gradio.props.value = await process_upload_files(files);
111
+ gradio.dispatch("upload", gradio.props.value);
112
+ gradio.dispatch("change", gradio.props.value);
113
+ }}
114
+ on:error={({ detail }) => {
115
+ gradio.shared.loading_status = gradio.shared.loading_status || {};
116
+ gradio.shared.loading_status.status = "error";
117
+ gradio.dispatch("error", detail);
118
+ }}
119
+ >
120
+ <UploadText i18n={gradio.i18n} type="gallery" />
121
+ </BaseFileUpload>
122
+ {:else}
123
+ <Gallery
124
+ on:change={() => gradio.dispatch("change")}
125
+ on:clear={() => gradio.dispatch("change")}
126
+ on:select={(e) => gradio.dispatch("select", e.detail)}
127
+ on:share={(e) => gradio.dispatch("share", e.detail)}
128
+ on:error={(e) => gradio.dispatch("error", e.detail)}
129
+ on:preview_open={() => gradio.dispatch("preview_open")}
130
+ on:preview_close={() => gradio.dispatch("preview_close")}
131
+ on:fullscreen={({ detail }) => {
132
+ fullscreen = detail;
133
+ }}
134
+ on:delete={handle_delete}
135
+ on:upload={async (e) => {
136
+ const files = Array.isArray(e.detail) ? e.detail : [e.detail];
137
+ const new_value = await process_upload_files(files);
138
+ gradio.props.value = gradio.props.value
139
+ ? [...gradio.props.value, ...new_value]
140
+ : new_value;
141
+ gradio.dispatch("upload", new_value);
142
+ gradio.dispatch("change", gradio.props.value);
143
+ }}
144
+ label={gradio.shared.label}
145
+ show_label={gradio.shared.show_label}
146
+ columns={gradio.props.columns}
147
+ rows={gradio.props.rows}
148
+ height={gradio.props.height}
149
+ preview={gradio.props.preview}
150
+ object_fit={gradio.props.object_fit}
151
+ interactive={gradio.shared.interactive}
152
+ allow_preview={gradio.props.allow_preview}
153
+ bind:selected_index={gradio.props.selected_index}
154
+ bind:value={gradio.props.value}
155
+ show_share_button={gradio.props.buttons.includes("share")}
156
+ show_download_button={gradio.props.buttons.includes("download")}
157
+ fit_columns={gradio.props.fit_columns}
158
+ i18n={gradio.i18n}
159
+ _fetch={(...args) => gradio.shared.client.fetch(...args)}
160
+ show_fullscreen_button={gradio.props.buttons.includes("fullscreen")}
161
+ {fullscreen}
162
+ root={gradio.shared.root}
163
+ file_types={gradio.props.file_types}
164
+ max_file_size={gradio.shared.max_file_size}
165
+ upload={(...args) => gradio.shared.client.upload(...args)}
166
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
167
+ />
168
+ {/if}
169
+ </Block>
6.0.2/gallery/package.json ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/gallery",
3
+ "version": "0.15.35",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/client": "workspace:^",
12
+ "@gradio/file": "workspace:^",
13
+ "@gradio/icons": "workspace:^",
14
+ "@gradio/image": "workspace:^",
15
+ "@gradio/statustracker": "workspace:^",
16
+ "@gradio/upload": "workspace:^",
17
+ "@gradio/utils": "workspace:^",
18
+ "@gradio/video": "workspace:^",
19
+ "dequal": "^2.0.3"
20
+ },
21
+ "devDependencies": {
22
+ "@gradio/preview": "workspace:^"
23
+ },
24
+ "main": "./Index.svelte",
25
+ "main_changeset": true,
26
+ "exports": {
27
+ ".": {
28
+ "gradio": "./Index.svelte",
29
+ "svelte": "./dist/Index.svelte",
30
+ "types": "./dist/Index.svelte.d.ts"
31
+ },
32
+ "./package.json": "./package.json",
33
+ "./base": {
34
+ "gradio": "./shared/Gallery.svelte",
35
+ "svelte": "./dist/shared/Gallery.svelte",
36
+ "types": "./dist/shared/Gallery.svelte.d.ts"
37
+ },
38
+ "./example": {
39
+ "gradio": "./Example.svelte",
40
+ "svelte": "./dist/Example.svelte",
41
+ "types": "./dist/Example.svelte.d.ts"
42
+ }
43
+ },
44
+ "peerDependencies": {
45
+ "svelte": "^5.43.4"
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/gradio-app/gradio.git",
50
+ "directory": "js/gallery"
51
+ }
52
+ }
6.0.2/gallery/shared/Gallery.svelte ADDED
@@ -0,0 +1,830 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import {
3
+ BlockLabel,
4
+ Empty,
5
+ ShareButton,
6
+ IconButton,
7
+ IconButtonWrapper,
8
+ FullscreenButton
9
+ } from "@gradio/atoms";
10
+ import { ModifyUpload, Upload as UploadComponent } from "@gradio/upload";
11
+ import type { SelectData } from "@gradio/utils";
12
+ import { Image } from "@gradio/image/shared";
13
+ import { Video } from "@gradio/video/shared";
14
+ import { dequal } from "dequal";
15
+ import { createEventDispatcher, onMount } from "svelte";
16
+ import { tick } from "svelte";
17
+ import type { GalleryImage, GalleryVideo } from "../types";
18
+
19
+ import {
20
+ Download,
21
+ Image as ImageIcon,
22
+ Clear,
23
+ Play,
24
+ Upload as UploadIcon
25
+ } from "@gradio/icons";
26
+ import { FileData } from "@gradio/client";
27
+ import type { Client } from "@gradio/client";
28
+ import { format_gallery_for_sharing } from "./utils";
29
+ import type { I18nFormatter } from "@gradio/utils";
30
+
31
+ type GalleryData = GalleryImage | GalleryVideo;
32
+
33
+ export let show_label = true;
34
+ export let label: string;
35
+ export let value: GalleryData[] | null = null;
36
+ export let columns: number | number[] | undefined = [2];
37
+ export let rows: number | number[] | undefined = undefined;
38
+ export let height: number | "auto" = "auto";
39
+ export let preview: boolean;
40
+ export let allow_preview = true;
41
+ export let object_fit: "contain" | "cover" | "fill" | "none" | "scale-down" =
42
+ "cover";
43
+ export let show_share_button = false;
44
+ export let show_download_button = false;
45
+ export let i18n: I18nFormatter;
46
+ export let selected_index: number | null = null;
47
+ export let interactive: boolean;
48
+ export let _fetch: typeof fetch;
49
+ export let mode: "normal" | "minimal" = "normal";
50
+ export let show_fullscreen_button = true;
51
+ export let display_icon_button_wrapper_top_corner = false;
52
+ export let fullscreen = false;
53
+ export let root = "";
54
+ export let file_types: string[] | null = ["image", "video"];
55
+ export let max_file_size: number | null = null;
56
+ export let upload: Client["upload"] | undefined = undefined;
57
+ export let stream_handler: Client["stream"] | undefined = undefined;
58
+ export let fit_columns = true;
59
+ export let upload_promise: Promise<any> | null = null;
60
+
61
+ let is_full_screen = false;
62
+ let image_container: HTMLElement;
63
+
64
+ const dispatch = createEventDispatcher<{
65
+ change: undefined;
66
+ select: SelectData;
67
+ preview_open: undefined;
68
+ preview_close: undefined;
69
+ fullscreen: boolean;
70
+ delete: { file: FileData; index: number };
71
+ upload: FileData | FileData[];
72
+ error: string;
73
+ clear: undefined;
74
+ }>();
75
+
76
+ // tracks whether the value of the gallery was reset
77
+ let was_reset = true;
78
+
79
+ $: was_reset = value == null || value.length === 0 ? true : was_reset;
80
+
81
+ let resolved_value: GalleryData[] | null = null;
82
+
83
+ $: resolved_value =
84
+ value == null
85
+ ? null
86
+ : (value.map((data) => {
87
+ if ("video" in data) {
88
+ return {
89
+ video: data.video as FileData,
90
+ caption: data.caption
91
+ };
92
+ } else if ("image" in data) {
93
+ return { image: data.image as FileData, caption: data.caption };
94
+ }
95
+ return {};
96
+ }) as GalleryData[]);
97
+
98
+ let effective_columns: number | number[] | undefined = columns;
99
+
100
+ $: {
101
+ if (resolved_value && columns && fit_columns) {
102
+ const item_count = resolved_value.length;
103
+ if (Array.isArray(columns)) {
104
+ effective_columns = columns.map((col) => Math.min(col, item_count));
105
+ } else {
106
+ effective_columns = Math.min(columns, item_count);
107
+ }
108
+ } else {
109
+ effective_columns = columns;
110
+ }
111
+ }
112
+
113
+ let prev_value: GalleryData[] | null = value;
114
+ if (selected_index == null && preview && value?.length) {
115
+ selected_index = 0;
116
+ }
117
+ let old_selected_index: number | null = selected_index;
118
+
119
+ $: if (!dequal(prev_value, value)) {
120
+ // When value is falsy (clear button or first load),
121
+ // preview determines the selected image
122
+ if (was_reset) {
123
+ selected_index = preview && value?.length ? 0 : null;
124
+ was_reset = false;
125
+ // Otherwise we keep the selected_index the same if the
126
+ // gallery has at least as many elements as it did before
127
+ } else {
128
+ if (selected_index !== null && value !== null) {
129
+ selected_index = Math.max(
130
+ 0,
131
+ Math.min(selected_index, value.length - 1)
132
+ );
133
+ } else {
134
+ selected_index = null;
135
+ }
136
+ }
137
+ dispatch("change");
138
+ prev_value = value;
139
+ }
140
+
141
+ $: previous =
142
+ ((selected_index ?? 0) + (resolved_value?.length ?? 0) - 1) %
143
+ (resolved_value?.length ?? 0);
144
+ $: next = ((selected_index ?? 0) + 1) % (resolved_value?.length ?? 0);
145
+
146
+ function handle_preview_click(event: MouseEvent): void {
147
+ const element = event.target as HTMLElement;
148
+ const x = event.offsetX;
149
+ const width = element.offsetWidth;
150
+ const centerX = width / 2;
151
+
152
+ if (x < centerX) {
153
+ selected_index = previous;
154
+ } else {
155
+ selected_index = next;
156
+ }
157
+ }
158
+
159
+ function on_keydown(e: KeyboardEvent): void {
160
+ switch (e.code) {
161
+ case "Escape":
162
+ e.preventDefault();
163
+ selected_index = null;
164
+ break;
165
+ case "ArrowLeft":
166
+ e.preventDefault();
167
+ selected_index = previous;
168
+ break;
169
+ case "ArrowRight":
170
+ e.preventDefault();
171
+ selected_index = next;
172
+ break;
173
+ default:
174
+ break;
175
+ }
176
+ }
177
+
178
+ $: {
179
+ if (selected_index !== old_selected_index) {
180
+ old_selected_index = selected_index;
181
+ if (selected_index !== null) {
182
+ if (resolved_value != null) {
183
+ selected_index = Math.max(
184
+ 0,
185
+ Math.min(selected_index, resolved_value.length - 1)
186
+ );
187
+ }
188
+ dispatch("select", {
189
+ index: selected_index,
190
+ value: resolved_value?.[selected_index]
191
+ });
192
+ }
193
+ }
194
+ }
195
+
196
+ $: if (allow_preview) {
197
+ scroll_to_img(selected_index);
198
+ }
199
+
200
+ let el: HTMLButtonElement[] = [];
201
+ let container_element: HTMLDivElement;
202
+
203
+ async function scroll_to_img(index: number | null): Promise<void> {
204
+ if (typeof index !== "number") return;
205
+ await tick();
206
+
207
+ if (el[index] === undefined) return;
208
+
209
+ el[index]?.focus();
210
+
211
+ const { left: container_left, width: container_width } =
212
+ container_element.getBoundingClientRect();
213
+ const { left, width } = el[index].getBoundingClientRect();
214
+
215
+ const relative_left = left - container_left;
216
+
217
+ const pos =
218
+ relative_left +
219
+ width / 2 -
220
+ container_width / 2 +
221
+ container_element.scrollLeft;
222
+
223
+ if (container_element && typeof container_element.scrollTo === "function") {
224
+ container_element.scrollTo({
225
+ left: pos < 0 ? 0 : pos,
226
+ behavior: "smooth"
227
+ });
228
+ }
229
+ }
230
+
231
+ let window_height = 0;
232
+
233
+ // Unlike `gr.Image()`, images specified via remote URLs are not cached in the server
234
+ // and their remote URLs are directly passed to the client as `value[].image.url`.
235
+ // The `download` attribute of the <a> tag doesn't work for remote URLs (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#download),
236
+ // so we need to download the image via JS as below.
237
+ async function download(file_url: string, name: string): Promise<void> {
238
+ let response;
239
+ try {
240
+ response = await _fetch(file_url);
241
+ } catch (error) {
242
+ if (error instanceof TypeError) {
243
+ // If CORS is not allowed (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful),
244
+ // open the link in a new tab instead, mimicing the behavior of the `download` attribute for remote URLs,
245
+ // which is not ideal, but a reasonable fallback.
246
+ window.open(file_url, "_blank", "noreferrer");
247
+ return;
248
+ }
249
+
250
+ throw error;
251
+ }
252
+ const blob = await response.blob();
253
+ const url = URL.createObjectURL(blob);
254
+ const link = document.createElement("a");
255
+ link.href = url;
256
+ link.download = name;
257
+ link.click();
258
+ URL.revokeObjectURL(url);
259
+ }
260
+
261
+ $: selected_media =
262
+ selected_index != null && resolved_value != null
263
+ ? resolved_value[selected_index]
264
+ : null;
265
+
266
+ let thumbnails_overflow = false;
267
+
268
+ function check_thumbnails_overflow(): void {
269
+ if (container_element) {
270
+ thumbnails_overflow =
271
+ container_element.scrollWidth > container_element.clientWidth;
272
+ }
273
+ }
274
+
275
+ onMount(() => {
276
+ check_thumbnails_overflow();
277
+ document.addEventListener("fullscreenchange", () => {
278
+ is_full_screen = !!document.fullscreenElement;
279
+ });
280
+ window.addEventListener("resize", check_thumbnails_overflow);
281
+ return () =>
282
+ window.removeEventListener("resize", check_thumbnails_overflow);
283
+ });
284
+
285
+ $: (resolved_value, check_thumbnails_overflow());
286
+ $: if (container_element) {
287
+ check_thumbnails_overflow();
288
+ }
289
+
290
+ function handle_item_delete(index: number): void {
291
+ if (!value || !resolved_value) return;
292
+
293
+ const deleted_item = resolved_value[index];
294
+ let deleted_file_data;
295
+
296
+ if ("image" in deleted_item) {
297
+ deleted_file_data = {
298
+ file: deleted_item.image,
299
+ index: index
300
+ };
301
+ } else if ("video" in deleted_item) {
302
+ deleted_file_data = {
303
+ file: deleted_item.video,
304
+ index: index
305
+ };
306
+ }
307
+
308
+ if (deleted_file_data) {
309
+ dispatch("delete", deleted_file_data);
310
+ }
311
+ }
312
+
313
+ let uploading = false;
314
+ </script>
315
+
316
+ <svelte:window bind:innerHeight={window_height} />
317
+
318
+ {#if show_label}
319
+ <BlockLabel {show_label} Icon={ImageIcon} label={label || "Gallery"} />
320
+ {/if}
321
+ {#if value == null || resolved_value == null || resolved_value.length === 0}
322
+ <Empty unpadded_box={true} size="large"><ImageIcon /></Empty>
323
+ {:else}
324
+ <div class="gallery-container" bind:this={image_container}>
325
+ {#if selected_media && allow_preview}
326
+ <span
327
+ on:keydown={on_keydown}
328
+ class="preview"
329
+ class:minimal={mode === "minimal"}
330
+ >
331
+ <IconButtonWrapper
332
+ display_top_corner={display_icon_button_wrapper_top_corner}
333
+ >
334
+ {#if show_download_button}
335
+ <IconButton
336
+ Icon={Download}
337
+ label={i18n("common.download")}
338
+ on:click={() => {
339
+ const image =
340
+ "image" in selected_media
341
+ ? selected_media?.image
342
+ : selected_media?.video;
343
+ if (image == null) {
344
+ return;
345
+ }
346
+ const { url, orig_name } = image;
347
+ if (url) {
348
+ download(url, orig_name ?? "image");
349
+ }
350
+ }}
351
+ />
352
+ {/if}
353
+
354
+ {#if show_fullscreen_button}
355
+ <FullscreenButton {fullscreen} on:fullscreen />
356
+ {/if}
357
+
358
+ {#if show_share_button}
359
+ <div class="icon-button">
360
+ <ShareButton
361
+ {i18n}
362
+ on:share
363
+ on:error
364
+ value={resolved_value}
365
+ formatter={format_gallery_for_sharing}
366
+ />
367
+ </div>
368
+ {/if}
369
+ {#if !is_full_screen}
370
+ <IconButton
371
+ Icon={Clear}
372
+ label="Close"
373
+ on:click={() => {
374
+ selected_index = null;
375
+ dispatch("preview_close");
376
+ }}
377
+ />
378
+ {/if}
379
+ </IconButtonWrapper>
380
+ <button
381
+ class="media-button"
382
+ on:click={"image" in selected_media
383
+ ? (event) => handle_preview_click(event)
384
+ : null}
385
+ style="height: calc(100% - {selected_media.caption
386
+ ? '80px'
387
+ : '60px'})"
388
+ aria-label="detailed view of selected image"
389
+ >
390
+ {#if "image" in selected_media}
391
+ <Image
392
+ restProps={{
393
+ alt: selected_media.caption || "",
394
+ title: selected_media.caption || null,
395
+ class: selected_media.caption && "with-caption",
396
+ loading: "lazy"
397
+ }}
398
+ src={selected_media.image.url}
399
+ data_testid="detailed-image"
400
+ />
401
+ {:else}
402
+ <Video
403
+ src={selected_media.video.url}
404
+ data-testid={"detailed-video"}
405
+ alt={selected_media.caption || ""}
406
+ loading="lazy"
407
+ loop={false}
408
+ is_stream={false}
409
+ muted={false}
410
+ controls={true}
411
+ />
412
+ {/if}
413
+ </button>
414
+ {#if selected_media?.caption}
415
+ <caption class="caption">
416
+ {selected_media.caption}
417
+ </caption>
418
+ {/if}
419
+ <div
420
+ bind:this={container_element}
421
+ class="thumbnails scroll-hide"
422
+ data-testid="container_el"
423
+ style="justify-content: {thumbnails_overflow
424
+ ? 'flex-start'
425
+ : 'center'};"
426
+ >
427
+ {#each resolved_value as media, i}
428
+ <button
429
+ bind:this={el[i]}
430
+ on:click={() => (selected_index = i)}
431
+ class="thumbnail-item thumbnail-small"
432
+ class:selected={selected_index === i && mode !== "minimal"}
433
+ aria-label={"Thumbnail " +
434
+ (i + 1) +
435
+ " of " +
436
+ resolved_value.length}
437
+ >
438
+ {#if "image" in media}
439
+ <Image
440
+ src={media.image.url}
441
+ restProps={{
442
+ title: media.caption || null,
443
+ alt: "",
444
+ class: "with-caption",
445
+ loading: "lazy"
446
+ }}
447
+ data_testid={`thumbnail ${i + 1}`}
448
+ />
449
+ {:else}
450
+ <Play />
451
+ <Video
452
+ src={media.video.url}
453
+ title={media.caption || null}
454
+ is_stream={false}
455
+ data-testid={"thumbnail " + (i + 1)}
456
+ alt=""
457
+ loading="lazy"
458
+ loop={false}
459
+ />
460
+ {/if}
461
+ </button>
462
+ {/each}
463
+ </div>
464
+ </span>
465
+ {/if}
466
+
467
+ <div
468
+ class="grid-wrap"
469
+ class:minimal={mode === "minimal"}
470
+ class:fixed-height={mode !== "minimal" && (!height || height == "auto")}
471
+ class:hidden={is_full_screen}
472
+ style:height={height !== "auto" ? height + "px" : null}
473
+ >
474
+ {#if interactive && selected_index === null}
475
+ <ModifyUpload
476
+ {i18n}
477
+ on:clear={() => {
478
+ value = [];
479
+ dispatch("clear");
480
+ }}
481
+ >
482
+ {#if upload && stream_handler}
483
+ <IconButton Icon={UploadIcon} label={i18n("common.upload")}>
484
+ <UploadComponent
485
+ bind:upload_promise
486
+ icon_upload={true}
487
+ on:load={(e) => dispatch("upload", e.detail)}
488
+ filetype={file_types}
489
+ file_count="multiple"
490
+ {max_file_size}
491
+ {root}
492
+ bind:uploading
493
+ on:error={(e) => dispatch("error", e.detail)}
494
+ {stream_handler}
495
+ {upload}
496
+ />
497
+ </IconButton>
498
+ {/if}
499
+ </ModifyUpload>
500
+ {/if}
501
+ <div
502
+ class="grid-container"
503
+ style="--grid-cols:{effective_columns}; --grid-rows:{rows}; --object-fit: {object_fit};"
504
+ class:pt-6={show_label}
505
+ >
506
+ {#each resolved_value as entry, i}
507
+ <div class="gallery-item">
508
+ <button
509
+ class="thumbnail-item thumbnail-lg"
510
+ class:selected={selected_index === i}
511
+ on:click={() => {
512
+ if (selected_index === null && allow_preview) {
513
+ dispatch("preview_open");
514
+ }
515
+ selected_index = i;
516
+ }}
517
+ aria-label={"Thumbnail " +
518
+ (i + 1) +
519
+ " of " +
520
+ resolved_value.length}
521
+ >
522
+ {#if "image" in entry}
523
+ <Image
524
+ alt={entry.caption || ""}
525
+ src={typeof entry.image === "string"
526
+ ? entry.image
527
+ : entry.image.url}
528
+ loading="lazy"
529
+ />
530
+ {:else}
531
+ <Play />
532
+ <Video
533
+ src={entry.video.url}
534
+ title={entry.caption || null}
535
+ is_stream={false}
536
+ data-testid={"thumbnail " + (i + 1)}
537
+ alt=""
538
+ loading="lazy"
539
+ loop={false}
540
+ />
541
+ {/if}
542
+ {#if entry.caption}
543
+ <div class="caption-label">
544
+ {entry.caption}
545
+ </div>
546
+ {/if}
547
+ </button>
548
+ {#if interactive}
549
+ <button
550
+ class="delete-button"
551
+ on:click|stopPropagation={() => handle_item_delete(i)}
552
+ aria-label="Delete image"
553
+ >
554
+ <Clear />
555
+ </button>
556
+ {/if}
557
+ </div>
558
+ {/each}
559
+ </div>
560
+ </div>
561
+ </div>
562
+ {/if}
563
+
564
+ <style lang="postcss">
565
+ .image-container {
566
+ height: 100%;
567
+ position: relative;
568
+ }
569
+ .image-container :global(img),
570
+ button {
571
+ width: var(--size-full);
572
+ height: var(--size-full);
573
+ object-fit: contain;
574
+ display: block;
575
+ border-radius: var(--radius-lg);
576
+ }
577
+
578
+ .preview {
579
+ display: flex;
580
+ position: absolute;
581
+ flex-direction: column;
582
+ z-index: var(--layer-2);
583
+ border-radius: calc(var(--block-radius) - var(--block-border-width));
584
+ -webkit-backdrop-filter: blur(8px);
585
+ backdrop-filter: blur(8px);
586
+ width: var(--size-full);
587
+ height: var(--size-full);
588
+ }
589
+
590
+ .preview.minimal {
591
+ width: fit-content;
592
+ height: fit-content;
593
+ }
594
+
595
+ .preview::before {
596
+ content: "";
597
+ position: absolute;
598
+ z-index: var(--layer-below);
599
+ background: var(--background-fill-primary);
600
+ opacity: 0.9;
601
+ width: var(--size-full);
602
+ height: var(--size-full);
603
+ }
604
+
605
+ .fixed-height {
606
+ min-height: var(--size-80);
607
+ max-height: 55vh;
608
+ }
609
+
610
+ @media (--screen-xl) {
611
+ .fixed-height {
612
+ min-height: 450px;
613
+ }
614
+ }
615
+
616
+ .media-button {
617
+ height: calc(100% - 60px);
618
+ width: 100%;
619
+ display: flex;
620
+ }
621
+ .media-button :global(img),
622
+ .media-button :global(video) {
623
+ width: var(--size-full);
624
+ height: var(--size-full);
625
+ object-fit: contain;
626
+ }
627
+ .thumbnails :global(img) {
628
+ object-fit: cover;
629
+ width: var(--size-full);
630
+ height: var(--size-full);
631
+ }
632
+ .thumbnails :global(svg) {
633
+ position: absolute;
634
+ top: var(--size-2);
635
+ left: var(--size-2);
636
+ width: 50%;
637
+ height: 50%;
638
+ opacity: 50%;
639
+ }
640
+ .preview :global(img.with-caption) {
641
+ height: var(--size-full);
642
+ }
643
+
644
+ .preview.minimal :global(img.with-caption) {
645
+ height: auto;
646
+ }
647
+
648
+ .selectable {
649
+ cursor: crosshair;
650
+ }
651
+
652
+ .caption {
653
+ padding: var(--size-2) var(--size-3);
654
+ overflow: hidden;
655
+ color: var(--block-label-text-color);
656
+ font-weight: var(--weight-semibold);
657
+ text-align: center;
658
+ text-overflow: ellipsis;
659
+ white-space: nowrap;
660
+ align-self: center;
661
+ }
662
+
663
+ .thumbnails {
664
+ display: flex;
665
+ position: absolute;
666
+ bottom: 0;
667
+ justify-content: flex-start;
668
+ align-items: center;
669
+ gap: var(--spacing-lg);
670
+ width: var(--size-full);
671
+ height: var(--size-14);
672
+ overflow-x: scroll;
673
+ }
674
+
675
+ .thumbnail-item {
676
+ --ring-color: transparent;
677
+ position: relative;
678
+ box-shadow:
679
+ inset 0 0 0 1px var(--ring-color),
680
+ var(--shadow-drop);
681
+ border: 1px solid var(--border-color-primary);
682
+ border-radius: var(--button-small-radius);
683
+ background: var(--background-fill-secondary);
684
+ aspect-ratio: var(--ratio-square);
685
+ width: var(--size-full);
686
+ height: var(--size-full);
687
+ overflow: clip;
688
+ }
689
+
690
+ .thumbnail-item:hover {
691
+ --ring-color: var(--color-accent);
692
+ border-color: var(--color-accent);
693
+ filter: brightness(1.1);
694
+ }
695
+
696
+ .thumbnail-item.selected {
697
+ --ring-color: var(--color-accent);
698
+ border-color: var(--color-accent);
699
+ }
700
+
701
+ .thumbnail-item :global(svg) {
702
+ position: absolute;
703
+ top: 50%;
704
+ left: 50%;
705
+ width: 50%;
706
+ height: 50%;
707
+ opacity: 50%;
708
+ transform: translate(-50%, -50%);
709
+ }
710
+
711
+ .thumbnail-item :global(video) {
712
+ width: var(--size-full);
713
+ height: var(--size-full);
714
+ overflow: hidden;
715
+ object-fit: cover;
716
+ }
717
+
718
+ .thumbnail-small {
719
+ flex: none;
720
+ transform: scale(0.9);
721
+ transition: 0.075s;
722
+ width: var(--size-9);
723
+ height: var(--size-9);
724
+ }
725
+ .thumbnail-small.selected {
726
+ --ring-color: var(--color-accent);
727
+ transform: scale(1);
728
+ border-color: var(--color-accent);
729
+ }
730
+
731
+ .thumbnail-small > img {
732
+ width: var(--size-full);
733
+ height: var(--size-full);
734
+ overflow: hidden;
735
+ object-fit: var(--object-fit);
736
+ }
737
+
738
+ .grid-wrap {
739
+ position: relative;
740
+ padding: var(--size-2);
741
+ overflow-y: scroll;
742
+ }
743
+
744
+ .grid-container {
745
+ display: grid;
746
+ position: relative;
747
+ grid-template-rows: repeat(var(--grid-rows), minmax(100px, 1fr));
748
+ grid-template-columns: repeat(var(--grid-cols), minmax(100px, 1fr));
749
+ grid-auto-rows: minmax(100px, 1fr);
750
+ gap: var(--spacing-lg);
751
+ }
752
+
753
+ .thumbnail-lg > :global(img) {
754
+ width: var(--size-full);
755
+ height: var(--size-full);
756
+ overflow: hidden;
757
+ object-fit: var(--object-fit);
758
+ }
759
+
760
+ .thumbnail-lg:hover .caption-label {
761
+ opacity: 0.5;
762
+ }
763
+
764
+ .caption-label {
765
+ position: absolute;
766
+ right: var(--block-label-margin);
767
+ bottom: var(--block-label-margin);
768
+ z-index: var(--layer-1);
769
+ border-top: 1px solid var(--border-color-primary);
770
+ border-left: 1px solid var(--border-color-primary);
771
+ border-radius: var(--block-label-radius);
772
+ background: var(--background-fill-secondary);
773
+ padding: var(--block-label-padding);
774
+ max-width: 80%;
775
+ overflow: hidden;
776
+ font-size: var(--block-label-text-size);
777
+ text-align: left;
778
+ text-overflow: ellipsis;
779
+ white-space: nowrap;
780
+ }
781
+
782
+ .grid-wrap.minimal {
783
+ padding: 0;
784
+ }
785
+
786
+ .gallery-item {
787
+ position: relative;
788
+ width: 100%;
789
+ height: 100%;
790
+ }
791
+
792
+ .delete-button {
793
+ position: absolute;
794
+ bottom: 0;
795
+ left: 0;
796
+ z-index: var(--layer-1);
797
+ border-top: 1px solid var(--border-color-primary);
798
+ border-right: 1px solid var(--border-color-primary);
799
+ border-radius: 0 var(--radius-sm) 0 var(--radius-sm);
800
+ background: var(--background-fill-secondary);
801
+ padding: var(--block-label-padding);
802
+ cursor: pointer;
803
+ display: flex;
804
+ align-items: center;
805
+ justify-content: center;
806
+ opacity: 0;
807
+ transition: opacity 0.2s ease;
808
+ font-size: var(--block-label-text-size);
809
+ color: var(--block-label-text-color);
810
+ font-weight: var(--weight-semibold);
811
+ width: auto;
812
+ height: auto;
813
+ min-width: fit-content;
814
+ min-height: fit-content;
815
+ }
816
+
817
+ .gallery-item:hover .delete-button {
818
+ opacity: 1;
819
+ }
820
+
821
+ .delete-button:hover {
822
+ opacity: 0.8;
823
+ }
824
+
825
+ .delete-button :global(svg) {
826
+ width: var(--text-xs);
827
+ height: var(--text-md);
828
+ color: var(--block-label-text-color);
829
+ }
830
+ </style>
6.0.2/gallery/shared/utils.ts ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { uploadToHuggingFace } from "@gradio/utils";
2
+ import type { FileData } from "@gradio/client";
3
+
4
+ export async function format_gallery_for_sharing(
5
+ value: [FileData, string | null][] | null
6
+ ): Promise<string> {
7
+ if (!value) return "";
8
+ let urls = await Promise.all(
9
+ value.map(async ([image, _]) => {
10
+ if (image === null || !image.url) return "";
11
+ return await uploadToHuggingFace(image.url, "url");
12
+ })
13
+ );
14
+
15
+ return `<div style="display: flex; flex-wrap: wrap; gap: 16px">${urls
16
+ .map((url) => `<img src="${url}" style="height: 400px" />`)
17
+ .join("")}</div>`;
18
+ }
6.0.2/gallery/types.ts ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData, SelectData } from "@gradio/client";
2
+
3
+ export interface GalleryImage {
4
+ image: FileData;
5
+ caption: string | null;
6
+ }
7
+
8
+ export interface GalleryVideo {
9
+ video: FileData;
10
+ caption: string | null;
11
+ }
12
+
13
+ export type GalleryData = GalleryImage | GalleryVideo;
14
+
15
+ export interface GalleryProps {
16
+ value: GalleryData[] | null;
17
+ file_types: string[] | null;
18
+ columns: number | number[] | undefined;
19
+ rows: number | number[] | undefined;
20
+ height: number | "auto";
21
+ preview: boolean;
22
+ allow_preview: boolean;
23
+ selected_index: number | null;
24
+ object_fit: "contain" | "cover" | "fill" | "none" | "scale-down";
25
+ buttons: string[];
26
+ type: "numpy" | "pil" | "filepath";
27
+ fit_columns: boolean;
28
+ }
29
+
30
+ export interface GalleryEvents {
31
+ change: GalleryData[] | null;
32
+ upload: GalleryData[] | null;
33
+ select: SelectData;
34
+ delete: { file: FileData; index: number };
35
+ preview_open: never;
36
+ preview_close: never;
37
+ clear_status: any;
38
+ share: any;
39
+ error: any;
40
+ }