Spaces:
Runtime error
Runtime error
Fetch compatible models & their tokenizer files server side (#25)
Browse filesFiles changed (4)
8 unmodified lines91011121314151617181920 unmodified lines404142434445464748495051525354555657585960305 unmodified lines36636736836937037137227 unmodified lines4004014024034044054068 unmodified linesimport PlaygroundTokenModal from './InferencePlaygroundHFTokenModal.svelte';import PlaygroundModelSelector from './InferencePlaygroundModelSelector.svelte';import Conversation from './InferencePlaygroundConversation.svelte';import { onMount } from 'svelte';import { type ModelEntry } from '@huggingface/hub';import { type ChatCompletionInputMessage } from '@huggingface/tasks';let compatibleModels: ModelEntry[] = [];const startMessages: ChatCompletionInputMessage[] = [{ role: 'user', content: '' }];20 unmodified lineslet abortControllers: AbortController[] = [];let waitForNonStreaming = true;onMount(() => {(async () => {// TODO: use hfjs.hub listModels after https://github.com/huggingface/huggingface.js/pull/795const res = await fetch('https://huggingface.co/api/models?pipeline_tag=text-generation&inference=Warm&filter=conversational');compatibleModels = (await res.json()) as ModelEntry[];compatibleModels.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase()));})();return () => {for (const abortController of abortControllers) {abortController.abort();}};});function addMessage() {305 unmodified lines<divclass="flex flex-1 flex-col gap-6 overflow-y-hidden rounded-xl border border-gray-200/80 bg-gradient-to-b from-white via-white p-3 shadow-sm dark:border-white/5 dark:from-gray-800/40 dark:via-gray-800/40"><PlaygroundModelSelector {compatibleModels} bind:currentModel={conversations[0].model} /><divclass="group relative -mt-4 flex h-[26px] w-full items-center justify-center gap-2 rounded-lg bg-black px-5 text-sm text-white hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 dark:border-gray-700 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-gray-700">27 unmodified lines];}}>{#each compatibleModels as model}<option value={model.id}>{model.id}</option>{/each}</select>8 unmodified lines91011121314151617181920 unmodified lines40414243444546474849305 unmodified lines35535635735835936036136236336427 unmodified lines3923933943953963973988 unmodified linesimport PlaygroundTokenModal from './InferencePlaygroundHFTokenModal.svelte';import PlaygroundModelSelector from './InferencePlaygroundModelSelector.svelte';import Conversation from './InferencePlaygroundConversation.svelte';import { onDestroy } from 'svelte';import { type ChatCompletionInputMessage } from '@huggingface/tasks';import type { ModelEntryWithTokenizer } from '$lib/types';export let models: ModelEntryWithTokenizer[];const startMessages: ChatCompletionInputMessage[] = [{ role: 'user', content: '' }];20 unmodified lineslet abortControllers: AbortController[] = [];let waitForNonStreaming = true;onDestroy(() => {for (const abortController of abortControllers) {abortController.abort();}});function addMessage() {305 unmodified lines<divclass="flex flex-1 flex-col gap-6 overflow-y-hidden rounded-xl border border-gray-200/80 bg-gradient-to-b from-white via-white p-3 shadow-sm dark:border-white/5 dark:from-gray-800/40 dark:via-gray-800/40"><PlaygroundModelSelectorcompatibleModels={models}bind:currentModel={conversations[0].model}/><divclass="group relative -mt-4 flex h-[26px] w-full items-center justify-center gap-2 rounded-lg bg-black px-5 text-sm text-white hover:bg-gray-900 focus:outline-none focus:ring-4 focus:ring-gray-300 dark:border-gray-700 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-gray-700">27 unmodified lines];}}>{#each models as model}<option value={model.id}>{model.id}</option>{/each}</select>
15 unmodified lines16171815 unmodified linesconfig: ModelConfig;messages: ChatCompletionInputMessage[];};15 unmodified lines16171819202122232425262715 unmodified linesconfig: ModelConfig;messages: ChatCompletionInputMessage[];};interface TokenizerConfig {chat_template?: string;model_max_length?: number;}export interface ModelEntryWithTokenizer extends ModelEntry {tokenizerConfig: TokenizerConfig;}
12345678910111213141516171819202122232425262728293031323334// src/routes/my-page/page.server.tsimport type { ModelEntryWithTokenizer } from '$lib/types';import type { ModelEntry } from '@huggingface/hub';import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ fetch }) => { const apiUrl = 'https://huggingface.co/api/models?pipeline_tag=text-generation&inference=Warm&filter=conversational'; const HF_TOKEN = import.meta.env.HF_TOKEN;
const res = await fetch(apiUrl, { headers: { Authorization: `Bearer ${HF_TOKEN}` } }); let compatibleModels: ModelEntry[] = await res.json(); compatibleModels.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase())); compatibleModels = compatibleModels.slice(0, 2);
const promises = compatibleModels.map(async (model) => { const configUrl = `https://huggingface.co/${model.modelId}/raw/main/tokenizer_config.json`; const res = await fetch(configUrl, { headers: { Authorization: `Bearer ${HF_TOKEN}` } }); const tokenizerConfig = await res.json(); return { ...model, tokenizerConfig } satisfies ModelEntryWithTokenizer; });
const models: ModelEntryWithTokenizer[] = await Promise.all(promises);
return { models };};12345<script lang="ts">import InferencePlayground from '$lib/components/InferencePlayground/InferencePlayground.svelte';</script><InferencePlayground />123456<script lang="ts">export let data;import InferencePlayground from '$lib/components/InferencePlayground/InferencePlayground.svelte';</script><InferencePlayground models={data.models} />
src/lib/components/InferencePlayground/InferencePlayground.svelte+12-20
src/lib/types/index.d.ts+9
src/routes/+page.server.tsAdded+34
src/routes/+page.svelte+2-1