Skip to content

Commit

Permalink
Complete rework of highlights (marks)
Browse files Browse the repository at this point in the history
  • Loading branch information
waleko committed Nov 27, 2023
1 parent 26e341c commit a6aa671
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 200 deletions.
58 changes: 44 additions & 14 deletions web/src/components/InvisibleMark.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,66 @@
import { TokenProbScore, TranslationData } from "@/highlighting/context.ts";
import { TokenProbScore } from "@/highlighting/dataclasses.ts";

import {
HoverCard,
HoverCardContent,
HoverCardTrigger,
} from "@/components/ui/hover-card.tsx";
import SynonymsList from "@/components/SynonymsList.tsx";
import { useEffect, useState } from "react";
import { Controller } from "@/highlighting/Controller.ts";

export class MarkId {
constructor(index: number, pos: number) {
this.index = index;
this.pos = pos;
}

index: number;
pos: number;

toInt() {
const sign = this.index == 0 ? 1 : -1;
return sign * (this.pos + 1);
}

equals(other: MarkId) {
return this.index == other.index && this.pos == other.pos;
}
}

export default function InvisibleMark(
synonyms: TokenProbScore[] | undefined,
translationData: TranslationData,
highlight_other: any | undefined,
translation_index: number,
mark_id: MarkId,
controller: Controller,
) {
return (props: any) => {
let isOpened = false;
let isHovered = false;

// FIXME: still buggy
const [isKilled, setIsKilled] = useState(false);

function updateHighlights() {
console.log("updateHighlights: " + isOpened + " " + isHovered);
if (isOpened || isHovered) {
if (highlight_other) {
translationData.controller.setHighlights[1 - translation_index](
highlight_other,
);
}
controller.activateMark(mark_id);
} else {
translationData.updateDefault();
controller.deactivateMark(mark_id);
}
}

const baseClassName = "my-invisible text-primary";
const [className, setClassName] = useState(baseClassName);

function setAttention(attn_style: string) {
if (attn_style == "killed") {
setIsKilled(true);
}
setClassName(baseClassName + " " + attn_style);
}

useEffect(() => {
controller.registerMark(mark_id, setAttention);
}, []);

return (
<>
<HoverCard
Expand All @@ -41,7 +71,7 @@ export default function InvisibleMark(
>
<HoverCardTrigger asChild>
<mark
className={"my-invisible text-primary"}
className={className}
onMouseOver={() => {
isHovered = true;
updateHighlights();
Expand All @@ -54,7 +84,7 @@ export default function InvisibleMark(
{props.children}
</mark>
</HoverCardTrigger>
{synonyms && (
{synonyms && !isKilled && (
<HoverCardContent className="w-50">
<SynonymsList synonyms={synonyms} />
</HoverCardContent>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/SynonymsList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TokenProbScore } from "@/highlighting/context.ts";
import { TokenProbScore } from "@/highlighting/dataclasses.ts";

export default function SynonymsList({
synonyms,
Expand Down
47 changes: 32 additions & 15 deletions web/src/components/TranslationModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Button } from "@/components/ui/button";
import { useState } from "react";
import TranslationTextarea from "./TranslationTextarea";
import { Controller } from "@/highlighting/Controller.ts";
import { TranslationData } from "@/highlighting/context.ts";
import { TranslationData } from "@/highlighting/dataclasses.ts";
import { Skeleton } from "@/components/ui/skeleton.tsx";
import { GradioTranslation } from "@/service/gradio-translation.ts";

Expand All @@ -18,28 +18,38 @@ export default function TranslationModule() {

const [isTranslated, setIsTranslated] = useState(false);

const controller = new Controller(
[inputSetValue, outputSetValue],
[inputSetHighlight, outputSetHighlight],
const [controller, _] = useState(
new Controller(
[inputSetValue, outputSetValue],
[inputSetHighlight, outputSetHighlight],
),
);

const [isLoading, setIsLoading] = useState(false);
const translationService = new GradioTranslation();

function translate() {
if (inputValue === "" || isLoading || isTranslated) {
if (inputValue === "" || isLoading) {
return;
}
console.log("To be translated: " + inputValue);
setIsLoading(true);
const result = translationService.predict(inputValue);
result.then((tokens) => {
console.log("Translated: " + tokens);

// Translation
try {
const result = translationService.predict(inputValue);
result.then((tokens) => {
console.log("Translated: " + tokens);
setIsLoading(false);
setIsTranslated(true);
const translationData = new TranslationData(tokens);
controller.setTranslationData(translationData);
});
} catch (e) {
console.error(e);
setIsLoading(false);
setIsTranslated(true);
const translationData = new TranslationData(tokens, controller);
translationData.updateDefault();
});
alert("Translation failed. Please try again.");
}
}

function clearInput() {
Expand All @@ -50,6 +60,13 @@ export default function TranslationModule() {
setIsTranslated(false);
}

function handleInput(inputText: string) {
inputSetValue(inputText);
if (isTranslated && inputText !== inputValue) {
controller.sendGlobalKill();
}
}

return (
<>
{/* Content */}
Expand All @@ -60,16 +77,16 @@ export default function TranslationModule() {
<TranslationTextarea
highlight={inputHighlight}
value={inputValue}
onChange={inputSetValue}
onChange={handleInput}
elementId={"textToTranslate"}
placeholder={"Enter text here..."}
readOnly={isTranslated || isLoading}
readOnly={isLoading}
/>
<div className="mt-4">
<Button
className="primary"
onClick={translate}
disabled={isLoading || isTranslated}
disabled={isLoading}
>
Translate
</Button>{" "}
Expand Down
20 changes: 10 additions & 10 deletions web/src/components/highlights.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
/* attention highlights gradations */

.attn5 {
border-radius: 3px;
background-color: #98b7f8e0;
background-color: #98b7f8e0 !important;
}

.attn4 {
border-radius: 3px;
background-color: #98b7f8c0;
background-color: #98b7f8c0 !important;
}

.attn3 {
border-radius: 3px;
background-color: #98b7f8a0;
background-color: #98b7f8a0 !important;
}

.attn2 {
border-radius: 3px;
background-color: #98b7f880;
background-color: #98b7f880 !important;
}

.attn1 {
border-radius: 3px;
background-color: #98b7f860;
background-color: #98b7f860 !important;
}

mark.my-invisible {
Expand All @@ -34,3 +29,8 @@ mark.my-invisible {
mark.my-invisible:hover {
background-color: #98b7f8e0;
}

mark.killed:hover {
background-color: transparent !important;
cursor: text !important;
}
Loading

0 comments on commit a6aa671

Please sign in to comment.