gradio-pr-bot commited on
Commit
7e0d6cd
·
verified ·
1 Parent(s): 4781d7f

Upload folder using huggingface_hub

Browse files
6.5.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.5.1/gallery/Index.svelte ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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={typeof gradio.props.height === "number"
162
+ ? gradio.props.height
163
+ : undefined}
164
+ bind:fullscreen
165
+ >
166
+ <StatusTracker
167
+ autoscroll={gradio.shared.autoscroll}
168
+ i18n={gradio.i18n}
169
+ {...gradio.shared.loading_status}
170
+ on_clear_status={() =>
171
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
172
+ />
173
+ {#if gradio.shared.interactive && no_value}
174
+ <div
175
+ class={!gradio.props.value ||
176
+ (active_source && active_source.includes("webcam"))
177
+ ? "hidden-upload-input"
178
+ : ""}
179
+ >
180
+ <BaseFileUpload
181
+ bind:upload_promise
182
+ bind:this={upload_input}
183
+ value={null}
184
+ root={gradio.shared.root}
185
+ label={gradio.shared.label}
186
+ max_file_size={gradio.shared.max_file_size}
187
+ file_count={"multiple"}
188
+ file_types={gradio.props.file_types}
189
+ i18n={gradio.i18n}
190
+ upload={(...args) => gradio.shared.client.upload(...args)}
191
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
192
+ onupload={async (e) => {
193
+ const files = Array.isArray(e) ? e : [e];
194
+ gradio.props.value = await process_upload_files(files);
195
+ active_source = null;
196
+ gradio.dispatch("upload", gradio.props.value);
197
+ gradio.dispatch("change", gradio.props.value);
198
+ }}
199
+ onerror={({ detail }) => {
200
+ gradio.shared.loading_status = gradio.shared.loading_status || {};
201
+ gradio.shared.loading_status.status = "error";
202
+ gradio.dispatch("error", detail);
203
+ }}
204
+ >
205
+ <UploadText i18n={gradio.i18n} type="gallery" />
206
+ </BaseFileUpload>
207
+ </div>
208
+ {#if active_source === "webcam"}
209
+ <Webcam
210
+ root={gradio.shared.root}
211
+ value={null}
212
+ on:capture={async (e) => {
213
+ const f = await handle_save(
214
+ e.detail,
215
+ (f) => gradio.shared.client.upload(f, gradio.shared.root),
216
+ "webcam_upload"
217
+ );
218
+ const processed_files = await process_upload_files(f);
219
+ gradio.props.value?.push(...processed_files);
220
+ active_source = null;
221
+ gradio.dispatch("change", gradio.props.value);
222
+ }}
223
+ on:error
224
+ on:drag
225
+ on:close_stream
226
+ mirror_webcam={true}
227
+ streaming={false}
228
+ mode="image"
229
+ include_audio={false}
230
+ i18n={gradio.i18n}
231
+ upload={(...args) => gradio.shared.client.upload(...args)}
232
+ />
233
+ {:else if active_source === "webcam-video"}
234
+ <Webcam
235
+ root={gradio.shared.root}
236
+ value={null}
237
+ on:capture={async (e) => {
238
+ const f = { ...e.detail };
239
+ f.mime_type = "video/webm";
240
+ const processed_files = await process_upload_files([f]);
241
+ gradio.props.value?.push(...processed_files);
242
+ active_source = null;
243
+ gradio.dispatch("change", gradio.props.value);
244
+ }}
245
+ on:error
246
+ on:drag
247
+ on:close_stream
248
+ mirror_webcam={true}
249
+ streaming={false}
250
+ mode="video"
251
+ include_audio={false}
252
+ i18n={gradio.i18n}
253
+ upload={(...args) => gradio.shared.client.upload(...args)}
254
+ />
255
+ {/if}
256
+ {#if sources.length > 1 || sources.includes("clipboard")}
257
+ <SelectSource
258
+ {sources}
259
+ bind:active_source
260
+ handle_clear={() => gradio.dispatch("clear")}
261
+ handle_select={handle_select_source}
262
+ />
263
+ {/if}
264
+ {:else}
265
+ <Gallery
266
+ onchange={() => gradio.dispatch("change")}
267
+ onclear={() => gradio.dispatch("change")}
268
+ onselect={(e) => gradio.dispatch("select", e)}
269
+ onshare={(e) => gradio.dispatch("share", e.detail)}
270
+ onerror={(e) => gradio.dispatch("error", e.detail)}
271
+ onpreview_open={() => {
272
+ gradio.dispatch("preview_open");
273
+ }}
274
+ onpreview_close={() => gradio.dispatch("preview_close")}
275
+ onfullscreen={({ detail }) => {
276
+ fullscreen = detail;
277
+ }}
278
+ ondelete={handle_delete}
279
+ onupload={async (e) => {
280
+ const files = Array.isArray(e) ? e : [e];
281
+ const new_value = await process_upload_files(files);
282
+ gradio.props.value = gradio.props.value
283
+ ? [...gradio.props.value, ...new_value]
284
+ : new_value;
285
+ gradio.dispatch("upload", new_value);
286
+ gradio.dispatch("change", gradio.props.value);
287
+ }}
288
+ {sources}
289
+ {onsource_change}
290
+ label={gradio.shared.label}
291
+ show_label={gradio.shared.show_label}
292
+ columns={gradio.props.columns}
293
+ rows={gradio.props.rows}
294
+ height={gradio.props.height}
295
+ preview={gradio.props.preview}
296
+ object_fit={gradio.props.object_fit}
297
+ interactive={gradio.shared.interactive}
298
+ allow_preview={gradio.props.allow_preview}
299
+ bind:selected_index={gradio.props.selected_index}
300
+ bind:value={gradio.props.value}
301
+ show_share_button={gradio.props.buttons.some(
302
+ (btn) => typeof btn === "string" && btn === "share"
303
+ )}
304
+ show_download_button={gradio.props.buttons.some(
305
+ (btn) => typeof btn === "string" && btn === "download"
306
+ )}
307
+ fit_columns={gradio.props.fit_columns}
308
+ i18n={gradio.i18n}
309
+ _fetch={(...args) => gradio.shared.client.fetch(...args)}
310
+ show_fullscreen_button={gradio.props.buttons.some(
311
+ (btn) => typeof btn === "string" && btn === "fullscreen"
312
+ )}
313
+ buttons={gradio.props.buttons}
314
+ on_custom_button_click={(id) => {
315
+ gradio.dispatch("custom_button_click", { id });
316
+ }}
317
+ {fullscreen}
318
+ root={gradio.shared.root}
319
+ file_types={gradio.props.file_types}
320
+ max_file_size={gradio.shared.max_file_size}
321
+ upload={(...args) => gradio.shared.client.upload(...args)}
322
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
323
+ />
324
+ {/if}
325
+ </Block>
326
+
327
+ <style>
328
+ .hidden-upload-input {
329
+ display: none;
330
+ }
331
+ </style>
6.5.1/gallery/package.json ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/gallery",
3
+ "version": "0.17.0",
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.5.1/gallery/shared/Gallery.svelte ADDED
@@ -0,0 +1,927 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 | "auto";
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 {fullscreen} onclick={(e) => onfullscreen(e)} />
418
+ {/if}
419
+
420
+ {#if show_share_button}
421
+ <div class="icon-button">
422
+ <ShareButton
423
+ {i18n}
424
+ on:share={(detail) => {
425
+ onshare(detail);
426
+ }}
427
+ on:error={(detail) => {
428
+ onerror(detail);
429
+ }}
430
+ value={resolved_value}
431
+ formatter={format_gallery_for_sharing}
432
+ />
433
+ </div>
434
+ {/if}
435
+ {#if !is_full_screen}
436
+ <IconButton
437
+ Icon={Clear}
438
+ label="Close"
439
+ onclick={() => {
440
+ selected_index = null;
441
+ onpreview_close();
442
+ }}
443
+ />
444
+ {/if}
445
+ </IconButtonWrapper>
446
+ <button
447
+ class="media-button"
448
+ on:click={"image" in selected_media
449
+ ? (event) => handle_preview_click(event)
450
+ : null}
451
+ style="height: calc(100% - {selected_media.caption
452
+ ? '80px'
453
+ : '60px'})"
454
+ aria-label="detailed view of selected image"
455
+ >
456
+ {#if "image" in selected_media}
457
+ <Image
458
+ restProps={{
459
+ alt: selected_media.caption || "",
460
+ title: selected_media.caption || null,
461
+ class: selected_media.caption && "with-caption",
462
+ loading: "lazy"
463
+ }}
464
+ src={selected_media.image.url}
465
+ data_testid="detailed-image"
466
+ />
467
+ {:else}
468
+ <Video
469
+ src={selected_media.video.url}
470
+ data-testid={"detailed-video"}
471
+ alt={selected_media.caption || ""}
472
+ loading="lazy"
473
+ loop={false}
474
+ is_stream={false}
475
+ muted={false}
476
+ controls={true}
477
+ />
478
+ {/if}
479
+ </button>
480
+ {#if selected_media?.caption}
481
+ <caption class="caption">
482
+ {selected_media.caption}
483
+ </caption>
484
+ {/if}
485
+ <div
486
+ bind:this={container_element}
487
+ class="thumbnails scroll-hide"
488
+ data-testid="container_el"
489
+ style="justify-content: {thumbnails_overflow
490
+ ? 'flex-start'
491
+ : 'center'};"
492
+ >
493
+ {#each resolved_value as media, i}
494
+ <button
495
+ bind:this={el[i]}
496
+ on:click={() => (selected_index = i)}
497
+ class="thumbnail-item thumbnail-small"
498
+ class:selected={selected_index === i && mode !== "minimal"}
499
+ aria-label={"Thumbnail " +
500
+ (i + 1) +
501
+ " of " +
502
+ resolved_value.length}
503
+ >
504
+ {#if "image" in media}
505
+ <Image
506
+ src={media.image.url}
507
+ restProps={{
508
+ title: media.caption || null,
509
+ alt: "",
510
+ class: "with-caption",
511
+ loading: "lazy"
512
+ }}
513
+ data_testid={`thumbnail ${i + 1}`}
514
+ />
515
+ {:else}
516
+ <Play />
517
+ <Video
518
+ src={media.video.url}
519
+ title={media.caption || null}
520
+ is_stream={false}
521
+ data-testid={"thumbnail " + (i + 1)}
522
+ alt=""
523
+ loading="lazy"
524
+ loop={false}
525
+ />
526
+ {/if}
527
+ </button>
528
+ {/each}
529
+ </div>
530
+ </span>
531
+ {/if}
532
+
533
+ <div
534
+ class="grid-wrap"
535
+ class:minimal={mode === "minimal"}
536
+ class:fixed-height={mode !== "minimal" && (!height || height == "auto")}
537
+ class:hidden={is_full_screen}
538
+ style:height={height !== "auto" ? height + "px" : null}
539
+ >
540
+ {#if interactive && selected_index === null}
541
+ <ModifyUpload
542
+ {i18n}
543
+ onclear={() => {
544
+ value = [];
545
+ onsource_change("upload");
546
+ onclear();
547
+ }}
548
+ >
549
+ {#if upload && stream_handler}
550
+ <IconButton
551
+ Icon={UploadIcon}
552
+ label={i18n("upload_text.click_to_upload")}
553
+ >
554
+ <UploadComponent
555
+ bind:upload_promise
556
+ icon_upload={true}
557
+ onload={(e) => onupload(e)}
558
+ filetype={file_types}
559
+ file_count="multiple"
560
+ {max_file_size}
561
+ {root}
562
+ bind:uploading
563
+ onerror={(e) => onerror(e)}
564
+ {stream_handler}
565
+ {upload}
566
+ />
567
+ </IconButton>
568
+ {/if}
569
+ {#if sources.includes("webcam")}
570
+ <IconButton
571
+ Icon={Webcam}
572
+ label={i18n("common.webcam")}
573
+ onclick={() => {
574
+ onsource_change("webcam");
575
+ }}
576
+ />
577
+ {/if}
578
+ {#if sources.includes("webcam-video")}
579
+ <IconButton
580
+ Icon={VideoIcon}
581
+ label={i18n("common.video")}
582
+ onclick={() => {
583
+ onsource_change("webcam-video");
584
+ }}
585
+ />
586
+ {/if}
587
+ {#if sources.includes("clipboard")}
588
+ <IconButton
589
+ label={i18n("upload_text.paste_clipboard")}
590
+ onclick={() => {
591
+ onsource_change("clipboard");
592
+ }}
593
+ Icon={ImagePaste}
594
+ />
595
+ {/if}
596
+ </ModifyUpload>
597
+ {/if}
598
+ <div
599
+ class="grid-container"
600
+ style="--grid-cols:{effective_columns}; --grid-rows:{rows}; --object-fit: {object_fit};"
601
+ class:pt-6={show_label}
602
+ >
603
+ {#each resolved_value as entry, i}
604
+ <div class="gallery-item">
605
+ <button
606
+ class="thumbnail-item thumbnail-lg"
607
+ class:selected={selected_index === i}
608
+ on:click={() => {
609
+ if (selected_index === null && allow_preview) {
610
+ onpreview_open();
611
+ }
612
+ selected_index = i;
613
+ }}
614
+ aria-label={"Thumbnail " +
615
+ (i + 1) +
616
+ " of " +
617
+ resolved_value.length}
618
+ >
619
+ {#if "image" in entry}
620
+ <Image
621
+ alt={entry.caption || ""}
622
+ src={typeof entry.image === "string"
623
+ ? entry.image
624
+ : entry.image.url}
625
+ loading="lazy"
626
+ />
627
+ {:else}
628
+ <Play />
629
+ <Video
630
+ src={entry.video.url}
631
+ title={entry.caption || null}
632
+ is_stream={false}
633
+ data-testid={"thumbnail " + (i + 1)}
634
+ alt=""
635
+ loading="lazy"
636
+ loop={false}
637
+ />
638
+ {/if}
639
+ {#if entry.caption}
640
+ <div class="caption-label">
641
+ {entry.caption}
642
+ </div>
643
+ {/if}
644
+ </button>
645
+ {#if interactive}
646
+ <button
647
+ class="delete-button"
648
+ on:click|stopPropagation={() => handle_item_delete(i)}
649
+ aria-label="Delete image"
650
+ >
651
+ <Clear />
652
+ </button>
653
+ {/if}
654
+ </div>
655
+ {/each}
656
+ </div>
657
+ </div>
658
+ </div>
659
+ {/if}
660
+
661
+ <style lang="postcss">
662
+ .image-container {
663
+ height: 100%;
664
+ position: relative;
665
+ }
666
+ .image-container :global(img),
667
+ button {
668
+ width: var(--size-full);
669
+ height: var(--size-full);
670
+ object-fit: contain;
671
+ display: block;
672
+ border-radius: var(--radius-lg);
673
+ }
674
+
675
+ .preview {
676
+ display: flex;
677
+ position: absolute;
678
+ flex-direction: column;
679
+ z-index: var(--layer-2);
680
+ border-radius: calc(var(--block-radius) - var(--block-border-width));
681
+ -webkit-backdrop-filter: blur(8px);
682
+ backdrop-filter: blur(8px);
683
+ width: var(--size-full);
684
+ height: var(--size-full);
685
+ }
686
+
687
+ .preview.minimal {
688
+ width: fit-content;
689
+ height: fit-content;
690
+ }
691
+
692
+ .preview::before {
693
+ content: "";
694
+ position: absolute;
695
+ z-index: var(--layer-below);
696
+ background: var(--background-fill-primary);
697
+ opacity: 0.9;
698
+ width: var(--size-full);
699
+ height: var(--size-full);
700
+ }
701
+
702
+ .fixed-height {
703
+ min-height: var(--size-80);
704
+ max-height: 55vh;
705
+ }
706
+
707
+ @media (--screen-xl) {
708
+ .fixed-height {
709
+ min-height: 450px;
710
+ }
711
+ }
712
+
713
+ .media-button {
714
+ height: calc(100% - 60px);
715
+ width: 100%;
716
+ display: flex;
717
+ }
718
+ .media-button :global(img),
719
+ .media-button :global(video) {
720
+ width: var(--size-full);
721
+ height: var(--size-full);
722
+ object-fit: contain;
723
+ }
724
+ .thumbnails :global(img) {
725
+ object-fit: cover;
726
+ width: var(--size-full);
727
+ height: var(--size-full);
728
+ }
729
+ .thumbnails :global(svg) {
730
+ position: absolute;
731
+ top: var(--size-2);
732
+ left: var(--size-2);
733
+ width: 50%;
734
+ height: 50%;
735
+ opacity: 50%;
736
+ }
737
+ .preview :global(img.with-caption) {
738
+ height: var(--size-full);
739
+ }
740
+
741
+ .preview.minimal :global(img.with-caption) {
742
+ height: auto;
743
+ }
744
+
745
+ .selectable {
746
+ cursor: crosshair;
747
+ }
748
+
749
+ .caption {
750
+ padding: var(--size-2) var(--size-3);
751
+ overflow: hidden;
752
+ color: var(--block-label-text-color);
753
+ font-weight: var(--weight-semibold);
754
+ text-align: center;
755
+ text-overflow: ellipsis;
756
+ white-space: nowrap;
757
+ align-self: center;
758
+ }
759
+
760
+ .thumbnails {
761
+ display: flex;
762
+ position: absolute;
763
+ bottom: 0;
764
+ justify-content: flex-start;
765
+ align-items: center;
766
+ gap: var(--spacing-lg);
767
+ width: var(--size-full);
768
+ height: var(--size-14);
769
+ overflow-x: scroll;
770
+ }
771
+
772
+ .thumbnail-item {
773
+ --ring-color: transparent;
774
+ position: relative;
775
+ box-shadow:
776
+ inset 0 0 0 1px var(--ring-color),
777
+ var(--shadow-drop);
778
+ border: 1px solid var(--border-color-primary);
779
+ border-radius: var(--button-small-radius);
780
+ background: var(--background-fill-secondary);
781
+ aspect-ratio: var(--ratio-square);
782
+ width: var(--size-full);
783
+ height: var(--size-full);
784
+ overflow: clip;
785
+ }
786
+
787
+ .thumbnail-item:hover {
788
+ --ring-color: var(--color-accent);
789
+ border-color: var(--color-accent);
790
+ filter: brightness(1.1);
791
+ }
792
+
793
+ .thumbnail-item.selected {
794
+ --ring-color: var(--color-accent);
795
+ border-color: var(--color-accent);
796
+ }
797
+
798
+ .thumbnail-item :global(svg) {
799
+ position: absolute;
800
+ top: 50%;
801
+ left: 50%;
802
+ width: 50%;
803
+ height: 50%;
804
+ opacity: 50%;
805
+ transform: translate(-50%, -50%);
806
+ }
807
+
808
+ .thumbnail-item :global(video) {
809
+ width: var(--size-full);
810
+ height: var(--size-full);
811
+ overflow: hidden;
812
+ object-fit: cover;
813
+ }
814
+
815
+ .thumbnail-small {
816
+ flex: none;
817
+ transform: scale(0.9);
818
+ transition: 0.075s;
819
+ width: var(--size-9);
820
+ height: var(--size-9);
821
+ }
822
+ .thumbnail-small.selected {
823
+ --ring-color: var(--color-accent);
824
+ transform: scale(1);
825
+ border-color: var(--color-accent);
826
+ }
827
+
828
+ .thumbnail-small > img {
829
+ width: var(--size-full);
830
+ height: var(--size-full);
831
+ overflow: hidden;
832
+ object-fit: var(--object-fit);
833
+ }
834
+
835
+ .grid-wrap {
836
+ position: relative;
837
+ padding: var(--size-2);
838
+ overflow-y: scroll;
839
+ }
840
+
841
+ .grid-container {
842
+ display: grid;
843
+ position: relative;
844
+ grid-template-rows: repeat(var(--grid-rows), minmax(100px, 1fr));
845
+ grid-template-columns: repeat(var(--grid-cols), minmax(100px, 1fr));
846
+ grid-auto-rows: minmax(100px, 1fr);
847
+ gap: var(--spacing-lg);
848
+ }
849
+
850
+ .thumbnail-lg > :global(img) {
851
+ width: var(--size-full);
852
+ height: var(--size-full);
853
+ overflow: hidden;
854
+ object-fit: var(--object-fit);
855
+ }
856
+
857
+ .thumbnail-lg:hover .caption-label {
858
+ opacity: 0.5;
859
+ }
860
+
861
+ .caption-label {
862
+ position: absolute;
863
+ right: var(--block-label-margin);
864
+ bottom: var(--block-label-margin);
865
+ z-index: var(--layer-1);
866
+ border-top: 1px solid var(--border-color-primary);
867
+ border-left: 1px solid var(--border-color-primary);
868
+ border-radius: var(--block-label-radius);
869
+ background: var(--background-fill-secondary);
870
+ padding: var(--block-label-padding);
871
+ max-width: 80%;
872
+ overflow: hidden;
873
+ font-size: var(--block-label-text-size);
874
+ text-align: left;
875
+ text-overflow: ellipsis;
876
+ white-space: nowrap;
877
+ }
878
+
879
+ .grid-wrap.minimal {
880
+ padding: 0;
881
+ }
882
+
883
+ .gallery-item {
884
+ position: relative;
885
+ width: 100%;
886
+ height: 100%;
887
+ }
888
+
889
+ .delete-button {
890
+ position: absolute;
891
+ bottom: 0;
892
+ left: 0;
893
+ z-index: var(--layer-1);
894
+ border-top: 1px solid var(--border-color-primary);
895
+ border-right: 1px solid var(--border-color-primary);
896
+ border-radius: 0 var(--radius-sm) 0 var(--radius-sm);
897
+ background: var(--background-fill-secondary);
898
+ padding: var(--block-label-padding);
899
+ cursor: pointer;
900
+ display: flex;
901
+ align-items: center;
902
+ justify-content: center;
903
+ opacity: 0;
904
+ transition: opacity 0.2s ease;
905
+ font-size: var(--block-label-text-size);
906
+ color: var(--block-label-text-color);
907
+ font-weight: var(--weight-semibold);
908
+ width: auto;
909
+ height: auto;
910
+ min-width: fit-content;
911
+ min-height: fit-content;
912
+ }
913
+
914
+ .gallery-item:hover .delete-button {
915
+ opacity: 1;
916
+ }
917
+
918
+ .delete-button:hover {
919
+ opacity: 0.8;
920
+ }
921
+
922
+ .delete-button :global(svg) {
923
+ width: var(--text-xs);
924
+ height: var(--text-md);
925
+ color: var(--block-label-text-color);
926
+ }
927
+ </style>
6.5.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.5.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 | "auto";
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
+ }