File size: 16,718 Bytes
1189124
2168f52
4b8b411
1189124
dd66861
631cc27
60216ec
 
4b8b411
c7f83e1
654f56a
25c63d0
8c7e7de
25c63d0
 
631cc27
 
 
 
 
 
 
e8b5344
60216ec
 
5c5d81b
aac02fe
654f56a
1189124
631cc27
5c5d81b
2168f52
 
 
1189124
4ae5188
1189124
5c5d81b
1189124
5c5d81b
 
 
 
 
 
2168f52
92b1a8e
 
da972f7
2168f52
 
dd66861
5c5d81b
2168f52
e8b5344
 
651e1d0
e8b5344
651e1d0
e8b5344
60216ec
 
651e1d0
e8b5344
1189124
 
5c5d81b
2168f52
e8b5344
1189124
 
 
2168f52
5c5d81b
 
 
cf0407a
1189124
 
 
5c5d81b
 
 
 
 
 
1189124
 
 
 
5c5d81b
 
25c63d0
5c5d81b
 
0ece011
5c5d81b
 
 
 
 
 
 
 
 
 
0ece011
 
 
 
e8b5344
 
 
5c5d81b
 
 
 
 
 
 
 
 
 
 
 
 
e8b5344
 
5c5d81b
 
 
 
e8b5344
 
5c5d81b
 
1189124
25c63d0
 
1189124
 
e4a4cf3
2168f52
5c5d81b
 
2168f52
5c5d81b
 
 
 
e4a4cf3
 
1189124
 
 
 
2168f52
5c5d81b
1189124
2168f52
5c5d81b
 
 
 
e8b5344
6e870f4
bf9fb3d
 
25c63d0
bf9fb3d
 
 
 
 
 
1189124
 
 
5c5d81b
1189124
 
 
 
45307d1
1189124
 
 
be78046
4d8160c
 
 
ed1ed33
 
 
25c63d0
ed1ed33
 
 
 
4d8160c
 
b924465
c7f83e1
2168f52
5c5d81b
 
2168f52
 
5c5d81b
 
 
 
2168f52
e8b5344
5c5d81b
 
 
 
be78046
5c5d81b
 
 
be78046
1189124
 
25c63d0
 
 
 
 
 
1189124
 
3b5cc6c
1189124
a251d41
8c5a2cf
 
1189124
654f56a
 
 
 
1189124
8c5a2cf
dd66861
 
1189124
dd66861
1189124
 
 
dd66861
60216ec
 
2168f52
5c5d81b
2168f52
5c5d81b
 
e8b5344
5c5d81b
8c5a2cf
1189124
 
 
90f84fa
6bb767e
7849c46
6bb767e
2168f52
382cad0
5c5d81b
 
 
 
 
 
 
 
 
 
 
1cd3833
5c5d81b
 
8709c70
5c5d81b
 
 
1189124
 
8c5a2cf
1189124
5c5d81b
 
 
 
 
8c5a2cf
1189124
5c5d81b
 
 
1189124
aac02fe
5c5d81b
 
 
8c5a2cf
5c5d81b
 
 
 
 
aac02fe
5c5d81b
 
 
 
 
 
 
 
 
8c5a2cf
5c5d81b
 
 
 
 
 
2168f52
5c5d81b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c5a2cf
8c7e7de
5c5d81b
 
 
 
1189124
 
5c5d81b
 
 
8c5a2cf
5c5d81b
 
2168f52
8c5a2cf
7ad8848
48dce05
7ad8848
 
 
 
 
5c5d81b
2168f52
b7dc03e
5c5d81b
48dce05
429b74f
aac02fe
5c5d81b
 
 
429b74f
1189124
2168f52
25c63d0
5c5d81b
25c63d0
5c5d81b
 
 
 
 
 
 
 
 
 
 
1457dc9
5c5d81b
 
 
 
 
8c5a2cf
5c5d81b
 
1189124
5c5d81b
 
 
 
 
1189124
 
 
5c5d81b
1189124
5541896
8709c70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ad8848
 
