Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dicts to be displayed in AnnotationsDisplay without a enclosing list #2700

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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