victor HF staff commited on
Commit
e1cf22b
1 Parent(s): a9b0dc6
src/lib/components/Playground/Playground.svelte CHANGED
@@ -2,6 +2,7 @@
2
  import { HfInference } from '@huggingface/inference';
3
  import { queryParam, ssp } from 'sveltekit-search-params';
4
 
 
5
  import PlaygroundMessage from '$lib/components/Playground/PlaygroundMessage.svelte';
6
  import PlaygroundOptions from '$lib/components/Playground/PlaygroundOptions.svelte';
7
 
@@ -28,7 +29,6 @@
28
  'mistralai/Mistral-7B-Instruct-v0.3'
29
  ];
30
 
31
- let hfToken: string | null = '';
32
  const currentModel = queryParam('model', ssp.string(compatibleModels[0]));
33
  const temperature = queryParam('temperature', ssp.number(0.5));
34
  const maxTokens = queryParam('max_tokens', ssp.number(2048));
@@ -36,11 +36,13 @@
36
  const jsonMode = queryParam('json_mode', ssp.boolean(false));
37
  $: systemMessage = { role: 'system', content: $systemMessageParam };
38
  $: messages = $messagesParam;
39
- let messageContainer: HTMLDivElement | null = null;
40
 
 
 
41
  let loading = false;
42
  let streamingMessage: Message | null = null;
43
  let latency = 0;
 
44
 