2168f52
7ad8848
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<script lang="ts">
	import type { Conversation, ConversationMessage, ModelWithTokenizer, Session } from "$lib/types";

	import {
		handleNonStreamingResponse,
		handleStreamingResponse,
		isSystemPromptSupported,
	} from "./inferencePlaygroundUtils";

	import { models } from "$lib/stores/models";
	import { getActiveProject, session } from "$lib/stores/session";
	import { token } from "$lib/stores/token";
	import { isMac } from "$lib/utils/platform";
	import { HfInference } from "@huggingface/inference";
	import { onDestroy } from "svelte";
	import IconCode from "../Icons/IconCode.svelte";
	import IconCompare from "../Icons/IconCompare.svelte";
	import IconDelete from "../Icons/IconDelete.svelte";
	import IconInfo from "../Icons/IconInfo.svelte";
	import IconThrashcan from "../Icons/IconThrashcan.svelte";
	import PlaygroundConversation from "./InferencePlaygroundConversation.svelte";
	import PlaygroundConversationHeader from "./InferencePlaygroundConversationHeader.svelte";
	import GenerationConfig from "./InferencePlaygroundGenerationConfig.svelte";
	import HFTokenModal from "./InferencePlaygroundHFTokenModal.svelte";
	import ModelSelector from "./InferencePlaygroundModelSelector.svelte";
	import ModelSelectorModal from "./InferencePlaygroundModelSelectorModal.svelte";
	import IconExternal from "~icons/carbon/arrow-up-right";
	import InferencePlaygroundProjectSelect from "./InferencePlaygroundProjectSelect.svelte";

	const startMessageUser: ConversationMessage = { role: "user", content: "" };

	$: project = getActiveProject($session);
	project = getActiveProject($session); // needed, otherwise its undefined on startup (not sure why).

	let viewCode = false;
	let viewSettings = false;
	let loading = false;
	let abortControllers: AbortController[] = [];
	let waitForNonStreaming = true;
	let selectCompareModelOpen = false;

	interface GenerationStatistics {
		latency: number;
		generatedTokensCount: number;
	}
	let generationStats = project.conversations.map(_ => ({ latency: 0, generatedTokensCount: 0 })) as
		| [GenerationStatistics]
		| [GenerationStatistics, GenerationStatistics];

	$: systemPromptSupported = project.conversations.some(conversation => isSystemPromptSupported(conversation.model));
	$: compareActive = project.conversations.length === 2;

	function addMessage(conversationIdx: number) {
		const conversation = project.conversations[conversationIdx];
		if (!conversation) return;
		const msgs = conversation.messages.slice();
		conversation.messages = [
			...msgs,
			{
				role: msgs.at(-1)?.role === "user" ? "assistant" : "user",
				content: "",
			},
		];
		$session = $session;
	}

	function deleteMessage(conversationIdx: number, idx: number) {
		project.conversations[conversationIdx]?.messages.splice(idx, 1)[0];
		$session = $session;
	}

	function reset() {
		project.conversations.map(conversation => {
			conversation.systemMessage.content = "";
			conversation.messages = [{ ...startMessageUser }];
		});
		$session = $session;
	}

	function abort() {
		if (abortControllers.length) {
			for (const abortController of abortControllers) {
				abortController.abort();
			}
			abortControllers = [];
		}
		loading = false;
		waitForNonStreaming = false;
	}

	async function runInference(conversation: Conversation, conversationIdx: number) {
		const startTime = performance.now();
		const hf = new HfInference($token.value);

		if (conversation.streaming) {
			let addStreamingMessage = true;
			const streamingMessage = { role: "assistant", content: "" };
			const abortController = new AbortController();
			abortControllers.push(abortController);

			await handleStreamingResponse(
				hf,
				conversation,
				content => {
					if (streamingMessage) {
						streamingMessage.content = content;
						if (addStreamingMessage) {
							conversation.messages = [...conversation.messages, streamingMessage];
							addStreamingMessage = false;
						}
						$session = $session;
						const c = generationStats[conversationIdx];
						if (c) c.generatedTokensCount += 1;
					}
				},
				abortController
			);
		} else {
			waitForNonStreaming = true;
			const { message: newMessage, completion_tokens: newTokensCount } = await handleNonStreamingResponse(
				hf,
				conversation
			);
			// check if the user did not abort the request
			if (waitForNonStreaming) {
				conversation.messages = [...conversation.messages, newMessage];
				const c = generationStats[conversationIdx];
				if (c) c.generatedTokensCount += newTokensCount;
			}
		}

		const endTime = performance.now();
		const c = generationStats[conversationIdx];
		if (c) c.latency = Math.round(endTime - startTime);
	}

	async function submit() {
		if (!$token.value) {
			$token.showModal = true;
			return;
		}

		for (const [idx, conversation] of project.conversations.entries()) {
			if (conversation.messages.at(-1)?.role === "assistant") {
				let prefix = "";
				if (project.conversations.length === 2) {
					prefix = `Error on ${idx === 0 ? "left" : "right"} conversation. `;
				}
				return alert(`${prefix}Messages must alternate between user/assistant roles.`);
			}
		}

		(document.activeElement as HTMLElement).blur();
		loading = true;

		try {
			const promises = project.conversations.map((conversation, idx) => runInference(conversation, idx));
			await Promise.all(promises);
		} catch (error) {
			for (const conversation of project.conversations) {
				if (conversation.messages.at(-1)?.role === "assistant" && !conversation.messages.at(-1)?.content?.trim()) {
					conversation.messages.pop();
					conversation.messages = [...conversation.messages];
				}
				$session = $session;
			}
			if (error instanceof Error) {
				if (error.message.includes("token seems invalid")) {
					token.reset();
				}
				if (error.name !== "AbortError") {
					alert("error: " + error.message);
				}
			} else {
				alert("An unknown error occurred");
			}
		} finally {
			loading = false;
			abortControllers = [];
		}
	}

	function onKeydown(event: KeyboardEvent) {
		if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
			submit();
		}
	}

	function handleTokenSubmit(e: Event) {
		const form = e.target as HTMLFormElement;
		const formData = new FormData(form);
		const submittedHfToken = (formData.get("hf-token") as string).trim() ?? "";
		const RE_HF_TOKEN = /\bhf_[a-zA-Z0-9]{34}\b/;
		if (RE_HF_TOKEN.test(submittedHfToken)) {
			token.setValue(submittedHfToken);
			submit();
		} else {
			alert("Please provide a valid HF token.");
		}
	}

	function addCompareModel(modelId: ModelWithTokenizer["id"]) {
		const model = $models.find(m => m.id === modelId);
		if (!model || project.conversations.length === 2) {
			return;
		}
		const newConversation = { ...JSON.parse(JSON.stringify(project.conversations[0])), model };
		project.conversations = [...project.conversations, newConversation];
		generationStats = [generationStats[0], { latency: 0, generatedTokensCount: 0 }];
	}

	function removeCompareModal(conversationIdx: number) {
		project.conversations.splice(conversationIdx, 1)[0];
		$session = $session;
		generationStats.splice(conversationIdx, 1)[0];
		generationStats = generationStats;
	}

	onDestroy(() => {
		for (const abortController of abortControllers) {
			abortController.abort();
		}
	});
