From b2ba06d025a6de85049e6470f1533f6f59b4e9e8 Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Fri, 29 Sep 2023 18:49:42 +0200 Subject: [PATCH] Typings for all kinds of widget examples (#979) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Typings for all kinds of widget examples * Add a //#region block * Tweaks * Fix some part of the CI already(?) cc @krampstudio @mishig25 for review :pray: * Revert "Fix some part of the CI already(?)" This reverts commit 3e89028d34db5c3f3a40a92f79f3e73141a64ffb. * nvm, probably better like this * sorry this was wrong * 🤯 Use generics in Svelte (i guess...) * Proposal: widget types (#980) * 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 * 💄 Lint & checks * Go for same syntax everywhere --------- Co-authored-by: Simon Brandeis <33657802+SBrandeis@users.noreply.github.com> Co-authored-by: SBrandeis --- .../InferenceWidget/shared/WidgetExample.ts | 89 +++++++++++++++++++ .../WidgetInputSamples.svelte | 10 ++- .../shared/WidgetWrapper/WidgetWrapper.svelte | 19 ++-- .../InferenceWidget/shared/inputValidation.ts | 87 ++++++++++++++++++ .../shared/outputValidation.ts | 22 +++++ .../AudioClassificationWidget.svelte | 33 ++++--- .../AudioToAudioWidget.svelte | 13 +-- .../AutomaticSpeechRecognitionWidget.svelte | 29 ++++-- .../ConversationalWidget.svelte | 7 +- .../FeatureExtractionWidget.svelte | 11 ++- .../FillMaskWidget/FillMaskWidget.svelte | 18 +++- .../ImageClassificationWidget.svelte | 27 ++++-- .../ImageSegmentationWidget.svelte | 7 +- .../ImageToImageWidget.svelte | 7 +- .../ImageToTextWidget.svelte | 7 +- .../ObjectDetectionWidget.svelte | 7 +- .../QuestionAnsweringWidget.svelte | 22 ++++- .../SentenceSimilarityWidget.svelte | 7 +- .../SummarizationWidget.svelte | 7 +- .../TableQuestionAnsweringWidget.svelte | 7 +- .../TabularDataWidget.svelte | 15 ++-- .../TextGenerationWidget.svelte | 20 ++++- .../TextToImageWidget.svelte | 17 +++- .../TextToSpeechWidget.svelte | 7 +- .../TokenClassificationWidget.svelte | 7 +- .../VisualQuestionAnsweringWidget.svelte | 7 +- .../ZeroShotImageClassificationWidget.svelte | 7 +- .../ZeroShotClassificationWidget.svelte | 7 +- js/src/lib/interfaces/DefaultWidget.ts | 3 +- js/src/lib/interfaces/Types.ts | 14 +-- js/src/routes/index.svelte | 89 +++++++++++++++++-- 31 files changed, 521 insertions(+), 108 deletions(-) create mode 100644 js/src/lib/components/InferenceWidget/shared/WidgetExample.ts create mode 100644 js/src/lib/components/InferenceWidget/shared/inputValidation.ts create mode 100644 js/src/lib/components/InferenceWidget/shared/outputValidation.ts diff --git a/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts b/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts new file mode 100644 index 000000000..4b43c9ead --- /dev/null +++ b/js/src/lib/components/InferenceWidget/shared/WidgetExample.ts @@ -0,0 +1,89 @@ +type TableData = Record; + +//#region outputs +export type WidgetExampleOutputLabels = Array<{ label: string; score: number; }>; +export interface WidgetExampleOutputAnswerScore { + answer: string; + score: number; +} +export interface WidgetExampleOutputText { + text: string; +} +export interface WidgetExampleOutputUrl { + url: string; +} + +export type WidgetExampleOutput = + | WidgetExampleOutputLabels + | WidgetExampleOutputAnswerScore + | WidgetExampleOutputText + | WidgetExampleOutputUrl; +//#endregion + +export interface WidgetExampleBase { + example_title?: string; + group?: string; + output?: TOutput; +} + +export interface WidgetExampleTextInput extends WidgetExampleBase { + text: string; +} + +export interface WidgetExampleTextAndContextInput + extends WidgetExampleTextInput { + context: string; +} + +export interface WidgetExampleTextAndTableInput extends WidgetExampleTextInput { + table: (string | number)[][]; +} + +export interface WidgetExampleAssetInput extends WidgetExampleBase { + src: string; +} +export interface WidgetExampleAssetAndPromptInput + extends WidgetExampleAssetInput { + prompt: string; +} + +export type WidgetExampleAssetAndTextInput = WidgetExampleAssetInput & + WidgetExampleTextInput; + +export type WidgetExampleAssetAndZeroShotInput = WidgetExampleAssetInput & + WidgetExampleZeroShotTextInput; + +export interface WidgetExampleStructuredDataInput extends WidgetExampleBase { + structuredData: TableData; +} + +export interface WidgetExampleTableDataInput extends WidgetExampleBase { + table: TableData; +} + +export interface WidgetExampleZeroShotTextInput extends WidgetExampleTextInput { + text: string; + candidate_labels: string; + multi_class: boolean; +} + +export interface WidgetExampleSentenceSimilarityInput + extends WidgetExampleBase { + source_sentence: string; + sentences: string[]; +} + +//#endregion + +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 0a5d4e750..26e962e15 100644 --- a/js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte +++ b/js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte @@ -1,5 +1,7 @@
diff --git a/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte b/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte index 91ac9c7f2..eb6ee69c6 100644 --- a/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte +++ b/js/src/lib/components/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte @@ -1,5 +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 @@