Skip to content

Commit

Permalink
Proposal: Slightly better than getDemoInputs already
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-c committed Oct 13, 2023
1 parent fdca4f1 commit 203625a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 9 additions & 0 deletions js/src/lib/components/InferenceWidget/shared/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ModelData } from "../../../interfaces/Types";
import { randomItem, parseJSON } from "../../../utils/ViewUtils";
import type { WidgetExample } from "./WidgetExample";
import type { ModelLoadInfo, TableData } from "./types";

export function getSearchParams(keys: string[]): string[] {
Expand All @@ -19,6 +20,14 @@ export function getDemoInputs(model: ModelData, keys: (number | string)[]): any[
});
}

export function getWidgetExample<TWidgetExample extends WidgetExample>(
model: ModelData,
validateExample: (sample: WidgetExample) => sample is TWidgetExample
): TWidgetExample | undefined {
const sample = model.widgetData?.length ? randomItem(model.widgetData) : undefined;
return sample && validateExample(sample) ? sample : undefined;
}

// Update current url search params, keeping existing keys intact.
export function updateUrl(obj: Record<string, string | undefined>): void {
if (!window) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import WidgetTextarea from "../../shared/WidgetTextarea/WidgetTextarea.svelte";
import WidgetSubmitBtn from "../../shared/WidgetSubmitBtn/WidgetSubmitBtn.svelte";
import WidgetWrapper from "../../shared/WidgetWrapper/WidgetWrapper.svelte";
import { addInferenceParameters, getDemoInputs, getResponse, getSearchParams, updateUrl } from "../../shared/helpers";
import {
addInferenceParameters,
getResponse,
getSearchParams,
getWidgetExample,
updateUrl,
} from "../../shared/helpers";
import { isValidOutputLabels } from "../../shared/outputValidation";
import { isTextInput } from "../../shared/inputValidation";
Expand Down Expand Up @@ -38,9 +44,8 @@
setTextAreaValue(textParam);
getOutput();
} else {
const [demoText] = getDemoInputs(model, ["text"]);
/// TODO(get rid of useless getDemoInputs)
setTextAreaValue(demoText ?? "");
const sample = getWidgetExample(model, validateExample);
setTextAreaValue(sample?.text ?? "");
if (text && callApiOnMount) {
getOutput({ isOnLoadCall: true });
}
Expand Down

0 comments on commit 203625a

Please sign in to comment.