freddyaboulton HF Staff commited on
Commit
0918187
·
verified ·
1 Parent(s): 5d54b49

Upload folder using huggingface_hub

Browse files
6.0.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
+ on:click={() => gradio.dispatch("click")}
50
+ on:change={({ detail }) => handle_event(detail, "change")}
51
+ on:upload={({ detail }) => handle_event(detail, "upload")}
52
+ on:error={({ detail }) => {
53
+ gradio.dispatch("error", detail);
54
+ }}
55
+ upload={(...args) => gradio.shared.client.upload(...args)}
56
+ >
57
+ {gradio.shared.label ?? ""}
58
+ </UploadButton>
6.0.0/uploadbutton/package.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/uploadbutton",
3
+ "version": "0.9.13",
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.43.4"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/gradio-app/gradio.git",
34
+ "directory": "js/uploadbutton"
35
+ }
36
+ }
6.0.0/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>
6.0.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
+ }