gradio-pr-bot commited on
Commit
2ffa944
·
verified ·
1 Parent(s): e20c452

Upload folder using huggingface_hub

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