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

Upload folder using huggingface_hub

Browse files
5.49.1/uploadbutton/Index.svelte ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { Gradio } from "@gradio/utils";
7
+ import type { FileData } from "@gradio/client";
8
+ import UploadButton from "./shared/UploadButton.svelte";
9
+
10
+ export let elem_id = "";
11
+ export let elem_classes: string[] = [];
12
+ export let visible: boolean | "hidden" = true;
13
+ export let label: string | null;
14
+ export let value: null | FileData | FileData[];
15
+ export let file_count: string;
16
+ export let file_types: string[] = [];
17
+ export let root: string;
18
+ export let size: "sm" | "lg" = "lg";
19
+ export let scale: number | null = null;
20
+ export let icon: FileData | null = null;
21
+ export let min_width: number | undefined = undefined;
22
+ export let variant: "primary" | "secondary" | "stop" = "secondary";
23
+ export let gradio: Gradio<{
24
+ change: never;
25
+ upload: never;
26
+ click: never;
27
+ error: string;
28
+ }>;
29
+ export let interactive: boolean;
30
+
31
+ $: disabled = !interactive;
32
+
33
+ async function handle_event(
34
+ detail: null | FileData | FileData[],
35
+ event: "change" | "upload" | "click"
36
+ ): Promise<void> {
37
+ value = detail;
38
+ gradio.dispatch(event);
39
+ }
40
+ </script>
41
+
42
+ <UploadButton
43
+ {elem_id}
44
+ {elem_classes}
45
+ {visible}
46
+ {file_count}
47
+ {file_types}
48
+ {size}
49
+ {scale}
50
+ {icon}
51
+ {min_width}
52
+ {root}
53
+ {value}
54
+ {disabled}
55
+ {variant}
56
+ {label}
57
+ max_file_size={gradio.max_file_size}
58
+ on:click={() => gradio.dispatch("click")}
59
+ on:change={({ detail }) => handle_event(detail, "change")}
60
+ on:upload={({ detail }) => handle_event(detail, "upload")}
61
+ on:error={({ detail }) => {
62
+ gradio.dispatch("error", detail);
63
+ }}
64
+ upload={(...args) => gradio.client.upload(...args)}
65
+ >
66
+ {label ?? ""}
67
+ </UploadButton>
5.49.1/uploadbutton/package.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/uploadbutton",
3
+ "version": "0.9.12",
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": "^4.0.0"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/gradio-app/gradio.git",
34
+ "directory": "js/uploadbutton"
35
+ }
36
+ }
5.49.1/uploadbutton/shared/UploadButton.svelte ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick, createEventDispatcher } from "svelte";
3
+ import { BaseButton } from "@gradio/button";
4
+ import { prepare_files, type FileData, type Client } from "@gradio/client";
5
+
6
+ export let elem_id = "";
7
+ export let elem_classes: string[] = [];
8
+ export let visible: boolean | "hidden" = true;
9
+ export let label: string | null;
10
+ export let value: null | FileData | FileData[];
11
+ export let file_count: string;
12
+ export let file_types: string[] = [];
13
+ export let root: string;
14
+ export let size: "sm" | "md" | "lg" = "lg";
15
+ export let icon: FileData | null = null;
16
+ export let scale: number | null = null;
17
+ export let min_width: number | undefined = undefined;
18
+ export let variant: "primary" | "secondary" | "stop" = "secondary";
19
+ export let disabled = false;
20
+ export let max_file_size: number | null = null;
21
+ export let upload: Client["upload"];
22
+
23
+ const dispatch = createEventDispatcher();
24
+
25
+ let hidden_upload: HTMLInputElement;
26
+ let accept_file_types: string | null;
27
+
28
+ if (file_types == null) {
29
+ accept_file_types = null;
30
+ } else {
31
+ file_types = file_types.map((x) => {
32
+ if (x.startsWith(".")) {
33
+ return x;
34
+ }
35
+ return x + "/*";
36
+ });
37
+ accept_file_types = file_types.join(", ");
38
+ }
39
+
40
+ function open_file_upload(): void {
41
+ dispatch("click");
42
+ hidden_upload.click();
43
+ }
44
+
45
+ async function load_files(files: FileList): Promise<void> {
46
+ let _files: File[] = Array.from(files);
47
+
48
+ if (!files.length) {
49
+ return;
50
+ }
51
+ if (file_count === "single") {
52
+ _files = [files[0]];
53
+ }
54
+ let all_file_data = await prepare_files(_files);
55
+ await tick();
56
+
57
+ try {
58
+ all_file_data = (
59
+ await upload(all_file_data, root, undefined, max_file_size ?? Infinity)
60
+ )?.filter((x) => x !== null) as FileData[];
61
+ } catch (e) {
62
+ dispatch("error", (e as Error).message);
63
+ return;
64
+ }
65
+ value = file_count === "single" ? all_file_data?.[0] : all_file_data;
66
+ dispatch("change", value);
67
+ dispatch("upload", value);
68
+ }
69
+
70
+ async function load_files_from_upload(e: Event): Promise<void> {
71
+ const target = e.target as HTMLInputElement;
72
+
73
+ if (!target.files) return;
74
+ await load_files(target.files);
75
+ }
76
+
77
+ function clear_input_value(e: Event): void {
78
+ const target = e.target as HTMLInputElement;
79
+ if (target.value) target.value = "";
80
+ }
81
+ </script>
82
+
83
+ <input
84
+ class="hide"
85
+ accept={accept_file_types}
86
+ type="file"
87
+ bind:this={hidden_upload}
88
+ on:change={load_files_from_upload}
89
+ on:click={clear_input_value}
90
+ multiple={file_count === "multiple" || undefined}
91
+ webkitdirectory={file_count === "directory" || undefined}
92
+ mozdirectory={file_count === "directory" || undefined}
93
+ data-testid="{label}-upload-button"
94
+ />
95
+
96
+ <BaseButton
97
+ {size}
98
+ {variant}
99
+ {elem_id}
100
+ {elem_classes}
101
+ {visible}
102
+ on:click={open_file_upload}
103
+ {scale}
104
+ {min_width}
105
+ {disabled}
106
+ >
107
+ {#if icon}
108
+ <img class="button-icon" src={icon.url} alt={`${value} icon`} />
109
+ {/if}
110
+ <slot />
111
+ </BaseButton>
112
+
113
+ <style>
114
+ .hide {
115
+ display: none;
116
+ }
117
+ .button-icon {
118
+ width: var(--text-xl);
119
+ height: var(--text-xl);
120
+ margin-right: var(--spacing-xl);
121
+ }
122
+ </style>