gradio-pr-bot commited on
Commit
383a96f
·
verified ·
1 Parent(s): 43bf1f4

Upload folder using huggingface_hub

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