From 5b91fc516930f686e510d60c17ac62794a1c0697 Mon Sep 17 00:00:00 2001 From: Simon Brandeis <33657802+SBrandeis@users.noreply.github.com> Date: Fri, 29 Sep 2023 18:06:44 +0200 Subject: [PATCH] Proposal: widget types (#980) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * 🩹 Fix types in widgets * 🩹 Minimize diff * 💄 Lint * Update js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte Co-authored-by: Julien Chaumond * 🧹 cleanup unwanted change * 🩹 wip: type safety * ✨ Typing & validation for ALL widgets * ♻️ Slightly less verbose version * 🩹 * 🩹 Import type? * 🩹 Alternative generic syntax to satisfy svelte-check --------- Co-authored-by: Julien Chaumond --- .../InferenceWidget/shared/WidgetExample.ts | 89 +++++++++---------- .../WidgetInputSamples.svelte | 14 +-- .../shared/WidgetWrapper/WidgetWrapper.svelte | 17 ++-- .../InferenceWidget/shared/inputValidation.ts | 87 ++++++++++++++++++ .../AudioClassificationWidget.svelte | 14 ++- .../AudioToAudioWidget.svelte | 13 +-- .../AutomaticSpeechRecognitionWidget.svelte | 12 ++- .../ConversationalWidget.svelte | 7 +- .../FeatureExtractionWidget.svelte | 11 ++- .../FillMaskWidget/FillMaskWidget.svelte | 14 ++- .../ImageClassificationWidget.svelte | 12 ++- .../ImageSegmentationWidget.svelte | 7 +- .../ImageToImageWidget.svelte | 7 +- .../ImageToTextWidget.svelte | 7 +- .../ObjectDetectionWidget.svelte | 7 +- .../QuestionAnsweringWidget.svelte | 17 +++- .../SentenceSimilarityWidget.svelte | 7 +- .../SummarizationWidget.svelte | 7 +- .../TableQuestionAnsweringWidget.svelte | 7 +- .../TabularDataWidget.svelte | 13 ++- .../TextGenerationWidget.svelte | 14 ++- .../TextToImageWidget.svelte | 14 ++- .../TextToSpeechWidget.svelte | 7 +- .../TokenClassificationWidget.svelte | 7 +- .../VisualQuestionAnsweringWidget.svelte | 7 +- .../ZeroShotImageClassificationWidget.svelte | 7 +- .../ZeroShotClassificationWidget.svelte | 7 +- 27 files changed, 310 insertions(+), 122 deletions(-) create mode 100644 js/src/lib/components/InferenceWidget/shared/inputValidation.ts diff --git a/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts b/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts index d642afd26..4b43c9ead 100644 --- a/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts +++ b/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts @@ -12,83 +12,78 @@ export interface WidgetExampleOutputText { export interface WidgetExampleOutputUrl { url: string; } + +export type WidgetExampleOutput = + | WidgetExampleOutputLabels + | WidgetExampleOutputAnswerScore + | WidgetExampleOutputText + | WidgetExampleOutputUrl; //#endregion -export interface WidgetExampleBase { +export interface WidgetExampleBase { example_title?: string; group?: string; + output?: TOutput; } -export interface WidgetExampleTextInputLabelsOutput extends WidgetExampleBase { - text: string; - output?: WidgetExampleOutputLabels; +export interface WidgetExampleTextInput extends WidgetExampleBase { + text: string; } -export interface WidgetExampleTextAndContextInputAnswerScoreOutput extends WidgetExampleBase { - text: string; +export interface WidgetExampleTextAndContextInput + extends WidgetExampleTextInput { context: string; - output?: WidgetExampleOutputAnswerScore; } -export interface WidgetExampleTextInputTextOutput extends WidgetExampleBase { - text: string; - output?: WidgetExampleOutputText; +export interface WidgetExampleTextAndTableInput extends WidgetExampleTextInput { + table: (string | number)[][]; } -export interface WidgetExampleTextInputUrlOutput extends WidgetExampleBase { - text: string; - output?: WidgetExampleOutputUrl; +export interface WidgetExampleAssetInput extends WidgetExampleBase { + src: string; } - -export interface WidgetExampleAssetInputLabelsOutput extends WidgetExampleBase { - src: string; - output?: WidgetExampleOutputLabels; +export interface WidgetExampleAssetAndPromptInput + extends WidgetExampleAssetInput { + prompt: string; } -export interface WidgetExampleAssetInputTextOutput extends WidgetExampleBase { - src: string; - output?: WidgetExampleOutputText; -} +export type WidgetExampleAssetAndTextInput = WidgetExampleAssetInput & + WidgetExampleTextInput; -export interface WidgetExampleAssetInputUrlOutput extends WidgetExampleBase { - src: string; - output?: WidgetExampleOutputUrl; -} +export type WidgetExampleAssetAndZeroShotInput = WidgetExampleAssetInput & + WidgetExampleZeroShotTextInput; -//#region more exotic stuff -export interface WidgetExampleStructuredDataInputLabelsOutput extends WidgetExampleBase { +export interface WidgetExampleStructuredDataInput extends WidgetExampleBase { structuredData: TableData; - output?: WidgetExampleOutputLabels; } -export interface WidgetExampleTableDataInputLabelsOutput extends WidgetExampleBase { - table: TableData; - output?: WidgetExampleOutputLabels; +export interface WidgetExampleTableDataInput extends WidgetExampleBase { + table: TableData; } -export interface WidgetExampleZeroShotTextInputLabelsOutput extends WidgetExampleBase { +export interface WidgetExampleZeroShotTextInput extends WidgetExampleTextInput { text: string; candidate_labels: string; multi_class: boolean; - output?: WidgetExampleOutputLabels; } -export interface WidgetExampleSentenceSimilarityInputLabelsOutput extends WidgetExampleBase { +export interface WidgetExampleSentenceSimilarityInput + extends WidgetExampleBase { source_sentence: string; sentences: string[]; - output?: WidgetExampleOutputLabels; } + //#endregion -export type WidgetExample = - | WidgetExampleTextInputLabelsOutput - | WidgetExampleTextAndContextInputAnswerScoreOutput - | WidgetExampleTextInputTextOutput - | WidgetExampleTextInputUrlOutput - | WidgetExampleAssetInputLabelsOutput - | WidgetExampleAssetInputTextOutput - | WidgetExampleAssetInputUrlOutput - | WidgetExampleStructuredDataInputLabelsOutput - | WidgetExampleTableDataInputLabelsOutput - | WidgetExampleZeroShotTextInputLabelsOutput - | WidgetExampleSentenceSimilarityInputLabelsOutput; +export type WidgetExample = + | WidgetExampleTextInput + | WidgetExampleTextAndContextInput + | WidgetExampleTextAndTableInput + | WidgetExampleAssetInput + | WidgetExampleAssetAndPromptInput + | WidgetExampleAssetAndTextInput + | WidgetExampleAssetAndZeroShotInput + | WidgetExampleStructuredDataInput + | WidgetExampleTableDataInput + | WidgetExampleZeroShotTextInput + | WidgetExampleSentenceSimilarityInput; diff --git a/js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte b/js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte index 59985626f..cc41078e1 100644 --- a/js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte +++ b/js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte @@ -1,15 +1,17 @@ -
diff --git a/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte b/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte index 57c82edee..eb6ee69c6 100644 --- a/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte +++ b/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte @@ -1,6 +1,6 @@ diff --git a/js/src/lib/components/InferenceWidget/widgets/SentenceSimilarityWidget/SentenceSimilarityWidget.svelte b/js/src/lib/components/InferenceWidget/widgets/SentenceSimilarityWidget/SentenceSimilarityWidget.svelte index 7d3f94884..a08755c22 100644 --- a/js/src/lib/components/InferenceWidget/widgets/SentenceSimilarityWidget/SentenceSimilarityWidget.svelte +++ b/js/src/lib/components/InferenceWidget/widgets/SentenceSimilarityWidget/SentenceSimilarityWidget.svelte @@ -1,5 +1,6 @@ diff --git a/js/src/lib/components/InferenceWidget/widgets/TextToSpeechWidget/TextToSpeechWidget.svelte b/js/src/lib/components/InferenceWidget/widgets/TextToSpeechWidget/TextToSpeechWidget.svelte index f266a0c43..870e5bb07 100644 --- a/js/src/lib/components/InferenceWidget/widgets/TextToSpeechWidget/TextToSpeechWidget.svelte +++ b/js/src/lib/components/InferenceWidget/widgets/TextToSpeechWidget/TextToSpeechWidget.svelte @@ -1,5 +1,6 @@