Skip to content

Commit

Permalink
run onMount widget example inside WidgetWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Oct 27, 2023
1 parent 44eb4f1 commit ada2b70
Show file tree
Hide file tree
Showing 24 changed files with 52 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import WidgetHeader from "../WidgetHeader/WidgetHeader.svelte";
import WidgetInfo from "../WidgetInfo/WidgetInfo.svelte";
import WidgetModelLoading from "../WidgetModelLoading/WidgetModelLoading.svelte";
import { getModelLoadInfo } from "../../shared/helpers";
import { getModelLoadInfo, getWidgetExample } from "../../shared/helpers";
import { modelLoadStates } from "../../stores";
export let apiUrl: string;
export let callApiOnMount: WidgetProps["callApiOnMount"];
export let computeTime: string;
export let error: string;
export let isLoading = false;
Expand All @@ -30,6 +31,7 @@
export let outputJson: string;
export let applyInputSample: (sample: TWidgetExample, opts?: ExampleRunOpts) => void = () => {};
export let validateExample: (sample: WidgetExample) => sample is TWidgetExample;
export let runExampleOnMount = true;
let isMaximized = false;
let modelLoadInfo: ModelLoadInfo | undefined = undefined;
Expand Down Expand Up @@ -63,6 +65,15 @@
(async () => {
modelLoadInfo = await getModelLoadInfo(apiUrl, model.id, includeCredentials);
$modelLoadStates[model.id] = modelLoadInfo;
// run widget example
if (!runExampleOnMount) {
return;
}
const example = getWidgetExample<TWidgetExample>(model, validateExample);
if (callApiOnMount && example) {
applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
})();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
import type { WidgetProps, ExampleRunOpts, InferenceRunFlags } from "../../shared/types";
import type { WidgetExample, WidgetExampleAssetInput, WidgetExampleOutputLabels } from "../../shared/WidgetExample";
import { onMount } from "svelte";
import WidgetAudioTrack from "../../shared/WidgetAudioTrack/WidgetAudioTrack.svelte";
import WidgetFileInput from "../../shared/WidgetFileInput/WidgetFileInput.svelte";
import WidgetOutputChart from "../../shared/WidgetOutputChart/WidgetOutputChart.svelte";
import WidgetRecorder from "../../shared/WidgetRecorder/WidgetRecorder.svelte";
import WidgetSubmitBtn from "../../shared/WidgetSubmitBtn/WidgetSubmitBtn.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { callInferenceApi, getBlobFromUrl, getWidgetExample } from "../../shared/helpers";
import { callInferenceApi, getBlobFromUrl } from "../../shared/helpers";
import { isValidOutputLabels } from "../../shared/outputValidation";
import { isAssetInput } from "../../shared/inputValidation";
Expand Down Expand Up @@ -144,16 +142,10 @@
function validateExample(sample: WidgetExample): sample is WidgetExampleAssetInput<WidgetExampleOutputLabels> {
return isAssetInput(sample) && (!sample.output || isValidOutputLabels(sample.output));
}
onMount(() => {
const example = getWidgetExample<WidgetExampleAssetInput<WidgetExampleOutputLabels>>(model, validateExample);
if (callApiOnMount && example) {
applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
import type { WidgetProps, ExampleRunOpts, InferenceRunFlags } from "../../shared/types";
import type { WidgetExample, WidgetExampleAssetInput, WidgetExampleOutputText } from "../../shared/WidgetExample";
import { onMount } from "svelte";
import WidgetAudioTrack from "../../shared/WidgetAudioTrack/WidgetAudioTrack.svelte";
import WidgetFileInput from "../../shared/WidgetFileInput/WidgetFileInput.svelte";
import WidgetOutputText from "../../shared/WidgetOutputText/WidgetOutputText.svelte";
import WidgetRecorder from "../../shared/WidgetRecorder/WidgetRecorder.svelte";
import WidgetRealtimeRecorder from "../../shared/WidgetRealtimeRecorder/WidgetRealtimeRecorder.svelte";
import WidgetSubmitBtn from "../../shared/WidgetSubmitBtn/WidgetSubmitBtn.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { callInferenceApi, getBlobFromUrl, getWidgetExample } from "../../shared/helpers";
import { callInferenceApi, getBlobFromUrl } from "../../shared/helpers";
import { isValidOutputText } from "../../shared/outputValidation";
import { isAssetInput } from "../../shared/inputValidation";
Expand Down Expand Up @@ -149,16 +147,10 @@
function validateExample(sample: WidgetExample): sample is WidgetExampleAssetInput<WidgetExampleOutputText> {
return isAssetInput(sample) && (!sample.output || isValidOutputText(sample.output));
}
onMount(() => {
const example = getWidgetExample<WidgetExampleAssetInput<WidgetExampleOutputText>>(model, validateExample);
if (callApiOnMount && example) {
applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand All @@ -148,6 +149,7 @@
{noTitle}
{outputJson}
validateExample={isTextInput}
runExampleOnMount={false}
>
<svelte:fragment slot="top">
<form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
getOutput();
} else {
const example = getWidgetExample<WidgetExampleTextInput<WidgetExampleOutputLabels>>(model, validateExample);
/// TODO(get rid of useless getWidgetExample)
setTextAreaValue(example?.text ?? "");
if (example && callApiOnMount) {
applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
Expand Down Expand Up @@ -144,6 +142,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand All @@ -155,6 +154,7 @@
{noTitle}
{outputJson}
{validateExample}
runExampleOnMount={false}
>
<svelte:fragment slot="top">
<form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import type { WidgetProps, InferenceRunFlags, ExampleRunOpts } from "../../shared/types";
import type { WidgetExample, WidgetExampleAssetInput, WidgetExampleOutputLabels } from "../../shared/WidgetExample";
import { onMount } from "svelte";
import WidgetFileInput from "../../shared/WidgetFileInput/WidgetFileInput.svelte";
import WidgetDropzone from "../../shared/WidgetDropzone/WidgetDropzone.svelte";
import WidgetOutputChart from "../../shared/WidgetOutputChart/WidgetOutputChart.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { callInferenceApi, getBlobFromUrl, getWidgetExample } from "../../shared/helpers";
import { callInferenceApi, getBlobFromUrl } from "../../shared/helpers";
import { isValidOutputLabels } from "../../shared/outputValidation";
import { isTextInput } from "../../shared/inputValidation";
Expand Down Expand Up @@ -116,18 +114,10 @@
function validateExample(sample: WidgetExample): sample is WidgetExampleAssetInput<WidgetExampleOutputLabels> {
return isTextInput(sample) && (!sample.output || isValidOutputLabels(sample.output));
}
onMount(() => {
(async () => {
const example = getWidgetExample<WidgetExampleAssetInput<WidgetExampleOutputLabels>>(model, validateExample);
if (example && callApiOnMount) {
await applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
})();
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { COLORS } from "../../shared/consts";
import { clamp, mod, hexToRgb } from "../../../../utils/ViewUtils";
import { callInferenceApi, getBlobFromUrl, getWidgetExample } from "../../shared/helpers";
import { callInferenceApi, getBlobFromUrl } from "../../shared/helpers";
import WidgetFileInput from "../../shared/WidgetFileInput/WidgetFileInput.svelte";
import WidgetDropzone from "../../shared/WidgetDropzone/WidgetDropzone.svelte";
import WidgetOutputChart from "../../shared/WidgetOutputChart/WidgetOutputChart.svelte";
Expand Down Expand Up @@ -224,17 +224,11 @@
if (typeof createImageBitmap === "undefined") {
polyfillCreateImageBitmap();
}
(async () => {
const example = getWidgetExample<WidgetExampleAssetInput>(model, isAssetInput);
if (callApiOnMount && example) {
await applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
})();
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import type { WidgetProps, ExampleRunOpts, InferenceRunFlags } from "../../shared/types";
import type { WidgetExampleAssetAndPromptInput } from "../../shared/WidgetExample";
import { onMount } from "svelte";
import WidgetFileInput from "../../shared/WidgetFileInput/WidgetFileInput.svelte";
import WidgetDropzone from "../../shared/WidgetDropzone/WidgetDropzone.svelte";
import WidgetTextInput from "../../shared/WidgetTextInput/WidgetTextInput.svelte";
import WidgetSubmitBtn from "../../shared/WidgetSubmitBtn/WidgetSubmitBtn.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { addInferenceParameters, getWidgetExample, callInferenceApi } from "../../shared/helpers";
import { addInferenceParameters, callInferenceApi } from "../../shared/helpers";
import { isAssetAndPromptInput } from "../../shared/inputValidation";
export let apiToken: WidgetProps["apiToken"];
Expand Down Expand Up @@ -128,18 +126,10 @@
error = res.error;
}
}
onMount(() => {
(async () => {
const example = getWidgetExample<WidgetExampleAssetAndPromptInput>(model, isAssetAndPromptInput);
if (callApiOnMount && example) {
await applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
})();
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import type { WidgetProps, ExampleRunOpts, InferenceRunFlags } from "../../shared/types";
import type { WidgetExampleAssetInput } from "../../shared/WidgetExample";
import { onMount } from "svelte";
import WidgetFileInput from "../../shared/WidgetFileInput/WidgetFileInput.svelte";
import WidgetDropzone from "../../shared/WidgetDropzone/WidgetDropzone.svelte";
import WidgetOutputText from "../../shared/WidgetOutputText/WidgetOutputText.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { callInferenceApi, getBlobFromUrl, getWidgetExample } from "../../shared/helpers";
import { callInferenceApi, getBlobFromUrl } from "../../shared/helpers";
import { isAssetInput } from "../../shared/inputValidation";
export let apiToken: WidgetProps["apiToken"];
Expand Down Expand Up @@ -104,18 +102,10 @@
const blob = await getBlobFromUrl(imgSrc);
getOutput(blob);
}
onMount(() => {
(async () => {
const example = getWidgetExample<WidgetExampleAssetInput>(model, isAssetInput);
if (callApiOnMount && example) {
await applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
})();
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import type { WidgetProps, ExampleRunOpts, InferenceRunFlags } from "../../shared/types";
import type { WidgetExampleSentenceSimilarityInput } from "../../shared/WidgetExample";
import { onMount } from "svelte";
import WidgetOutputChart from "../../shared/WidgetOutputChart/WidgetOutputChart.svelte";
import WidgetSubmitBtn from "../../shared/WidgetSubmitBtn/WidgetSubmitBtn.svelte";
import WidgetAddSentenceBtn from "../../shared/WidgetAddSentenceBtn/WidgetAddSentenceBtn.svelte";
import WidgetTextInput from "../../shared/WidgetTextInput/WidgetTextInput.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { addInferenceParameters, getWidgetExample, callInferenceApi } from "../../shared/helpers";
import { addInferenceParameters, callInferenceApi } from "../../shared/helpers";
import { isSentenceSimilarityInput } from "../../shared/inputValidation";
export let apiToken: WidgetProps["apiToken"];
Expand Down Expand Up @@ -128,16 +126,10 @@
}
getOutput(opts.inferenceOpts);
}
onMount(() => {
const example = getWidgetExample<WidgetExampleSentenceSimilarityInput>(model, isSentenceSimilarityInput);
if (callApiOnMount && example) {
applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } });
}
});
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand All @@ -185,6 +186,7 @@
{noTitle}
{outputJson}
validateExample={isTextAndTableInput}
runExampleOnMount={false}
>
<svelte:fragment slot="top">
<form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
</script>

<WidgetWrapper
{callApiOnMount}
{apiUrl}
{includeCredentials}
{applyInputSample}
Expand All @@ -207,6 +208,7 @@
{noTitle}
{outputJson}
validateExample={isStructuredDataInput}
runExampleOnMount={false}
>
<svelte:fragment slot="top">
<form>
Expand Down
Loading

0 comments on commit ada2b70

Please sign in to comment.