gradio-pr-bot commited on
Commit
efc8f6d
·
verified ·
1 Parent(s): 22ac372

Upload folder using huggingface_hub

Browse files
6.9.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.9.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.9.1/gallery/package.json ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/gallery",
3
+ "version": "0.17.3",
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.9.1/gallery/shared/Gallery.svelte ADDED
@@ -0,0 +1,935 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ if (selected_index !== old_selected_index) {
233
+ old_selected_index = selected_index;
234
+ if (selected_index !== null) {
235
+ if (resolved_value != null) {
236
+ selected_index = Math.max(
237
+ 0,
238
+ Math.min(selected_index, resolved_value.length - 1)
239
+ );
240
+ }
241
+ onselect({
242
+ index: selected_index,
243
+ value: resolved_value?.[selected_index]
244
+ });
245
+ }
246
+ }
247
+ });
248
+
249
+ $effect(() => {
250
+ if (allow_preview && container_element) {
251
+ scroll_to_img(selected_index);
252
+ }
253
+ });
254
+
255
+ let el: HTMLButtonElement[] = [];
256
+ let container_element: HTMLDivElement | undefined = $state();
257
+
258
+ async function scroll_to_img(index: number | null): Promise<void> {
259
+ if (typeof index !== "number") return;
260
+ await tick();
261
+
262
+ if (el[index] === undefined) return;
263
+ if (!container_element) return;
264
+
265
+ el[index]?.focus();
266
+
267
+ const { left: container_left, width: container_width } =
268
+ container_element.getBoundingClientRect();
269
+ const { left, width } = el[index].getBoundingClientRect();
270
+
271
+ const relative_left = left - container_left;
272
+
273
+ const pos =
274
+ relative_left +
275
+ width / 2 -
276
+ container_width / 2 +
277
+ container_element.scrollLeft;
278
+
279
+ if (container_element && typeof container_element.scrollTo === "function") {
280
+ container_element.scrollTo({
281
+ left: pos < 0 ? 0 : pos,
282
+ behavior: "smooth"
283
+ });
284
+ }
285
+ }
286
+
287
+ let window_height = 0;
288
+
289
+ // Unlike `gr.Image()`, images specified via remote URLs are not cached in the server
290
+ // and their remote URLs are directly passed to the client as `value[].image.url`.
291
+ // 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),
292
+ // so we need to download the image via JS as below.
293
+ async function download(file_url: string, name: string): Promise<void> {
294
+ let response;
295
+ try {
296
+ response = await _fetch(file_url);
297
+ } catch (error) {
298
+ if (error instanceof TypeError) {
299
+ // If CORS is not allowed (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful),
300
+ // open the link in a new tab instead, mimicing the behavior of the `download` attribute for remote URLs,
301
+ // which is not ideal, but a reasonable fallback.
302
+ window.open(file_url, "_blank", "noreferrer");
303
+ return;
304
+ }
305
+
306
+ throw error;
307
+ }
308
+ const blob = await response.blob();
309
+ const url = URL.createObjectURL(blob);
310
+ const link = document.createElement("a");
311
+ link.href = url;
312
+ link.download = name;
313
+ link.click();
314
+ URL.revokeObjectURL(url);
315
+ }
316
+
317
+ let selected_media = $derived.by(() =>
318
+ selected_index != null && resolved_value != null
319
+ ? resolved_value[selected_index]
320
+ : null
321
+ );
322
+
323
+ let thumbnails_overflow = false;
324
+
325
+ function check_thumbnails_overflow(): void {
326
+ if (container_element) {
327
+ thumbnails_overflow =
328
+ container_element.scrollWidth > container_element.clientWidth;
329
+ }
330
+ }
331
+
332
+ onMount(() => {
333
+ check_thumbnails_overflow();
334
+ document.addEventListener("fullscreenchange", () => {
335
+ is_full_screen = !!document.fullscreenElement;
336
+ });
337
+ window.addEventListener("resize", check_thumbnails_overflow);
338
+ return () =>
339
+ window.removeEventListener("resize", check_thumbnails_overflow);
340
+ });
341
+
342
+ $effect(() => {
343
+ resolved_value;
344
+ check_thumbnails_overflow();
345
+ if (container_element) {
346
+ check_thumbnails_overflow();
347
+ }
348
+ });
349
+
350
+ function handle_item_delete(index: number): void {
351
+ if (!value || !resolved_value) return;
352
+
353
+ const deleted_item = resolved_value[index];
354
+ let deleted_file_data;
355
+
356
+ if ("image" in deleted_item) {
357
+ deleted_file_data = {
358
+ file: deleted_item.image,
359
+ index: index
360
+ };
361
+ } else if ("video" in deleted_item) {
362
+ deleted_file_data = {
363
+ file: deleted_item.video,
364
+ index: index
365
+ };
366
+ }
367
+
368
+ if (deleted_file_data) {
369
+ ondelete(deleted_file_data);
370
+ }
371
+ }
372
+
373
+ let uploading = false;
374
+ </script>
375
+
376
+ <svelte:window bind:innerHeight={window_height} />
377
+
378
+ {#if show_label}
379
+ <BlockLabel {show_label} Icon={ImageIcon} label={label || "Gallery"} />
380
+ {/if}
381
+ {#if value == null || resolved_value == null || resolved_value.length === 0}
382
+ <Empty unpadded_box={true} size="large"><ImageIcon /></Empty>
383
+ {:else}
384
+ <div class="gallery-container" bind:this={image_container}>
385
+ {#if selected_media && allow_preview}
386
+ <span
387
+ on:keydown={on_keydown}
388
+ class="preview"
389
+ class:minimal={mode === "minimal"}
390
+ >
391
+ <IconButtonWrapper
392
+ display_top_corner={display_icon_button_wrapper_top_corner}
393
+ {buttons}
394
+ on_custom_button_click={oncustom_button_click}
395
+ >
396
+ {#if show_download_button}
397
+ <IconButton
398
+ Icon={Download}
399
+ label={i18n("common.download")}
400
+ onclick={() => {
401
+ const image =
402
+ "image" in selected_media
403
+ ? selected_media?.image
404
+ : selected_media?.video;
405
+ if (image == null) {
406
+ return;
407
+ }
408
+ const { url, orig_name } = image;
409
+ if (url) {
410
+ download(url, orig_name ?? "image");
411
+ }
412
+ }}
413
+ />
414
+ {/if}
415
+
416
+ {#if show_fullscreen_button}
417
+ <FullscreenButton
418
+ {fullscreen}
419
+ onclick={(is_fullscreen) =>
420
+ onfullscreen({ detail: is_fullscreen })}
421
+ />
422
+ {/if}
423
+
424
+ {#if show_share_button}
425
+ <div class="icon-button">
426
+ <ShareButton
427
+ {i18n}
428
+ on:share={(detail) => {
429
+ onshare(detail);
430
+ }}
431
+ on:error={(detail) => {
432
+ onerror(detail);
433
+ }}
434
+ value={resolved_value}
435
+ formatter={format_gallery_for_sharing}
436
+ />
437
+ </div>
438
+ {/if}
439
+ {#if !is_full_screen}
440
+ <IconButton
441
+ Icon={Clear}
442
+ label="Close"
443
+ onclick={() => {
444
+ selected_index = null;
445
+ onpreview_close();
446
+ }}
447
+ />
448
+ {/if}
449
+ </IconButtonWrapper>
450
+ <button
451
+ class="media-button"
452
+ on:click={"image" in selected_media
453
+ ? (event) => handle_preview_click(event)
454
+ : null}
455
+ style="height: calc(100% - {selected_media.caption
456
+ ? '80px'
457
+ : '60px'})"
458
+ aria-label="detailed view of selected image"
459
+ >
460
+ {#if "image" in selected_media}
461
+ <Image
462
+ restProps={{
463
+ alt: selected_media.caption || "",
464
+ title: selected_media.caption || null,
465
+ class: selected_media.caption && "with-caption",
466
+ loading: "lazy"
467
+ }}
468
+ src={selected_media.image.url}
469
+ data_testid="detailed-image"
470
+ />
471
+ {:else}
472
+ <Video
473
+ src={selected_media.video.url}
474
+ data-testid={"detailed-video"}
475
+ alt={selected_media.caption || ""}
476
+ loading="lazy"
477
+ loop={false}
478
+ is_stream={false}
479
+ muted={false}
480
+ controls={true}
481
+ />
482
+ {/if}
483
+ </button>
484
+ {#if selected_media?.caption}
485
+ <caption class="caption">
486
+ {selected_media.caption}
487
+ </caption>
488
+ {/if}
489
+ <div
490
+ bind:this={container_element}
491
+ class="thumbnails scroll-hide"
492
+ data-testid="container_el"
493
+ style="justify-content: {thumbnails_overflow
494
+ ? 'flex-start'
495
+ : 'center'};"
496
+ >
497
+ {#each resolved_value as media, i}
498
+ <button
499
+ bind:this={el[i]}
500
+ on:click={() => (selected_index = i)}
501
+ class="thumbnail-item thumbnail-small"
502
+ class:selected={selected_index === i && mode !== "minimal"}
503
+ aria-label={"Thumbnail " +
504
+ (i + 1) +
505
+ " of " +
506
+ resolved_value.length}
507
+ >
508
+ {#if "image" in media}
509
+ <Image
510
+ src={media.image.url}
511
+ restProps={{
512
+ title: media.caption || null,
513
+ alt: "",
514
+ class: "with-caption",
515
+ loading: "lazy"
516
+ }}
517
+ data_testid={`thumbnail ${i + 1}`}
518
+ />
519
+ {:else}
520
+ <Play />
521
+ <Video
522
+ src={media.video.url}
523
+ title={media.caption || null}
524
+ is_stream={false}
525
+ data-testid={"thumbnail " + (i + 1)}
526
+ alt=""
527
+ loading="lazy"
528
+ loop={false}
529
+ />
530
+ {/if}
531
+ </button>
532
+ {/each}
533
+ </div>
534
+ </span>
535
+ {/if}
536
+
537
+ <div
538
+ class="grid-wrap"
539
+ class:minimal={mode === "minimal"}
540
+ class:fixed-height={mode !== "minimal" && (!height || height == "auto")}
541
+ class:hidden={is_full_screen}
542
+ style:height={height !== "auto"
543
+ ? typeof height === "number"
544
+ ? height + "px"
545
+ : height
546
+ : null}
547
+ >
548
+ {#if interactive && selected_index === null}
549
+ <ModifyUpload
550
+ {i18n}
551
+ onclear={() => {
552
+ value = [];
553
+ onsource_change("upload");
554
+ onclear();
555
+ }}
556
+ >
557
+ {#if upload && stream_handler}
558
+ <IconButton
559
+ Icon={UploadIcon}
560
+ label={i18n("upload_text.click_to_upload")}
561
+ >
562
+ <UploadComponent
563
+ bind:upload_promise
564
+ icon_upload={true}
565
+ onload={(e) => onupload(e)}
566
+ filetype={file_types}
567
+ file_count="multiple"
568
+ {max_file_size}
569
+ {root}
570
+ bind:uploading
571
+ onerror={(e) => onerror(e)}
572
+ {stream_handler}
573
+ {upload}
574
+ />
575
+ </IconButton>
576
+ {/if}
577
+ {#if sources.includes("webcam")}
578
+ <IconButton
579
+ Icon={Webcam}
580
+ label={i18n("common.webcam")}
581
+ onclick={() => {
582
+ onsource_change("webcam");
583
+ }}
584
+ />
585
+ {/if}
586
+ {#if sources.includes("webcam-video")}
587
+ <IconButton
588
+ Icon={VideoIcon}
589
+ label={i18n("common.video")}
590
+ onclick={() => {
591
+ onsource_change("webcam-video");
592
+ }}
593
+ />
594
+ {/if}
595
+ {#if sources.includes("clipboard")}
596
+ <IconButton
597
+ label={i18n("upload_text.paste_clipboard")}
598
+ onclick={() => {
599
+ onsource_change("clipboard");
600
+ }}
601
+ Icon={ImagePaste}
602
+ />
603
+ {/if}
604
+ </ModifyUpload>
605
+ {/if}
606
+ <div
607
+ class="grid-container"
608
+ style="--grid-cols:{effective_columns}; --grid-rows:{rows}; --object-fit: {object_fit};"
609
+ class:pt-6={show_label}
610
+ >
611
+ {#each resolved_value as entry, i}
612
+ <div class="gallery-item">
613
+ <button
614
+ class="thumbnail-item thumbnail-lg"
615
+ class:selected={selected_index === i}
616
+ on:click={() => {
617
+ if (selected_index === null && allow_preview) {
618
+ onpreview_open();
619
+ }
620
+ selected_index = i;
621
+ }}
622
+ aria-label={"Thumbnail " +
623
+ (i + 1) +
624
+ " of " +
625
+ resolved_value.length}
626
+ >
627
+ {#if "image" in entry}
628
+ <Image
629
+ alt={entry.caption || ""}
630
+ src={typeof entry.image === "string"
631
+ ? entry.image
632
+ : entry.image.url}
633
+ loading="lazy"
634
+ />
635
+ {:else}
636
+ <Play />
637
+ <Video
638
+ src={entry.video.url}
639
+ title={entry.caption || null}
640
+ is_stream={false}
641
+ data-testid={"thumbnail " + (i + 1)}
642
+ alt=""
643
+ loading="lazy"
644
+ loop={false}
645
+ />
646
+ {/if}
647
+ {#if entry.caption}
648
+ <div class="caption-label">
649
+ {entry.caption}
650
+ </div>
651
+ {/if}
652
+ </button>
653
+ {#if interactive}
654
+ <button
655
+ class="delete-button"
656
+ on:click|stopPropagation={() => handle_item_delete(i)}
657
+ aria-label="Delete image"
658
+ >
659
+ <Clear />
660
+ </button>
661
+ {/if}
662
+ </div>
663
+ {/each}
664
+ </div>
665
+ </div>
666
+ </div>
667
+ {/if}
668
+
669
+ <style lang="postcss">
670
+ .gallery-container {
671
+ height: 100%;
672
+ position: relative;
673
+ }
674
+ .gallery-container :global(img),
675
+ button {
676
+ width: var(--size-full);
677
+ height: var(--size-full);
678
+ object-fit: contain;
679
+ display: block;
680
+ border-radius: var(--radius-lg);
681
+ }
682
+
683
+ .preview {
684
+ display: flex;
685
+ position: absolute;
686
+ flex-direction: column;
687
+ z-index: var(--layer-2);
688
+ border-radius: calc(var(--block-radius) - var(--block-border-width));
689
+ -webkit-backdrop-filter: blur(8px);
690
+ backdrop-filter: blur(8px);
691
+ width: var(--size-full);
692
+ height: var(--size-full);
693
+ }
694
+
695
+ .preview.minimal {
696
+ width: fit-content;
697
+ height: fit-content;
698
+ }
699
+
700
+ .preview::before {
701
+ content: "";
702
+ position: absolute;
703
+ z-index: var(--layer-below);
704
+ background: var(--background-fill-primary);
705
+ opacity: 0.9;
706
+ width: var(--size-full);
707
+ height: var(--size-full);
708
+ }
709
+
710
+ .fixed-height {
711
+ min-height: var(--size-80);
712
+ max-height: 55vh;
713
+ }
714
+
715
+ @media (--screen-xl) {
716
+ .fixed-height {
717
+ min-height: 450px;
718
+ }
719
+ }
720
+
721
+ .media-button {
722
+ height: calc(100% - 60px);
723
+ width: 100%;
724
+ display: flex;
725
+ }
726
+ .media-button :global(img),
727
+ .media-button :global(video) {
728
+ width: var(--size-full);
729
+ height: var(--size-full);
730
+ object-fit: contain;
731
+ }
732
+ .thumbnails :global(img) {
733
+ object-fit: cover;
734
+ width: var(--size-full);
735
+ height: var(--size-full);
736
+ }
737
+ .thumbnails :global(svg) {
738
+ position: absolute;
739
+ top: var(--size-2);
740
+ left: var(--size-2);
741
+ width: 50%;
742
+ height: 50%;
743
+ opacity: 50%;
744
+ }
745
+ .preview :global(img.with-caption) {
746
+ height: var(--size-full);
747
+ }
748
+
749
+ .preview.minimal :global(img.with-caption) {
750
+ height: auto;
751
+ }
752
+
753
+ .selectable {
754
+ cursor: crosshair;
755
+ }
756
+
757
+ .caption {
758
+ padding: var(--size-2) var(--size-3);
759
+ overflow: hidden;
760
+ color: var(--block-label-text-color);
761
+ font-weight: var(--weight-semibold);
762
+ text-align: center;
763
+ text-overflow: ellipsis;
764
+ white-space: nowrap;
765
+ align-self: center;
766
+ }
767
+
768
+ .thumbnails {
769
+ display: flex;
770
+ position: absolute;
771
+ bottom: 0;
772
+ justify-content: flex-start;
773
+ align-items: center;
774
+ gap: var(--spacing-lg);
775
+ width: var(--size-full);
776
+ height: var(--size-14);
777
+ overflow-x: scroll;
778
+ }
779
+
780
+ .thumbnail-item {
781
+ --ring-color: transparent;
782
+ position: relative;
783
+ box-shadow:
784
+ inset 0 0 0 1px var(--ring-color),
785
+ var(--shadow-drop);
786
+ border: 1px solid var(--border-color-primary);
787
+ border-radius: var(--button-small-radius);
788
+ background: var(--background-fill-secondary);
789
+ aspect-ratio: var(--ratio-square);
790
+ width: var(--size-full);
791
+ height: var(--size-full);
792
+ overflow: clip;
793
+ }
794
+
795
+ .thumbnail-item:hover {
796
+ --ring-color: var(--color-accent);
797
+ border-color: var(--color-accent);
798
+ filter: brightness(1.1);
799
+ }
800
+
801
+ .thumbnail-item.selected {
802
+ --ring-color: var(--color-accent);
803
+ border-color: var(--color-accent);
804
+ }
805
+
806
+ .thumbnail-item :global(svg) {
807
+ position: absolute;
808
+ top: 50%;
809
+ left: 50%;
810
+ width: 50%;
811
+ height: 50%;
812
+ opacity: 50%;
813
+ transform: translate(-50%, -50%);
814
+ }
815
+
816
+ .thumbnail-item :global(video) {
817
+ width: var(--size-full);
818
+ height: var(--size-full);
819
+ overflow: hidden;
820
+ object-fit: cover;
821
+ }
822
+
823
+ .thumbnail-small {
824
+ flex: none;
825
+ transform: scale(0.9);
826
+ transition: 0.075s;
827
+ width: var(--size-9);
828
+ height: var(--size-9);
829
+ }
830
+ .thumbnail-small.selected {
831
+ --ring-color: var(--color-accent);
832
+ transform: scale(1);
833
+ border-color: var(--color-accent);
834
+ }
835
+
836
+ .thumbnail-small > img {
837
+ width: var(--size-full);
838
+ height: var(--size-full);
839
+ overflow: hidden;
840
+ object-fit: var(--object-fit);
841
+ }
842
+
843
+ .grid-wrap {
844
+ position: relative;
845
+ padding: var(--size-2);
846
+ overflow-y: scroll;
847
+ }
848
+
849
+ .grid-container {
850
+ display: grid;
851
+ position: relative;
852
+ grid-template-rows: repeat(var(--grid-rows), minmax(100px, 1fr));
853
+ grid-template-columns: repeat(var(--grid-cols), minmax(100px, 1fr));
854
+ grid-auto-rows: minmax(100px, 1fr);
855
+ gap: var(--spacing-lg);
856
+ }
857
+
858
+ .thumbnail-lg > :global(img) {
859
+ width: var(--size-full);
860
+ height: var(--size-full);
861
+ overflow: hidden;
862
+ object-fit: var(--object-fit);
863
+ }
864
+
865
+ .thumbnail-lg:hover .caption-label {
866
+ opacity: 0.5;
867
+ }
868
+
869
+ .caption-label {
870
+ position: absolute;
871
+ right: var(--block-label-margin);
872
+ bottom: var(--block-label-margin);
873
+ z-index: var(--layer-1);
874
+ border-top: 1px solid var(--border-color-primary);
875
+ border-left: 1px solid var(--border-color-primary);
876
+ border-radius: var(--block-label-radius);
877
+ background: var(--background-fill-secondary);
878
+ padding: var(--block-label-padding);
879
+ max-width: 80%;
880
+ overflow: hidden;
881
+ font-size: var(--block-label-text-size);
882
+ text-align: left;
883
+ text-overflow: ellipsis;
884
+ white-space: nowrap;
885
+ }
886
+
887
+ .grid-wrap.minimal {
888
+ padding: 0;
889
+ }
890
+
891
+ .gallery-item {
892
+ position: relative;
893
+ width: 100%;
894
+ height: 100%;
895
+ }
896
+
897
+ .delete-button {
898
+ position: absolute;
899
+ bottom: 0;
900
+ left: 0;
901
+ z-index: var(--layer-1);
902
+ border-top: 1px solid var(--border-color-primary);
903
+ border-right: 1px solid var(--border-color-primary);
904
+ border-radius: 0 var(--radius-sm) 0 var(--radius-sm);
905
+ background: var(--background-fill-secondary);
906
+ padding: var(--block-label-padding);
907
+ cursor: pointer;
908
+ display: flex;
909
+ align-items: center;
910
+ justify-content: center;
911
+ opacity: 0;
912
+ transition: opacity 0.2s ease;
913
+ font-size: var(--block-label-text-size);
914
+ color: var(--block-label-text-color);
915
+ font-weight: var(--weight-semibold);
916
+ width: auto;
917
+ height: auto;
918
+ min-width: fit-content;
919
+ min-height: fit-content;
920
+ }
921
+
922
+ .gallery-item:hover .delete-button {
923
+ opacity: 1;
924
+ }
925
+
926
+ .delete-button:hover {
927
+ opacity: 0.8;
928
+ }
929
+
930
+ .delete-button :global(svg) {
931
+ width: var(--text-xs);
932
+ height: var(--text-md);
933
+ color: var(--block-label-text-color);
934
+ }
935
+ </style>
6.9.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.9.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
+ }