gradio-pr-bot commited on
Commit
8d533ff
·
verified ·
1 Parent(s): ebe63c1

Upload folder using huggingface_hub

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