gradio-pr-bot commited on
Commit
393bff2
·
verified ·
1 Parent(s): de2e8e0

Upload folder using huggingface_hub

Browse files
6.4.1/file/Example.svelte ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { FileData } from "@gradio/client";
3
+
4
+ let {
5
+ value,
6
+ type,
7
+ selected = false
8
+ }: {
9
+ value: FileData | null;
10
+ type: "gallery" | "table";
11
+ selected: boolean;
12
+ } = $props();
13
+ </script>
14
+
15
+ <div
16
+ class:table={type === "table"}
17
+ class:gallery={type === "gallery"}
18
+ class:selected
19
+ >
20
+ {value ? (Array.isArray(value) ? value.join(", ") : value) : ""}
21
+ </div>
22
+
23
+ <style>
24
+ div {
25
+ overflow: hidden;
26
+ text-overflow: ellipsis;
27
+ white-space: nowrap;
28
+ }
29
+ .gallery {
30
+ display: flex;
31
+ align-items: center;
32
+ cursor: pointer;
33
+ padding: var(--size-1) var(--size-2);
34
+ text-align: left;
35
+ }
36
+ </style>
6.4.1/file/Index.svelte ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script context="module" lang="ts">
4
+ export { default as FilePreview } from "./shared/FilePreview.svelte";
5
+ export { default as BaseFileUpload } from "./shared/FileUpload.svelte";
6
+ export { default as BaseFile } from "./shared/File.svelte";
7
+ export { default as BaseExample } from "./Example.svelte";
8
+ </script>
9
+
10
+ <script lang="ts">
11
+ import { Gradio } from "@gradio/utils";
12
+ import File from "./shared/File.svelte";
13
+ import FileUpload from "./shared/FileUpload.svelte";
14
+ import { Block, UploadText } from "@gradio/atoms";
15
+ import type { FileEvents, FileProps } from "./types";
16
+ import type { SelectData } from "@gradio/utils";
17
+ import { StatusTracker } from "@gradio/statustracker";
18
+ import { tick } from "svelte";
19
+
20
+ const props = $props();
21
+ let upload_promise = $state<Promise<any> | null>(null);
22
+
23
+ let dragging = $state(false);
24
+ let pending_upload = $state(false);
25
+
26
+ class FileGradio extends Gradio<FileEvents, FileProps> {
27
+ async get_data() {
28
+ if (upload_promise) {
29
+ await upload_promise;
30
+ await tick();
31
+ }
32
+ const data = await super.get_data();
33
+
34
+ return data;
35
+ }
36
+ }
37
+
38
+ const gradio = new FileGradio(props);
39
+
40
+ let old_value = $state(gradio.props.value);
41
+
42
+ $effect(() => {
43
+ if (old_value !== gradio.props.value) {
44
+ old_value = gradio.props.value;
45
+ gradio.dispatch("change", $state.snapshot(gradio.props.value));
46
+ }
47
+ });
48
+ </script>
49
+
50
+ <Block
51
+ visible={gradio.shared.visible}
52
+ variant={gradio.props.value ? "solid" : "dashed"}
53
+ border_mode={dragging ? "focus" : "base"}
54
+ padding={false}
55
+ elem_id={gradio.shared.elem_id}
56
+ elem_classes={gradio.shared.elem_classes}
57
+ container={gradio.shared.container}
58
+ scale={gradio.shared.scale}
59
+ min_width={gradio.shared.min_width}
60
+ allow_overflow={false}
61
+ >
62
+ <StatusTracker
63
+ autoscroll={gradio.shared.autoscroll}
64
+ i18n={gradio.i18n}
65
+ {...gradio.shared.loading_status}
66
+ status={pending_upload
67
+ ? "generating"
68
+ : gradio.shared.loading_status?.status || "complete"}
69
+ on_clear_status={() =>
70
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
71
+ />
72
+ {#if !gradio.shared.interactive}
73
+ <File
74
+ on_select={({ detail }) => gradio.dispatch("select", detail)}
75
+ on_download={({ detail }) => gradio.dispatch("download", detail)}
76
+ selectable={gradio.props._selectable}
77
+ value={gradio.props.value}
78
+ label={gradio.shared.label}
79
+ show_label={gradio.shared.show_label}
80
+ height={gradio.props.height}
81
+ i18n={gradio.i18n}
82
+ buttons={gradio.props.buttons}
83
+ on_custom_button_click={(id) => {
84
+ gradio.dispatch("custom_button_click", { id });
85
+ }}
86
+ />
87
+ {:else}
88
+ <FileUpload
89
+ bind:upload_promise
90
+ upload={(...args) => gradio.shared.client.upload(...args)}
91
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
92
+ label={gradio.shared.label}
93
+ show_label={gradio.shared.show_label}
94
+ value={gradio.props.value}
95
+ file_count={gradio.props.file_count}
96
+ file_types={gradio.props.file_types}
97
+ selectable={gradio.props._selectable}
98
+ height={gradio.props.height ?? undefined}
99
+ root={gradio.shared.root}
100
+ allow_reordering={gradio.props.allow_reordering}
101
+ max_file_size={gradio.shared.max_file_size}
102
+ buttons={gradio.props.buttons}
103
+ on_custom_button_click={(id) => {
104
+ gradio.dispatch("custom_button_click", { id });
105
+ }}
106
+ onchange={(detail) => {
107
+ gradio.props.value = detail;
108
+ }}
109
+ ondrag={(detail) => (dragging = detail)}
110
+ onclear={() => gradio.dispatch("clear")}
111
+ onselect={(detail: SelectData) => gradio.dispatch("select", detail)}
112
+ onupload={() => gradio.dispatch("upload")}
113
+ onerror={(error) => {
114
+ gradio.shared.loading_status = gradio.shared.loading_status || {};
115
+ gradio.shared.loading_status.status = "error";
116
+ gradio.dispatch("error", error);
117
+ }}
118
+ ondelete={(detail) => {
119
+ gradio.dispatch("delete", detail);
120
+ }}
121
+ i18n={gradio.i18n}
122
+ >
123
+ <UploadText i18n={gradio.i18n} type="file" />
124
+ </FileUpload>
125
+ {/if}
126
+ </Block>
6.4.1/file/package.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/file",
3
+ "version": "0.14.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/client": "workspace:^",
12
+ "@gradio/icons": "workspace:^",
13
+ "@gradio/statustracker": "workspace:^",
14
+ "@gradio/upload": "workspace:^",
15
+ "@gradio/utils": "workspace:^"
16
+ },
17
+ "devDependencies": {
18
+ "@gradio/preview": "workspace:^"
19
+ },
20
+ "main": "./Index.svelte",
21
+ "main_changeset": true,
22
+ "exports": {
23
+ ".": {
24
+ "gradio": "./Index.svelte",
25
+ "svelte": "./dist/Index.svelte",
26
+ "types": "./dist/Index.svelte.d.ts"
27
+ },
28
+ "./example": {
29
+ "gradio": "./Example.svelte",
30
+ "svelte": "./dist/Example.svelte",
31
+ "types": "./dist/Example.svelte.d.ts"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "peerDependencies": {
36
+ "svelte": "^5.48.0"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/gradio-app/gradio.git",
41
+ "directory": "js/file"
42
+ }
43
+ }
6.4.1/file/shared/File.svelte ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { BlockLabel, Empty, IconButtonWrapper } from "@gradio/atoms";
3
+ import { File } from "@gradio/icons";
4
+ import FilePreview from "./FilePreview.svelte";
5
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
6
+
7
+ let {
8
+ value,
9
+ label,
10
+ show_label,
11
+ selectable,
12
+ i18n,
13
+ height,
14
+ buttons = null,
15
+ on_custom_button_click = null,
16
+ on_select,
17
+ on_download
18
+ } = $props();
19
+ </script>
20
+
21
+ {#if show_label && buttons && buttons.length > 0}
22
+ <IconButtonWrapper {buttons} {on_custom_button_click} />
23
+ {/if}
24
+ <BlockLabel
25
+ {show_label}
26
+ float={value === null}
27
+ Icon={File}
28
+ label={label || "File"}
29
+ />
30
+
31
+ {#if value && (Array.isArray(value) ? value.length > 0 : true)}
32
+ <FilePreview
33
+ {i18n}
34
+ {selectable}
35
+ on:select={on_select}
36
+ on:download={on_download}
37
+ {value}
38
+ {height}
39
+ />
40
+ {:else}
41
+ <Empty unpadded_box={true} size="large"><File /></Empty>
42
+ {/if}
6.4.1/file/shared/FilePreview.svelte ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { FileData } from "@gradio/client";
3
+ import { prettyBytes } from "./utils";
4
+ import type { I18nFormatter, SelectData } from "@gradio/utils";
5
+ import { DownloadLink } from "@gradio/atoms";
6
+
7
+ let {
8
+ value,
9
+ selectable = false,
10
+ height = undefined,
11
+ i18n,
12
+ allow_reordering = false,
13
+ onselect,
14
+ onchange,
15
+ ondelete,
16
+ ondownload
17
+ }: {
18
+ value: FileData | FileData[];
19
+ selectable?: boolean;
20
+ height?: number | string | undefined;
21
+ i18n: I18nFormatter;
22
+ allow_reordering?: boolean;
23
+ onselect?: (event_data: SelectData) => void;
24
+ onchange?: (event_data: FileData[] | FileData) => void;
25
+ ondelete?: (event_data: FileData) => void;
26
+ ondownload?: (event_data: FileData) => void;
27
+ } = $props();
28
+
29
+ let dragging_index: number | null = $state(null);
30
+ let drop_target_index: number | null = $state(null);
31
+
32
+ function handle_drag_start(event: DragEvent, index: number): void {
33
+ dragging_index = index;
34
+ if (event.dataTransfer) {
35
+ event.dataTransfer.effectAllowed = "move";
36
+ event.dataTransfer.setData("text/plain", index.toString());
37
+ }
38
+ }
39
+
40
+ function handle_drag_over(event: DragEvent, index: number): void {
41
+ event.preventDefault();
42
+ if (index === normalized_files.length - 1) {
43
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
44
+ const midY = rect.top + rect.height / 2;
45
+ drop_target_index =
46
+ event.clientY > midY ? normalized_files.length : index;
47
+ } else {
48
+ drop_target_index = index;
49
+ }
50
+ if (event.dataTransfer) {
51
+ event.dataTransfer.dropEffect = "move";
52
+ }
53
+ }
54
+
55
+ function handle_drag_end(event: DragEvent): void {
56
+ if (
57
+ !event.dataTransfer?.dropEffect ||
58
+ event.dataTransfer.dropEffect === "none"
59
+ ) {
60
+ dragging_index = null;
61
+ drop_target_index = null;
62
+ }
63
+ }
64
+
65
+ function handle_drop(event: DragEvent, index: number): void {
66
+ event.preventDefault();
67
+ if (dragging_index === null || dragging_index === index) return;
68
+
69
+ const files = Array.isArray(value) ? [...value] : [value];
70
+ const [removed] = files.splice(dragging_index, 1);
71
+ files.splice(
72
+ drop_target_index === normalized_files.length
73
+ ? normalized_files.length
74
+ : index,
75
+ 0,
76
+ removed
77
+ );
78
+
79
+ const new_value = Array.isArray(value) ? files : files[0];
80
+ onchange?.(new_value);
81
+
82
+ dragging_index = null;
83
+ drop_target_index = null;
84
+ }
85
+
86
+ function split_filename(filename: string): [string, string] {
87
+ const last_dot = filename.lastIndexOf(".");
88
+ if (last_dot === -1) {
89
+ return [filename, ""];
90
+ }
91
+ return [filename.slice(0, last_dot), filename.slice(last_dot)];
92
+ }
93
+
94
+ let normalized_files = $derived(
95
+ (Array.isArray(value) ? value : [value]).map((file) => {
96
+ const [filename_stem, filename_ext] = split_filename(
97
+ file.orig_name ?? ""
98
+ );
99
+ return {
100
+ ...file,
101
+ filename_stem,
102
+ filename_ext
103
+ };
104
+ })
105
+ );
106
+
107
+ function handle_row_click(
108
+ event: MouseEvent & { currentTarget: HTMLTableRowElement },
109
+ index: number
110
+ ): void {
111
+ const tr = event.currentTarget;
112
+ const should_select =
113
+ event.target === tr || // Only select if the click is on the row itself
114
+ (tr &&
115
+ tr.firstElementChild &&
116
+ event.composedPath().includes(tr.firstElementChild)); // Or if the click is on the name column
117
+
118
+ if (should_select) {
119
+ onselect?.({ value: normalized_files[index].orig_name, index });
120
+ }
121
+ }
122
+
123
+ function remove_file(index: number): void {
124
+ const files = Array.isArray(value) ? [...value] : [value];
125
+ const removed = files.splice(index, 1);
126
+ const new_value = Array.isArray(value) ? files : files[0];
127
+ value = new_value;
128
+ ondelete?.(removed[0]);
129
+ onchange?.(new_value);
130
+ }
131
+
132
+ function handle_download(file: FileData): void {
133
+ ondownload?.(file);
134
+ }
135
+
136
+ const is_browser = typeof window !== "undefined";
137
+ </script>
138
+
139
+ <div
140
+ class="file-preview-holder"
141
+ style:max-height={height
142
+ ? typeof height === "number"
143
+ ? height + "px"
144
+ : height
145
+ : "auto"}
146
+ >
147
+ <table class="file-preview">
148
+ <tbody>
149
+ {#each normalized_files as file, i (file.url)}
150
+ <tr
151
+ class="file"
152
+ class:selectable
153
+ class:dragging={dragging_index === i}
154
+ class:drop-target={drop_target_index === i ||
155
+ (i === normalized_files.length - 1 &&
156
+ drop_target_index === normalized_files.length)}
157
+ data-drop-target={drop_target_index === normalized_files.length &&
158
+ i === normalized_files.length - 1
159
+ ? "after"
160
+ : drop_target_index === i + 1
161
+ ? "after"
162
+ : "before"}
163
+ draggable={allow_reordering && normalized_files.length > 1}
164
+ onclick={(event) => {
165
+ handle_row_click(event, i);
166
+ }}
167
+ ondragstart={(event) => handle_drag_start(event, i)}
168
+ ondragenter={(event) => event.preventDefault()}
169
+ ondragover={(event) => handle_drag_over(event, i)}
170
+ ondrop={(event) => handle_drop(event, i)}
171
+ ondragend={handle_drag_end}
172
+ >
173
+ <td class="filename" aria-label={file.orig_name}>
174
+ {#if allow_reordering && normalized_files.length > 1}
175
+ <span class="drag-handle">⋮⋮</span>
176
+ {/if}
177
+ <span class="stem">{file.filename_stem}</span>
178
+ <span class="ext">{file.filename_ext}</span>
179
+ </td>
180
+
181
+ <td class="download">
182
+ {#if file.url}
183
+ <DownloadLink
184
+ href={file.url}
185
+ on:click={() => handle_download(file)}
186
+ download={is_browser && window.__is_colab__
187
+ ? null
188
+ : file.orig_name}
189
+ >
190
+ {@html file.size != null
191
+ ? prettyBytes(file.size)
192
+ : "(size unknown)"}&nbsp;&#8675;
193
+ </DownloadLink>
194
+ {:else}
195
+ {i18n("file.uploading")}
196
+ {/if}
197
+ </td>
198
+
199
+ {#if normalized_files.length > 1}
200
+ <td>
201
+ <button
202
+ class="label-clear-button"
203
+ aria-label="Remove this file"
204
+ onclick={() => {
205
+ remove_file(i);
206
+ }}
207
+ onkeydown={(event) => {
208
+ if (event.key === "Enter") {
209
+ remove_file(i);
210
+ }
211
+ }}
212
+
213
+ </button>
214
+ </td>
215
+ {/if}
216
+ </tr>
217
+ {/each}
218
+ </tbody>
219
+ </table>
220
+ </div>
221
+
222
+ <style>
223
+ .label-clear-button {
224
+ color: var(--body-text-color-subdued);
225
+ position: relative;
226
+ left: -3px;
227
+ }
228
+
229
+ .label-clear-button:hover {
230
+ color: var(--body-text-color);
231
+ }
232
+
233
+ .file-preview {
234
+ table-layout: fixed;
235
+ width: var(--size-full);
236
+ max-height: var(--size-60);
237
+ overflow-y: auto;
238
+ margin-top: var(--size-1);
239
+ color: var(--body-text-color);
240
+ }
241
+
242
+ .file-preview-holder {
243
+ overflow: auto;
244
+ }
245
+
246
+ .file {
247
+ display: flex;
248
+ width: var(--size-full);
249
+ }
250
+
251
+ .file > * {
252
+ padding: var(--size-1) var(--size-2-5);
253
+ }
254
+
255
+ .filename {
256
+ flex-grow: 1;
257
+ display: flex;
258
+ overflow: hidden;
259
+ }
260
+ .filename .stem {
261
+ overflow: hidden;
262
+ text-overflow: ellipsis;
263
+ white-space: nowrap;
264
+ }
265
+ .filename .ext {
266
+ white-space: nowrap;
267
+ }
268
+
269
+ .download {
270
+ min-width: 8rem;
271
+ width: 10%;
272
+ white-space: nowrap;
273
+ text-align: right;
274
+ }
275
+ .download:hover {
276
+ text-decoration: underline;
277
+ }
278
+ .download > :global(a) {
279
+ color: var(--link-text-color);
280
+ }
281
+
282
+ .download > :global(a:hover) {
283
+ color: var(--link-text-color-hover);
284
+ }
285
+ .download > :global(a:visited) {
286
+ color: var(--link-text-color-visited);
287
+ }
288
+ .download > :global(a:active) {
289
+ color: var(--link-text-color-active);
290
+ }
291
+ .selectable {
292
+ cursor: pointer;
293
+ }
294
+
295
+ tbody > tr:nth-child(even) {
296
+ background: var(--block-background-fill);
297
+ }
298
+
299
+ tbody > tr:nth-child(odd) {
300
+ background: var(--table-odd-background-fill);
301
+ }
302
+
303
+ .drag-handle {
304
+ cursor: grab;
305
+ color: var(--body-text-color-subdued);
306
+ padding-right: var(--size-2);
307
+ user-select: none;
308
+ }
309
+
310
+ .dragging {
311
+ opacity: 0.5;
312
+ cursor: grabbing;
313
+ }
314
+
315
+ .drop-target {
316
+ border-top: 2px solid var(--color-accent);
317
+ }
318
+
319
+ tr:last-child.drop-target[data-drop-target="before"] {
320
+ border-top: 2px solid var(--color-accent);
321
+ border-bottom: none;
322
+ }
323
+
324
+ tr:last-child.drop-target[data-drop-target="after"] {
325
+ border-top: none;
326
+ border-bottom: 2px solid var(--color-accent);
327
+ }
328
+ </style>
6.4.1/file/shared/FileUpload.svelte ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick } from "svelte";
3
+ import { Upload, ModifyUpload } from "@gradio/upload";
4
+ import type { FileData, Client } from "@gradio/client";
5
+ import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
6
+ import { File, Clear, Upload as UploadIcon } from "@gradio/icons";
7
+
8
+ import FilePreview from "./FilePreview.svelte";
9
+ import type { I18nFormatter, SelectData } from "@gradio/utils";
10
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
11
+
12
+ let {
13
+ value = $bindable<null | FileData | FileData[]>(),
14
+ label,
15
+ show_label = true,
16
+ file_count = "single",
17
+ file_types = null,
18
+ selectable = false,
19
+ root,
20
+ height = undefined,
21
+ i18n,
22
+ max_file_size = null,
23
+ upload,
24
+ stream_handler,
25
+ uploading = $bindable(false),
26
+ allow_reordering = false,
27
+ upload_promise = $bindable<Promise<(FileData | null)[]> | null>(),
28
+ buttons = null,
29
+ on_custom_button_click = null,
30
+ onchange,
31
+ onclear,
32
+ ondrag,
33
+ onupload,
34
+ onerror,
35
+ ondelete,
36
+ onselect
37
+ }: {
38
+ value: null | FileData | FileData[];
39
+ label: string;
40
+ show_label?: boolean;
41
+ file_count: "single" | "multiple" | "directory";
42
+ file_types: string[] | null;
43
+ selectable?: boolean;
44
+ root: string;
45
+ height?: number | undefined;
46
+ i18n: I18nFormatter;
47
+ max_file_size: number | null;
48
+ upload: Client["upload"];
49
+ stream_handler: Client["stream"];
50
+ uploading?: boolean;
51
+ allow_reordering?: boolean;
52
+ upload_promise?: Promise<(FileData | null)[]> | null;
53
+ buttons?: (string | CustomButtonType)[] | null;
54
+ on_custom_button_click?: ((id: number) => void) | null;
55
+ onchange?: (event_data: FileData[] | FileData | null) => void;
56
+ onclear?: () => void;
57
+ ondrag?: (dragging: boolean) => void;
58
+ onupload?: (event_data: FileData[] | FileData) => void;
59
+ onerror?: (error: string) => void;
60
+ ondelete?: (event_data: FileData) => void;
61
+ onselect?: (event_data: SelectData) => void;
62
+ } = $props();
63
+
64
+ async function handle_upload(detail: FileData | FileData[]): Promise<void> {
65
+ if (Array.isArray(value)) {
66
+ value = [...value, ...(Array.isArray(detail) ? detail : [detail])];
67
+ } else if (value) {
68
+ value = [value, ...(Array.isArray(detail) ? detail : [detail])];
69
+ } else {
70
+ value = detail;
71
+ }
72
+ await tick();
73
+ onchange?.(value);
74
+ onupload?.(value);
75
+ }
76
+
77
+ function handle_clear(): void {
78
+ value = null;
79
+ onchange?.(null);
80
+ onclear?.();
81
+ }
82
+
83
+ let dragging = $state(false);
84
+ </script>
85
+
86
+ {#if show_label && buttons && buttons.length > 0}
87
+ <IconButtonWrapper {buttons} {on_custom_button_click} />
88
+ {/if}
89
+ <BlockLabel {show_label} Icon={File} float={!value} label={label || "File"} />
90
+
91
+ {#if value && (Array.isArray(value) ? value.length > 0 : true)}
92
+ <IconButtonWrapper buttons={buttons || []} {on_custom_button_click}>
93
+ {#if !(file_count === "single" && (Array.isArray(value) ? value.length > 0 : value !== null))}
94
+ <IconButton Icon={UploadIcon} label={i18n("common.upload")}>
95
+ <Upload
96
+ bind:upload_promise
97
+ icon_upload={true}
98
+ onload={handle_upload}
99
+ filetype={file_types}
100
+ {file_count}
101
+ {max_file_size}
102
+ {root}
103
+ bind:dragging
104
+ bind:uploading
105
+ {onerror}
106
+ {stream_handler}
107
+ {upload}
108
+ />
109
+ </IconButton>
110
+ {/if}
111
+ <IconButton
112
+ Icon={Clear}
113
+ label={i18n("common.clear")}
114
+ onclick={(event) => {
115
+ event.stopPropagation();
116
+ handle_clear();
117
+ }}
118
+ />
119
+ </IconButtonWrapper>
120
+
121
+ <FilePreview
122
+ {i18n}
123
+ {onselect}
124
+ {selectable}
125
+ {value}
126
+ {height}
127
+ {onchange}
128
+ {ondelete}
129
+ {allow_reordering}
130
+ />
131
+ {:else}
132
+ <Upload
133
+ bind:upload_promise
134
+ onload={handle_upload}
135
+ filetype={file_types}
136
+ {file_count}
137
+ {max_file_size}
138
+ {root}
139
+ bind:dragging
140
+ bind:uploading
141
+ {onerror}
142
+ {stream_handler}
143
+ {upload}
144
+ {height}
145
+ >
146
+ <slot />
147
+ </Upload>
148
+ {/if}
6.4.1/file/shared/utils.ts ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+
3
+ export const prettyBytes = (bytes: number): string => {
4
+ let units = ["B", "KB", "MB", "GB", "PB"];
5
+ let i = 0;
6
+ while (bytes > 1024) {
7
+ bytes /= 1024;
8
+ i++;
9
+ }
10
+ let unit = units[i];
11
+ return bytes.toFixed(1) + "&nbsp;" + unit;
12
+ };
6.4.1/file/types.ts ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+ import type { LoadingStatus } from "js/statustracker";
3
+ import type { SelectData, CustomButton } from "@gradio/utils";
4
+
5
+ export interface FileProps {
6
+ value: FileData | FileData[] | null;
7
+ file_types: string[];
8
+ file_count: "single" | "multiple" | "directory";
9
+ allow_reordering: boolean;
10
+ type: "filepath" | "binary";
11
+ _selectable: boolean;
12
+ height: number | null;
13
+ buttons: (string | CustomButton)[] | null;
14
+ }
15
+
16
+ export interface FileEvents {
17
+ upload: FileData | FileData[];
18
+ download: void;
19
+ error: string;
20
+ clear_status: LoadingStatus;
21
+ clear: void;
22
+ select: SelectData | null;
23
+ change: FileData | FileData[] | null;
24
+ delete: FileData;
25
+ custom_button_click: { id: number };
26
+ }