Upload folder using huggingface_hub
Browse files- 6.0.0/file/Example.svelte +30 -0
- 6.0.0/file/Index.svelte +117 -0
- 6.0.0/file/package.json +43 -0
- 6.0.0/file/shared/File.svelte +36 -0
- 6.0.0/file/shared/FilePreview.svelte +314 -0
- 6.0.0/file/shared/FileUpload.svelte +122 -0
- 6.0.0/file/shared/utils.ts +12 -0
- 6.0.0/file/types.ts +24 -0
6.0.0/file/Example.svelte
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import type { FileData } from "@gradio/client";
|
| 3 |
+
|
| 4 |
+
export let value: FileData | null;
|
| 5 |
+
export let type: "gallery" | "table";
|
| 6 |
+
export let selected = false;
|
| 7 |
+
</script>
|
| 8 |
+
|
| 9 |
+
<div
|
| 10 |
+
class:table={type === "table"}
|
| 11 |
+
class:gallery={type === "gallery"}
|
| 12 |
+
class:selected
|
| 13 |
+
>
|
| 14 |
+
{value ? (Array.isArray(value) ? value.join(", ") : value) : ""}
|
| 15 |
+
</div>
|
| 16 |
+
|
| 17 |
+
<style>
|
| 18 |
+
div {
|
| 19 |
+
overflow: hidden;
|
| 20 |
+
text-overflow: ellipsis;
|
| 21 |
+
white-space: nowrap;
|
| 22 |
+
}
|
| 23 |
+
.gallery {
|
| 24 |
+
display: flex;
|
| 25 |
+
align-items: center;
|
| 26 |
+
cursor: pointer;
|
| 27 |
+
padding: var(--size-1) var(--size-2);
|
| 28 |
+
text-align: left;
|
| 29 |
+
}
|
| 30 |
+
</style>
|
6.0.0/file/Index.svelte
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} />
|
| 2 |
+
|
| 3 |
+
<script context="module" lang="ts">
|
| 4 |
+
export { default as FilePreview } from "./shared/FilePreview.svelte";
|
| 5 |
+
export { default as BaseFileUpload } from "./shared/FileUpload.svelte";
|
| 6 |
+
export { default as BaseFile } from "./shared/File.svelte";
|
| 7 |
+
export { default as BaseExample } from "./Example.svelte";
|
| 8 |
+
</script>
|
| 9 |
+
|
| 10 |
+
<script lang="ts">
|
| 11 |
+
import { Gradio } from "@gradio/utils";
|
| 12 |
+
import File from "./shared/File.svelte";
|
| 13 |
+
import FileUpload from "./shared/FileUpload.svelte";
|
| 14 |
+
import { Block, UploadText } from "@gradio/atoms";
|
| 15 |
+
import type { FileEvents, FileProps } from "./types";
|
| 16 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 17 |
+
import { tick } from "svelte";
|
| 18 |
+
|
| 19 |
+
const props = $props();
|
| 20 |
+
let upload_promise = $state<Promise<any>>();
|
| 21 |
+
|
| 22 |
+
let dragging = $state(false);
|
| 23 |
+
let pending_upload = $state(false);
|
| 24 |
+
|
| 25 |
+
class FileGradio extends Gradio<FileEvents, FileProps> {
|
| 26 |
+
async get_data() {
|
| 27 |
+
if (upload_promise) {
|
| 28 |
+
await upload_promise;
|
| 29 |
+
await tick();
|
| 30 |
+
}
|
| 31 |
+
const data = await super.get_data();
|
| 32 |
+
|
| 33 |
+
return data;
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
const gradio = new FileGradio(props);
|
| 38 |
+
|
| 39 |
+
let old_value = $state(gradio.props.value);
|
| 40 |
+
|
| 41 |
+
$effect(() => {
|
| 42 |
+
if (old_value !== gradio.props.value) {
|
| 43 |
+
old_value = gradio.props.value;
|
| 44 |
+
gradio.dispatch("change", $state.snapshot(gradio.props.value));
|
| 45 |
+
}
|
| 46 |
+
});
|
| 47 |
+
</script>
|
| 48 |
+
|
| 49 |
+
<Block
|
| 50 |
+
visible={gradio.shared.visible}
|
| 51 |
+
variant={gradio.props.value ? "solid" : "dashed"}
|
| 52 |
+
border_mode={dragging ? "focus" : "base"}
|
| 53 |
+
padding={false}
|
| 54 |
+
elem_id={gradio.shared.elem_id}
|
| 55 |
+
elem_classes={gradio.shared.elem_classes}
|
| 56 |
+
container={gradio.shared.container}
|
| 57 |
+
scale={gradio.shared.scale}
|
| 58 |
+
min_width={gradio.shared.min_width}
|
| 59 |
+
allow_overflow={false}
|
| 60 |
+
>
|
| 61 |
+
<StatusTracker
|
| 62 |
+
autoscroll={gradio.shared.autoscroll}
|
| 63 |
+
i18n={gradio.i18n}
|
| 64 |
+
{...gradio.shared.loading_status}
|
| 65 |
+
status={pending_upload
|
| 66 |
+
? "generating"
|
| 67 |
+
: gradio.shared.loading_status?.status || "complete"}
|
| 68 |
+
on_clear_status={() =>
|
| 69 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 70 |
+
/>
|
| 71 |
+
{#if !gradio.shared.interactive}
|
| 72 |
+
<File
|
| 73 |
+
on_select={({ detail }) => gradio.dispatch("select", detail)}
|
| 74 |
+
on_download={({ detail }) => gradio.dispatch("download", detail)}
|
| 75 |
+
selectable={gradio.props._selectable}
|
| 76 |
+
value={gradio.props.value}
|
| 77 |
+
label={gradio.shared.label}
|
| 78 |
+
show_label={gradio.shared.show_label}
|
| 79 |
+
height={gradio.props.height}
|
| 80 |
+
i18n={gradio.i18n}
|
| 81 |
+
/>
|
| 82 |
+
{:else}
|
| 83 |
+
<FileUpload
|
| 84 |
+
bind:upload_promise
|
| 85 |
+
upload={(...args) => gradio.shared.client.upload(...args)}
|
| 86 |
+
stream_handler={(...args) => gradio.shared.client.stream(...args)}
|
| 87 |
+
label={gradio.shared.label}
|
| 88 |
+
show_label={gradio.shared.show_label}
|
| 89 |
+
value={gradio.props.value}
|
| 90 |
+
file_count={gradio.props.file_count}
|
| 91 |
+
file_types={gradio.props.file_types}
|
| 92 |
+
selectable={gradio.props._selectable}
|
| 93 |
+
height={gradio.props.height}
|
| 94 |
+
root={gradio.shared.root}
|
| 95 |
+
allow_reordering={gradio.props.allow_reordering}
|
| 96 |
+
max_file_size={gradio.shared.max_file_size}
|
| 97 |
+
on:change={({ detail }) => {
|
| 98 |
+
gradio.props.value = detail;
|
| 99 |
+
}}
|
| 100 |
+
on:drag={({ detail }) => (dragging = detail)}
|
| 101 |
+
on:clear={() => gradio.dispatch("clear")}
|
| 102 |
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
| 103 |
+
on:upload={() => gradio.dispatch("upload")}
|
| 104 |
+
on:error={({ detail }) => {
|
| 105 |
+
gradio.shared.loading_status = gradio.shared.loading_status || {};
|
| 106 |
+
gradio.shared.loading_status.status = "error";
|
| 107 |
+
gradio.dispatch("error", detail);
|
| 108 |
+
}}
|
| 109 |
+
on:delete={({ detail }) => {
|
| 110 |
+
gradio.dispatch("delete", detail);
|
| 111 |
+
}}
|
| 112 |
+
i18n={gradio.i18n}
|
| 113 |
+
>
|
| 114 |
+
<UploadText i18n={gradio.i18n} type="file" />
|
| 115 |
+
</FileUpload>
|
| 116 |
+
{/if}
|
| 117 |
+
</Block>
|
6.0.0/file/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/file",
|
| 3 |
+
"version": "0.13.1",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"@gradio/atoms": "workspace:^",
|
| 11 |
+
"@gradio/client": "workspace:^",
|
| 12 |
+
"@gradio/icons": "workspace:^",
|
| 13 |
+
"@gradio/statustracker": "workspace:^",
|
| 14 |
+
"@gradio/upload": "workspace:^",
|
| 15 |
+
"@gradio/utils": "workspace:^"
|
| 16 |
+
},
|
| 17 |
+
"devDependencies": {
|
| 18 |
+
"@gradio/preview": "workspace:^"
|
| 19 |
+
},
|
| 20 |
+
"main": "./Index.svelte",
|
| 21 |
+
"main_changeset": true,
|
| 22 |
+
"exports": {
|
| 23 |
+
".": {
|
| 24 |
+
"gradio": "./Index.svelte",
|
| 25 |
+
"svelte": "./dist/Index.svelte",
|
| 26 |
+
"types": "./dist/Index.svelte.d.ts"
|
| 27 |
+
},
|
| 28 |
+
"./example": {
|
| 29 |
+
"gradio": "./Example.svelte",
|
| 30 |
+
"svelte": "./dist/Example.svelte",
|
| 31 |
+
"types": "./dist/Example.svelte.d.ts"
|
| 32 |
+
},
|
| 33 |
+
"./package.json": "./package.json"
|
| 34 |
+
},
|
| 35 |
+
"peerDependencies": {
|
| 36 |
+
"svelte": "^5.43.4"
|
| 37 |
+
},
|
| 38 |
+
"repository": {
|
| 39 |
+
"type": "git",
|
| 40 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 41 |
+
"directory": "js/file"
|
| 42 |
+
}
|
| 43 |
+
}
|
6.0.0/file/shared/File.svelte
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { BlockLabel, Empty } from "@gradio/atoms";
|
| 3 |
+
import { File } from "@gradio/icons";
|
| 4 |
+
import FilePreview from "./FilePreview.svelte";
|
| 5 |
+
|
| 6 |
+
let {
|
| 7 |
+
value,
|
| 8 |
+
label,
|
| 9 |
+
show_label,
|
| 10 |
+
selectable,
|
| 11 |
+
i18n,
|
| 12 |
+
height,
|
| 13 |
+
on_select,
|
| 14 |
+
on_download
|
| 15 |
+
} = $props();
|
| 16 |
+
</script>
|
| 17 |
+
|
| 18 |
+
<BlockLabel
|
| 19 |
+
{show_label}
|
| 20 |
+
float={value === null}
|
| 21 |
+
Icon={File}
|
| 22 |
+
label={label || "File"}
|
| 23 |
+
/>
|
| 24 |
+
|
| 25 |
+
{#if value && (Array.isArray(value) ? value.length > 0 : true)}
|
| 26 |
+
<FilePreview
|
| 27 |
+
{i18n}
|
| 28 |
+
{selectable}
|
| 29 |
+
on:select={on_select}
|
| 30 |
+
on:download={on_download}
|
| 31 |
+
{value}
|
| 32 |
+
{height}
|
| 33 |
+
/>
|
| 34 |
+
{:else}
|
| 35 |
+
<Empty unpadded_box={true} size="large"><File /></Empty>
|
| 36 |
+
{/if}
|
6.0.0/file/shared/FilePreview.svelte
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import type { FileData } from "@gradio/client";
|
| 3 |
+
import { prettyBytes } from "./utils";
|
| 4 |
+
import { createEventDispatcher } from "svelte";
|
| 5 |
+
import type { I18nFormatter, SelectData } from "@gradio/utils";
|
| 6 |
+
import { DownloadLink } from "@gradio/atoms";
|
| 7 |
+
|
| 8 |
+
const dispatch = createEventDispatcher<{
|
| 9 |
+
select: SelectData;
|
| 10 |
+
change: FileData[] | FileData;
|
| 11 |
+
delete: FileData;
|
| 12 |
+
download: FileData;
|
| 13 |
+
}>();
|
| 14 |
+
export let value: FileData | FileData[];
|
| 15 |
+
export let selectable = false;
|
| 16 |
+
export let height: number | string | undefined = undefined;
|
| 17 |
+
export let i18n: I18nFormatter;
|
| 18 |
+
export let allow_reordering = false;
|
| 19 |
+
|
| 20 |
+
let dragging_index: number | null = null;
|
| 21 |
+
let drop_target_index: number | null = null;
|
| 22 |
+
|
| 23 |
+
function handle_drag_start(event: DragEvent, index: number): void {
|
| 24 |
+
dragging_index = index;
|
| 25 |
+
if (event.dataTransfer) {
|
| 26 |
+
event.dataTransfer.effectAllowed = "move";
|
| 27 |
+
event.dataTransfer.setData("text/plain", index.toString());
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function handle_drag_over(event: DragEvent, index: number): void {
|
| 32 |
+
event.preventDefault();
|
| 33 |
+
if (index === normalized_files.length - 1) {
|
| 34 |
+
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
|
| 35 |
+
const midY = rect.top + rect.height / 2;
|
| 36 |
+
drop_target_index =
|
| 37 |
+
event.clientY > midY ? normalized_files.length : index;
|
| 38 |
+
} else {
|
| 39 |
+
drop_target_index = index;
|
| 40 |
+
}
|
| 41 |
+
if (event.dataTransfer) {
|
| 42 |
+
event.dataTransfer.dropEffect = "move";
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
function handle_drag_end(event: DragEvent): void {
|
| 47 |
+
if (
|
| 48 |
+
!event.dataTransfer?.dropEffect ||
|
| 49 |
+
event.dataTransfer.dropEffect === "none"
|
| 50 |
+
) {
|
| 51 |
+
dragging_index = null;
|
| 52 |
+
drop_target_index = null;
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
function handle_drop(event: DragEvent, index: number): void {
|
| 57 |
+
event.preventDefault();
|
| 58 |
+
if (dragging_index === null || dragging_index === index) return;
|
| 59 |
+
|
| 60 |
+
const files = Array.isArray(value) ? [...value] : [value];
|
| 61 |
+
const [removed] = files.splice(dragging_index, 1);
|
| 62 |
+
files.splice(
|
| 63 |
+
drop_target_index === normalized_files.length
|
| 64 |
+
? normalized_files.length
|
| 65 |
+
: index,
|
| 66 |
+
0,
|
| 67 |
+
removed
|
| 68 |
+
);
|
| 69 |
+
|
| 70 |
+
const new_value = Array.isArray(value) ? files : files[0];
|
| 71 |
+
dispatch("change", new_value);
|
| 72 |
+
|
| 73 |
+
dragging_index = null;
|
| 74 |
+
drop_target_index = null;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
function split_filename(filename: string): [string, string] {
|
| 78 |
+
const last_dot = filename.lastIndexOf(".");
|
| 79 |
+
if (last_dot === -1) {
|
| 80 |
+
return [filename, ""];
|
| 81 |
+
}
|
| 82 |
+
return [filename.slice(0, last_dot), filename.slice(last_dot)];
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
$: normalized_files = (Array.isArray(value) ? value : [value]).map((file) => {
|
| 86 |
+
const [filename_stem, filename_ext] = split_filename(file.orig_name ?? "");
|
| 87 |
+
return {
|
| 88 |
+
...file,
|
| 89 |
+
filename_stem,
|
| 90 |
+
filename_ext
|
| 91 |
+
};
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
function handle_row_click(
|
| 95 |
+
event: MouseEvent & { currentTarget: HTMLTableRowElement },
|
| 96 |
+
index: number
|
| 97 |
+
): void {
|
| 98 |
+
const tr = event.currentTarget;
|
| 99 |
+
const should_select =
|
| 100 |
+
event.target === tr || // Only select if the click is on the row itself
|
| 101 |
+
(tr &&
|
| 102 |
+
tr.firstElementChild &&
|
| 103 |
+
event.composedPath().includes(tr.firstElementChild)); // Or if the click is on the name column
|
| 104 |
+
|
| 105 |
+
if (should_select) {
|
| 106 |
+
dispatch("select", { value: normalized_files[index].orig_name, index });
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
function remove_file(index: number): void {
|
| 111 |
+
const removed = normalized_files.splice(index, 1);
|
| 112 |
+
normalized_files = [...normalized_files];
|
| 113 |
+
value = normalized_files;
|
| 114 |
+
dispatch("delete", removed[0]);
|
| 115 |
+
dispatch("change", normalized_files);
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
function handle_download(file: FileData): void {
|
| 119 |
+
dispatch("download", file);
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
const is_browser = typeof window !== "undefined";
|
| 123 |
+
</script>
|
| 124 |
+
|
| 125 |
+
<div
|
| 126 |
+
class="file-preview-holder"
|
| 127 |
+
style:max-height={height
|
| 128 |
+
? typeof height === "number"
|
| 129 |
+
? height + "px"
|
| 130 |
+
: height
|
| 131 |
+
: "auto"}
|
| 132 |
+
>
|
| 133 |
+
<table class="file-preview">
|
| 134 |
+
<tbody>
|
| 135 |
+
{#each normalized_files as file, i (file.url)}
|
| 136 |
+
<tr
|
| 137 |
+
class="file"
|
| 138 |
+
class:selectable
|
| 139 |
+
class:dragging={dragging_index === i}
|
| 140 |
+
class:drop-target={drop_target_index === i ||
|
| 141 |
+
(i === normalized_files.length - 1 &&
|
| 142 |
+
drop_target_index === normalized_files.length)}
|
| 143 |
+
data-drop-target={drop_target_index === normalized_files.length &&
|
| 144 |
+
i === normalized_files.length - 1
|
| 145 |
+
? "after"
|
| 146 |
+
: drop_target_index === i + 1
|
| 147 |
+
? "after"
|
| 148 |
+
: "before"}
|
| 149 |
+
draggable={allow_reordering && normalized_files.length > 1}
|
| 150 |
+
on:click={(event) => {
|
| 151 |
+
handle_row_click(event, i);
|
| 152 |
+
}}
|
| 153 |
+
on:dragstart={(event) => handle_drag_start(event, i)}
|
| 154 |
+
on:dragenter|preventDefault
|
| 155 |
+
on:dragover={(event) => handle_drag_over(event, i)}
|
| 156 |
+
on:drop={(event) => handle_drop(event, i)}
|
| 157 |
+
on:dragend={handle_drag_end}
|
| 158 |
+
>
|
| 159 |
+
<td class="filename" aria-label={file.orig_name}>
|
| 160 |
+
{#if allow_reordering && normalized_files.length > 1}
|
| 161 |
+
<span class="drag-handle">⋮⋮</span>
|
| 162 |
+
{/if}
|
| 163 |
+
<span class="stem">{file.filename_stem}</span>
|
| 164 |
+
<span class="ext">{file.filename_ext}</span>
|
| 165 |
+
</td>
|
| 166 |
+
|
| 167 |
+
<td class="download">
|
| 168 |
+
{#if file.url}
|
| 169 |
+
<DownloadLink
|
| 170 |
+
href={file.url}
|
| 171 |
+
on:click={() => handle_download(file)}
|
| 172 |
+
download={is_browser && window.__is_colab__
|
| 173 |
+
? null
|
| 174 |
+
: file.orig_name}
|
| 175 |
+
>
|
| 176 |
+
{@html file.size != null
|
| 177 |
+
? prettyBytes(file.size)
|
| 178 |
+
: "(size unknown)"} ⇣
|
| 179 |
+
</DownloadLink>
|
| 180 |
+
{:else}
|
| 181 |
+
{i18n("file.uploading")}
|
| 182 |
+
{/if}
|
| 183 |
+
</td>
|
| 184 |
+
|
| 185 |
+
{#if normalized_files.length > 1}
|
| 186 |
+
<td>
|
| 187 |
+
<button
|
| 188 |
+
class="label-clear-button"
|
| 189 |
+
aria-label="Remove this file"
|
| 190 |
+
on:click={() => {
|
| 191 |
+
remove_file(i);
|
| 192 |
+
}}
|
| 193 |
+
on:keydown={(event) => {
|
| 194 |
+
if (event.key === "Enter") {
|
| 195 |
+
remove_file(i);
|
| 196 |
+
}
|
| 197 |
+
}}
|
| 198 |
+
>×
|
| 199 |
+
</button>
|
| 200 |
+
</td>
|
| 201 |
+
{/if}
|
| 202 |
+
</tr>
|
| 203 |
+
{/each}
|
| 204 |
+
</tbody>
|
| 205 |
+
</table>
|
| 206 |
+
</div>
|
| 207 |
+
|
| 208 |
+
<style>
|
| 209 |
+
.label-clear-button {
|
| 210 |
+
color: var(--body-text-color-subdued);
|
| 211 |
+
position: relative;
|
| 212 |
+
left: -3px;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
.label-clear-button:hover {
|
| 216 |
+
color: var(--body-text-color);
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
.file-preview {
|
| 220 |
+
table-layout: fixed;
|
| 221 |
+
width: var(--size-full);
|
| 222 |
+
max-height: var(--size-60);
|
| 223 |
+
overflow-y: auto;
|
| 224 |
+
margin-top: var(--size-1);
|
| 225 |
+
color: var(--body-text-color);
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.file-preview-holder {
|
| 229 |
+
overflow: auto;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
.file {
|
| 233 |
+
display: flex;
|
| 234 |
+
width: var(--size-full);
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
.file > * {
|
| 238 |
+
padding: var(--size-1) var(--size-2-5);
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
.filename {
|
| 242 |
+
flex-grow: 1;
|
| 243 |
+
display: flex;
|
| 244 |
+
overflow: hidden;
|
| 245 |
+
}
|
| 246 |
+
.filename .stem {
|
| 247 |
+
overflow: hidden;
|
| 248 |
+
text-overflow: ellipsis;
|
| 249 |
+
white-space: nowrap;
|
| 250 |
+
}
|
| 251 |
+
.filename .ext {
|
| 252 |
+
white-space: nowrap;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
.download {
|
| 256 |
+
min-width: 8rem;
|
| 257 |
+
width: 10%;
|
| 258 |
+
white-space: nowrap;
|
| 259 |
+
text-align: right;
|
| 260 |
+
}
|
| 261 |
+
.download:hover {
|
| 262 |
+
text-decoration: underline;
|
| 263 |
+
}
|
| 264 |
+
.download > :global(a) {
|
| 265 |
+
color: var(--link-text-color);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
.download > :global(a:hover) {
|
| 269 |
+
color: var(--link-text-color-hover);
|
| 270 |
+
}
|
| 271 |
+
.download > :global(a:visited) {
|
| 272 |
+
color: var(--link-text-color-visited);
|
| 273 |
+
}
|
| 274 |
+
.download > :global(a:active) {
|
| 275 |
+
color: var(--link-text-color-active);
|
| 276 |
+
}
|
| 277 |
+
.selectable {
|
| 278 |
+
cursor: pointer;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
tbody > tr:nth-child(even) {
|
| 282 |
+
background: var(--block-background-fill);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
tbody > tr:nth-child(odd) {
|
| 286 |
+
background: var(--table-odd-background-fill);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
.drag-handle {
|
| 290 |
+
cursor: grab;
|
| 291 |
+
color: var(--body-text-color-subdued);
|
| 292 |
+
padding-right: var(--size-2);
|
| 293 |
+
user-select: none;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
.dragging {
|
| 297 |
+
opacity: 0.5;
|
| 298 |
+
cursor: grabbing;
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
.drop-target {
|
| 302 |
+
border-top: 2px solid var(--color-accent);
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
tr:last-child.drop-target[data-drop-target="before"] {
|
| 306 |
+
border-top: 2px solid var(--color-accent);
|
| 307 |
+
border-bottom: none;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
tr:last-child.drop-target[data-drop-target="after"] {
|
| 311 |
+
border-top: none;
|
| 312 |
+
border-bottom: 2px solid var(--color-accent);
|
| 313 |
+
}
|
| 314 |
+
</style>
|
6.0.0/file/shared/FileUpload.svelte
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher, tick } from "svelte";
|
| 3 |
+
import { Upload, ModifyUpload } from "@gradio/upload";
|
| 4 |
+
import type { FileData, Client } from "@gradio/client";
|
| 5 |
+
import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
|
| 6 |
+
import { File, Clear, Upload as UploadIcon } from "@gradio/icons";
|
| 7 |
+
|
| 8 |
+
import FilePreview from "./FilePreview.svelte";
|
| 9 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 10 |
+
|
| 11 |
+
export let value: null | FileData | FileData[];
|
| 12 |
+
|
| 13 |
+
export let label: string;
|
| 14 |
+
export let show_label = true;
|
| 15 |
+
export let file_count: "single" | "multiple" | "directory" = "single";
|
| 16 |
+
export let file_types: string[] | null = null;
|
| 17 |
+
export let selectable = false;
|
| 18 |
+
export let root: string;
|
| 19 |
+
export let height: number | undefined = undefined;
|
| 20 |
+
export let i18n: I18nFormatter;
|
| 21 |
+
export let max_file_size: number | null = null;
|
| 22 |
+
export let upload: Client["upload"];
|
| 23 |
+
export let stream_handler: Client["stream"];
|
| 24 |
+
export let uploading = false;
|
| 25 |
+
export let allow_reordering = false;
|
| 26 |
+
export let upload_promise: Promise<(FileData | null)[]> | null = null;
|
| 27 |
+
|
| 28 |
+
async function handle_upload({
|
| 29 |
+
detail
|
| 30 |
+
}: CustomEvent<FileData | FileData[]>): Promise<void> {
|
| 31 |
+
if (Array.isArray(value)) {
|
| 32 |
+
value = [...value, ...(Array.isArray(detail) ? detail : [detail])];
|
| 33 |
+
} else if (value) {
|
| 34 |
+
value = [value, ...(Array.isArray(detail) ? detail : [detail])];
|
| 35 |
+
} else {
|
| 36 |
+
value = detail;
|
| 37 |
+
}
|
| 38 |
+
await tick();
|
| 39 |
+
dispatch("change", value);
|
| 40 |
+
dispatch("upload", detail);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function handle_clear(): void {
|
| 44 |
+
value = null;
|
| 45 |
+
dispatch("change", null);
|
| 46 |
+
dispatch("clear");
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
const dispatch = createEventDispatcher<{
|
| 50 |
+
change: FileData[] | FileData | null;
|
| 51 |
+
clear: undefined;
|
| 52 |
+
drag: boolean;
|
| 53 |
+
upload: FileData[] | FileData;
|
| 54 |
+
load: FileData[] | FileData;
|
| 55 |
+
error: string;
|
| 56 |
+
}>();
|
| 57 |
+
|
| 58 |
+
let dragging = false;
|
| 59 |
+
$: dispatch("drag", dragging);
|
| 60 |
+
</script>
|
| 61 |
+
|
| 62 |
+
<BlockLabel {show_label} Icon={File} float={!value} label={label || "File"} />
|
| 63 |
+
|
| 64 |
+
{#if value && (Array.isArray(value) ? value.length > 0 : true)}
|
| 65 |
+
<IconButtonWrapper>
|
| 66 |
+
{#if !(file_count === "single" && (Array.isArray(value) ? value.length > 0 : value !== null))}
|
| 67 |
+
<IconButton Icon={UploadIcon} label={i18n("common.upload")}>
|
| 68 |
+
<Upload
|
| 69 |
+
bind:upload_promise
|
| 70 |
+
icon_upload={true}
|
| 71 |
+
on:load={handle_upload}
|
| 72 |
+
filetype={file_types}
|
| 73 |
+
{file_count}
|
| 74 |
+
{max_file_size}
|
| 75 |
+
{root}
|
| 76 |
+
bind:dragging
|
| 77 |
+
bind:uploading
|
| 78 |
+
on:error
|
| 79 |
+
{stream_handler}
|
| 80 |
+
{upload}
|
| 81 |
+
/>
|
| 82 |
+
</IconButton>
|
| 83 |
+
{/if}
|
| 84 |
+
<IconButton
|
| 85 |
+
Icon={Clear}
|
| 86 |
+
label={i18n("common.clear")}
|
| 87 |
+
on:click={(event) => {
|
| 88 |
+
dispatch("clear");
|
| 89 |
+
event.stopPropagation();
|
| 90 |
+
handle_clear();
|
| 91 |
+
}}
|
| 92 |
+
/>
|
| 93 |
+
</IconButtonWrapper>
|
| 94 |
+
|
| 95 |
+
<FilePreview
|
| 96 |
+
{i18n}
|
| 97 |
+
on:select
|
| 98 |
+
{selectable}
|
| 99 |
+
{value}
|
| 100 |
+
{height}
|
| 101 |
+
on:change
|
| 102 |
+
on:delete
|
| 103 |
+
{allow_reordering}
|
| 104 |
+
/>
|
| 105 |
+
{:else}
|
| 106 |
+
<Upload
|
| 107 |
+
bind:upload_promise
|
| 108 |
+
on:load={handle_upload}
|
| 109 |
+
filetype={file_types}
|
| 110 |
+
{file_count}
|
| 111 |
+
{max_file_size}
|
| 112 |
+
{root}
|
| 113 |
+
bind:dragging
|
| 114 |
+
bind:uploading
|
| 115 |
+
on:error
|
| 116 |
+
{stream_handler}
|
| 117 |
+
{upload}
|
| 118 |
+
{height}
|
| 119 |
+
>
|
| 120 |
+
<slot />
|
| 121 |
+
</Upload>
|
| 122 |
+
{/if}
|
6.0.0/file/shared/utils.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { FileData } from "@gradio/client";
|
| 2 |
+
|
| 3 |
+
export const prettyBytes = (bytes: number): string => {
|
| 4 |
+
let units = ["B", "KB", "MB", "GB", "PB"];
|
| 5 |
+
let i = 0;
|
| 6 |
+
while (bytes > 1024) {
|
| 7 |
+
bytes /= 1024;
|
| 8 |
+
i++;
|
| 9 |
+
}
|
| 10 |
+
let unit = units[i];
|
| 11 |
+
return bytes.toFixed(1) + " " + unit;
|
| 12 |
+
};
|
6.0.0/file/types.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { FileData } from "@gradio/client";
|
| 2 |
+
import type { LoadingStatus } from "js/statustracker";
|
| 3 |
+
import type { SelectData } from "@gradio/utils";
|
| 4 |
+
|
| 5 |
+
export interface FileProps {
|
| 6 |
+
value: FileData | FileData[] | null;
|
| 7 |
+
file_types: string[];
|
| 8 |
+
file_count: "single" | "multiple" | "directory";
|
| 9 |
+
allow_reordering: boolean;
|
| 10 |
+
type: "filepath" | "binary";
|
| 11 |
+
_selectable: boolean;
|
| 12 |
+
height: number | null;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
export interface FileEvents {
|
| 16 |
+
upload: FileData | FileData[];
|
| 17 |
+
download: void;
|
| 18 |
+
error: string;
|
| 19 |
+
clear_status: LoadingStatus;
|
| 20 |
+
clear: void;
|
| 21 |
+
select: SelectData | null;
|
| 22 |
+
change: FileData | FileData[] | null;
|
| 23 |
+
delete: void;
|
| 24 |
+
}
|