45
  function addMessage() {
46
  $messagesParam = [
@@ -145,7 +147,7 @@
145
  <svelte:window on:keydown={onKeydown} />
146
 
147
  <div
148
- class="grid h-dvh max-h-dvh divide-gray-200 overflow-hidden max-md:grid-cols-1 max-md:divide-y md:grid-cols-[260px,1fr,260px] md:divide-x dark:divide-gray-800 dark:bg-gray-900 dark:text-gray-300"
149
  >
150
  <div class="relative flex flex-col overflow-y-auto px-5 pb-24 pt-7">
151
  <div class="pb-2 text-sm font-semibold">SYSTEM</div>
@@ -159,19 +161,23 @@
159
  </div>
160
  <div class="relative divide-y divide-gray-200 dark:divide-gray-800">
161
  <div
162
- class="flex max-h-[calc(100dvh-5rem)] flex-col divide-y divide-gray-200 overflow-y-auto dark:divide-gray-800"
163
  bind:this={messageContainer}
164
  >
165
- {#each messages as message, i}
166
- <PlaygroundMessage {message} on:delete={() => deleteMessage(i)} />
167
- {/each}
168
-
169
- <button
170
- class="flex px-6 py-6 hover:bg-gray-50 dark:hover:bg-gray-800/50"
171
- on:click={addMessage}
172
- >
173
- <div class="!p-0 text-sm font-semibold">Add message</div>
174
- </button>
 
 
 
 
175
  </div>
176
 
177
  <div
@@ -194,8 +200,9 @@
194
  </div>
195
  <button
196
  type="button"
 
197
  class="rounded-lg border border-gray-200 bg-white px-5 py-2.5 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:outline-none focus:ring-4 focus:ring-gray-100 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
198
- >View Code</button
199
  >
200
  <button
201
  on:click={submit}
 
2
  import { HfInference } from '@huggingface/inference';
3
  import { queryParam, ssp } from 'sveltekit-search-params';
4
 
5
+ import PlaygroundCode from './PlaygroundCode.svelte';
6
  import PlaygroundMessage from '$lib/components/Playground/PlaygroundMessage.svelte';
7
  import PlaygroundOptions from '$lib/components/Playground/PlaygroundOptions.svelte';
8
 
 
29
  'mistralai/Mistral-7B-Instruct-v0.3'
30
  ];
31
 
 
32
  const currentModel = queryParam('model', ssp.string(compatibleModels[0]));
33
  const temperature = queryParam('temperature', ssp.number(0.5));
34
  const maxTokens = queryParam('max_tokens', ssp.number(2048));
 
36
  const jsonMode = queryParam('json_mode', ssp.boolean(false));
37
  $: systemMessage = { role: 'system', content: $systemMessageParam };
38
  $: messages = $messagesParam;
 
39
 
40
+ let hfToken: string | null = '';
41
+ let viewCode = false;
42
  let loading = false;
43
  let streamingMessage: Message | null = null;
44
  let latency = 0;
45
+ let messageContainer: HTMLDivElement | null = null;
46
 
47
  function addMessage() {
48
  $messagesParam = [
 
147
  <svelte:window on:keydown={onKeydown} />
148
 
149
  <div
150
+ class="w-dvh maxdivide-gray-200 grid h-dvh overflow-hidden max-md:grid-cols-1 max-md:divide-y md:grid-cols-[260px,minmax(0,1fr),260px] md:divide-x dark:divide-gray-800 dark:bg-gray-900 dark:text-gray-300"
151
  >
152
  <div class="relative flex flex-col overflow-y-auto px-5 pb-24 pt-7">
153
  <div class="pb-2 text-sm font-semibold">SYSTEM</div>
 
161
  </div>
162
  <div class="relative divide-y divide-gray-200 dark:divide-gray-800">
163
  <div
164
+ class="flex max-h-[calc(100dvh-5rem)] flex-col divide-y divide-gray-200 overflow-y-auto overflow-x-hidden dark:divide-gray-800"
165
  bind:this={messageContainer}
166
  >
167
+ {#if !viewCode}
168
+ {#each messages as message, i}
169
+ <PlaygroundMessage {message} on:delete={() => deleteMessage(i)} />
170
+ {/each}
171
+
172
+ <button
173
+ class="flex px-6 py-6 hover:bg-gray-50 dark:hover:bg-gray-800/50"
174
+ on:click={addMessage}
175
+ >
176
+ <div class="!p-0 text-sm font-semibold">Add message</div>
177
+ </button>
178
+ {:else}
179
+ <PlaygroundCode />
180
+ {/if}
181
  </div>
182
 
183
  <div
 
200
  </div>
201
  <button
202
  type="button"
203
+ on:click={() => (viewCode = !viewCode)}
204
  class="rounded-lg border border-gray-200 bg-white px-5 py-2.5 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:outline-none focus:ring-4 focus:ring-gray-100 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
205
+ >{!viewCode ? 'View Code' : 'Hide Code'}</button
206
  >
207
  <button
208
  on:click={submit}
src/lib/components/Playground/PlaygroundCode.svelte ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script>
2
+ const nonStreaming = `await hf.chatCompletion({
3
+ model: "mistrali/Mistral-7B-Instruct-v0.2",
4
+ messages: [{ role: "user", content: "Complete the this sentence with words one plus one is equal " }],
5
+ max_tokens: 500,
6
+ temperature: 0.1,
7
+ seed: 0,
8
+ });`;
9
+
10
+ const streaming = `let out = "";
11
+
12
+ for await (const chunk of hf.chatCompletionStream({
13
+ model: "mistrali/Mistral-7B-Instruct-v0.2",
14
+ messages: [
15
+ { role: "user", content: "Complete the equation 1+1= ,just the answer" },
16
+ ],
17
+ max_tokens: 500,
18
+ temperature: 0.1,
19
+ seed: 0,
20
+ })) {
21
+ if (chunk.choices && chunk.choices.length > 0) {
22
+ out += chunk.choices[0].delta.content;
23
+ }
24
+ }`;
25
+ </script>
26
+
27
+ <div class="overflow-hidden p-8">
28
+ <h2 class="mb-4 font-bold">Non-streaming API</h2>
29
+ <pre
30
+ class="overflow-x-auto rounded-md bg-gray-800 p-4 text-sm text-white">{@html nonStreaming}</pre>
31
+
32
+ <h2 class="my-4 font-bold">Streaming API</h2>
33
+ <pre class="overflow-x-auto rounded-md bg-gray-800 p-4 text-sm text-white">{streaming}</pre>
34
+ </div>
src/lib/components/Playground/PlaygroundOptions.svelte CHANGED
@@ -7,7 +7,7 @@
7
  </script>
8
 
9
  <div>
10
- <form class="mx-auto max-w-sm">
11
  <label for="countries" class="mb-2 block text-sm font-medium text-gray-900 dark:text-white"
12
  >Model</label
13
  >
@@ -19,7 +19,7 @@
19
  <option value={model}>{model}</option>
20
  {/each}
21
  </select>
22
- </form>
23
  </div>
24
  <div>
25
  <div class="flex items-center justify-between">
 
7
  </script>
8
 
9
  <div>
10
+ <div>
11
  <label for="countries" class="mb-2 block text-sm font-medium text-gray-900 dark:text-white"
12
  >Model</label
13
  >
 
19
  <option value={model}>{model}</option>
20
  {/each}
21
  </select>
22
+ </div>
23
  </div>
24
  <div>
25
  <div class="flex items-center justify-between">