gradio-pr-bot commited on
Commit
4fa3179
·
verified ·
1 Parent(s): ff7791e

Upload folder using huggingface_hub

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