gradio-pr-bot commited on
Commit
0303d7a
·
verified ·
1 Parent(s): c94dee6

Upload folder using huggingface_hub

Browse files
6.5.0/uploadbutton/Index.svelte ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts" context="module">
2
+ export { default as BaseUploadButton } from "./shared/UploadButton.svelte";
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import type { UploadButtonProps, UploadButtonEvents } from "./types";
7
+ import type { FileData } from "@gradio/client";
8
+ import { Gradio } from "@gradio/utils";
9
+ import UploadButton from "./shared/UploadButton.svelte";
10
+
11
+ const props = $props();
12
+ const gradio = new Gradio<UploadButtonEvents, UploadButtonProps>(props);
13
+
14
+ let value = $state(gradio.props.value);
15
+
16
+ $effect(() => {
17
+ if (value !== gradio.props.value) {
18
+ gradio.props.value = value;
19
+ }
20
+ });
21
+
22
+ async function handle_event(
23
+ detail: null | FileData | FileData[],
24
+ event: "change" | "upload" | "click"
25
+ ): Promise<void> {
26
+ gradio.props.value = detail;
27
+ gradio.dispatch(event);
28
+ }
29
+
30
+ const disabled = $derived(!gradio.shared.interactive);
31
+ </script>
32
+
33
+ <UploadButton
34
+ elem_id={gradio.shared.elem_id}
35
+ elem_classes={gradio.shared.elem_classes}
36
+ visible={gradio.shared.visible}
37
+ file_count={gradio.props.file_count}
38
+ file_types={gradio.props.file_types}
39
+ size={gradio.props.size}
40
+ scale={gradio.shared.scale}
41
+ icon={gradio.props.icon}
42
+ min_width={gradio.shared.min_width}
43
+ root={gradio.shared.root}
44
+ {value}
45
+ {disabled}
46
+ variant={gradio.props.variant}
47
+ label={gradio.shared.label}
48
+ max_file_size={gradio.shared.max_file_size}
49
+ onclick={() => gradio.dispatch("click")}
50
+ onchange={(detail) => handle_event(detail, "change")}
51
+ onupload={(detail) => handle_event(detail, "upload")}
52
+ onerror={(detail) => {
53
+ gradio.dispatch("error", detail);
54
+ }}
55
+ upload={(...args) => gradio.shared.client.upload(...args)}
56
+ >
57
+ {gradio.shared.label ?? ""}
58
+ </UploadButton>
6.5.0/uploadbutton/package.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/uploadbutton",
3
+ "version": "0.9.17",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": {
12
+ "gradio": "./Index.svelte",
13
+ "svelte": "./dist/Index.svelte",
14
+ "types": "./dist/Index.svelte.d.ts"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "main": "./Index.svelte",
19
+ "dependencies": {
20
+ "@gradio/button": "workspace:^",
21
+ "@gradio/client": "workspace:^",
22
+ "@gradio/upload": "workspace:^",
23
+ "@gradio/utils": "workspace:^"
24
+ },
25
+ "devDependencies": {
26
+ "@gradio/preview": "workspace:^"
27
+ },
28
+ "peerDependencies": {
29
+ "svelte": "^5.48.0"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/gradio-app/gradio.git",
34
+ "directory": "js/uploadbutton"
35
+ }
36
+ }
6.5.0/uploadbutton/shared/UploadButton.svelte ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick } from "svelte";
3
+ import { BaseButton } from "@gradio/button";
4
+ import { prepare_files, type FileData, type Client } from "@gradio/client";
5
+
6
+ let {
7
+ elem_id = "",
8
+ elem_classes = [],
9
+ visible = true,
10
+ label,
11
+ value,
12
+ file_count,
13
+ file_types = [],
14
+ root,
15
+ size = "lg",
16
+ icon = null,
17
+ scale = null,
18
+ min_width = undefined,
19
+ variant = "secondary",
20
+ disabled = false,
21
+ max_file_size = null,
22
+ upload,
23
+ onclick,
24
+ onchange,
25
+ onupload,
26
+ onerror,
27
+ children
28
+ }: {
29
+ elem_id?: string;
30
+ elem_classes?: string[];
31
+ visible?: boolean | "hidden";
32
+ label: string | null;
33
+ value?: null | FileData | FileData[];
34
+ file_count: string;
35
+ file_types?: string[];
36
+ root: string;
37
+ size?: "sm" | "md" | "lg";
38
+ icon?: FileData | null;
39
+ scale?: number | null;
40
+ min_width?: number | undefined;
41
+ variant?: "primary" | "secondary" | "stop";
42
+ disabled?: boolean;
43
+ max_file_size?: number | null;
44
+ upload: Client["upload"];
45
+ onclick?: () => void;
46
+ onchange?: (value: null | FileData | FileData[]) => void;
47
+ onupload?: (value: null | FileData | FileData[]) => void;
48
+ onerror?: (message: string) => void;
49
+ children?: import("svelte").Snippet;
50
+ } = $props();
51
+
52
+ let hidden_upload: HTMLInputElement;
53
+
54
+ let accept_file_types = $derived.by(() => {
55
+ if (file_types == null) {
56
+ return null;
57
+ }
58
+ const mapped = file_types.map((x) => {
59
+ if (x.startsWith(".")) {
60
+ return x;
61
+ }
62
+ return x + "/*";
63
+ });
64
+ return mapped.join(", ");
65
+ });
66
+
67
+ function open_file_upload(): void {
68
+ onclick?.();
69
+ hidden_upload.click();
70
+ }
71
+
72
+ async function load_files(files: FileList): Promise<void> {
73
+ let _files: File[] = Array.from(files);
74
+
75
+ if (!files.length) {
76
+ return;
77
+ }
78
+ if (file_count === "single") {
79
+ _files = [files[0]];
80
+ }
81
+ let all_file_data = await prepare_files(_files);
82
+ await tick();
83
+
84
+ try {
85
+ all_file_data = (
86
+ await upload(all_file_data, root, undefined, max_file_size ?? Infinity)
87
+ )?.filter((x) => x !== null) as FileData[];
88
+ } catch (e) {
89
+ onerror?.((e as Error).message);
90
+ return;
91
+ }
92
+ const new_value =
93
+ file_count === "single" ? all_file_data?.[0] : all_file_data;
94
+ onchange?.(new_value);
95
+ onupload?.(new_value);
96
+ }
97
+
98
+ async function load_files_from_upload(e: Event): Promise<void> {
99
+ const target = e.target as HTMLInputElement;
100
+
101
+ if (!target.files) return;
102
+ await load_files(target.files);
103
+ }
104
+
105
+ function clear_input_value(e: Event): void {
106
+ const target = e.target as HTMLInputElement;
107
+ if (target.value) target.value = "";
108
+ }
109
+ </script>
110
+
111
+ <input
112
+ class="hide"
113
+ accept={accept_file_types}
114
+ type="file"
115
+ bind:this={hidden_upload}
116
+ onchange={load_files_from_upload}
117
+ onclick={clear_input_value}
118
+ multiple={file_count === "multiple" || undefined}
119
+ webkitdirectory={file_count === "directory" || undefined}
120
+ mozdirectory={file_count === "directory" || undefined}
121
+ data-testid="{label}-upload-button"
122
+ />
123
+
124
+ <BaseButton
125
+ {size}
126
+ {variant}
127
+ {elem_id}
128
+ {elem_classes}
129
+ {visible}
130
+ onclick={open_file_upload}
131
+ {scale}
132
+ {min_width}
133
+ {disabled}
134
+ >
135
+ {#if icon}
136
+ <img class="button-icon" src={icon.url} alt={`${value} icon`} />
137
+ {/if}
138
+ {#if children}{@render children()}{/if}
139
+ </BaseButton>
140
+
141
+ <style>
142
+ .hide {
143
+ display: none;
144
+ }
145
+ .button-icon {
146
+ width: var(--text-xl);
147
+ height: var(--text-xl);
148
+ margin-right: var(--spacing-xl);
149
+ }
150
+ </style>
6.5.0/uploadbutton/types.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+
3
+ export interface UploadButtonProps {
4
+ value: null | FileData | FileData[];
5
+ file_count: string;
6
+ file_types: string[];
7
+ size: "sm" | "lg";
8
+ icon: FileData | null;
9
+ variant: "primary" | "secondary" | "stop";
10
+ }
11
+
12
+ export interface UploadButtonEvents {
13
+ change: never;
14
+ upload: never;
15
+ click: never;
16
+ error: string;
17
+ }