|
1 | 1 | <script lang="ts"> |
| 2 | + import { randomItem } from "$lib/utils/ViewUtils.js"; |
| 3 | +
|
2 | 4 | import type { ExampleRunOpts, WidgetProps } from "../types.js"; |
3 | 5 | import type { WidgetExample, WidgetExampleAttribute } from "@huggingface/tasks"; |
4 | 6 |
|
|
9 | 11 |
|
10 | 12 | import IconCaretDownV2 from "../../..//Icons/IconCaretDownV2.svelte"; |
11 | 13 | import WidgetExamplesGroup from "./WidgetExamplesGroup.svelte"; |
12 | | - import { getQueryParamVal, getWidgetExample } from "../../..//InferenceWidget/shared/helpers.js"; |
| 14 | + import { getQueryParamVal } from "../../..//InferenceWidget/shared/helpers.js"; |
13 | 15 |
|
14 | 16 | export let isLoading = false; |
15 | | - export let model: WidgetProps["model"]; |
16 | 17 | export let callApiOnMount: WidgetProps["callApiOnMount"]; |
17 | 18 | export let exampleQueryParams: WidgetExampleAttribute[] = []; |
18 | 19 | export let applyWidgetExample: (sample: TWidgetExample, opts?: ExampleRunOpts) => void; |
19 | | - export let validateExample: (sample: WidgetExample) => sample is TWidgetExample; |
20 | 20 |
|
21 | | - export let examplesAll: TWidgetExample[]; |
| 21 | + export let validExamples: TWidgetExample[]; |
22 | 22 |
|
23 | 23 | interface ExamplesGroup { |
24 | 24 | group: string; |
25 | 25 | examples: TWidgetExample[]; |
26 | 26 | } |
27 | 27 |
|
28 | | - $: exampleGroups = getExamplesGroups(examplesAll); |
| 28 | + $: exampleGroups = getExamplesGroups(validExamples); |
29 | 29 | $: examples = exampleGroups?.[0]?.examples ?? []; |
30 | 30 | // for examples with multiple groups, a group needs to be selected first, before an example can be clicked |
31 | 31 | $: clickable = exampleGroups?.length === 1; |
32 | 32 | let containerEl: HTMLElement; |
33 | 33 | let isOptionsVisible = false; |
34 | 34 | let title = "Examples"; |
35 | 35 |
|
36 | | - function getExamplesGroups(_examplesAll: TWidgetExample[]): ExamplesGroup[] { |
37 | | - const examplesAll = _examplesAll.map((sample, idx) => ({ |
| 36 | + function getExamplesGroups(_examples: TWidgetExample[]): ExamplesGroup[] { |
| 37 | + const examples = _examples.map((sample, idx) => ({ |
38 | 38 | example_title: `Example ${++idx}`, |
39 | 39 | group: "Group 1", |
40 | 40 | ...sample, |
41 | 41 | })); |
42 | 42 | const examplesGroups: ExamplesGroup[] = []; |
43 | | - for (const example of examplesAll) { |
| 43 | + for (const example of examples) { |
44 | 44 | const groupExists = examplesGroups.find(({ group }) => group === example.group); |
45 | 45 | if (!groupExists) { |
46 | 46 | examplesGroups.push({ group: example.group as string, examples: [] }); |
|
109 | 109 | applyWidgetExample(exampleFromQueryParams); |
110 | 110 | } else { |
111 | 111 | // run random widget example |
112 | | - const example = getWidgetExample<TWidgetExample>(model, validateExample); |
| 112 | + const example = randomItem(validExamples); |
113 | 113 | if (callApiOnMount && example) { |
114 | 114 | applyWidgetExample(example, { inferenceOpts: { isOnLoadCall: true } }); |
115 | 115 | } |
|
120 | 120 |
|
121 | 121 | <svelte:window on:click={onClick} /> |
122 | 122 |
|
123 | | -{#if examplesAll.length} |
124 | | - <div class="ml-auto flex gap-x-1"> |
125 | | - <!-- Example Groups --> |
126 | | - {#if exampleGroups.length > 1} |
127 | | - <WidgetExamplesGroup |
128 | | - on:groupSelected={changeGroup} |
129 | | - {isLoading} |
130 | | - groupNames={exampleGroups.map(({ group }) => group)} |
131 | | - /> |
132 | | - {/if} |
133 | | - |
134 | | - <!-- Example picker --> |
| 123 | +<div class="ml-auto flex gap-x-1"> |
| 124 | + <!-- Example Groups --> |
| 125 | + {#if exampleGroups.length > 1} |
| 126 | + <WidgetExamplesGroup |
| 127 | + on:groupSelected={changeGroup} |
| 128 | + {isLoading} |
| 129 | + groupNames={exampleGroups.map(({ group }) => group)} |
| 130 | + /> |
| 131 | + {/if} |
| 132 | + |
| 133 | + <!-- Example picker --> |
| 134 | + <div |
| 135 | + class="relative mb-1.5 |
| 136 | + {isLoading || !clickable ? 'pointer-events-none opacity-50' : ''} |
| 137 | + {isOptionsVisible ? 'z-10' : ''}" |
| 138 | + bind:this={containerEl} |
| 139 | + > |
| 140 | + <!-- svelte-ignore a11y-click-events-have-key-events --> |
135 | 141 | <div |
136 | | - class="relative mb-1.5 |
137 | | - {isLoading || !clickable ? 'pointer-events-none opacity-50' : ''} |
138 | | - {isOptionsVisible ? 'z-10' : ''}" |
139 | | - bind:this={containerEl} |
| 142 | + class="inline-flex w-32 justify-between rounded-md border border-gray-100 px-4 py-1" |
| 143 | + on:click={toggleOptionsVisibility} |
140 | 144 | > |
141 | | - <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 145 | + <div class="truncate text-sm">{title}</div> |
| 146 | + <IconCaretDownV2 |
| 147 | + classNames="-mr-1 ml-2 h-5 w-5 transition ease-in-out transform {isOptionsVisible && '-rotate-180'}" |
| 148 | + /> |
| 149 | + </div> |
| 150 | + |
| 151 | + {#if isOptionsVisible} |
142 | 152 | <div |
143 | | - class="inline-flex w-32 justify-between rounded-md border border-gray-100 px-4 py-1" |
144 | | - on:click={toggleOptionsVisibility} |
| 153 | + class="absolute right-0 mt-1 w-full origin-top-right rounded-md ring-1 ring-black ring-opacity-10" |
| 154 | + transition:slide |
145 | 155 | > |
146 | | - <div class="truncate text-sm">{title}</div> |
147 | | - <IconCaretDownV2 |
148 | | - classNames="-mr-1 ml-2 h-5 w-5 transition ease-in-out transform {isOptionsVisible && '-rotate-180'}" |
149 | | - /> |
150 | | - </div> |
151 | | - |
152 | | - {#if isOptionsVisible} |
153 | | - <div |
154 | | - class="absolute right-0 mt-1 w-full origin-top-right rounded-md ring-1 ring-black ring-opacity-10" |
155 | | - transition:slide |
156 | | - > |
157 | | - <div class="rounded-md bg-white py-1" role="none"> |
158 | | - {#each examples as { example_title }, i} |
159 | | - <!-- svelte-ignore a11y-click-events-have-key-events a11y-mouse-events-have-key-events --> |
160 | | - <div |
161 | | - class="cursor-pointer truncate px-4 py-2 text-sm hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-200" |
162 | | - on:mouseover={() => _previewInputSample(i)} |
163 | | - on:click={() => _applyWidgetExample(i)} |
164 | | - > |
165 | | - {example_title} |
166 | | - </div> |
167 | | - {/each} |
168 | | - </div> |
| 156 | + <div class="rounded-md bg-white py-1" role="none"> |
| 157 | + {#each examples as { example_title }, i} |
| 158 | + <!-- svelte-ignore a11y-click-events-have-key-events a11y-mouse-events-have-key-events --> |
| 159 | + <div |
| 160 | + class="cursor-pointer truncate px-4 py-2 text-sm hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-200" |
| 161 | + on:mouseover={() => _previewInputSample(i)} |
| 162 | + on:click={() => _applyWidgetExample(i)} |
| 163 | + > |
| 164 | + {example_title} |
| 165 | + </div> |
| 166 | + {/each} |
169 | 167 | </div> |
170 | | - {/if} |
171 | | - </div> |
| 168 | + </div> |
| 169 | + {/if} |
172 | 170 | </div> |
173 | | -{/if} |
| 171 | +</div> |
0 commit comments