</script>

{#if $token.showModal}
	<HFTokenModal
		bind:storeLocallyHfToken={$token.writeToLocalStorage}
		on:close={() => ($token.showModal = false)}
		on:submit={handleTokenSubmit}
	/>
{/if}

<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
	class="motion-safe:animate-fade-in grid h-dvh divide-gray-200 overflow-hidden bg-gray-100/50 max-md:grid-rows-[120px_1fr] max-md:divide-y dark:divide-gray-800 dark:bg-gray-900 dark:text-gray-300 dark:[color-scheme:dark] {compareActive
		? 'md:grid-cols-[clamp(220px,20%,350px)_minmax(0,1fr)]'
		: 'md:grid-cols-[clamp(220px,20%,350px)_minmax(0,1fr)_clamp(270px,25%,300px)]'}"
>
	<div class="flex flex-col gap-2 overflow-y-auto py-3 pr-3 max-md:pl-3">
		<div class="pl-2">
			<InferencePlaygroundProjectSelect />
		</div>
		<div
			class="relative flex flex-1 flex-col gap-6 overflow-y-hidden rounded-r-xl border-x border-y border-gray-200/80 bg-linear-to-b from-white via-white p-3 shadow-xs max-md:rounded-xl dark:border-white/5 dark:from-gray-800/40 dark:via-gray-800/40"
			class:pointer-events-none={!systemPromptSupported}
			class:opacity-70={!systemPromptSupported}
		>
			<div class="pb-2 text-sm font-semibold uppercase">system</div>
			<textarea
				name=""
				id=""
				placeholder={systemPromptSupported
					? "Enter a custom prompt"
					: "System prompt is not supported with the chosen model."}
				value={systemPromptSupported ? project.conversations[0].systemMessage.content : ""}
				on:input={e => {
					for (const conversation of project.conversations) {
						conversation.systemMessage.content = e.currentTarget.value;
					}
					$session = $session;
				}}
				class="absolute inset-x-0 bottom-0 h-full resize-none bg-transparent px-3 pt-10 text-sm outline-hidden"
			></textarea>
		</div>
	</div>
	<div class="relative divide-y divide-gray-200 dark:divide-gray-800" on:keydown={onKeydown}>
		<div
			class="flex h-[calc(100dvh-5rem-120px)] divide-x divide-gray-200 overflow-x-auto overflow-y-hidden *:w-full max-sm:w-dvw md:h-[calc(100dvh-5rem)] md:pt-3 dark:divide-gray-800"
		>
			{#each project.conversations as conversation, conversationIdx}
				<div class="max-sm:min-w-full">
					{#if compareActive}
						<PlaygroundConversationHeader
							{conversationIdx}
							bind:conversation
							on:close={() => removeCompareModal(conversationIdx)}
						/>
					{/if}
					<PlaygroundConversation
						{loading}
						{conversation}
						{viewCode}
						{compareActive}
						on:addMessage={() => addMessage(conversationIdx)}
						on:deleteMessage={e => deleteMessage(conversationIdx, e.detail)}
						on:closeCode={() => (viewCode = false)}
					/>
				</div>
			{/each}
		</div>
		<div
			class="fixed inset-x-0 bottom-0 flex h-20 items-center justify-center gap-2 overflow-hidden px-3 whitespace-nowrap md:absolute"
		>
			<div class="flex flex-1 justify-start gap-x-2">
				{#if !compareActive}
					<button
						type="button"
						on:click={() => (viewSettings = !viewSettings)}
						class="flex h-[39px] items-center gap-1 rounded-lg border border-gray-200 bg-white px-3 py-2.5 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100 focus:outline-hidden md:hidden 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"
					>
						<IconThrashcan classNames="text-black dark:text-white" />
						{!viewSettings ? "Settings" : "Hide Settings"}
					</button>
				{/if}
				<button type="button" on:click={reset} class="btn size-[39px]">
					<IconDelete />
				</button>
			</div>
			<div class="flex flex-1 shrink-0 items-center justify-center gap-x-8 text-center text-sm text-gray-500">
				{#each generationStats as { latency, generatedTokensCount }}
					<span class="max-xl:hidden">{generatedTokensCount} tokens · Latency {latency}ms</span>
				{/each}
			</div>
			<div class="flex flex-1 justify-end gap-x-2">
				<button type="button" on:click={() => (viewCode = !viewCode)} class="btn">
					<IconCode />
					{!viewCode ? "View Code" : "Hide Code"}</button
				>
				<button
					on:click={() => {
						viewCode = false;
						loading ? abort() : submit();
					}}
					type="button"
					class="flex h-[39px] w-24 items-center justify-center gap-2 rounded-lg px-5 py-2.5 text-sm font-medium text-white focus:ring-4 focus:ring-gray-300 focus:outline-hidden dark:border-gray-700 dark:focus:ring-gray-700 {loading
						? 'bg-red-900 hover:bg-red-800 dark:bg-red-600 dark:hover:bg-red-700'
						: 'bg-black hover:bg-gray-900 dark:bg-blue-600 dark:hover:bg-blue-700'}"
				>
					{#if loading}
						<div class="flex flex-none items-center gap-[3px]">
							<span class="mr-2">
								{#if project.conversations[0].streaming || project.conversations[1]?.streaming}
									Stop
								{:else}
									Cancel
								{/if}
							</span>
							<div
								class="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"
								style="animation-delay: 0.25s;"
							/>
							<div
								class="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"
								style="animation-delay: 0.5s;"
							/>
							<div
								class="h-1 w-1 flex-none animate-bounce rounded-full bg-gray-500 dark:bg-gray-100"
								style="animation-delay: 0.75s;"
							/>
						</div>
					{:else}
						Run <span
							class="inline-flex gap-0.5 rounded-sm border border-white/20 bg-white/10 px-0.5 text-xs text-white/70"
							>{isMac() ? "" : "Ctrl"}<span class="translate-y-px">↵</span></span
						>
					{/if}
				</button>
			</div>
		</div>
	</div>
	{#if !compareActive}
		<div class="flex flex-col p-3 {viewSettings ? 'max-md:fixed' : 'max-md:hidden'} max-md:inset-x-0 max-md:bottom-20">
			<div
				class="flex flex-1 flex-col gap-6 overflow-y-hidden rounded-xl border border-gray-200/80 bg-white bg-linear-to-b from-white via-white p-3 shadow-xs dark:border-white/5 dark:bg-gray-900 dark:from-gray-800/40 dark:via-gray-800/40"
			>
				<div class="flex flex-col gap-2">
					<ModelSelector bind:conversation={project.conversations[0]} />
					<div class="flex items-center gap-2 self-end px-2 text-xs whitespace-nowrap">
						<button
							class="flex items-center gap-0.5 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
							on:click={() => (selectCompareModelOpen = true)}
						>
							<IconCompare />
							Compare
						</button>
						<a
							href="https://huggingface.co/{project.conversations[0].model.id}?inference_provider={project
								.conversations[0].provider}"
							target="_blank"
							class="flex items-center gap-0.5 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
						>
							<IconExternal class="text-2xs" />
							Model page
						</a>
					</div>
				</div>

				<GenerationConfig bind:conversation={project.conversations[0]} />
				{#if $token.value}
					<button
						on:click={token.reset}
						class="mt-auto flex items-center gap-1 self-end text-sm text-gray-500 underline decoration-gray-300 hover:text-gray-800 dark:text-gray-400 dark:decoration-gray-600 dark:hover:text-gray-200"
						><svg xmlns="http://www.w3.org/2000/svg" class="text-xs" width="1em" height="1em" viewBox="0 0 32 32"
							><path
								fill="currentColor"
								d="M23.216 4H26V2h-7v6h2V5.096A11.96 11.96 0 0 1 28 16c0 6.617-5.383 12-12 12v2c7.72 0 14-6.28 14-14c0-5.009-2.632-9.512-6.784-12"
							/><path fill="currentColor" d="M16 20a1.5 1.5 0 1 0 0 3a1.5 1.5 0 0 0 0-3M15 9h2v9h-2z" /><path
								fill="currentColor"
								d="M16 4V2C8.28 2 2 8.28 2 16c0 4.977 2.607 9.494 6.784 12H6v2h7v-6h-2v2.903A11.97 11.97 0 0 1 4 16C4 9.383 9.383 4 16 4"
							/></svg
						>
						Reset token</button
					>
				{/if}
				<div class="mt-auto hidden">
					<div class="mb-3 flex items-center justify-between gap-2">
						<label for="default-range" class="block text-sm font-medium text-gray-900 dark:text-white">API Quota</label>
						<span
							class="rounded-sm bg-gray-100 px-1.5 py-0.5 text-xs font-medium text-gray-800 dark:bg-gray-700 dark:text-gray-300"
							>Free</span
						>

						<div class="ml-auto w-12 text-right text-sm">76%</div>
					</div>
					<div class="h-2 w-full rounded-full bg-gray-200 dark:bg-gray-700">
						<div class="h-2 rounded-full bg-black dark:bg-gray-400" style="width: 75%"></div>
					</div>
				</div>
			</div>
		</div>
	{/if}
</div>

<div class="absolute bottom-6 left-4 flex items-center gap-2 max-md:hidden">
	<a
		target="_blank"
		href="https://huggingface.co/docs/api-inference/tasks/chat-completion"
		class="flex items-center gap-1 text-sm text-gray-500 underline decoration-gray-300 hover:text-gray-800 dark:text-gray-400 dark:decoration-gray-600 dark:hover:text-gray-200"
	>
		<IconInfo classNames="text-xs" />
		View Docs
	</a>
	<span class="dark:text-gray-500">·</span>
	<a
		target="_blank"
		href="https://huggingface.co/spaces/huggingface/inference-playground/discussions/1"
		class="flex items-center gap-1 text-sm text-gray-500 underline decoration-gray-300 hover:text-gray-800 dark:text-gray-400 dark:decoration-gray-600 dark:hover:text-gray-200"
	>
		Give feedback
	</a>
</div>

{#if selectCompareModelOpen}
	<ModelSelectorModal
		conversation={project.conversations[0]}
		on:modelSelected={e => addCompareModel(e.detail)}
		on:close={() => (selectCompareModelOpen = false)}
	/>
{/if}