gradio-pr-bot commited on
Commit
25f10ca
·
verified ·
1 Parent(s): 23f2d6b

Upload folder using huggingface_hub

Browse files
6.0.0-dev.6/highlightedtext/Index.svelte ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 } from "@gradio/atoms";
10
+ import { TextHighlight } from "@gradio/icons";
11
+ import { StatusTracker } from "@gradio/statustracker";
12
+ import { Gradio } from "@gradio/utils";
13
+ import { merge_elements } from "./shared/utils";
14
+ import type { HighlightedTextProps, HighlightedTextEvents } from "./types";
15
+ import { merge } from "@storybook/manager-api";
16
+
17
+ const props = $props();
18
+ const gradio = new Gradio<HighlightedTextEvents, HighlightedTextProps>(props);
19
+
20
+ let old_value = $state(gradio.props.value);
21
+
22
+ $effect(() => {
23
+ if (old_value != gradio.props.value) {
24
+ old_value = gradio.props.value;
25
+ gradio.dispatch("change");
26
+ }
27
+ });
28
+
29
+ let value = $derived.by(() =>
30
+ gradio.props.combine_adjacent
31
+ ? merge_elements(gradio.props.value, "equal")
32
+ : gradio.props.value
33
+ );
34
+ </script>
35
+
36
+ {#if !gradio.shared.interactive}
37
+ <Block
38
+ variant={"solid"}
39
+ test_id="highlighted-text"
40
+ visible={gradio.shared.visible}
41
+ elem_id={gradio.shared.elem_id}
42
+ elem_classes={gradio.shared.elem_classes}
43
+ padding={false}
44
+ container={gradio.shared.container}
45
+ scale={gradio.shared.scale}
46
+ min_width={gradio.shared.min_width}
47
+ rtl={gradio.props.rtl}
48
+ >
49
+ <StatusTracker
50
+ autoscroll={gradio.shared.autoscroll}
51
+ i18n={gradio.i18n}
52
+ {...gradio.shared.loading_status}
53
+ on:clear_status={() =>
54
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
55
+ />
56
+ {#if gradio.shared.label && gradio.shared.show_label}
57
+ <BlockLabel
58
+ Icon={TextHighlight}
59
+ label={gradio.shared.label ||
60
+ gradio.i18n("highlighted_text.highlighted_text")}
61
+ float={false}
62
+ disable={gradio.shared.container === false}
63
+ show_label={gradio.shared.show_label}
64
+ rtl={gradio.props.rtl}
65
+ />
66
+ {/if}
67
+
68
+ {#if value}
69
+ <StaticHighlightedText
70
+ on:select={({ detail }) => gradio.dispatch("select", detail)}
71
+ selectable={false}
72
+ {value}
73
+ show_legend={gradio.props.show_legend}
74
+ show_inline_category={gradio.props.show_inline_category}
75
+ color_map={gradio.props.color_map}
76
+ />
77
+ {:else}
78
+ <Empty>
79
+ <TextHighlight />
80
+ </Empty>
81
+ {/if}
82
+ </Block>
83
+ {:else}
84
+ <Block
85
+ variant={gradio.shared.interactive ? "dashed" : "solid"}
86
+ test_id="highlighted-text"
87
+ visible={gradio.shared.visible}
88
+ elem_id={gradio.shared.elem_id}
89
+ elem_classes={gradio.shared.elem_classes}
90
+ padding={false}
91
+ container={gradio.shared.container}
92
+ scale={gradio.shared.scale}
93
+ min_width={gradio.shared.min_width}
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}
103
+ <BlockLabel
104
+ Icon={TextHighlight}
105
+ label={gradio.shared.label}
106
+ float={false}
107
+ disable={gradio.shared.container === false}
108
+ show_label={gradio.shared.show_label}
109
+ rtl={gradio.props.rtl}
110
+ />
111
+ {/if}
112
+
113
+ {#if value}
114
+ <InteractiveHighlightedText
115
+ bind:value
116
+ on:change={() => {
117
+ gradio.props.value = value;
118
+ gradio.dispatch("change");
119
+ }}
120
+ selectable={false}
121
+ show_legend={gradio.props.show_legend}
122
+ color_map={gradio.props.color_map}
123
+ />
124
+ {:else}
125
+ <Empty size="small" unpadded_box={true}>
126
+ <TextHighlight />
127
+ </Empty>
128
+ {/if}
129
+ </Block>
130
+ {/if}
6.0.0-dev.6/highlightedtext/package.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/highlightedtext",
3
+ "version": "0.9.14-dev.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.43.4"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/gradio-app/gradio.git",
35
+ "directory": "js/highlightedtext"
36
+ }
37
+ }
6.0.0-dev.6/highlightedtext/shared/InteractiveHighlightedtext.svelte ADDED
@@ -0,0 +1,510 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ const browser = typeof document !== "undefined";
3
+ import { get_next_color } from "@gradio/utils";
4
+ import type { SelectData } from "@gradio/utils";
5
+ import { createEventDispatcher, onMount } from "svelte";
6
+ import { correct_color_map, merge_elements } from "./utils";
7
+ import LabelInput from "./LabelInput.svelte";
8
+
9
+ export let value: {
10
+ token: string;
11
+ class_or_confidence: string | number | null;
12
+ }[] = [];
13
+ export let show_legend = false;
14
+ export let color_map: Record<string, string> = {};
15
+ export let selectable = false;
16
+
17
+ let activeElementIndex = -1;
18
+ let ctx: CanvasRenderingContext2D;
19
+ let _color_map: Record<string, { primary: string; secondary: string }> = {};
20
+ let active = "";
21
+ let selection: Selection | null;
22
+ let labelToEdit = -1;
23
+
24
+ onMount(() => {
25
+ const mouseUpHandler = (): void => {
26
+ selection = window.getSelection();
27
+ handleSelectionComplete();
28
+ window.removeEventListener("mouseup", mouseUpHandler);
29
+ };
30
+
31
+ window.addEventListener("mousedown", () => {
32
+ window.addEventListener("mouseup", mouseUpHandler);
33
+ });
34
+ });
35
+
36
+ async function handleTextSelected(
37
+ startIndex: number,
38
+ endIndex: number
39
+ ): Promise<void> {
40
+ if (
41
+ selection?.toString() &&
42
+ activeElementIndex !== -1 &&
43
+ value[activeElementIndex].token.toString().includes(selection.toString())
44
+ ) {
45
+ const tempFlag = Symbol();
46
+
47
+ const str = value[activeElementIndex].token;
48
+ const [before, selected, after] = [
49
+ str.substring(0, startIndex),
50
+ str.substring(startIndex, endIndex),
51
+ str.substring(endIndex)
52
+ ];
53
+
54
+ let tempValue: {
55
+ token: string;
56
+ class_or_confidence: string | number | null;
57
+ flag?: symbol;
58
+ }[] = [
59
+ ...value.slice(0, activeElementIndex),
60
+ { token: before, class_or_confidence: null },
61
+ {
62
+ token: selected,
63
+ class_or_confidence: mode === "scores" ? 1 : "label",
64
+ flag: tempFlag
65
+ }, // add a temp flag to the new highlighted text element
66
+ { token: after, class_or_confidence: null },
67
+ ...value.slice(activeElementIndex + 1)
68
+ ];
69
+
70
+ // store the index of the new highlighted text element and remove the flag
71
+ labelToEdit = tempValue.findIndex(({ flag }) => flag === tempFlag);
72
+ // tempValue[labelToEdit].pop();
73
+
74
+ // remove elements with empty labels
75
+ tempValue = tempValue.filter((item) => item.token.trim() !== "");
76
+ value = tempValue.map(({ flag, ...rest }) => rest);
77
+
78
+ handleValueChange();
79
+ document.getElementById(`label-input-${labelToEdit}`)?.focus();
80
+ }
81
+ }
82
+
83
+ const dispatch = createEventDispatcher<{
84
+ select: SelectData;
85
+ change: typeof value;
86
+ input: never;
87
+ }>();
88
+
89
+ function splitTextByNewline(text: string): string[] {
90
+ return text.split("\n");
91
+ }
92
+
93
+ function removeHighlightedText(index: number): void {
94
+ if (!value || index < 0 || index >= value.length) return;
95
+ value[index].class_or_confidence = null;
96
+ value = merge_elements(value, "equal");
97
+ handleValueChange();
98
+ window.getSelection()?.empty();
99
+ }
100
+
101
+ function handleValueChange(): void {
102
+ dispatch("change", value);
103
+ labelToEdit = -1;
104
+
105
+ // reset legend color maps
106
+ if (show_legend) {
107
+ color_map = {};
108
+ _color_map = {};
109
+ }
110
+ }
111
+
112
+ let mode: "categories" | "scores";
113
+
114
+ $: {
115
+ if (!color_map) {
116
+ color_map = {};
117
+ }
118
+ if (value.length > 0) {
119
+ for (let entry of value) {
120
+ if (entry.class_or_confidence !== null) {
121
+ if (typeof entry.class_or_confidence === "string") {
122
+ mode = "categories";
123
+ if (!(entry.class_or_confidence in color_map)) {
124
+ let color = get_next_color(Object.keys(color_map).length);
125
+ color_map[entry.class_or_confidence] = color;
126
+ }
127
+ } else {
128
+ mode = "scores";
129
+ }
130
+ }
131
+ }
132
+ }
133
+ _color_map = {};
134
+ correct_color_map(color_map, _color_map, browser, ctx);
135
+ }
136
+
137
+ function handle_mouseover(label: string): void {
138
+ active = label;
139
+ }
140
+ function handle_mouseout(): void {
141
+ active = "";
142
+ }
143
+
144
+ async function handleKeydownSelection(event: KeyboardEvent): Promise<void> {
145
+ selection = window.getSelection();
146
+
147
+ if (event.key === "Enter") {
148
+ handleSelectionComplete();
149
+ }
150
+ }
151
+
152
+ function handleSelectionComplete(): void {
153
+ if (selection && selection?.toString().trim() !== "") {
154
+ const textBeginningIndex = selection.getRangeAt(0).startOffset;
155
+ const textEndIndex = selection.getRangeAt(0).endOffset;
156
+ handleTextSelected(textBeginningIndex, textEndIndex);
157
+ }
158
+ }
159
+
160
+ function handleSelect(
161
+ i: number,
162
+ text: string,
163
+ class_or_confidence: string | number | null
164
+ ): void {
165
+ dispatch("select", {
166
+ index: i,
167
+ value: [text, class_or_confidence]
168
+ });
169
+ }
170
+ </script>
171
+
172
+ <div class="container">
173
+ {#if mode === "categories"}
174
+ {#if show_legend}
175
+ <div
176
+ class="class_or_confidence-legend"
177
+ data-testid="highlighted-text:class_or_confidence-legend"
178
+ >
179
+ {#if _color_map}
180
+ {#each Object.entries(_color_map) as [class_or_confidence, color], i}
181
+ <div
182
+ role="button"
183
+ aria-roledescription="Categories of highlighted text. Hover to see text with this class_or_confidence highlighted."
184
+ tabindex="0"
185
+ on:mouseover={() => handle_mouseover(class_or_confidence)}
186
+ on:focus={() => handle_mouseover(class_or_confidence)}
187
+ on:mouseout={() => handle_mouseout()}
188
+ on:blur={() => handle_mouseout()}
189
+ class="class_or_confidence-label"
190
+ style={"background-color:" + color.secondary}
191
+ >
192
+ {class_or_confidence}
193
+ </div>
194
+ {/each}
195
+ {/if}
196
+ </div>
197
+ {/if}
198
+
199
+ <div class="textfield">
200
+ {#each value as { token, class_or_confidence }, i}
201
+ {#each splitTextByNewline(token) as line, j}
202
+ {#if line.trim() !== ""}
203
+ <span class="text-class_or_confidence-container">
204
+ <span
205
+ role="button"
206
+ tabindex="0"
207
+ class="textspan"
208
+ style:background-color={class_or_confidence === null ||
209
+ (active && active !== class_or_confidence)
210
+ ? ""
211
+ : class_or_confidence && _color_map[class_or_confidence]
212
+ ? _color_map[class_or_confidence].secondary
213
+ : ""}
214
+ class:no-cat={class_or_confidence === null ||
215
+ (active && active !== class_or_confidence)}
216
+ class:hl={class_or_confidence !== null}
217
+ class:selectable
218
+ on:click={() => {
219
+ if (class_or_confidence !== null) {
220
+ handleSelect(i, token, class_or_confidence);
221
+ }
222
+ }}
223
+ on:keydown={(e) => {
224
+ if (class_or_confidence !== null) {
225
+ labelToEdit = i;
226
+ handleSelect(i, token, class_or_confidence);
227
+ } else {
228
+ handleKeydownSelection(e);
229
+ }
230
+ }}
231
+ on:focus={() => (activeElementIndex = i)}
232
+ on:mouseover={() => (activeElementIndex = i)}
233
+ >
234
+ <span
235
+ class:no-label={class_or_confidence === null}
236
+ class="text"
237
+ role="button"
238
+ on:keydown={(e) => handleKeydownSelection(e)}
239
+ on:focus={() => (activeElementIndex = i)}
240
+ on:mouseover={() => (activeElementIndex = i)}
241
+ on:click={() => (labelToEdit = i)}
242
+ tabindex="0">{line}</span
243
+ >
244
+ {#if !show_legend && class_or_confidence !== null && labelToEdit !== i}
245
+ <span
246
+ id={`label-tag-${i}`}
247
+ class="label"
248
+ role="button"
249
+ tabindex="0"
250
+ style:background-color={class_or_confidence === null ||
251
+ (active && active !== class_or_confidence)
252
+ ? ""
253
+ : _color_map[class_or_confidence].primary}
254
+ on:click={() => (labelToEdit = i)}
255
+ on:keydown={() => (labelToEdit = i)}
256
+ >
257
+ {class_or_confidence}
258
+ </span>
259
+ {/if}
260
+ {#if labelToEdit === i && class_or_confidence !== null}
261
+ &nbsp;
262
+ <LabelInput
263
+ bind:value
264
+ {labelToEdit}
265
+ category={class_or_confidence}
266
+ {active}
267
+ {_color_map}
268
+ indexOfLabel={i}
269
+ text={token}
270
+ {handleValueChange}
271
+ />
272
+ {/if}
273
+ </span>
274
+ {#if class_or_confidence !== null}
275
+ <span
276
+ class="label-clear-button"
277
+ role="button"
278
+ aria-roledescription="Remove label from text"
279
+ tabindex="0"
280
+ on:click={() => removeHighlightedText(i)}
281
+ on:keydown={(event) => {
282
+ if (event.key === "Enter") {
283
+ removeHighlightedText(i);
284
+ }
285
+ }}
286
+
287
+ </span>
288
+ {/if}
289
+ </span>
290
+ {/if}
291
+ {#if j < splitTextByNewline(token).length - 1}
292
+ <br />
293
+ {/if}
294
+ {/each}
295
+ {/each}
296
+ </div>
297
+ {:else}
298
+ {#if show_legend}
299
+ <div class="color-legend" data-testid="highlighted-text:color-legend">
300
+ <span>-1</span>
301
+ <span>0</span>
302
+ <span>+1</span>
303
+ </div>
304
+ {/if}
305
+
306
+ <div class="textfield" data-testid="highlighted-text:textfield">
307
+ {#each value as { token, class_or_confidence }, i}
308
+ {@const score =
309
+ typeof class_or_confidence === "string"
310
+ ? parseInt(class_or_confidence)
311
+ : class_or_confidence}
312
+ <span class="score-text-container">
313
+ <span
314
+ class="textspan score-text"
315
+ role="button"
316
+ tabindex="0"
317
+ class:no-cat={class_or_confidence === null ||
318
+ (active && active !== class_or_confidence)}
319
+ class:hl={class_or_confidence !== null}
320
+ on:mouseover={() => (activeElementIndex = i)}
321
+ on:focus={() => (activeElementIndex = i)}
322
+ on:click={() => (labelToEdit = i)}
323
+ on:keydown={(e) => {
324
+ if (e.key === "Enter") {
325
+ labelToEdit = i;
326
+ }
327
+ }}
328
+ style={"background-color: rgba(" +
329
+ (score && score < 0
330
+ ? "128, 90, 213," + -score
331
+ : "239, 68, 60," + score) +
332
+ ")"}
333
+ >
334
+ <span class="text">{token}</span>
335
+ {#if class_or_confidence && labelToEdit === i}
336
+ <LabelInput
337
+ bind:value
338
+ {labelToEdit}
339
+ {_color_map}
340
+ category={class_or_confidence}
341
+ {active}
342
+ indexOfLabel={i}
343
+ text={token}
344
+ {handleValueChange}
345
+ isScoresMode
346
+ />
347
+ {/if}
348
+ </span>
349
+ {#if class_or_confidence && activeElementIndex === i}
350
+ <span
351
+ class="label-clear-button"
352
+ role="button"
353
+ aria-roledescription="Remove label from text"
354
+ tabindex="0"
355
+ on:click={() => removeHighlightedText(i)}
356
+ on:keydown={(event) => {
357
+ if (event.key === "Enter") {
358
+ removeHighlightedText(i);
359
+ }
360
+ }}
361
+
362
+ </span>
363
+ {/if}
364
+ </span>
365
+ {/each}
366
+ </div>
367
+ {/if}
368
+ </div>
369
+
370
+ <style>
371
+ .label-clear-button {
372
+ display: none;
373
+ border-radius: var(--radius-xs);
374
+ padding-top: 2.5px;
375
+ padding-right: var(--size-1);
376
+ padding-bottom: 3.5px;
377
+ padding-left: var(--size-1);
378
+ color: black;
379
+ background-color: var(--background-fill-secondary);
380
+ user-select: none;
381
+ position: relative;
382
+ left: -3px;
383
+ border-radius: 0 var(--radius-xs) var(--radius-xs) 0;
384
+ color: var(--block-label-text-color);
385
+ }
386
+
387
+ .text-class_or_confidence-container:hover .label-clear-button,
388
+ .text-class_or_confidence-container:focus-within .label-clear-button,
389
+ .score-text-container:hover .label-clear-button,
390
+ .score-text-container:focus-within .label-clear-button {
391
+ display: inline;
392
+ }
393
+
394
+ .text-class_or_confidence-container:hover .textspan.hl,
395
+ .text-class_or_confidence-container:focus-within .textspan.hl,
396
+ .score-text:hover {
397
+ border-radius: var(--radius-xs) 0 0 var(--radius-xs);
398
+ }
399
+
400
+ .container {
401
+ display: flex;
402
+ flex-direction: column;
403
+ gap: var(--spacing-sm);
404
+ padding: var(--block-padding);
405
+ }
406
+
407
+ .hl {
408
+ margin-left: var(--size-1);
409
+ transition: background-color 0.3s;
410
+ user-select: none;
411
+ }
412
+
413
+ .textspan:last-child > .label {
414
+ margin-right: 0;
415
+ }
416
+
417
+ .class_or_confidence-legend {
418
+ display: flex;
419
+ flex-wrap: wrap;
420
+ gap: var(--spacing-sm);
421
+ color: black;
422
+ }
423
+
424
+ .class_or_confidence-label {
425
+ cursor: pointer;
426
+ border-radius: var(--radius-xs);
427
+ padding-right: var(--size-2);
428
+ padding-left: var(--size-2);
429
+ font-weight: var(--weight-semibold);
430
+ }
431
+
432
+ .color-legend {
433
+ display: flex;
434
+ justify-content: space-between;
435
+ border-radius: var(--radius-xs);
436
+ background: linear-gradient(
437
+ to right,
438
+ var(--color-purple),
439
+ rgba(255, 255, 255, 0),
440
+ var(--color-red)
441
+ );
442
+ padding: var(--size-1) var(--size-2);
443
+ font-weight: var(--weight-semibold);
444
+ }
445
+
446
+ .textfield {
447
+ box-sizing: border-box;
448
+ border-radius: var(--radius-xs);
449
+ background: var(--background-fill-primary);
450
+ background-color: transparent;
451
+ max-width: var(--size-full);
452
+ line-height: var(--scale-4);
453
+ word-break: break-all;
454
+ }
455
+
456
+ .textspan {
457
+ transition: 150ms;
458
+ border-radius: var(--radius-xs);
459
+ padding-top: 2.5px;
460
+ padding-right: var(--size-1);
461
+ padding-bottom: 3.5px;
462
+ padding-left: var(--size-1);
463
+ color: black;
464
+ cursor: text;
465
+ }
466
+
467
+ .label {
468
+ transition: 150ms;
469
+ margin-top: 1px;
470
+ border-radius: var(--radius-xs);
471
+ padding: 1px 5px;
472
+ color: var(--body-text-color);
473
+ color: white;
474
+ font-weight: var(--weight-bold);
475
+ font-size: var(--text-sm);
476
+ text-transform: uppercase;
477
+ user-select: none;
478
+ }
479
+
480
+ .text {
481
+ color: black;
482
+ white-space: pre-wrap;
483
+ }
484
+
485
+ .textspan.hl {
486
+ user-select: none;
487
+ }
488
+
489
+ .score-text-container {
490
+ margin-right: var(--size-1);
491
+ }
492
+
493
+ .score-text .text {
494
+ color: var(--body-text-color);
495
+ }
496
+
497
+ .no-cat {
498
+ color: var(--body-text-color);
499
+ }
500
+
501
+ .no-label {
502
+ color: var(--body-text-color);
503
+ user-select: text;
504
+ }
505
+
506
+ .selectable {
507
+ cursor: text;
508
+ user-select: text;
509
+ }
510
+ </style>
6.0.0-dev.6/highlightedtext/shared/LabelInput.svelte ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>
6.0.0-dev.6/highlightedtext/shared/StaticHighlightedtext.svelte ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ const browser = typeof document !== "undefined";
3
+ import { get_next_color } from "@gradio/utils";
4
+ import type { SelectData } from "@gradio/utils";
5
+ import { createEventDispatcher } from "svelte";
6
+ import { correct_color_map } from "./utils";
7
+
8
+ export let value: {
9
+ token: string;
10
+ class_or_confidence: string | number | null;
11
+ }[] = [];
12
+ export let show_legend = false;
13
+ export let show_inline_category = true;
14
+ export let color_map: Record<string, string> = {};
15
+ export let selectable = false;
16
+
17
+ let ctx: CanvasRenderingContext2D;
18
+ let _color_map: Record<string, { primary: string; secondary: string }> = {};
19
+ let active = "";
20
+
21
+ function splitTextByNewline(text: string): string[] {
22
+ return text.split("\n");
23
+ }
24
+
25
+ const dispatch = createEventDispatcher<{
26
+ select: SelectData;
27
+ }>();
28
+
29
+ let mode: "categories" | "scores";
30
+
31
+ $: {
32
+ if (!color_map) {
33
+ color_map = {};
34
+ }
35
+ if (value.length > 0) {
36
+ for (let entry of value) {
37
+ if (entry.class_or_confidence !== null) {
38
+ if (typeof entry.class_or_confidence === "string") {
39
+ mode = "categories";
40
+ if (!(entry.class_or_confidence in color_map)) {
41
+ let color = get_next_color(Object.keys(color_map).length);
42
+ color_map[entry.class_or_confidence] = color;
43
+ }
44
+ } else {
45
+ mode = "scores";
46
+ }
47
+ }
48
+ }
49
+ }
50
+ _color_map = {};
51
+ correct_color_map(color_map, _color_map, browser, ctx);
52
+ }
53
+
54
+ function handle_mouseover(label: string): void {
55
+ active = label;
56
+ }
57
+ function handle_mouseout(): void {
58
+ active = "";
59
+ }
60
+ </script>
61
+
62
+ <!--
63
+ @todo victor: try reimplementing without flex (negative margins on container to avoid left margin on linebreak).
64
+ If not possible hijack the copy execution like this:
65
+
66
+ <svelte:window
67
+ on:copy|preventDefault={() => {
68
+ const selection =.getSelection()?.toString();
69
+ console.log(selection?.replaceAll("\n", " "));
70
+ }}
71
+ />
72
+ -->
73
+
74
+ <div class="container">
75
+ {#if mode === "categories"}
76
+ {#if show_legend}
77
+ <div
78
+ class="category-legend"
79
+ data-testid="highlighted-text:category-legend"
80
+ >
81
+ {#each Object.entries(_color_map) as [category, color], i}
82
+ <!-- TODO: fix -->
83
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
84
+ <div
85
+ on:mouseover={() => handle_mouseover(category)}
86
+ on:focus={() => handle_mouseover(category)}
87
+ on:mouseout={() => handle_mouseout()}
88
+ on:blur={() => handle_mouseout()}
89
+ class="category-label"
90
+ style={"background-color:" + color.secondary}
91
+ >
92
+ {category}
93
+ </div>
94
+ {/each}
95
+ </div>
96
+ {/if}
97
+ <div class="textfield">
98
+ {#each value as v, i}
99
+ {#each splitTextByNewline(v.token) as line, j}
100
+ {#if line.trim() !== ""}
101
+ <!-- TODO: fix -->
102
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
103
+ <!-- svelte-ignore a11y-click-events-have-key-events-->
104
+ <span
105
+ class="textspan"
106
+ style:background-color={v.class_or_confidence === null ||
107
+ (active && active !== v.class_or_confidence)
108
+ ? ""
109
+ : _color_map[v.class_or_confidence].secondary}
110
+ class:no-cat={v.class_or_confidence === null ||
111
+ (active && active !== v.class_or_confidence)}
112
+ class:hl={v.class_or_confidence !== null}
113
+ class:selectable
114
+ on:click={() => {
115
+ dispatch("select", {
116
+ index: i,
117
+ value: [v.token, v.class_or_confidence]
118
+ });
119
+ }}
120
+ >
121
+ <span
122
+ class:no-label={v.class_or_confidence === null ||
123
+ !_color_map[v.class_or_confidence]}
124
+ class="text">{line}</span
125
+ >
126
+ {#if !show_legend && show_inline_category && v.class_or_confidence !== null}
127
+ &nbsp;
128
+ <span
129
+ class="label"
130
+ style:background-color={v.class_or_confidence === null ||
131
+ (active && active !== v.class_or_confidence)
132
+ ? ""
133
+ : _color_map[v.class_or_confidence].primary}
134
+ >
135
+ {v.class_or_confidence}
136
+ </span>
137
+ {/if}
138
+ </span>
139
+ {/if}
140
+ {#if j < splitTextByNewline(v.token).length - 1}
141
+ <br />
142
+ {/if}
143
+ {/each}
144
+ {/each}
145
+ </div>
146
+ {:else}
147
+ {#if show_legend}
148
+ <div class="color-legend" data-testid="highlighted-text:color-legend">
149
+ <span>-1</span>
150
+ <span>0</span>
151
+ <span>+1</span>
152
+ </div>
153
+ {/if}
154
+ <div class="textfield" data-testid="highlighted-text:textfield">
155
+ {#each value as v}
156
+ {@const score =
157
+ typeof v.class_or_confidence === "string"
158
+ ? parseInt(v.class_or_confidence)
159
+ : v.class_or_confidence}
160
+ <span
161
+ class="textspan score-text"
162
+ style={"background-color: rgba(" +
163
+ (score && score < 0
164
+ ? "128, 90, 213," + -score
165
+ : "239, 68, 60," + score) +
166
+ ")"}
167
+ >
168
+ <span class="text">{v.token}</span>
169
+ </span>
170
+ {/each}
171
+ </div>
172
+ {/if}
173
+ </div>
174
+
175
+ <style>
176
+ .container {
177
+ display: flex;
178
+ flex-direction: column;
179
+ gap: var(--spacing-sm);
180
+ padding: var(--block-padding);
181
+ }
182
+ .hl + .hl {
183
+ margin-left: var(--size-1);
184
+ }
185
+
186
+ .textspan:last-child > .label {
187
+ margin-right: 0;
188
+ }
189
+
190
+ .category-legend {
191
+ display: flex;
192
+ flex-wrap: wrap;
193
+ gap: var(--spacing-sm);
194
+ color: black;
195
+ }
196
+
197
+ .category-label {
198
+ cursor: pointer;
199
+ border-radius: var(--radius-xs);
200
+ padding-right: var(--size-2);
201
+ padding-left: var(--size-2);
202
+ font-weight: var(--weight-semibold);
203
+ }
204
+
205
+ .color-legend {
206
+ display: flex;
207
+ justify-content: space-between;
208
+ border-radius: var(--radius-xs);
209
+ background: linear-gradient(
210
+ to right,
211
+ var(--color-purple),
212
+ rgba(255, 255, 255, 0),
213
+ var(--color-red)
214
+ );
215
+ padding: var(--size-1) var(--size-2);
216
+ font-weight: var(--weight-semibold);
217
+ }
218
+
219
+ .textfield {
220
+ box-sizing: border-box;
221
+ border-radius: var(--radius-xs);
222
+ background: var(--background-fill-primary);
223
+ background-color: transparent;
224
+ max-width: var(--size-full);
225
+ line-height: var(--scale-4);
226
+ word-break: break-all;
227
+ }
228
+
229
+ .textspan {
230
+ transition: 150ms;
231
+ border-radius: var(--radius-xs);
232
+ padding-top: 2.5px;
233
+ padding-right: var(--size-1);
234
+ padding-bottom: 3.5px;
235
+ padding-left: var(--size-1);
236
+ color: black;
237
+ }
238
+
239
+ .label {
240
+ transition: 150ms;
241
+ margin-top: 1px;
242
+ border-radius: var(--radius-xs);
243
+ padding: 1px 5px;
244
+ color: var(--body-text-color);
245
+ color: white;
246
+ font-weight: var(--weight-bold);
247
+ font-size: var(--text-sm);
248
+ text-transform: uppercase;
249
+ }
250
+
251
+ .text {
252
+ color: black;
253
+ white-space: pre-wrap;
254
+ }
255
+
256
+ .score-text .text {
257
+ color: var(--body-text-color);
258
+ }
259
+
260
+ .score-text {
261
+ margin-right: var(--size-1);
262
+ padding: var(--size-1);
263
+ }
264
+
265
+ .no-cat {
266
+ color: var(--body-text-color);
267
+ }
268
+
269
+ .no-label {
270
+ color: var(--body-text-color);
271
+ }
272
+
273
+ .selectable {
274
+ cursor: pointer;
275
+ }
276
+ </style>
6.0.0-dev.6/highlightedtext/shared/utils.ts ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
6.0.0-dev.6/highlightedtext/types.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { SelectData } 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
+ }
17
+
18
+ export interface HighlightedTextEvents {
19
+ change: never;
20
+ select: SelectData;
21
+ clear_status: LoadingStatus;
22
+ }