-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typings for all kinds of widget examples (#979)
* Typings for all kinds of widget examples * Add a //#region block * Tweaks * Fix some part of the CI already(?) cc @krampstudio @mishig25 for review 🙏 * Revert "Fix some part of the CI already(?)" This reverts commit 3e89028. * 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 <[email protected]> * 🧹 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 <[email protected]> * 💄 Lint & checks * Go for same syntax everywhere --------- Co-authored-by: Simon Brandeis <[email protected]> Co-authored-by: SBrandeis <[email protected]>
- Loading branch information
1 parent
bf08e8d
commit b2ba06d
Showing
31 changed files
with
521 additions
and
108 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
js/src/lib/components/InferenceWidget/shared/WidgetExample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
type TableData = Record<string, (string | number)[]>; | ||
|
||
//#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<TOutput> { | ||
example_title?: string; | ||
group?: string; | ||
output?: TOutput; | ||
} | ||
|
||
export interface WidgetExampleTextInput<TOutput = WidgetExampleOutput> extends WidgetExampleBase<TOutput> { | ||
text: string; | ||
} | ||
|
||
export interface WidgetExampleTextAndContextInput<TOutput = WidgetExampleOutput> | ||
extends WidgetExampleTextInput<TOutput> { | ||
context: string; | ||
} | ||
|
||
export interface WidgetExampleTextAndTableInput<TOutput = WidgetExampleOutput> extends WidgetExampleTextInput<TOutput> { | ||
table: (string | number)[][]; | ||
} | ||
|
||
export interface WidgetExampleAssetInput<TOutput = WidgetExampleOutput> extends WidgetExampleBase<TOutput> { | ||
src: string; | ||
} | ||
export interface WidgetExampleAssetAndPromptInput<TOutput = WidgetExampleOutput> | ||
extends WidgetExampleAssetInput<TOutput> { | ||
prompt: string; | ||
} | ||
|
||
export type WidgetExampleAssetAndTextInput<TOutput = WidgetExampleOutput> = WidgetExampleAssetInput<TOutput> & | ||
WidgetExampleTextInput<TOutput>; | ||
|
||
export type WidgetExampleAssetAndZeroShotInput<TOutput = WidgetExampleOutput> = WidgetExampleAssetInput<TOutput> & | ||
WidgetExampleZeroShotTextInput<TOutput>; | ||
|
||
export interface WidgetExampleStructuredDataInput<TOutput = WidgetExampleOutput> extends WidgetExampleBase<TOutput> { | ||
structuredData: TableData; | ||
} | ||
|
||
export interface WidgetExampleTableDataInput<TOutput = WidgetExampleOutput> extends WidgetExampleBase<TOutput> { | ||
table: TableData; | ||
} | ||
|
||
export interface WidgetExampleZeroShotTextInput<TOutput = WidgetExampleOutput> extends WidgetExampleTextInput<TOutput> { | ||
text: string; | ||
candidate_labels: string; | ||
multi_class: boolean; | ||
} | ||
|
||
export interface WidgetExampleSentenceSimilarityInput<TOutput = WidgetExampleOutput> | ||
extends WidgetExampleBase<TOutput> { | ||
source_sentence: string; | ||
sentences: string[]; | ||
} | ||
|
||
//#endregion | ||
|
||
export type WidgetExample<TOutput = WidgetExampleOutput> = | ||
| WidgetExampleTextInput<TOutput> | ||
| WidgetExampleTextAndContextInput<TOutput> | ||
| WidgetExampleTextAndTableInput<TOutput> | ||
| WidgetExampleAssetInput<TOutput> | ||
| WidgetExampleAssetAndPromptInput<TOutput> | ||
| WidgetExampleAssetAndTextInput<TOutput> | ||
| WidgetExampleAssetAndZeroShotInput<TOutput> | ||
| WidgetExampleStructuredDataInput<TOutput> | ||
| WidgetExampleTableDataInput<TOutput> | ||
| WidgetExampleZeroShotTextInput<TOutput> | ||
| WidgetExampleSentenceSimilarityInput<TOutput>; |
10 changes: 6 additions & 4 deletions
10
js/src/lib/components/InferenceWidget/shared/WidgetInputSamples/WidgetInputSamples.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
js/src/lib/components/InferenceWidget/shared/inputValidation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { | ||
WidgetExample, | ||
WidgetExampleAssetAndPromptInput, | ||
WidgetExampleAssetAndTextInput, | ||
WidgetExampleAssetAndZeroShotInput, | ||
WidgetExampleAssetInput, | ||
WidgetExampleSentenceSimilarityInput, | ||
WidgetExampleStructuredDataInput, | ||
WidgetExampleTableDataInput, | ||
WidgetExampleTextAndContextInput, | ||
WidgetExampleTextAndTableInput, | ||
WidgetExampleTextInput, | ||
WidgetExampleZeroShotTextInput, | ||
} from "./WidgetExample"; | ||
|
||
export function isTextInput<TOutput>(sample: WidgetExample<TOutput>): sample is WidgetExampleTextInput<TOutput> { | ||
return "text" in sample; | ||
} | ||
|
||
export function isTextAndContextInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleTextAndContextInput<TOutput> { | ||
return isTextInput(sample) && "context" in sample; | ||
} | ||
|
||
export function isAssetInput<TOutput>(sample: WidgetExample<TOutput>): sample is WidgetExampleAssetInput<TOutput> { | ||
return "src" in sample; | ||
} | ||
|
||
export function isAssetAndPromptInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleAssetAndPromptInput<TOutput> { | ||
return isAssetInput(sample) && "prompt" in sample && typeof sample.prompt === "string"; | ||
} | ||
|
||
export function isAssetAndTextInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleAssetAndTextInput<TOutput> { | ||
return isAssetInput(sample) && isTextInput(sample); | ||
} | ||
|
||
export function isStructuredDataInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleStructuredDataInput<TOutput> { | ||
return "structuredData" in sample; | ||
} | ||
|
||
export function isTableDataInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleTableDataInput<TOutput> { | ||
return "table" in sample; | ||
} | ||
|
||
function _isZeroShotTextInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is Exclude<WidgetExampleZeroShotTextInput<TOutput>, "text"> { | ||
return "candidate_labels" in sample && "multi_class" in sample; | ||
} | ||
|
||
export function isZeroShotTextInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleZeroShotTextInput<TOutput> { | ||
return isTextInput(sample) && _isZeroShotTextInput(sample); | ||
} | ||
|
||
export function isSentenceSimilarityInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleSentenceSimilarityInput<TOutput> { | ||
return "source_sentence" in sample && "sentences" in sample; | ||
} | ||
|
||
export function isTextAndTableInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleTextAndTableInput<TOutput> { | ||
return ( | ||
isTextInput(sample) | ||
&& "table" in sample | ||
&& Array.isArray(sample.table) | ||
&& sample.table.every(r => Array.isArray(r) && r.every(c => typeof c === "string" || typeof c === "number")) | ||
); | ||
} | ||
|
||
export function isAssetAndZeroShotInput<TOutput>( | ||
sample: WidgetExample<TOutput> | ||
): sample is WidgetExampleAssetAndZeroShotInput<TOutput> { | ||
return isAssetInput(sample) && _isZeroShotTextInput(sample); | ||
} |
22 changes: 22 additions & 0 deletions
22
js/src/lib/components/InferenceWidget/shared/outputValidation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { | ||
WidgetExampleOutputLabels, | ||
WidgetExampleOutputAnswerScore, | ||
WidgetExampleOutputText, | ||
WidgetExampleOutputUrl, | ||
} from "./WidgetExample"; | ||
|
||
export function isValidOutputLabels(arg: unknown): arg is WidgetExampleOutputLabels { | ||
return Array.isArray(arg) && arg.every(x => typeof x.label === "string" && typeof x.score === "number"); | ||
} | ||
|
||
export function isValidOutputAnswerScore(arg: unknown): arg is WidgetExampleOutputAnswerScore { | ||
return !!arg && typeof arg === "object" && typeof arg["answer"] === "string" && typeof arg["score"] === "number"; | ||
} | ||
|
||
export function isValidOutputText(arg: unknown): arg is WidgetExampleOutputText { | ||
return !!arg && typeof arg === "object" && typeof arg["text"] === "string"; | ||
} | ||
|
||
export function isValidOutputUrl(arg: unknown): arg is WidgetExampleOutputUrl { | ||
return !!arg && typeof arg === "object" && typeof arg["url"] === "string" && arg["url"].startsWith("https://"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.