Upload folder using huggingface_hub
Browse files
6.5.0/highlightedtext/Index.svelte
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script module lang="ts">
|
| 2 |
+
export { default as BaseHighlightedText } from "./shared/HighlightedText.svelte";
|
| 3 |
+
</script>
|
| 4 |
+
|
| 5 |
+
<script lang="ts">
|
| 6 |
+
import HighlightedText from "./shared/HighlightedText.svelte";
|
| 7 |
+
import { Block, BlockLabel, Empty, IconButtonWrapper } from "@gradio/atoms";
|
| 8 |
+
import { TextHighlight } from "@gradio/icons";
|
| 9 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 10 |
+
import { Gradio } from "@gradio/utils";
|
| 11 |
+
import { merge_elements } from "./shared/utils";
|
| 12 |
+
import type { HighlightedTextProps, HighlightedTextEvents } from "./types";
|
| 13 |
+
|
| 14 |
+
const props = $props();
|
| 15 |
+
const gradio = new Gradio<HighlightedTextEvents, HighlightedTextProps>(props);
|
| 16 |
+
|
| 17 |
+
let old_value = $state(gradio.props.value);
|
| 18 |
+
|
| 19 |
+
$effect(() => {
|
| 20 |
+
if (old_value !== gradio.props.value) {
|
| 21 |
+
old_value = gradio.props.value;
|
| 22 |
+
gradio.dispatch("change");
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
let value = $derived.by(() =>
|
| 27 |
+
gradio.props.combine_adjacent
|
| 28 |
+
? merge_elements(gradio.props.value || [], "equal")
|
| 29 |
+
: gradio.props.value
|
| 30 |
+
);
|
| 31 |
+
</script>
|
| 32 |
+
|
| 33 |
+
<Block
|
| 34 |
+
variant={gradio.shared.interactive ? "dashed" : "solid"}
|
| 35 |
+
test_id="highlighted-text"
|
| 36 |
+
visible={gradio.shared.visible}
|
| 37 |
+
elem_id={gradio.shared.elem_id}
|
| 38 |
+
elem_classes={gradio.shared.elem_classes}
|
| 39 |
+
padding={false}
|
| 40 |
+
container={gradio.shared.container}
|
| 41 |
+
scale={gradio.shared.scale}
|
| 42 |
+
min_width={gradio.shared.min_width}
|
| 43 |
+
rtl={gradio.props.rtl}
|
| 44 |
+
>
|
| 45 |
+
<StatusTracker
|
| 46 |
+
autoscroll={gradio.shared.autoscroll}
|
| 47 |
+
i18n={gradio.i18n}
|
| 48 |
+
{...gradio.shared.loading_status}
|
| 49 |
+
onclearstatus={() =>
|
| 50 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 51 |
+
/>
|
| 52 |
+
|
| 53 |
+
{#if gradio.shared.interactive && gradio.shared.label && gradio.shared.show_label && gradio.props.buttons?.length}
|
| 54 |
+
<IconButtonWrapper
|
| 55 |
+
buttons={gradio.props.buttons}
|
| 56 |
+
on_custom_button_click={(id) =>
|
| 57 |
+
gradio.dispatch("custom_button_click", { id })}
|
| 58 |
+
/>
|
| 59 |
+
{/if}
|
| 60 |
+
|
| 61 |
+
{#if gradio.shared.label && gradio.shared.show_label}
|
| 62 |
+
<BlockLabel
|
| 63 |
+
Icon={TextHighlight}
|
| 64 |
+
label={gradio.shared.label ||
|
| 65 |
+
gradio.i18n("highlighted_text.highlighted_text")}
|
| 66 |
+
float={false}
|
| 67 |
+
disable={gradio.shared.container === false}
|
| 68 |
+
show_label={gradio.shared.show_label}
|
| 69 |
+
rtl={gradio.props.rtl}
|
| 70 |
+
/>
|
| 71 |
+
{/if}
|
| 72 |
+
|
| 73 |
+
{#if value}
|
| 74 |
+
<HighlightedText
|
| 75 |
+
bind:value
|
| 76 |
+
interactive={gradio.shared.interactive}
|
| 77 |
+
show_legend={gradio.props.show_legend}
|
| 78 |
+
show_inline_category={gradio.props.show_inline_category}
|
| 79 |
+
color_map={gradio.props.color_map}
|
| 80 |
+
onselect={(detail) => gradio.dispatch("select", detail)}
|
| 81 |
+
onchange={() => {
|
| 82 |
+
gradio.props.value = value;
|
| 83 |
+
gradio.dispatch("change");
|
| 84 |
+
}}
|
| 85 |
+
/>
|
| 86 |
+
{:else}
|
| 87 |
+
<Empty
|
| 88 |
+
size={gradio.shared.interactive ? "small" : "large"}
|
| 89 |
+
unpadded_box={gradio.shared.interactive}
|
| 90 |
+
>
|
| 91 |
+
<TextHighlight />
|
| 92 |
+
</Empty>
|
| 93 |
+
{/if}
|
| 94 |
+
</Block>
|
6.5.0/highlightedtext/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/highlightedtext",
|
| 3 |
+
"version": "0.11.1",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"main_changeset": true,
|
| 10 |
+
"dependencies": {
|
| 11 |
+
"@gradio/atoms": "workspace:^",
|
| 12 |
+
"@gradio/icons": "workspace:^",
|
| 13 |
+
"@gradio/statustracker": "workspace:^",
|
| 14 |
+
"@gradio/theme": "workspace:^",
|
| 15 |
+
"@gradio/utils": "workspace:^"
|
| 16 |
+
},
|
| 17 |
+
"devDependencies": {
|
| 18 |
+
"@gradio/preview": "workspace:^"
|
| 19 |
+
},
|
| 20 |
+
"main": "./Index.svelte",
|
| 21 |
+
"exports": {
|
| 22 |
+
".": {
|
| 23 |
+
"gradio": "./Index.svelte",
|
| 24 |
+
"svelte": "./dist/Index.svelte",
|
| 25 |
+
"types": "./dist/Index.svelte.d.ts"
|
| 26 |
+
},
|
| 27 |
+
"./package.json": "./package.json"
|
| 28 |
+
},
|
| 29 |
+
"peerDependencies": {
|
| 30 |
+
"svelte": "^5.48.0"
|
| 31 |
+
},
|
| 32 |
+
"repository": {
|
| 33 |
+
"type": "git",
|
| 34 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 35 |
+
"directory": "js/highlightedtext"
|
| 36 |
+
}
|
| 37 |
+
}
|
6.5.0/highlightedtext/shared/HighlightedText.svelte
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { get_next_color } from "@gradio/utils";
|
| 3 |
+
import type { SelectData } from "@gradio/utils";
|
| 4 |
+
import { onMount } from "svelte";
|
| 5 |
+
import {
|
| 6 |
+
generate_color_map,
|
| 7 |
+
merge_elements,
|
| 8 |
+
get_score_color,
|
| 9 |
+
is_transparent,
|
| 10 |
+
type HighlightedToken,
|
| 11 |
+
type ColorPair
|
| 12 |
+
} from "./utils";
|
| 13 |
+
import LabelInput from "./LabelInput.svelte";
|
| 14 |
+
import { Clear } from "@gradio/icons";
|
| 15 |
+
|
| 16 |
+
const is_browser = typeof window !== "undefined";
|
| 17 |
+
|
| 18 |
+
let {
|
| 19 |
+
value = $bindable([]),
|
| 20 |
+
show_legend = false,
|
| 21 |
+
show_inline_category = true,
|
| 22 |
+
color_map = {},
|
| 23 |
+
interactive = false,
|
| 24 |
+
onselect,
|
| 25 |
+
onchange
|
| 26 |
+
}: {
|
| 27 |
+
value?: HighlightedToken[];
|
| 28 |
+
show_legend?: boolean;
|
| 29 |
+
show_inline_category?: boolean;
|
| 30 |
+
color_map?: Record<string, string>;
|
| 31 |
+
interactive?: boolean;
|
| 32 |
+
onselect?: (data: SelectData) => void;
|
| 33 |
+
onchange?: (data: HighlightedToken[]) => void;
|
| 34 |
+
} = $props();
|
| 35 |
+
|
| 36 |
+
let active_element_index = $state(-1);
|
| 37 |
+
let active_legend = $state("");
|
| 38 |
+
let label_to_edit = $state(-1);
|
| 39 |
+
let selection: Selection | null = $state(null);
|
| 40 |
+
|
| 41 |
+
let mode: "categories" | "scores" = $state("categories");
|
| 42 |
+
let resolved_color_map: Record<string, ColorPair> = $state({});
|
| 43 |
+
|
| 44 |
+
$effect(() => {
|
| 45 |
+
let local_colors = { ...color_map };
|
| 46 |
+
|
| 47 |
+
for (const entry of value) {
|
| 48 |
+
if (entry.class_or_confidence === null) continue;
|
| 49 |
+
|
| 50 |
+
if (typeof entry.class_or_confidence === "string") {
|
| 51 |
+
mode = "categories";
|
| 52 |
+
if (!(entry.class_or_confidence in local_colors)) {
|
| 53 |
+
local_colors[entry.class_or_confidence] = get_next_color(
|
| 54 |
+
Object.keys(local_colors).length
|
| 55 |
+
);
|
| 56 |
+
}
|
| 57 |
+
} else {
|
| 58 |
+
mode = "scores";
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
resolved_color_map = generate_color_map(local_colors, is_browser);
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
onMount(() => {
|
| 66 |
+
if (!interactive) return;
|
| 67 |
+
|
| 68 |
+
const on_mouse_up = (): void => {
|
| 69 |
+
selection = window.getSelection();
|
| 70 |
+
handle_selection_complete();
|
| 71 |
+
window.removeEventListener("mouseup", on_mouse_up);
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
window.addEventListener("mousedown", () => {
|
| 75 |
+
window.addEventListener("mouseup", on_mouse_up);
|
| 76 |
+
});
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
function handle_selection_complete(): void {
|
| 80 |
+
if (!selection || !selection.toString().trim()) return;
|
| 81 |
+
|
| 82 |
+
const start = selection.getRangeAt(0).startOffset;
|
| 83 |
+
const end = selection.getRangeAt(0).endOffset;
|
| 84 |
+
handle_text_selected(start, end);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
function handle_text_selected(start: number, end: number): void {
|
| 88 |
+
if (
|
| 89 |
+
!selection?.toString() ||
|
| 90 |
+
active_element_index === -1 ||
|
| 91 |
+
!value[active_element_index].token.includes(selection.toString())
|
| 92 |
+
) {
|
| 93 |
+
return;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
const str = value[active_element_index].token;
|
| 97 |
+
const new_entries: HighlightedToken[] = [
|
| 98 |
+
{ token: str.substring(0, start), class_or_confidence: null },
|
| 99 |
+
{
|
| 100 |
+
token: str.substring(start, end),
|
| 101 |
+
class_or_confidence: mode === "scores" ? 1 : "label"
|
| 102 |
+
},
|
| 103 |
+
{ token: str.substring(end), class_or_confidence: null }
|
| 104 |
+
].filter((e) => e.token.trim() !== "");
|
| 105 |
+
|
| 106 |
+
value = [
|
| 107 |
+
...value.slice(0, active_element_index),
|
| 108 |
+
...new_entries,
|
| 109 |
+
...value.slice(active_element_index + 1)
|
| 110 |
+
];
|
| 111 |
+
|
| 112 |
+
label_to_edit = value.findIndex(
|
| 113 |
+
(v, i) =>
|
| 114 |
+
i >= active_element_index &&
|
| 115 |
+
v.token === str.substring(start, end) &&
|
| 116 |
+
v.class_or_confidence !== null
|
| 117 |
+
);
|
| 118 |
+
|
| 119 |
+
handle_value_change();
|
| 120 |
+
document.getElementById(`label-input-${label_to_edit}`)?.focus();
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
function remove_highlight(index: number): void {
|
| 124 |
+
if (index < 0 || index >= value.length) return;
|
| 125 |
+
value[index].class_or_confidence = null;
|
| 126 |
+
value = merge_elements(value, "equal");
|
| 127 |
+
handle_value_change();
|
| 128 |
+
window.getSelection()?.empty();
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
function handle_value_change(): void {
|
| 132 |
+
onchange?.(value);
|
| 133 |
+
label_to_edit = -1;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
function get_background_color(
|
| 137 |
+
class_or_confidence: string | number | null
|
| 138 |
+
): string {
|
| 139 |
+
if (class_or_confidence === null) return "";
|
| 140 |
+
if (active_legend && active_legend !== class_or_confidence) return "";
|
| 141 |
+
return resolved_color_map[class_or_confidence]?.secondary ?? "";
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
function get_label_color(
|
| 145 |
+
class_or_confidence: string | number | null
|
| 146 |
+
): string {
|
| 147 |
+
if (class_or_confidence === null) return "";
|
| 148 |
+
if (active_legend && active_legend !== class_or_confidence) return "";
|
| 149 |
+
return resolved_color_map[class_or_confidence]?.primary ?? "";
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
function get_text_color(class_or_confidence: string | number | null): string {
|
| 153 |
+
const bg = get_background_color(class_or_confidence);
|
| 154 |
+
return is_transparent(bg) ? "" : "black";
|
| 155 |
+
}
|
| 156 |
+
</script>
|
| 157 |
+
|
| 158 |
+
<div class="container">
|
| 159 |
+
{#if mode === "categories"}
|
| 160 |
+
{#if show_legend}
|
| 161 |
+
<div class="legend" data-testid="highlighted-text:category-legend">
|
| 162 |
+
{#each Object.entries(resolved_color_map) as [category, colors]}
|
| 163 |
+
<button
|
| 164 |
+
class="legend-item"
|
| 165 |
+
style:background-color={colors.secondary}
|
| 166 |
+
onmouseenter={() => (active_legend = category)}
|
| 167 |
+
onmouseleave={() => (active_legend = "")}
|
| 168 |
+
onfocus={() => (active_legend = category)}
|
| 169 |
+
onblur={() => (active_legend = "")}
|
| 170 |
+
>
|
| 171 |
+
{category}
|
| 172 |
+
</button>
|
| 173 |
+
{/each}
|
| 174 |
+
</div>
|
| 175 |
+
{/if}
|
| 176 |
+
|
| 177 |
+
<div class="textfield">
|
| 178 |
+
{#each value as { token, class_or_confidence }, i}
|
| 179 |
+
{@const lines = token.split("\n")}
|
| 180 |
+
{#each lines as line, j}
|
| 181 |
+
{#if line.trim()}
|
| 182 |
+
{@const bg_color = get_background_color(class_or_confidence)}
|
| 183 |
+
<span class="token-container">
|
| 184 |
+
<span
|
| 185 |
+
class="token"
|
| 186 |
+
class:highlighted={class_or_confidence !== null}
|
| 187 |
+
class:transparent={class_or_confidence !== null &&
|
| 188 |
+
is_transparent(bg_color)}
|
| 189 |
+
class:dimmed={active_legend &&
|
| 190 |
+
active_legend !== class_or_confidence}
|
| 191 |
+
style:background-color={bg_color}
|
| 192 |
+
style:color={get_text_color(class_or_confidence)}
|
| 193 |
+
role={class_or_confidence !== null ? "button" : undefined}
|
| 194 |
+
tabindex={class_or_confidence !== null ? 0 : undefined}
|
| 195 |
+
onclick={() => {
|
| 196 |
+
if (class_or_confidence === null) return;
|
| 197 |
+
if (interactive) {
|
| 198 |
+
onselect?.({
|
| 199 |
+
index: i,
|
| 200 |
+
value: [token, class_or_confidence]
|
| 201 |
+
});
|
| 202 |
+
label_to_edit = i;
|
| 203 |
+
} else {
|
| 204 |
+
onselect?.({
|
| 205 |
+
index: i,
|
| 206 |
+
value: [token, class_or_confidence]
|
| 207 |
+
});
|
| 208 |
+
}
|
| 209 |
+
}}
|
| 210 |
+
onkeydown={(e) => {
|
| 211 |
+
if (!interactive) return;
|
| 212 |
+
if (e.key === "Enter" && class_or_confidence !== null) {
|
| 213 |
+
label_to_edit = i;
|
| 214 |
+
}
|
| 215 |
+
}}
|
| 216 |
+
onfocus={() => (active_element_index = i)}
|
| 217 |
+
onmouseenter={() => (active_element_index = i)}
|
| 218 |
+
>
|
| 219 |
+
<span
|
| 220 |
+
class="text"
|
| 221 |
+
class:unlabeled={class_or_confidence === null}>{line}</span
|
| 222 |
+
>
|
| 223 |
+
|
| 224 |
+
{#if !show_legend && show_inline_category && class_or_confidence !== null && label_to_edit !== i}
|
| 225 |
+
<span
|
| 226 |
+
class="label"
|
| 227 |
+
style:background-color={get_label_color(
|
| 228 |
+
class_or_confidence
|
| 229 |
+
)}
|
| 230 |
+
>
|
| 231 |
+
{class_or_confidence}
|
| 232 |
+
</span>
|
| 233 |
+
{/if}
|
| 234 |
+
|
| 235 |
+
{#if interactive && label_to_edit === i && class_or_confidence !== null}
|
| 236 |
+
<LabelInput
|
| 237 |
+
bind:value
|
| 238 |
+
bind:label_to_edit
|
| 239 |
+
category={class_or_confidence}
|
| 240 |
+
{active_legend}
|
| 241 |
+
color_map={resolved_color_map}
|
| 242 |
+
label_index={i}
|
| 243 |
+
{token}
|
| 244 |
+
onchange={handle_value_change}
|
| 245 |
+
/>
|
| 246 |
+
{/if}
|
| 247 |
+
</span>
|
| 248 |
+
|
| 249 |
+
{#if interactive && class_or_confidence !== null}
|
| 250 |
+
<button
|
| 251 |
+
class="remove-btn"
|
| 252 |
+
aria-label="Remove label"
|
| 253 |
+
onclick={() => remove_highlight(i)}
|
| 254 |
+
>
|
| 255 |
+
<Clear />
|
| 256 |
+
</button>
|
| 257 |
+
{/if}
|
| 258 |
+
</span>
|
| 259 |
+
{/if}
|
| 260 |
+
{#if j < lines.length - 1}
|
| 261 |
+
<span class="line-break"></span>
|
| 262 |
+
{/if}
|
| 263 |
+
{/each}
|
| 264 |
+
{/each}
|
| 265 |
+
</div>
|
| 266 |
+
{:else}
|
| 267 |
+
{#if show_legend}
|
| 268 |
+
<div class="score-legend" data-testid="highlighted-text:color-legend">
|
| 269 |
+
<span>-1</span>
|
| 270 |
+
<span>0</span>
|
| 271 |
+
<span>+1</span>
|
| 272 |
+
</div>
|
| 273 |
+
{/if}
|
| 274 |
+
|
| 275 |
+
<div class="textfield" data-testid="highlighted-text:textfield">
|
| 276 |
+
{#each value as { token, class_or_confidence }, i}
|
| 277 |
+
{@const score =
|
| 278 |
+
typeof class_or_confidence === "string"
|
| 279 |
+
? parseFloat(class_or_confidence)
|
| 280 |
+
: class_or_confidence}
|
| 281 |
+
<span class="token-container">
|
| 282 |
+
<span
|
| 283 |
+
class="token score-token"
|
| 284 |
+
class:highlighted={score !== null}
|
| 285 |
+
style:background-color={get_score_color(score)}
|
| 286 |
+
role="button"
|
| 287 |
+
tabindex={0}
|
| 288 |
+
onmouseenter={() => (active_element_index = i)}
|
| 289 |
+
onfocus={() => (active_element_index = i)}
|
| 290 |
+
onclick={() => {
|
| 291 |
+
if (interactive) {
|
| 292 |
+
label_to_edit = i;
|
| 293 |
+
} else {
|
| 294 |
+
onselect?.({
|
| 295 |
+
index: i,
|
| 296 |
+
value: [token, class_or_confidence]
|
| 297 |
+
});
|
| 298 |
+
}
|
| 299 |
+
}}
|
| 300 |
+
onkeydown={(e) => {
|
| 301 |
+
if (e.key === "Enter") {
|
| 302 |
+
if (interactive) {
|
| 303 |
+
label_to_edit = i;
|
| 304 |
+
} else {
|
| 305 |
+
onselect?.({
|
| 306 |
+
index: i,
|
| 307 |
+
value: [token, class_or_confidence]
|
| 308 |
+
});
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
}}
|
| 312 |
+
>
|
| 313 |
+
<span class="text">{token}</span>
|
| 314 |
+
|
| 315 |
+
{#if interactive && class_or_confidence !== null && label_to_edit === i}
|
| 316 |
+
<LabelInput
|
| 317 |
+
bind:value
|
| 318 |
+
bind:label_to_edit
|
| 319 |
+
category={class_or_confidence}
|
| 320 |
+
{active_legend}
|
| 321 |
+
color_map={resolved_color_map}
|
| 322 |
+
label_index={i}
|
| 323 |
+
{token}
|
| 324 |
+
onchange={handle_value_change}
|
| 325 |
+
is_scores_mode
|
| 326 |
+
/>
|
| 327 |
+
{/if}
|
| 328 |
+
</span>
|
| 329 |
+
|
| 330 |
+
{#if interactive && class_or_confidence !== null && active_element_index === i}
|
| 331 |
+
<button
|
| 332 |
+
class="remove-btn"
|
| 333 |
+
aria-label="Remove label"
|
| 334 |
+
onclick={() => remove_highlight(i)}
|
| 335 |
+
>
|
| 336 |
+
<Clear />
|
| 337 |
+
</button>
|
| 338 |
+
{/if}
|
| 339 |
+
</span>
|
| 340 |
+
{/each}
|
| 341 |
+
</div>
|
| 342 |
+
{/if}
|
| 343 |
+
</div>
|
| 344 |
+
|
| 345 |
+
<style>
|
| 346 |
+
.container {
|
| 347 |
+
display: flex;
|
| 348 |
+
flex-direction: column;
|
| 349 |
+
gap: var(--spacing-sm);
|
| 350 |
+
padding: var(--block-padding);
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
.legend,
|
| 354 |
+
.score-legend {
|
| 355 |
+
display: flex;
|
| 356 |
+
flex-wrap: wrap;
|
| 357 |
+
gap: var(--spacing-sm);
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
.legend-item {
|
| 361 |
+
cursor: pointer;
|
| 362 |
+
border: none;
|
| 363 |
+
border-radius: var(--radius-xs);
|
| 364 |
+
padding: 0 var(--size-2);
|
| 365 |
+
font-weight: var(--weight-semibold);
|
| 366 |
+
color: black;
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
.score-legend {
|
| 370 |
+
justify-content: space-between;
|
| 371 |
+
border-radius: var(--radius-xs);
|
| 372 |
+
background: linear-gradient(
|
| 373 |
+
to right,
|
| 374 |
+
var(--color-purple),
|
| 375 |
+
transparent,
|
| 376 |
+
var(--color-red)
|
| 377 |
+
);
|
| 378 |
+
padding: var(--size-1) var(--size-2);
|
| 379 |
+
font-weight: var(--weight-semibold);
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
.textfield {
|
| 383 |
+
display: flex;
|
| 384 |
+
flex-wrap: wrap;
|
| 385 |
+
line-height: var(--scale-4);
|
| 386 |
+
word-break: break-all;
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
.line-break {
|
| 390 |
+
flex-basis: 100%;
|
| 391 |
+
height: 0;
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
.token-container {
|
| 395 |
+
position: relative;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
.token {
|
| 399 |
+
transition: 150ms;
|
| 400 |
+
border-radius: var(--radius-xs);
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
.token.highlighted {
|
| 404 |
+
cursor: pointer;
|
| 405 |
+
padding: var(--size-0-5) var(--size-1);
|
| 406 |
+
margin-left: var(--size-1);
|
| 407 |
+
margin-right: var(--size-2);
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
.token.highlighted.transparent {
|
| 411 |
+
padding: var(--size-0-5) var(--size-0-5);
|
| 412 |
+
margin: 0;
|
| 413 |
+
outline: 1px solid transparent;
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
.token.highlighted.transparent:hover {
|
| 417 |
+
outline-color: var(--neutral-400);
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
:global(.dark) .token.highlighted.transparent:hover {
|
| 421 |
+
outline-color: var(--neutral-500);
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
.token.dimmed {
|
| 425 |
+
color: var(--body-text-color);
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
.text {
|
| 429 |
+
white-space: pre-wrap;
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
.text.unlabeled {
|
| 433 |
+
color: var(--body-text-color);
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
.score-token .text {
|
| 437 |
+
color: var(--body-text-color);
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
.label {
|
| 441 |
+
margin-left: 4px;
|
| 442 |
+
border-radius: var(--radius-xs);
|
| 443 |
+
padding: 1px 5px;
|
| 444 |
+
color: var(--color-white);
|
| 445 |
+
font-weight: var(--weight-bold);
|
| 446 |
+
text-transform: uppercase;
|
| 447 |
+
font-size: 70%;
|
| 448 |
+
vertical-align: middle;
|
| 449 |
+
bottom: 1px;
|
| 450 |
+
position: relative;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
.remove-btn {
|
| 454 |
+
display: none;
|
| 455 |
+
position: absolute;
|
| 456 |
+
top: 0;
|
| 457 |
+
right: 0;
|
| 458 |
+
width: var(--size-3);
|
| 459 |
+
height: var(--size-3);
|
| 460 |
+
border: none;
|
| 461 |
+
border-radius: 50%;
|
| 462 |
+
background: var(--neutral-400);
|
| 463 |
+
color: white;
|
| 464 |
+
font-size: 10px;
|
| 465 |
+
cursor: pointer;
|
| 466 |
+
justify-content: center;
|
| 467 |
+
align-items: center;
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
:global(.dark) .remove-btn {
|
| 471 |
+
background: var(--neutral-500);
|
| 472 |
+
color: var(--neutral-950);
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
.remove-btn :global(svg) {
|
| 476 |
+
width: var(--size-1-5);
|
| 477 |
+
height: var(--size-1-5);
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
.remove-btn:hover {
|
| 481 |
+
background: var(--neutral-500);
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
:global(.dark) .remove-btn:hover {
|
| 485 |
+
background: var(--neutral-400);
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
.token-container:hover .remove-btn,
|
| 489 |
+
.token-container:focus-within .remove-btn {
|
| 490 |
+
display: flex;
|
| 491 |
+
}
|
| 492 |
+
</style>
|
6.5.0/highlightedtext/shared/LabelInput.svelte
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import type { HighlightedToken, ColorPair } from "./utils";
|
| 3 |
+
import { get_score_color } from "./utils";
|
| 4 |
+
|
| 5 |
+
let {
|
| 6 |
+
value = $bindable([]),
|
| 7 |
+
label_to_edit = $bindable(-1),
|
| 8 |
+
category,
|
| 9 |
+
active_legend,
|
| 10 |
+
color_map,
|
| 11 |
+
label_index,
|
| 12 |
+
token,
|
| 13 |
+
onchange,
|
| 14 |
+
is_scores_mode = false
|
| 15 |
+
}: {
|
| 16 |
+
value: HighlightedToken[];
|
| 17 |
+
label_to_edit: number;
|
| 18 |
+
category: string | number | null;
|
| 19 |
+
active_legend: string;
|
| 20 |
+
color_map: Record<string, ColorPair>;
|
| 21 |
+
label_index: number;
|
| 22 |
+
token: string;
|
| 23 |
+
onchange: () => void;
|
| 24 |
+
is_scores_mode?: boolean;
|
| 25 |
+
} = $props();
|
| 26 |
+
|
| 27 |
+
let input_value = $state(category?.toString() ?? "");
|
| 28 |
+
|
| 29 |
+
function get_background_color(): string {
|
| 30 |
+
if (is_scores_mode) {
|
| 31 |
+
const score =
|
| 32 |
+
typeof category === "number" ? category : parseFloat(category ?? "0");
|
| 33 |
+
return get_score_color(score);
|
| 34 |
+
}
|
| 35 |
+
if (category === null || (active_legend && active_legend !== category)) {
|
| 36 |
+
return "";
|
| 37 |
+
}
|
| 38 |
+
return color_map[category]?.primary ?? "";
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
function update_value(e: Event): void {
|
| 42 |
+
const target = e.target as HTMLInputElement;
|
| 43 |
+
const new_value = target.value.trim();
|
| 44 |
+
|
| 45 |
+
value = [
|
| 46 |
+
...value.slice(0, label_index),
|
| 47 |
+
{
|
| 48 |
+
token,
|
| 49 |
+
class_or_confidence:
|
| 50 |
+
new_value === ""
|
| 51 |
+
? null
|
| 52 |
+
: is_scores_mode
|
| 53 |
+
? Number(new_value)
|
| 54 |
+
: new_value
|
| 55 |
+
},
|
| 56 |
+
...value.slice(label_index + 1)
|
| 57 |
+
];
|
| 58 |
+
|
| 59 |
+
onchange();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
function handle_keydown(e: KeyboardEvent): void {
|
| 63 |
+
if (e.key === "Enter") {
|
| 64 |
+
update_value(e);
|
| 65 |
+
label_to_edit = -1;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
</script>
|
| 69 |
+
|
| 70 |
+
<input
|
| 71 |
+
class="label-input"
|
| 72 |
+
autofocus
|
| 73 |
+
type={is_scores_mode ? "number" : "text"}
|
| 74 |
+
step={is_scores_mode ? "0.1" : undefined}
|
| 75 |
+
placeholder={is_scores_mode ? undefined : "label"}
|
| 76 |
+
value={category}
|
| 77 |
+
style:background-color={get_background_color()}
|
| 78 |
+
style:width={is_scores_mode ? "7ch" : `${(input_value?.length || 4) + 4}ch`}
|
| 79 |
+
oninput={(e) => {
|
| 80 |
+
input_value = (e.target as HTMLInputElement).value;
|
| 81 |
+
}}
|
| 82 |
+
onblur={update_value}
|
| 83 |
+
onkeydown={handle_keydown}
|
| 84 |
+
/>
|
| 85 |
+
|
| 86 |
+
<style>
|
| 87 |
+
.label-input {
|
| 88 |
+
margin-top: 1px;
|
| 89 |
+
margin-left: 4px;
|
| 90 |
+
border: none;
|
| 91 |
+
border-radius: var(--radius-xs);
|
| 92 |
+
padding: 1px 5px;
|
| 93 |
+
color: var(--color-white);
|
| 94 |
+
font-weight: var(--weight-bold);
|
| 95 |
+
font-size: var(--text-sm);
|
| 96 |
+
text-transform: uppercase;
|
| 97 |
+
line-height: 1;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.label-input::placeholder {
|
| 101 |
+
color: rgba(255, 255, 255, 0.5);
|
| 102 |
+
}
|
| 103 |
+
</style>
|
6.5.0/highlightedtext/shared/utils.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { colors } from "@gradio/theme";
|
| 2 |
+
|
| 3 |
+
export interface HighlightedToken {
|
| 4 |
+
token: string;
|
| 5 |
+
class_or_confidence: string | number | null;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
export interface ColorPair {
|
| 9 |
+
primary: string;
|
| 10 |
+
secondary: string;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
function name_to_rgba(name: string, alpha: number): string {
|
| 14 |
+
const canvas = document.createElement("canvas");
|
| 15 |
+
const ctx = canvas.getContext("2d")!;
|
| 16 |
+
ctx.fillStyle = name;
|
| 17 |
+
ctx.fillRect(0, 0, 1, 1);
|
| 18 |
+
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
|
| 19 |
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
export function is_transparent(color: string): boolean {
|
| 23 |
+
if (!color) return true;
|
| 24 |
+
const c = color.toLowerCase().trim();
|
| 25 |
+
// 9 chars + ends with 00 = #RRGGBBAA format with 0 alpha
|
| 26 |
+
return c === "transparent" || (c.length === 9 && c.endsWith("00"));
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
export function generate_color_map(
|
| 30 |
+
color_map: Record<string, string>,
|
| 31 |
+
is_browser: boolean
|
| 32 |
+
): Record<string, ColorPair> {
|
| 33 |
+
const result: Record<string, ColorPair> = {};
|
| 34 |
+
|
| 35 |
+
for (const key in color_map) {
|
| 36 |
+
const color = color_map[key].trim();
|
| 37 |
+
|
| 38 |
+
if (color in colors) {
|
| 39 |
+
result[key] = colors[color as keyof typeof colors];
|
| 40 |
+
} else if (is_transparent(color)) {
|
| 41 |
+
result[key] = {
|
| 42 |
+
primary: "transparent",
|
| 43 |
+
secondary: "transparent"
|
| 44 |
+
};
|
| 45 |
+
} else {
|
| 46 |
+
result[key] = {
|
| 47 |
+
primary: is_browser ? name_to_rgba(color, 1) : color,
|
| 48 |
+
secondary: is_browser ? name_to_rgba(color, 0.5) : color
|
| 49 |
+
};
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
return result;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
export function merge_elements(
|
| 57 |
+
value: HighlightedToken[],
|
| 58 |
+
merge_mode: "empty" | "equal"
|
| 59 |
+
): HighlightedToken[] {
|
| 60 |
+
if (value.length === 0) return [];
|
| 61 |
+
|
| 62 |
+
const result: HighlightedToken[] = [];
|
| 63 |
+
let current_token = value[0].token;
|
| 64 |
+
let current_class = value[0].class_or_confidence;
|
| 65 |
+
|
| 66 |
+
for (let i = 1; i < value.length; i++) {
|
| 67 |
+
const { token, class_or_confidence } = value[i];
|
| 68 |
+
const should_merge =
|
| 69 |
+
merge_mode === "empty"
|
| 70 |
+
? class_or_confidence === null
|
| 71 |
+
: current_class === class_or_confidence;
|
| 72 |
+
|
| 73 |
+
if (should_merge) {
|
| 74 |
+
current_token += token;
|
| 75 |
+
} else {
|
| 76 |
+
result.push({ token: current_token, class_or_confidence: current_class });
|
| 77 |
+
current_token = token;
|
| 78 |
+
current_class = class_or_confidence;
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
result.push({ token: current_token, class_or_confidence: current_class });
|
| 83 |
+
return result;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
export function get_score_color(score: number | null): string {
|
| 87 |
+
if (score === null) return "";
|
| 88 |
+
if (score < 0) {
|
| 89 |
+
return `rgba(128, 90, 213, ${-score})`;
|
| 90 |
+
}
|
| 91 |
+
return `rgba(239, 68, 60, ${score})`;
|
| 92 |
+
}
|
6.5.0/highlightedtext/types.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { SelectData, CustomButton } from "@gradio/utils";
|
| 2 |
+
import type { LoadingStatus } from "js/statustracker";
|
| 3 |
+
|
| 4 |
+
export interface HighlightedToken {
|
| 5 |
+
token: string;
|
| 6 |
+
class_or_confidence: string | number | null;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export interface HighlightedTextProps {
|
| 10 |
+
value: HighlightedToken[];
|
| 11 |
+
show_legend: boolean;
|
| 12 |
+
show_inline_category: boolean;
|
| 13 |
+
color_map: Record<string, string>;
|
| 14 |
+
combine_adjacent: boolean;
|
| 15 |
+
rtl: boolean;
|
| 16 |
+
buttons: (string | CustomButton)[] | null;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export interface HighlightedTextEvents {
|
| 20 |
+
change: never;
|
| 21 |
+
select: SelectData;
|
| 22 |
+
clear_status: LoadingStatus;
|
| 23 |
+
custom_button_click: { id: number };
|
| 24 |
+
}
|