-
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.
* 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]>
- Loading branch information
Showing
27 changed files
with
310 additions
and
122 deletions.
There are no files selected for viewing
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
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); | ||
} |
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.