Upload folder using huggingface_hub
Browse files- 6.5.0/dataset/Dataset.svelte +91 -79
6.5.0/dataset/Dataset.svelte
CHANGED
|
@@ -1,91 +1,105 @@
|
|
| 1 |
<script lang="ts">
|
| 2 |
-
import { Block } from "@gradio/atoms";
|
| 3 |
import type { SvelteComponent, ComponentType } from "svelte";
|
| 4 |
-
import type {
|
| 5 |
import { BaseExample } from "@gradio/textbox";
|
| 6 |
import type { load_component as load_component_type } from "@gradio/utils";
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
// Although the `samples_dir` prop is not used in any of the core Gradio component, it is kept for backward compatibility
|
| 31 |
// with any custom components created with gradio<=4.20.0
|
| 32 |
-
let samples_dir: string =
|
| 33 |
-
? `/proxy=${proxy_url}file=`
|
| 34 |
-
|
| 35 |
-
let page = 0;
|
| 36 |
|
| 37 |
-
|
| 38 |
-
(components.length < 2 || sample_labels !== null) && layout !== "table";
|
| 39 |
-
let paginate = samples ? samples.length > samples_per_page : false;
|
| 40 |
|
| 41 |
-
let
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
current_hover = -1;
|
| 53 |
-
}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
if (sample_labels) {
|
| 57 |
-
samples = sample_labels.map((e) => [e]);
|
| 58 |
-
} else if (!samples) {
|
| 59 |
-
samples = [];
|
| 60 |
-
}
|
| 61 |
-
if (JSON.stringify(samples) !== JSON.stringify(old_samples)) {
|
| 62 |
-
page = 0;
|
| 63 |
-
old_samples = samples;
|
| 64 |
-
}
|
| 65 |
-
paginate = samples.length > samples_per_page;
|
| 66 |
if (paginate) {
|
| 67 |
-
|
| 68 |
-
selected_samples = samples.slice(
|
| 69 |
page * samples_per_page,
|
| 70 |
(page + 1) * samples_per_page
|
| 71 |
);
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
|
|
|
| 84 |
}
|
| 85 |
-
}
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
let component_meta: {
|
|
@@ -115,11 +129,9 @@
|
|
| 115 |
);
|
| 116 |
}
|
| 117 |
|
| 118 |
-
// Need to
|
| 119 |
// Saw this when rendering examples in a gr.render block
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
// $: get_component_meta(selected_samples_json);
|
| 123 |
</script>
|
| 124 |
|
| 125 |
{#await get_component_meta(selected_samples_json) then _}
|
|
@@ -129,13 +141,13 @@
|
|
| 129 |
{#if sample_row[0] != null}
|
| 130 |
<button
|
| 131 |
class="gallery-item"
|
| 132 |
-
|
| 133 |
value = i + page * samples_per_page;
|
| 134 |
onclick({ index: value, value: sample_row });
|
| 135 |
onselect({ index: value, value: sample_row });
|
| 136 |
}}
|
| 137 |
-
|
| 138 |
-
|
| 139 |
>
|
| 140 |
{#if sample_labels}
|
| 141 |
<BaseExample
|
|
@@ -179,7 +191,7 @@
|
|
| 179 |
{#each component_meta as sample_row, i}
|
| 180 |
<tr
|
| 181 |
class="tr-body"
|
| 182 |
-
|
| 183 |
value = i + page * samples_per_page;
|
| 184 |
onclick({ index: value, value: sample_row });
|
| 185 |
onselect({
|
|
@@ -187,8 +199,8 @@
|
|
| 187 |
value: selected_samples[i]
|
| 188 |
});
|
| 189 |
}}
|
| 190 |
-
|
| 191 |
-
|
| 192 |
>
|
| 193 |
{#each sample_row as { value, component }, j}
|
| 194 |
{@const component_name = components[j]}
|
|
@@ -230,7 +242,7 @@
|
|
| 230 |
{:else}
|
| 231 |
<button
|
| 232 |
class:current-page={page === visible_page}
|
| 233 |
-
|
| 234 |
>
|
| 235 |
{visible_page + 1}
|
| 236 |
</button>
|
|
|
|
| 1 |
<script lang="ts">
|
|
|
|
| 2 |
import type { SvelteComponent, ComponentType } from "svelte";
|
| 3 |
+
import type { SelectData } from "@gradio/utils";
|
| 4 |
import { BaseExample } from "@gradio/textbox";
|
| 5 |
import type { load_component as load_component_type } from "@gradio/utils";
|
| 6 |
|
| 7 |
+
interface Props {
|
| 8 |
+
components: string[];
|
| 9 |
+
component_props: Record<string, any>[];
|
| 10 |
+
load_component: load_component_type;
|
| 11 |
+
headers: string[];
|
| 12 |
+
samples: any[][] | null;
|
| 13 |
+
sample_labels?: string[] | null;
|
| 14 |
+
value?: number | null;
|
| 15 |
+
root: string;
|
| 16 |
+
proxy_url: null | string;
|
| 17 |
+
samples_per_page?: number;
|
| 18 |
+
onclick: (data: SelectData) => void;
|
| 19 |
+
onselect: (data: SelectData) => void;
|
| 20 |
+
layout?: "gallery" | "table" | null;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
let {
|
| 24 |
+
components,
|
| 25 |
+
component_props,
|
| 26 |
+
load_component,
|
| 27 |
+
headers,
|
| 28 |
+
samples,
|
| 29 |
+
sample_labels = null,
|
| 30 |
+
value = $bindable(null),
|
| 31 |
+
root,
|
| 32 |
+
proxy_url,
|
| 33 |
+
samples_per_page = 10,
|
| 34 |
+
onclick,
|
| 35 |
+
onselect,
|
| 36 |
+
layout = null
|
| 37 |
+
}: Props = $props();
|
| 38 |
|
| 39 |
// Although the `samples_dir` prop is not used in any of the core Gradio component, it is kept for backward compatibility
|
| 40 |
// with any custom components created with gradio<=4.20.0
|
| 41 |
+
let samples_dir: string = $derived(
|
| 42 |
+
proxy_url ? `/proxy=${proxy_url}file=` : `${root}/file=`
|
| 43 |
+
);
|
|
|
|
| 44 |
|
| 45 |
+
let current_hover = $state(-1);
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
let gallery = $derived(
|
| 48 |
+
(components.length < 2 || sample_labels !== null) && layout !== "table"
|
| 49 |
+
);
|
| 50 |
|
| 51 |
+
let effective_samples = $derived.by(() => {
|
| 52 |
+
if (sample_labels) {
|
| 53 |
+
return sample_labels.map((e) => [e]);
|
| 54 |
+
}
|
| 55 |
+
return samples ?? [];
|
| 56 |
+
});
|
| 57 |
|
| 58 |
+
// page resets to 0 whenever effective_samples changes,
|
| 59 |
+
// but can still be overwritten by user clicks
|
| 60 |
+
let page = $derived.by(() => {
|
| 61 |
+
effective_samples;
|
| 62 |
+
return 0;
|
| 63 |
+
});
|
| 64 |
|
| 65 |
+
let paginate = $derived(effective_samples.length > samples_per_page);
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
let selected_samples = $derived.by(() => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if (paginate) {
|
| 69 |
+
return effective_samples.slice(
|
|
|
|
| 70 |
page * samples_per_page,
|
| 71 |
(page + 1) * samples_per_page
|
| 72 |
);
|
| 73 |
+
}
|
| 74 |
+
return effective_samples.slice();
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
let page_count = $derived(
|
| 78 |
+
Math.ceil(effective_samples.length / samples_per_page)
|
| 79 |
+
);
|
| 80 |
+
|
| 81 |
+
let visible_pages = $derived.by(() => {
|
| 82 |
+
if (!paginate) return [];
|
| 83 |
+
let pages: number[] = [];
|
| 84 |
+
[0, page, page_count - 1].forEach((anchor) => {
|
| 85 |
+
for (let i = anchor - 2; i <= anchor + 2; i++) {
|
| 86 |
+
if (i >= 0 && i < page_count && !pages.includes(i)) {
|
| 87 |
+
if (pages.length > 0 && i - pages[pages.length - 1] > 1) {
|
| 88 |
+
pages.push(-1);
|
| 89 |
}
|
| 90 |
+
pages.push(i);
|
| 91 |
}
|
| 92 |
+
}
|
| 93 |
+
});
|
| 94 |
+
return pages;
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
function handle_mouseenter(i: number): void {
|
| 98 |
+
current_hover = i;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
function handle_mouseleave(): void {
|
| 102 |
+
current_hover = -1;
|
| 103 |
}
|
| 104 |
|
| 105 |
let component_meta: {
|
|
|
|
| 129 |
);
|
| 130 |
}
|
| 131 |
|
| 132 |
+
// Need to stringify the samples otherwise get_component_meta will trigger infinitely
|
| 133 |
// Saw this when rendering examples in a gr.render block
|
| 134 |
+
let selected_samples_json = $derived(JSON.stringify(selected_samples || []));
|
|
|
|
|
|
|
| 135 |
</script>
|
| 136 |
|
| 137 |
{#await get_component_meta(selected_samples_json) then _}
|
|
|
|
| 141 |
{#if sample_row[0] != null}
|
| 142 |
<button
|
| 143 |
class="gallery-item"
|
| 144 |
+
onclick={() => {
|
| 145 |
value = i + page * samples_per_page;
|
| 146 |
onclick({ index: value, value: sample_row });
|
| 147 |
onselect({ index: value, value: sample_row });
|
| 148 |
}}
|
| 149 |
+
onmouseenter={() => handle_mouseenter(i)}
|
| 150 |
+
onmouseleave={() => handle_mouseleave()}
|
| 151 |
>
|
| 152 |
{#if sample_labels}
|
| 153 |
<BaseExample
|
|
|
|
| 191 |
{#each component_meta as sample_row, i}
|
| 192 |
<tr
|
| 193 |
class="tr-body"
|
| 194 |
+
onclick={() => {
|
| 195 |
value = i + page * samples_per_page;
|
| 196 |
onclick({ index: value, value: sample_row });
|
| 197 |
onselect({
|
|
|
|
| 199 |
value: selected_samples[i]
|
| 200 |
});
|
| 201 |
}}
|
| 202 |
+
onmouseenter={() => handle_mouseenter(i)}
|
| 203 |
+
onmouseleave={() => handle_mouseleave()}
|
| 204 |
>
|
| 205 |
{#each sample_row as { value, component }, j}
|
| 206 |
{@const component_name = components[j]}
|
|
|
|
| 242 |
{:else}
|
| 243 |
<button
|
| 244 |
class:current-page={page === visible_page}
|
| 245 |
+
onclick={() => (page = visible_page)}
|
| 246 |
>
|
| 247 |
{visible_page + 1}
|
| 248 |
</button>
|