gradio-pr-bot commited on
Commit
743cab7
·
verified ·
1 Parent(s): b19cb3f

Upload folder using huggingface_hub

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