Skip to content

Commit

Permalink
first progress to annotation suggestion visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
bigabig committed Sep 24, 2024
1 parent 07a6829 commit a2a6dc8
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { AnnotationLLMJobResult } from "../../../../api/openapi/models/Annotatio
import { useAppDispatch, useAppSelector } from "../../../../plugins/ReduxHooks.ts";
import { CRUDDialogActions } from "../../../dialogSlice.ts";
import SdocRenderer from "../../../SourceDocument/SdocRenderer.tsx";
import SpanAnnotationRenderer from "../../../SpanAnnotation/SpanAnnotationRenderer.tsx";
import LLMUtterance from "../LLMUtterance.tsx";
import AnnotationResultStepDocument from "./AnnotationResultStepDocument.tsx";

function AnnotationResultStep() {
// get the job
Expand Down Expand Up @@ -64,9 +64,7 @@ function AnnotationResultStep() {
</Box>
{(llmJob.data.result.specific_llm_job_result as AnnotationLLMJobResult).results.map((annotationResult) => (
<TabPanel key={annotationResult.sdoc_id} value={annotationResult.sdoc_id.toString()} sx={{ px: 0 }}>
{annotationResult.suggested_annotations.map((annotation, idx) => (
<SpanAnnotationRenderer key={idx} spanAnnotation={annotation} />
))}
<AnnotationResultStepDocument annotationResult={annotationResult} />
</TabPanel>
))}
</TabContext>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AnnotationResult } from "../../../../api/openapi/models/AnnotationResult.ts";
import SdocHooks from "../../../../api/SdocHooks.ts";
import TextAnnotatorValidator from "../../../../views/annotation/TextAnnotatorValidator/TextAnnotatorValidator.tsx";

function AnnotationResultStepDocument({ annotationResult }: { annotationResult: AnnotationResult }) {
const sdoc = SdocHooks.useGetDocument(annotationResult.sdoc_id);

if (sdoc.isSuccess) {
return <TextAnnotatorValidator sdoc={sdoc.data} annotations={annotationResult.suggested_annotations} />;
}
return null;
}

export default AnnotationResultStepDocument;
4 changes: 4 additions & 0 deletions frontend/src/components/dialogSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface DialogState {
llmStep: number;
llmTags: DocumentTagRead[];
llmMetadata: ProjectMetadataRead[];
llmCodes: CodeRead[];
llmPrompts: LLMPromptTemplates[];
llmJobId?: string;
llmJobResult: LLMJobResult | null | undefined;
Expand Down Expand Up @@ -86,6 +87,7 @@ const initialState: DialogState = {
llmStep: 0,
llmTags: [],
llmMetadata: [],
llmCodes: [],
llmPrompts: [],
llmJobId: undefined,
llmJobResult: undefined,
Expand Down Expand Up @@ -211,12 +213,14 @@ export const dialogSlice = createSlice({
prompts: LLMPromptTemplates[];
tags: DocumentTagRead[];
metadata: ProjectMetadataRead[];
codes: CodeRead[];
}>,
) => {
state.llmStep = 2;
state.llmPrompts = action.payload.prompts;
state.llmTags = action.payload.tags;
state.llmMetadata = action.payload.metadata;
state.llmCodes = action.payload.codes;
},
updateLLMPrompts: (
state,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { useMemo } from "react";
import { SourceDocumentWithDataRead } from "../../../api/openapi/models/SourceDocumentWithDataRead.ts";
import { SpanAnnotationReadResolved } from "../../../api/openapi/models/SpanAnnotationReadResolved.ts";
import { IToken } from "./IToken.ts";

function useComputeTokenDataWithAnnotations({
sdoc,
annotations,
}: {
sdoc: SourceDocumentWithDataRead;
annotations: SpanAnnotationReadResolved[];
}) {
// computed
// todo: maybe implement with selector?
const tokenData: IToken[] | undefined = useMemo(() => {
const offsets = sdoc.token_character_offsets;
const texts = sdoc.tokens;
const result = texts.map((text, index) => ({
beginChar: offsets[index][0],
endChar: offsets[index][1],
index,
text,
whitespace: offsets.length > index + 1 && offsets[index + 1][0] - offsets[index][1] > 0,
newLine: text.split("\n").length - 1,
}));
return result;
}, [sdoc]);

// todo: maybe implement with selector?
// this map stores annotationId -> SpanAnnotationReadResolved
const annotationMap = useMemo(() => {
const result = new Map<number, SpanAnnotationReadResolved>();
annotations.forEach((a) => result.set(a.id, a));
return result;
}, [annotations]);

// this map stores tokenId -> spanAnnotationId[]
const annotationsPerToken = useMemo(() => {
const result = new Map<number, number[]>();
annotations.forEach((annotation) => {
for (let i = annotation.begin_token; i <= annotation.end_token - 1; i++) {
const tokenAnnotations = result.get(i) || [];
tokenAnnotations.push(annotation.id);
result.set(i, tokenAnnotations);
}
});
return result;
}, [annotations]);

return { tokenData, annotationsPerToken, annotationMap };
}

export default useComputeTokenDataWithAnnotations;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SourceDocumentWithDataRead } from "../../../api/openapi/models/SourceDocumentWithDataRead.ts";
import { SpanAnnotationReadResolved } from "../../../api/openapi/models/SpanAnnotationReadResolved.ts";
import DocumentRenderer from "../DocumentRenderer/DocumentRenderer.tsx";
import useComputeTokenDataWithAnnotations from "../DocumentRenderer/useComputeTokenDataWithAnnotations.ts";

interface TextAnnotatorValidatorProps {
sdoc: SourceDocumentWithDataRead;
annotations: SpanAnnotationReadResolved[];
}

function TextAnnotatorValidator({ sdoc, annotations }: TextAnnotatorValidatorProps) {
// computed / custom hooks
const { tokenData, annotationsPerToken, annotationMap } = useComputeTokenDataWithAnnotations({
sdoc: sdoc,
annotations: annotations,
});

return (
<>
<DocumentRenderer
className="myFlexFillAllContainer"
onMouseUp={() => console.log("HI")}
html={sdoc.html}
tokenData={tokenData}
annotationsPerToken={annotationsPerToken}
annotationMap={annotationMap}
isViewer={false}
projectId={sdoc.project_id}
style={{ zIndex: 1, overflowY: "auto" }}
/>
</>
);
}

export default TextAnnotatorValidator;

0 comments on commit a2a6dc8

Please sign in to comment.