Skip to content

Commit

Permalink
Allow dicts to be displayed in AnnotationsDisplay without a enclosing…
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanmai authored and xuwangyin committed Jun 23, 2024
1 parent 808ae62 commit e6a69d6
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions helm-frontend/src/components/AnnotationsDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,56 @@ import CompletionAnnotation from "@/types/CompletionAnnotation";
import Preview from "./Preview";
import MediaObjectDisplay from "./MediaObjectDisplay";

// TODO: This is a dirty hack to support annotations from
// Image2Structure and AIRBench, but eventually we should make sure
// all annotations are supported generally.
type Props = {
predictionAnnotations:
| Record<string, Array<CompletionAnnotation>>
| Record<string, Array<CompletionAnnotation> | Map<string, string | number>>
| undefined;
};

function listAnnotationDisplay(listAnnotation: Array<CompletionAnnotation>) {
return (
<div>
{listAnnotation.map((annotation, idx) => (
<div key={idx}>
{annotation.error && (
<div>
<h3 className="ml-1">Error</h3>
<Preview value={annotation["error"]} />{" "}
</div>
)}
{annotation.text && (
<div>
<h3 className="ml-1">Text</h3>
<Preview value={annotation["text"]} />{" "}
</div>
)}
{annotation.media_object && (
<MediaObjectDisplay mediaObject={annotation["media_object"]} />
)}
</div>
))}
</div>
);
}

function dictAnnotationDisplay(
dictAnnotation: Record<string, string | number>,
) {
return (
<div>
{Object.entries(dictAnnotation).map(([key, value]) => (
<div>
<h3 className="ml-1">{key}</h3>
<Preview value={value.toString()} />
</div>
))}
</div>
);
}

export default function AnnotationDisplay({ predictionAnnotations }: Props) {
return (
<div>
Expand All @@ -17,27 +61,9 @@ export default function AnnotationDisplay({ predictionAnnotations }: Props) {
<h3>
<strong>{key}</strong>
</h3>
{value.map((annotation, idx) => (
<div key={idx}>
{annotation.error && (
<div>
<h3 className="ml-1">Error</h3>
<Preview value={annotation["error"]} />{" "}
</div>
)}
{annotation.text && (
<div>
<h3 className="ml-1">Text</h3>
<Preview value={annotation["text"]} />{" "}
</div>
)}
{annotation.media_object && (
<MediaObjectDisplay
mediaObject={annotation["media_object"]}
/>
)}
</div>
))}
{Array.isArray(value)
? listAnnotationDisplay(value)
: dictAnnotationDisplay(value)}
</div>
))
: null}
Expand Down

0 comments on commit e6a69d6

Please sign in to comment.