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

Upload folder using huggingface_hub

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