gradio-pr-bot commited on
Commit
4959be9
·
verified ·
1 Parent(s): c6d57e9

Upload folder using huggingface_hub

Browse files
6.4.0/highlightedtext/Index.svelte CHANGED
@@ -1,11 +1,9 @@
1
- <script context="module" lang="ts">
2
- export { default as BaseStaticHighlightedText } from "./shared/StaticHighlightedtext.svelte";
3
- export { default as BaseInteractiveHighlightedText } from "./shared/InteractiveHighlightedtext.svelte";
4
  </script>
5
 
6
  <script lang="ts">
7
- import StaticHighlightedText from "./shared/StaticHighlightedtext.svelte";
8
- import InteractiveHighlightedText from "./shared/InteractiveHighlightedtext.svelte";
9
  import { Block, BlockLabel, Empty, IconButtonWrapper } from "@gradio/atoms";
10
  import { TextHighlight } from "@gradio/icons";
11
  import { StatusTracker } from "@gradio/statustracker";
@@ -19,7 +17,7 @@
19
  let old_value = $state(gradio.props.value);
20
 
21
  $effect(() => {
22
- if (old_value != gradio.props.value) {
23
  old_value = gradio.props.value;
24
  gradio.dispatch("change");
25
  }
@@ -32,107 +30,65 @@
32
  );
33
  </script>
34
 
35
- {#if !gradio.shared.interactive}
36
- <Block
37
- variant={"solid"}
38
- test_id="highlighted-text"
39
- visible={gradio.shared.visible}
40
- elem_id={gradio.shared.elem_id}
41
- elem_classes={gradio.shared.elem_classes}
42
- padding={false}
43
- container={gradio.shared.container}
44
- scale={gradio.shared.scale}
45
- min_width={gradio.shared.min_width}
46
- rtl={gradio.props.rtl}
47
- >
48
- <StatusTracker
49
- autoscroll={gradio.shared.autoscroll}
50
- i18n={gradio.i18n}
51
- {...gradio.shared.loading_status}
52
- on_clear_status={() =>
53
- gradio.dispatch("clear_status", gradio.shared.loading_status)}
 
 
 
 
 
 
54
  />
55
- {#if gradio.shared.label && gradio.shared.show_label}
56
- <BlockLabel
57
- Icon={TextHighlight}
58
- label={gradio.shared.label ||
59
- gradio.i18n("highlighted_text.highlighted_text")}
60
- float={false}
61
- disable={gradio.shared.container === false}
62
- show_label={gradio.shared.show_label}
63
- rtl={gradio.props.rtl}
64
- />
65
- {/if}
66
 
67
- {#if value}
68
- <StaticHighlightedText
69
- on:select={({ detail }) => gradio.dispatch("select", detail)}
70
- selectable={false}
71
- {value}
72
- show_legend={gradio.props.show_legend}
73
- show_inline_category={gradio.props.show_inline_category}
74
- color_map={gradio.props.color_map}
75
- />
76
- {:else}
77
- <Empty>
78
- <TextHighlight />
79
- </Empty>
80
- {/if}
81
- </Block>
82
- {:else}
83
- <Block
84
- variant={gradio.shared.interactive ? "dashed" : "solid"}
85
- test_id="highlighted-text"
86
- visible={gradio.shared.visible}
87
- elem_id={gradio.shared.elem_id}
88
- elem_classes={gradio.shared.elem_classes}
89
- padding={false}
90
- container={gradio.shared.container}
91
- scale={gradio.shared.scale}
92
- min_width={gradio.shared.min_width}
93
- rtl={gradio.props.rtl}
94
- >
95
- <StatusTracker
96
- autoscroll={gradio.shared.autoscroll}
97
- {...gradio.shared.loading_status}
98
- i18n={gradio.i18n}
99
- on_clear_status={() =>
100
- gradio.dispatch("clear_status", gradio.shared.loading_status)}
101
  />
102
- {#if gradio.shared.label && gradio.shared.show_label && gradio.props.buttons && gradio.props.buttons.length > 0}
103
- <IconButtonWrapper
104
- buttons={gradio.props.buttons}
105
- on_custom_button_click={(id) => {
106
- gradio.dispatch("custom_button_click", { id });
107
- }}
108
- />
109
- {/if}
110
- {#if gradio.shared.label && gradio.shared.show_label}
111
- <BlockLabel
112
- Icon={TextHighlight}
113
- label={gradio.shared.label}
114
- float={false}
115
- disable={gradio.shared.container === false}
116
- show_label={gradio.shared.show_label}
117
- rtl={gradio.props.rtl}
118
- />
119
- {/if}
120
 
121
- {#if value}
122
- <InteractiveHighlightedText
123
- bind:value
124
- on:change={() => {
125
- gradio.props.value = value;
126
- gradio.dispatch("change");
127
- }}
128
- selectable={false}
129
- show_legend={gradio.props.show_legend}
130
- color_map={gradio.props.color_map}
131
- />
132
- {:else}
133
- <Empty size="small" unpadded_box={true}>
134
- <TextHighlight />
135
- </Empty>
136
- {/if}
137
- </Block>
138
- {/if}
 
 
 
 
 
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";
 
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
  }
 
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.4.0/highlightedtext/package.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "@gradio/highlightedtext",
3
- "version": "0.10.1",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "author": "",
 
1
  {
2
  "name": "@gradio/highlightedtext",
3
+ "version": "0.11.0",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "author": "",
6.4.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.4.0/highlightedtext/shared/LabelInput.svelte CHANGED
@@ -1,124 +1,103 @@
1
  <script lang="ts">
2
- type HighlightedTextType = {
3
- token: string;
4
- class_or_confidence: string | number | null;
5
- };
6
 
7
- export let value: HighlightedTextType[];
8
- export let category: string | number | null;
9
- export let active: string;
10
- export let labelToEdit: number;
11
- export let indexOfLabel: number;
12
- export let text: string;
13
- export let handleValueChange: () => void;
14
- export let isScoresMode = false;
15
- export let _color_map: Record<string, { primary: string; secondary: string }>;
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- let _input_value = category;
18
 
19
- function handleInput(e: Event): void {
20
- let target = e.target as HTMLInputElement;
21
- if (target) {
22
- _input_value = target.value;
 
 
 
 
23
  }
 
24
  }
25
 
26
- function updateLabelValue(
27
- e: Event,
28
- elementIndex: number,
29
- text: string
30
- ): void {
31
- let target = e.target as HTMLInputElement;
32
  value = [
33
- ...value.slice(0, elementIndex),
34
  {
35
- token: text,
36
  class_or_confidence:
37
- target.value === ""
38
  ? null
39
- : isScoresMode
40
- ? Number(target.value)
41
- : target.value
42
  },
43
- ...value.slice(elementIndex + 1)
44
  ];
45
 
46
- handleValueChange();
47
  }
48
 
49
- function clearPlaceHolderOnFocus(e: FocusEvent): void {
50
- let target = e.target as HTMLInputElement;
51
- if (target && target.placeholder) target.placeholder = "";
 
 
52
  }
53
  </script>
54
 
55
- <!-- svelte-ignore a11y-autofocus -->
56
- <!-- autofocus should not be disorienting for a screen reader users
57
- as input is only rendered once a new label is created -->
58
- {#if !isScoresMode}
59
- <input
60
- class="label-input"
61
- autofocus
62
- id={`label-input-${indexOfLabel}`}
63
- type="text"
64
- placeholder="label"
65
- value={category}
66
- style:background-color={category === null || (active && active !== category)
67
- ? ""
68
- : _color_map[category].primary}
69
- style:width={_input_value
70
- ? _input_value.toString()?.length + 4 + "ch"
71
- : "8ch"}
72
- on:input={handleInput}
73
- on:blur={(e) => updateLabelValue(e, indexOfLabel, text)}
74
- on:keydown={(e) => {
75
- if (e.key === "Enter") {
76
- updateLabelValue(e, indexOfLabel, text);
77
- labelToEdit = -1;
78
- }
79
- }}
80
- on:focus={clearPlaceHolderOnFocus}
81
- />
82
- {:else}
83
- <input
84
- class="label-input"
85
- autofocus
86
- type="number"
87
- step="0.1"
88
- style={"background-color: rgba(" +
89
- (typeof category === "number" && category < 0
90
- ? "128, 90, 213," + -category
91
- : "239, 68, 60," + category) +
92
- ")"}
93
- value={category}
94
- style:width="7ch"
95
- on:input={handleInput}
96
- on:blur={(e) => updateLabelValue(e, indexOfLabel, text)}
97
- on:keydown={(e) => {
98
- if (e.key === "Enter") {
99
- updateLabelValue(e, indexOfLabel, text);
100
- labelToEdit = -1;
101
- }
102
- }}
103
- />
104
- {/if}
105
 
106
  <style>
107
  .label-input {
108
- transition: 150ms;
109
  margin-top: 1px;
110
- margin-right: calc(var(--size-1));
 
111
  border-radius: var(--radius-xs);
112
  padding: 1px 5px;
113
- color: black;
114
  font-weight: var(--weight-bold);
115
  font-size: var(--text-sm);
116
  text-transform: uppercase;
117
  line-height: 1;
118
- color: white;
119
  }
120
 
121
  .label-input::placeholder {
122
- color: rgba(1, 1, 1, 0.5);
123
  }
124
  </style>
 
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.4.0/highlightedtext/shared/utils.ts CHANGED
@@ -1,77 +1,92 @@
1
  import { colors } from "@gradio/theme";
2
 
3
- export function name_to_rgba(
4
- name: string,
5
- a: number,
6
- ctx: CanvasRenderingContext2D | null
7
- ): string {
8
- if (!ctx) {
9
- var canvas = document.createElement("canvas");
10
- ctx = canvas.getContext("2d")!;
11
- }
 
 
 
 
12
  ctx.fillStyle = name;
13
  ctx.fillRect(0, 0, 1, 1);
14
  const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
15
- ctx.clearRect(0, 0, 1, 1);
16
- return `rgba(${r}, ${g}, ${b}, ${255 / a})`;
17
  }
18
 
19
- export function correct_color_map(
 
 
 
 
 
 
 
20
  color_map: Record<string, string>,
21
- _color_map: Record<string, { primary: string; secondary: string }>,
22
- browser: any,
23
- ctx: CanvasRenderingContext2D | null
24
- ): void {
25
- for (const col in color_map) {
26
- const _c = color_map[col].trim();
27
 
28
- if (_c in colors) {
29
- _color_map[col] = colors[_c as keyof typeof colors];
 
 
 
 
 
 
 
 
30
  } else {
31
- _color_map[col] = {
32
- primary: browser
33
- ? name_to_rgba(color_map[col], 1, ctx)
34
- : color_map[col],
35
- secondary: browser
36
- ? name_to_rgba(color_map[col], 0.5, ctx)
37
- : color_map[col]
38
  };
39
  }
40
  }
 
 
41
  }
42
 
43
  export function merge_elements(
44
- value: { token: string; class_or_confidence: string | number | null }[],
45
- mergeMode: "empty" | "equal"
46
- ): { token: string; class_or_confidence: string | number | null }[] {
47
- let result: typeof value = [];
48
- let tempStr: string | null = null;
49
- let tempVal: string | number | null = null;
 
 
50
 
51
- for (const val of value) {
52
- if (
53
- (mergeMode === "empty" && val.class_or_confidence === null) ||
54
- (mergeMode === "equal" && tempVal === val.class_or_confidence)
55
- ) {
56
- tempStr = tempStr ? tempStr + val.token : val.token;
 
 
 
57
  } else {
58
- if (tempStr !== null) {
59
- result.push({
60
- token: tempStr,
61
- class_or_confidence: tempVal
62
- });
63
- }
64
- tempStr = val.token;
65
- tempVal = val.class_or_confidence;
66
  }
67
  }
68
 
69
- if (tempStr !== null) {
70
- result.push({
71
- token: tempStr,
72
- class_or_confidence: tempVal
73
- });
74
- }
75
-
76
  return result;
77
  }
 
 
 
 
 
 
 
 
 
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
+ }