Upload folder using huggingface_hub
Browse files- 6.4.0/upload/src/ModifyUpload.svelte +24 -17
- 6.4.0/upload/src/Upload.svelte +75 -51
- 6.4.0/upload/src/UploadProgress.svelte +33 -23
6.4.0/upload/src/ModifyUpload.svelte
CHANGED
|
@@ -4,18 +4,25 @@
|
|
| 4 |
import { Edit, Clear, Undo, Download } from "@gradio/icons";
|
| 5 |
import { DownloadLink } from "@gradio/atoms";
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</script>
|
| 20 |
|
| 21 |
<IconButtonWrapper>
|
|
@@ -23,7 +30,7 @@
|
|
| 23 |
<IconButton
|
| 24 |
Icon={Edit}
|
| 25 |
label={i18n("common.edit")}
|
| 26 |
-
|
| 27 |
/>
|
| 28 |
{/if}
|
| 29 |
|
|
@@ -31,7 +38,7 @@
|
|
| 31 |
<IconButton
|
| 32 |
Icon={Undo}
|
| 33 |
label={i18n("common.undo")}
|
| 34 |
-
|
| 35 |
/>
|
| 36 |
{/if}
|
| 37 |
|
|
@@ -41,13 +48,13 @@
|
|
| 41 |
</DownloadLink>
|
| 42 |
{/if}
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
<IconButton
|
| 47 |
Icon={Clear}
|
| 48 |
label={i18n("common.clear")}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
event.stopPropagation();
|
| 52 |
}}
|
| 53 |
/>
|
|
|
|
| 4 |
import { Edit, Clear, Undo, Download } from "@gradio/icons";
|
| 5 |
import { DownloadLink } from "@gradio/atoms";
|
| 6 |
|
| 7 |
+
let {
|
| 8 |
+
editable = false,
|
| 9 |
+
undoable = false,
|
| 10 |
+
download = null,
|
| 11 |
+
i18n,
|
| 12 |
+
onedit,
|
| 13 |
+
onclear,
|
| 14 |
+
onundo,
|
| 15 |
+
children
|
| 16 |
+
}: {
|
| 17 |
+
editable?: boolean;
|
| 18 |
+
undoable?: boolean;
|
| 19 |
+
download?: string | null;
|
| 20 |
+
i18n: I18nFormatter;
|
| 21 |
+
onedit?: () => void;
|
| 22 |
+
onclear?: () => void;
|
| 23 |
+
onundo?: () => void;
|
| 24 |
+
children?: import("svelte").Snippet;
|
| 25 |
+
} = $props();
|
| 26 |
</script>
|
| 27 |
|
| 28 |
<IconButtonWrapper>
|
|
|
|
| 30 |
<IconButton
|
| 31 |
Icon={Edit}
|
| 32 |
label={i18n("common.edit")}
|
| 33 |
+
onclick={() => onedit?.()}
|
| 34 |
/>
|
| 35 |
{/if}
|
| 36 |
|
|
|
|
| 38 |
<IconButton
|
| 39 |
Icon={Undo}
|
| 40 |
label={i18n("common.undo")}
|
| 41 |
+
onclick={() => onundo?.()}
|
| 42 |
/>
|
| 43 |
{/if}
|
| 44 |
|
|
|
|
| 48 |
</DownloadLink>
|
| 49 |
{/if}
|
| 50 |
|
| 51 |
+
{#if children}{@render children()}{/if}
|
| 52 |
|
| 53 |
<IconButton
|
| 54 |
Icon={Clear}
|
| 55 |
label={i18n("common.clear")}
|
| 56 |
+
onclick={(event) => {
|
| 57 |
+
onclear?.();
|
| 58 |
event.stopPropagation();
|
| 59 |
}}
|
| 60 |
/>
|
6.4.0/upload/src/Upload.svelte
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
<script lang="ts">
|
| 2 |
-
import {
|
| 3 |
import type { FileData } from "@gradio/client";
|
| 4 |
import { prepare_files, type Client } from "@gradio/client";
|
| 5 |
import UploadProgress from "./UploadProgress.svelte";
|
|
@@ -7,32 +7,61 @@
|
|
| 7 |
|
| 8 |
const { drag, open_file_upload: _open_file_upload } = create_drag();
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
export function open_upload(): void {
|
| 31 |
_open_file_upload();
|
| 32 |
}
|
|
|
|
| 33 |
let upload_id: string = "";
|
| 34 |
let file_data: FileData[];
|
| 35 |
-
let accept_file_types: string | null;
|
| 36 |
let use_post_upload_validation: boolean | null = null;
|
| 37 |
|
| 38 |
const get_ios = (): boolean => {
|
|
@@ -43,9 +72,8 @@
|
|
| 43 |
return false;
|
| 44 |
};
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
const dispatch = createEventDispatcher();
|
| 49 |
const validFileTypes = ["image", "video", "audio", "text", "file"];
|
| 50 |
const process_file_type = (type: string): string => {
|
| 51 |
if (ios && type.startsWith(".")) {
|
|
@@ -64,16 +92,18 @@
|
|
| 64 |
return "." + type;
|
| 65 |
};
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
export function paste_clipboard(): void {
|
| 79 |
navigator.clipboard.read().then(async (items) => {
|
|
@@ -117,14 +147,11 @@
|
|
| 117 |
upload_id,
|
| 118 |
max_file_size ?? Infinity
|
| 119 |
);
|
| 120 |
-
|
| 121 |
-
"load",
|
| 122 |
-
file_count === "single" ? _file_data?.[0] : _file_data
|
| 123 |
-
);
|
| 124 |
resolve(_file_data || []);
|
| 125 |
uploading = false;
|
| 126 |
} catch (e) {
|
| 127 |
-
|
| 128 |
uploading = false;
|
| 129 |
resolve([]);
|
| 130 |
}
|
|
@@ -184,10 +211,7 @@
|
|
| 184 |
if (is_valid_file(file)) {
|
| 185 |
return true;
|
| 186 |
}
|
| 187 |
-
|
| 188 |
-
"error",
|
| 189 |
-
`Invalid file type: ${file.name}. Only ${filetype} allowed.`
|
| 190 |
-
);
|
| 191 |
return false;
|
| 192 |
});
|
| 193 |
|
|
@@ -241,17 +265,17 @@
|
|
| 241 |
) {
|
| 242 |
return true;
|
| 243 |
}
|
| 244 |
-
|
| 245 |
return false;
|
| 246 |
});
|
| 247 |
if (format != "blob") {
|
| 248 |
await load_files(files_to_load);
|
| 249 |
} else {
|
| 250 |
if (file_count === "single") {
|
| 251 |
-
|
| 252 |
return;
|
| 253 |
}
|
| 254 |
-
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
|
@@ -266,10 +290,10 @@
|
|
| 266 |
await load_files(files_to_load);
|
| 267 |
} else {
|
| 268 |
if (file_count === "single") {
|
| 269 |
-
|
| 270 |
return;
|
| 271 |
}
|
| 272 |
-
|
| 273 |
}
|
| 274 |
}
|
| 275 |
</script>
|
|
@@ -289,10 +313,10 @@
|
|
| 289 |
: height
|
| 290 |
: "100%"}
|
| 291 |
tabindex={hidden ? -1 : 0}
|
| 292 |
-
|
| 293 |
aria-label={aria_label || "Paste from clipboard"}
|
| 294 |
>
|
| 295 |
-
|
| 296 |
</button>
|
| 297 |
{:else if uploading && show_progress}
|
| 298 |
{#if !hidden}
|
|
@@ -315,7 +339,7 @@
|
|
| 315 |
: "100%"}
|
| 316 |
tabindex={hidden ? -1 : 0}
|
| 317 |
use:drag={{
|
| 318 |
-
on_drag_change: (
|
| 319 |
on_files: (files) => load_files_from_upload(files),
|
| 320 |
accepted_types: accept_file_types,
|
| 321 |
mode: file_count,
|
|
@@ -324,7 +348,7 @@
|
|
| 324 |
aria-label={aria_label || "Click to upload or drop files"}
|
| 325 |
aria-dropeffect="copy"
|
| 326 |
>
|
| 327 |
-
|
| 328 |
</button>
|
| 329 |
{/if}
|
| 330 |
|
|
|
|
| 1 |
<script lang="ts">
|
| 2 |
+
import { tick } from "svelte";
|
| 3 |
import type { FileData } from "@gradio/client";
|
| 4 |
import { prepare_files, type Client } from "@gradio/client";
|
| 5 |
import UploadProgress from "./UploadProgress.svelte";
|
|
|
|
| 7 |
|
| 8 |
const { drag, open_file_upload: _open_file_upload } = create_drag();
|
| 9 |
|
| 10 |
+
let {
|
| 11 |
+
filetype = null,
|
| 12 |
+
dragging = $bindable(false),
|
| 13 |
+
boundedheight = true,
|
| 14 |
+
center = true,
|
| 15 |
+
flex = true,
|
| 16 |
+
file_count = "single",
|
| 17 |
+
disable_click = false,
|
| 18 |
+
root,
|
| 19 |
+
hidden = false,
|
| 20 |
+
format = "file",
|
| 21 |
+
uploading = $bindable(false),
|
| 22 |
+
show_progress = true,
|
| 23 |
+
max_file_size = null,
|
| 24 |
+
upload,
|
| 25 |
+
stream_handler,
|
| 26 |
+
icon_upload = false,
|
| 27 |
+
height = undefined,
|
| 28 |
+
aria_label = undefined,
|
| 29 |
+
upload_promise = $bindable(),
|
| 30 |
+
onload,
|
| 31 |
+
onerror,
|
| 32 |
+
children
|
| 33 |
+
}: {
|
| 34 |
+
filetype?: string | string[] | null;
|
| 35 |
+
dragging?: boolean;
|
| 36 |
+
boundedheight?: boolean;
|
| 37 |
+
center?: boolean;
|
| 38 |
+
flex?: boolean;
|
| 39 |
+
file_count?: "single" | "multiple" | "directory";
|
| 40 |
+
disable_click?: boolean;
|
| 41 |
+
root: string;
|
| 42 |
+
hidden?: boolean;
|
| 43 |
+
format?: "blob" | "file";
|
| 44 |
+
uploading?: boolean;
|
| 45 |
+
show_progress?: boolean;
|
| 46 |
+
max_file_size?: number | null;
|
| 47 |
+
upload: Client["upload"];
|
| 48 |
+
stream_handler: Client["stream"];
|
| 49 |
+
icon_upload?: boolean;
|
| 50 |
+
height?: number | string | undefined;
|
| 51 |
+
aria_label?: string | undefined;
|
| 52 |
+
upload_promise?: Promise<(FileData | null)[]>;
|
| 53 |
+
onload?: (data: FileData | FileData[] | Blob | File) => void;
|
| 54 |
+
onerror?: (error: string) => void;
|
| 55 |
+
children?: import("svelte").Snippet;
|
| 56 |
+
} = $props();
|
| 57 |
|
| 58 |
export function open_upload(): void {
|
| 59 |
_open_file_upload();
|
| 60 |
}
|
| 61 |
+
|
| 62 |
let upload_id: string = "";
|
| 63 |
let file_data: FileData[];
|
| 64 |
+
let accept_file_types: string | null = $state(null);
|
| 65 |
let use_post_upload_validation: boolean | null = null;
|
| 66 |
|
| 67 |
const get_ios = (): boolean => {
|
|
|
|
| 72 |
return false;
|
| 73 |
};
|
| 74 |
|
| 75 |
+
let ios = get_ios();
|
| 76 |
|
|
|
|
| 77 |
const validFileTypes = ["image", "video", "audio", "text", "file"];
|
| 78 |
const process_file_type = (type: string): string => {
|
| 79 |
if (ios && type.startsWith(".")) {
|
|
|
|
| 92 |
return "." + type;
|
| 93 |
};
|
| 94 |
|
| 95 |
+
$effect(() => {
|
| 96 |
+
if (filetype == null) {
|
| 97 |
+
accept_file_types = null;
|
| 98 |
+
} else if (typeof filetype === "string") {
|
| 99 |
+
accept_file_types = process_file_type(filetype);
|
| 100 |
+
} else if (ios && filetype.includes("file/*")) {
|
| 101 |
+
accept_file_types = "*";
|
| 102 |
+
} else {
|
| 103 |
+
const processed = filetype.map(process_file_type);
|
| 104 |
+
accept_file_types = processed.join(", ");
|
| 105 |
+
}
|
| 106 |
+
});
|
| 107 |
|
| 108 |
export function paste_clipboard(): void {
|
| 109 |
navigator.clipboard.read().then(async (items) => {
|
|
|
|
| 147 |
upload_id,
|
| 148 |
max_file_size ?? Infinity
|
| 149 |
);
|
| 150 |
+
onload?.(file_count === "single" ? _file_data?.[0] : _file_data);
|
|
|
|
|
|
|
|
|
|
| 151 |
resolve(_file_data || []);
|
| 152 |
uploading = false;
|
| 153 |
} catch (e) {
|
| 154 |
+
onerror?.((e as Error).message);
|
| 155 |
uploading = false;
|
| 156 |
resolve([]);
|
| 157 |
}
|
|
|
|
| 211 |
if (is_valid_file(file)) {
|
| 212 |
return true;
|
| 213 |
}
|
| 214 |
+
onerror?.(`Invalid file type: ${file.name}. Only ${filetype} allowed.`);
|
|
|
|
|
|
|
|
|
|
| 215 |
return false;
|
| 216 |
});
|
| 217 |
|
|
|
|
| 265 |
) {
|
| 266 |
return true;
|
| 267 |
}
|
| 268 |
+
onerror?.(`Invalid file type only ${filetype} allowed.`);
|
| 269 |
return false;
|
| 270 |
});
|
| 271 |
if (format != "blob") {
|
| 272 |
await load_files(files_to_load);
|
| 273 |
} else {
|
| 274 |
if (file_count === "single") {
|
| 275 |
+
onload?.(files_to_load[0]);
|
| 276 |
return;
|
| 277 |
}
|
| 278 |
+
onload?.(files_to_load);
|
| 279 |
}
|
| 280 |
}
|
| 281 |
|
|
|
|
| 290 |
await load_files(files_to_load);
|
| 291 |
} else {
|
| 292 |
if (file_count === "single") {
|
| 293 |
+
onload?.(files_to_load[0]);
|
| 294 |
return;
|
| 295 |
}
|
| 296 |
+
onload?.(files_to_load);
|
| 297 |
}
|
| 298 |
}
|
| 299 |
</script>
|
|
|
|
| 313 |
: height
|
| 314 |
: "100%"}
|
| 315 |
tabindex={hidden ? -1 : 0}
|
| 316 |
+
onclick={paste_clipboard}
|
| 317 |
aria-label={aria_label || "Paste from clipboard"}
|
| 318 |
>
|
| 319 |
+
{#if children}{@render children()}{/if}
|
| 320 |
</button>
|
| 321 |
{:else if uploading && show_progress}
|
| 322 |
{#if !hidden}
|
|
|
|
| 339 |
: "100%"}
|
| 340 |
tabindex={hidden ? -1 : 0}
|
| 341 |
use:drag={{
|
| 342 |
+
on_drag_change: (d) => (dragging = d),
|
| 343 |
on_files: (files) => load_files_from_upload(files),
|
| 344 |
accepted_types: accept_file_types,
|
| 345 |
mode: file_count,
|
|
|
|
| 348 |
aria-label={aria_label || "Click to upload or drop files"}
|
| 349 |
aria-dropeffect="copy"
|
| 350 |
>
|
| 351 |
+
{#if children}{@render children()}{/if}
|
| 352 |
</button>
|
| 353 |
{/if}
|
| 354 |
|
6.4.0/upload/src/UploadProgress.svelte
CHANGED
|
@@ -1,27 +1,36 @@
|
|
| 1 |
<script lang="ts">
|
| 2 |
import { FileData, type Client } from "@gradio/client";
|
| 3 |
-
import { onMount,
|
| 4 |
|
| 5 |
type FileDataWithProgress = FileData & { progress: number };
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
let stream: Awaited<ReturnType<Client["stream"]>>;
|
| 13 |
-
let progress = false;
|
| 14 |
-
let current_file_upload
|
| 15 |
-
let file_to_display
|
| 16 |
-
|
| 17 |
-
let files_with_progress
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
function handleProgress(filename: string, chunk_size: number): void {
|
| 27 |
// Find the corresponding file in the array and update its progress
|
|
@@ -52,22 +61,23 @@
|
|
| 52 |
if (_data.msg === "done") {
|
| 53 |
// the stream will close itself but is here for clarity; remove .close() in 5.0
|
| 54 |
stream?.close();
|
| 55 |
-
|
| 56 |
} else {
|
| 57 |
current_file_upload = _data;
|
| 58 |
handleProgress(_data.orig_name, _data.chunk_size);
|
| 59 |
}
|
| 60 |
};
|
| 61 |
});
|
|
|
|
| 62 |
onDestroy(() => {
|
| 63 |
// the stream will close itself but is here for clarity; remove .close() in 5.0
|
| 64 |
if (stream != null || stream != undefined) stream.close();
|
| 65 |
});
|
| 66 |
|
| 67 |
-
function calculateTotalProgress(files:
|
| 68 |
let totalProgress = 0;
|
| 69 |
files.forEach((file) => {
|
| 70 |
-
totalProgress += getProgress(file
|
| 71 |
});
|
| 72 |
|
| 73 |
document.documentElement.style.setProperty(
|
|
@@ -78,9 +88,9 @@
|
|
| 78 |
return totalProgress / files.length;
|
| 79 |
}
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
</script>
|
| 85 |
|
| 86 |
<div class="wrap" class:progress>
|
|
|
|
| 1 |
<script lang="ts">
|
| 2 |
import { FileData, type Client } from "@gradio/client";
|
| 3 |
+
import { onMount, onDestroy } from "svelte";
|
| 4 |
|
| 5 |
type FileDataWithProgress = FileData & { progress: number };
|
| 6 |
|
| 7 |
+
let {
|
| 8 |
+
upload_id,
|
| 9 |
+
root,
|
| 10 |
+
files,
|
| 11 |
+
stream_handler,
|
| 12 |
+
ondone
|
| 13 |
+
}: {
|
| 14 |
+
upload_id: string;
|
| 15 |
+
root: string;
|
| 16 |
+
files: FileData[];
|
| 17 |
+
stream_handler: Client["stream"];
|
| 18 |
+
ondone?: () => void;
|
| 19 |
+
} = $props();
|
| 20 |
|
| 21 |
let stream: Awaited<ReturnType<Client["stream"]>>;
|
| 22 |
+
let progress = $state(false);
|
| 23 |
+
let current_file_upload = $state<FileDataWithProgress>();
|
| 24 |
+
let file_to_display = $derived(current_file_upload || files_with_progress[0]);
|
| 25 |
+
|
| 26 |
+
let files_with_progress = $state<FileDataWithProgress[]>(
|
| 27 |
+
files.map((file) => {
|
| 28 |
+
return {
|
| 29 |
+
...file,
|
| 30 |
+
progress: 0
|
| 31 |
+
};
|
| 32 |
+
})
|
| 33 |
+
);
|
| 34 |
|
| 35 |
function handleProgress(filename: string, chunk_size: number): void {
|
| 36 |
// Find the corresponding file in the array and update its progress
|
|
|
|
| 61 |
if (_data.msg === "done") {
|
| 62 |
// the stream will close itself but is here for clarity; remove .close() in 5.0
|
| 63 |
stream?.close();
|
| 64 |
+
ondone?.();
|
| 65 |
} else {
|
| 66 |
current_file_upload = _data;
|
| 67 |
handleProgress(_data.orig_name, _data.chunk_size);
|
| 68 |
}
|
| 69 |
};
|
| 70 |
});
|
| 71 |
+
|
| 72 |
onDestroy(() => {
|
| 73 |
// the stream will close itself but is here for clarity; remove .close() in 5.0
|
| 74 |
if (stream != null || stream != undefined) stream.close();
|
| 75 |
});
|
| 76 |
|
| 77 |
+
function calculateTotalProgress(files: FileDataWithProgress[]): number {
|
| 78 |
let totalProgress = 0;
|
| 79 |
files.forEach((file) => {
|
| 80 |
+
totalProgress += getProgress(file);
|
| 81 |
});
|
| 82 |
|
| 83 |
document.documentElement.style.setProperty(
|
|
|
|
| 88 |
return totalProgress / files.length;
|
| 89 |
}
|
| 90 |
|
| 91 |
+
$effect(() => {
|
| 92 |
+
calculateTotalProgress(files_with_progress);
|
| 93 |
+
});
|
| 94 |
</script>
|
| 95 |
|
| 96 |
<div class="wrap" class:progress>
|