Skip to content

Commit

Permalink
style(provider-transformers): imporve ui
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <neko@ayaka.moe>
  • Loading branch information
nekomeowww committed Feb 27, 2025
1 parent 9be00ea commit b5a3d81
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
46 changes: 30 additions & 16 deletions packages/provider-transformers/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const loadingItems = ref<(InitiateProgressInfo | ProgressStatusInfo)[]>([])
const transformersProvider = createTransformers({ embedWorkerURL })
onMounted(async () => {
await load()
})
async function load() {
await transformersProvider.loadEmbed(modelId.value, {
onProgress: (progress) => {
switch (progress.status) {
Expand All @@ -43,7 +47,7 @@ onMounted(async () => {
}
},
})
})
}
async function execute() {
const result = await embed({
Expand All @@ -53,44 +57,54 @@ async function execute() {
results.value = result
}
async function handleLoad() {
await transformersProvider.terminateEmbed()
await load()
}
</script>

<template>
<div flex flex-col gap-2 p-4>
<div mx-auto max-w-screen-lg flex flex-col gap-2 p-4>
<header flex flex-row items-center justify-between>
<h1 text-2xl>
<a href="https://huggingface.co/docs/transformers.js/index">🤗 Transformers.js</a> + <a
href="https://github.com/moeru-ai/xsai"
>xsai</a> Playground
</h1>
<button text-lg @click="() => toggleDark()">
<div v-if="isDark" i-solar:moon-stars-bold-duotone />
<div v-else i-solar:sun-bold />
</button>
<div flex flex-row gap-2>
<button text-lg @click="() => toggleDark()">
<div v-if="isDark" i-solar:moon-stars-bold-duotone />
<div v-else i-solar:sun-bold />
</button>
<a href="https://github.com/moeru-ai/airi/tree/main/packages/provider-transformers">
<div i-simple-icons:github />
</a>
</div>
</header>
<div flex flex-col gap-2>
<h2 text-xl>
Options
</h2>
<div w-full flex flex-row gap-2>
<div w-full flex flex-row gap-2>
<label flex flex-row items-center gap-2>
<label flex flex-1 flex-row items-center gap-2>
<div text-nowrap><span>Model ID</span></div>
<input v-model="modelId" bg="neutral-100 dark:neutral-800" block min-w-full w-full rounded-lg p-2>
<input v-model="modelId" bg="neutral-100 dark:neutral-800" block w-full rounded-lg p-2>
</label>
<button rounded-lg bg="blue-100 dark:blue-900" px-4 py-2 @click="() => handleLoad()">
Load
</button>
</div>
</div>
<div v-if="loadingItems.length > 0" class="w-[50%] flex flex-col gap-2">
<div v-if="loadingItems.length > 0" class="w-full flex flex-col gap-2">
<Progress
v-for="(item, index) of loadingItems"
:key="index"
:text="item.file"
:percentage="'progress' in item ? item.progress || 0 : 0"
:total="'total' in item ? item.total || 0 : 0"
v-for="(item, index) of loadingItems" :key="index" :text="item.file"
:percentage="'progress' in item ? item.progress || 0 : 0" :total="'total' in item ? item.total || 0 : 0"
/>
</div>
</div>
<div grid grid-cols-2 gap-2>
<div flex flex-col gap-2>
<div flex flex-col gap-2>
<h2 text-xl>
Inference
Expand All @@ -100,7 +114,7 @@ async function execute() {
</div>
<div flex flex-row gap-2>
<button rounded-lg bg="blue-100 dark:blue-900" px-4 py-2 @click="execute">
Execute
Extract
</button>
</div>
<div flex flex-col gap-2>
Expand Down
7 changes: 7 additions & 0 deletions packages/provider-transformers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { merge } from '@xsai-ext/shared-providers'

export type Loadable<P, T = string, T2 = undefined> = P & {
loadEmbed: (model: (string & {}) | T, options?: T2) => Promise<void>
terminateEmbed: () => void
}

export function createEmbedProvider<T extends string, T2 extends Omit<CommonRequestOptions, 'baseURL' | 'model'> & LoadOptions>(createOptions: CreateProviderOptions): Loadable<EmbedProviderWithExtraOptions<T, T2>, T, T2> {
Expand Down Expand Up @@ -111,6 +112,12 @@ export function createEmbedProvider<T extends string, T2 extends Omit<CommonRequ
},
}) as unknown as Omit<CommonRequestOptions, 'baseURL'> & Partial<T2> as any,
loadEmbed: loadModel,
terminateEmbed: () => {
if (worker) {
worker.terminate()
worker = undefined
}
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/utils-transformers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"dependencies": {
"@huggingface/transformers": "^3.3.3",
"onnxruntime-common": "^1.20.1"
},
"devDependencies": {
"@iconify-json/simple-icons": "^1.2.26"
}
}
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b5a3d81

Please sign in to comment.