mishig HF staff commited on
Commit
7956c78
1 Parent(s): e779e06

Fetch compatible models & their tokenizer files server side (#25)

Browse files
src/lib/components/InferencePlayground/InferencePlayground.svelte CHANGED
@@ -9,11 +9,11 @@
9
  import PlaygroundTokenModal from './InferencePlaygroundHFTokenModal.svelte';
10
  import PlaygroundModelSelector from './InferencePlaygroundModelSelector.svelte';
11
  import Conversation from './InferencePlaygroundConversation.svelte';
12
- import { onMount } from 'svelte';
13
- import { type ModelEntry } from '@huggingface/hub';
14
  import { type ChatCompletionInputMessage } from '@huggingface/tasks';
 
15
 
16
- let compatibleModels: ModelEntry[] = [];
17
 
18
  const startMessages: ChatCompletionInputMessage[] = [{ role: 'user', content: '' }];
19
 
@@ -40,21 +40,10 @@
40
  let abortControllers: AbortController[] = [];
41
  let waitForNonStreaming = true;
42
 
43
- onMount(() => {
44
- (async () => {
45
- // TODO: use hfjs.hub listModels after https://github.com/huggingface/huggingface.js/pull/795
46
- const res = await fetch(
47
- 'https://huggingface.co/api/models?pipeline_tag=text-generation&inference=Warm&filter=conversational'
48
- );
49
- compatibleModels = (await res.json()) as ModelEntry[];
50
- compatibleModels.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase()));
51
- })();
52
-
53
- return () => {
54
- for (const abortController of abortControllers) {
55
- abortController.abort();
56
- }
57
- };
58
  });
59
 
60
  function addMessage() {
@@ -366,7 +355,10 @@
366
  <div
367
  class="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"
368
  >
369
- <PlaygroundModelSelector {compatibleModels} bind:currentModel={conversations[0].model} />
 
 
 
370
  <div
371
  class="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"
372
  >
@@ -400,7 +392,7 @@
400
  ];
401
  }}
402
  >
403
- {#each compatibleModels as model}
404
  <option value={model.id}>{model.id}</option>
405
  {/each}
406
  </select>
 
9
  import PlaygroundTokenModal from './InferencePlaygroundHFTokenModal.svelte';
10
  import PlaygroundModelSelector from './InferencePlaygroundModelSelector.svelte';
11
  import Conversation from './InferencePlaygroundConversation.svelte';
12
+ import { onDestroy } from 'svelte';
 
13
  import { type ChatCompletionInputMessage } from '@huggingface/tasks';
14
+ import type { ModelEntryWithTokenizer } from '$lib/types';
15
 
16
+ export let models: ModelEntryWithTokenizer[];
17
 
18
  const startMessages: ChatCompletionInputMessage[] = [{ role: 'user', content: '' }];
19
 
 
40
  let abortControllers: AbortController[] = [];
41
  let waitForNonStreaming = true;
42
 
43
+ onDestroy(() => {
44
+ for (const abortController of abortControllers) {
45
+ abortController.abort();
46
+ }
 
 
 
 
 
 
 
 
 
 
 
47
  });
48
 
49
  function addMessage() {
 
355
  <div
356
  class="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"
357
  >
358
+ <PlaygroundModelSelector
359
+ compatibleModels={models}
360
+ bind:currentModel={conversations[0].model}
361
+ />
362
  <div
363
  class="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"
364
  >
 
392
  ];
393
  }}
394
  >
395
+ {#each models as model}
396
  <option value={model.id}>{model.id}</option>
397
  {/each}
398
  </select>
src/lib/types/index.d.ts CHANGED
@@ -16,3 +16,12 @@ type Conversation = {
16
  config: ModelConfig;
17
  messages: ChatCompletionInputMessage[];
18
  };
 
 
 
 
 
 
 
 
 
 
16
  config: ModelConfig;
17
  messages: ChatCompletionInputMessage[];
18
  };
19
+
20
+ interface TokenizerConfig {
21
+ chat_template?: string;
22
+ model_max_length?: number;
23
+ }
24
+
25
+ export interface ModelEntryWithTokenizer extends ModelEntry {
26
+ tokenizerConfig: TokenizerConfig;
27
+ }
src/routes/+page.server.ts ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // src/routes/my-page/page.server.ts
2
+ import type { ModelEntryWithTokenizer } from '$lib/types';
3
+ import type { ModelEntry } from '@huggingface/hub';
4
+ import type { PageServerLoad } from './$types';
5
+
6
+ export const load: PageServerLoad = async ({ fetch }) => {
7
+ const apiUrl =
8
+ 'https://huggingface.co/api/models?pipeline_tag=text-generation&inference=Warm&filter=conversational';
9
+ const HF_TOKEN = import.meta.env.HF_TOKEN;
10
+
11
+ const res = await fetch(apiUrl, {
12
+ headers: {
13
+ Authorization: `Bearer ${HF_TOKEN}`
14
+ }
15
+ });
16
+ let compatibleModels: ModelEntry[] = await res.json();
17
+ compatibleModels.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase()));
18
+ compatibleModels = compatibleModels.slice(0, 2);
19
+
20
+ const promises = compatibleModels.map(async (model) => {
21
+ const configUrl = `https://huggingface.co/${model.modelId}/raw/main/tokenizer_config.json`;
22
+ const res = await fetch(configUrl, {
23
+ headers: {
24
+ Authorization: `Bearer ${HF_TOKEN}`
25
+ }
26
+ });
27
+ const tokenizerConfig = await res.json();
28
+ return { ...model, tokenizerConfig } satisfies ModelEntryWithTokenizer;
29
+ });
30
+
31
+ const models: ModelEntryWithTokenizer[] = await Promise.all(promises);
32
+
33
+ return { models };
34
+ };
src/routes/+page.svelte CHANGED
@@ -1,5 +1,6 @@
1
  <script lang="ts">
 
2
  import InferencePlayground from '$lib/components/InferencePlayground/InferencePlayground.svelte';
3
  </script>
4
 
5
- <InferencePlayground />
 
1
  <script lang="ts">
2
+ export let data;
3
  import InferencePlayground from '$lib/components/InferencePlayground/InferencePlayground.svelte';
4
  </script>
5
 
6
+ <InferencePlayground models={data.models} />