-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Widgets] Refactor examples running #1023
Changes from 25 commits
667710d
85aac2e
c56c64f
aa2396e
d2374e4
d5325f9
2730c9c
3ba2298
7858186
fd40b25
756223d
580c830
f89936c
0654367
94249c4
353f172
23a2fe0
c36fcdc
06420aa
44eb4f1
ada2b70
37f2ccd
8446c3e
7ebca52
c89086b
4d03786
9ea310a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<script lang="ts"> | ||
import type { WidgetProps, ModelLoadInfo } from "../types"; | ||
import type { WidgetProps, ModelLoadInfo, ExampleRunOpts } from "../types"; | ||
import type { WidgetExample } from "../WidgetExample"; | ||
import type { QueryParam } from "../../shared/helpers"; | ||
|
||
type TWidgetExample = $$Generic<WidgetExample>; | ||
|
||
|
@@ -13,10 +14,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, getQueryParamVal, 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; | ||
|
@@ -28,9 +30,9 @@ | |
}; | ||
export let noTitle = false; | ||
export let outputJson: string; | ||
export let applyInputSample: (sample: TWidgetExample) => void = () => {}; | ||
export let previewInputSample: (sample: TWidgetExample) => void = () => {}; | ||
export let applyInputSample: (sample: TWidgetExample, opts?: ExampleRunOpts) => void = () => {}; | ||
export let validateExample: (sample: WidgetExample) => sample is TWidgetExample; | ||
export let exampleQueryParams: QueryParam[] = []; | ||
|
||
let isMaximized = false; | ||
let modelLoadInfo: ModelLoadInfo | undefined = undefined; | ||
|
@@ -64,6 +66,24 @@ | |
(async () => { | ||
modelLoadInfo = await getModelLoadInfo(apiUrl, model.id, includeCredentials); | ||
$modelLoadStates[model.id] = modelLoadInfo; | ||
|
||
const exampleFromQueryParams = {} as TWidgetExample; | ||
for (const key of exampleQueryParams) { | ||
const val = getQueryParamVal(key); | ||
if(val){ | ||
exampleFromQueryParams[key] = val; | ||
} | ||
} | ||
if (Object.keys(exampleFromQueryParams).length) { | ||
// run widget example from query params | ||
applyInputSample(exampleFromQueryParams); | ||
} else { | ||
// run random widget example | ||
const example = getWidgetExample<TWidgetExample>(model, validateExample); | ||
if (callApiOnMount && example) { | ||
mishig25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
applyInputSample(example, { inferenceOpts: { isOnLoadCall: true } }); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure but |
||
})(); | ||
}); | ||
|
||
|
@@ -107,7 +127,6 @@ | |
{isLoading} | ||
inputSamples={selectedInputSamples?.inputSamples ?? []} | ||
{applyInputSample} | ||
{previewInputSample} | ||
/> | ||
</div> | ||
{/if} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,37 @@ | ||
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[] { | ||
type KeysOfUnion<T> = T extends any ? keyof T : never; | ||
export type QueryParam = KeysOfUnion<WidgetExample>; | ||
type QueryParamVal = string | null | boolean | (string | number)[][]; | ||
export function getQueryParamVal(key: QueryParam): QueryParamVal { | ||
const searchParams = new URL(window.location.href).searchParams; | ||
return keys.map(key => { | ||
const value = searchParams.get(key); | ||
return value || ""; | ||
}); | ||
const value = searchParams.get(key); | ||
if (["text", "context", "question", "query", "candidate_labels"].includes(key)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will not work on existing URLs of ZeroShotClassification widget with URL query There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i agree for https://huggingface.co/docs/hub/models-widgets-examples#structured-data-classification There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my comment there |
||
return value; | ||
} else if (["table", "structured_data"].includes(key)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the comment above but for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const table = convertDataToTable((parseJSON(value) as TableData) ?? {}); | ||
return table; | ||
} else if (key === "multi_class") { | ||
return value === "true"; | ||
} | ||
return value; | ||
} | ||
|
||
export function getDemoInputs(model: ModelData, keys: (number | string)[]): any[] { | ||
const widgetData = Array.isArray(model.widgetData) ? model.widgetData : []; | ||
const randomEntry = (randomItem(widgetData) ?? {}) as any; | ||
return keys.map(key => { | ||
const value = randomEntry[key] ? randomEntry[key] : null; | ||
return value ? randomEntry[key] : null; | ||
}); | ||
export function getWidgetExample<TWidgetExample extends WidgetExample>( | ||
model: ModelData, | ||
validateExample: (sample: WidgetExample) => sample is TWidgetExample | ||
): TWidgetExample | undefined { | ||
const validExamples = model.widgetData?.filter( | ||
(sample): sample is TWidgetExample => sample && validateExample(sample) | ||
); | ||
return validExamples?.length ? randomItem(validExamples) : undefined; | ||
} | ||
|
||
// Update current url search params, keeping existing keys intact. | ||
export function updateUrl(obj: Record<string, string | undefined>): void { | ||
export function updateUrl(obj: Partial<Record<QueryParam, string | undefined>>): void { | ||
if (!window) { | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be typed further?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this for example (requires some ugly casts in
onMount
but I think I'm fine with it as it makes it almost impossible to pass wrong query params)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, stronger typing is possible c89086b