Spaces:
Runtime error
Runtime error
More refactor (#41)
Browse filesFiles changed (11)
- src/lib/components/InferencePlayground/InferencePlayground.svelte+10-8
- src/lib/components/InferencePlayground/InferencePlaygroundCodeSnippets.svelte+16-8
- src/lib/components/InferencePlayground/InferencePlaygroundConversation.svelte+5-3
- src/lib/components/InferencePlayground/InferencePlaygroundGenerationConfig.svelte+1
- src/lib/components/InferencePlayground/InferencePlaygroundHFTokenModal.svelte+5-4
- src/lib/components/InferencePlayground/InferencePlaygroundMessage.svelte+1-1
- src/lib/components/InferencePlayground/InferencePlaygroundModelSelector.svelte+3-2
- src/lib/components/InferencePlayground/InferencePlaygroundModelSelectorModal.svelte+5-3
- src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts+2-1
- src/lib/types/index.d.ts → src/lib/components/InferencePlayground/types.tsRenamed+1-1
- src/routes/+page.server.ts+2-2
12345678910111213141516171213141518192020 unmodified lines4142434243444544454683 unmodified lines130131132133134135136137138139<script lang="ts"> import type { ModelEntryWithTokenizer } from "./types"; import { type ChatCompletionInputMessage } from "@huggingface/tasks";
import { defaultGenerationConfig } from "./generationConfigSettings"; import { createHfInference, handleStreamingResponse, handleNonStreamingResponse, isSystemPromptSupported, } from "./inferencePlaygroundUtils";
import { onDestroy } from "svelte"; import GenerationConfig from "./InferencePlaygroundGenerationConfig.svelte"; import HFTokenModal from "./InferencePlaygroundHFTokenModal.svelte"; import ModelSelector from "./InferencePlaygroundModelSelector.svelte"; import Conversation from "./InferencePlaygroundConversation.svelte"; import { onDestroy } from "svelte"; import { type ChatCompletionInputMessage } from "@huggingface/tasks"; import type { ModelEntryWithTokenizer } from "$lib/types"; import { defaultGenerationConfig } from "./generationConfigSettings"; import IconShare from "../Icons/IconShare.svelte"; import IconDelete from "../Icons/IconDelete.svelte"; import IconCode from "../Icons/IconCode.svelte";20 unmodified lines
$: systemPromptSupported = isSystemPromptSupported(conversation.model);
onDestroy(() => { abortController?.abort(); });
function addMessage() { conversation.messages = [ ...conversation.messages,83 unmodified lines submit(); } }
onDestroy(() => { abortController?.abort(); });</script>
{#if showTokenModal}123456786910811121315 unmodified lines29303132333435363738394017 unmodified lines5859605361626364656667606869707152 unmodified lines12412512611912712812913013113213312613413513613746 unmodified lines184185186179187188189190191192193186194195196197<script lang="ts"> import type { Conversation } from "./types";
import { onDestroy } from "svelte"; import hljs from "highlight.js/lib/core"; import javascript from "highlight.js/lib/languages/javascript"; import python from "highlight.js/lib/languages/python"; import http from "highlight.js/lib/languages/http"; import type { Conversation } from "$lib/types";
import IconCopyCode from "../Icons/IconCopyCode.svelte"; import { onDestroy } from "svelte";
hljs.registerLanguage("javascript", javascript); hljs.registerLanguage("python", python);15 unmodified lines language?: Language; }
interface MessagesJoiner { sep: string; start: string; end: string; }
$: snippetsByLanguage = { javascript: getJavascriptSnippets(conversation), python: getPythonSnippets(conversation),17 unmodified lines }
function getJavascriptSnippets(conversation: Conversation) { const formattedMessages = ({ sep, start, end }) => const formattedMessages = ({ sep, start, end }: MessagesJoiner) => start + getMessages() .map(({ role, content }) => `{ role: "${role}", content: "${content}" }`) .join(sep) + end;
const formattedConfig = ({ sep, start, end }) => const formattedConfig = ({ sep, start, end }: MessagesJoiner) => start + Object.entries(conversation.config) .map(([key, val]) => `${key}: ${val}`)52 unmodified lines }
function getPythonSnippets(conversation: Conversation) { const formattedMessages = ({ sep, start, end }) => const formattedMessages = ({ sep, start, end }: MessagesJoiner) => start + getMessages() .map(({ role, content }) => `{ "role": "${role}", "content": "${content}" }`) .join(sep) + end;
const formattedConfig = ({ sep, start, end }) => const formattedConfig = ({ sep, start, end }: MessagesJoiner) => start + Object.entries(conversation.config) .map(([key, val]) => `${key}: ${val}`)46 unmodified lines }
function getHttpSnippets(conversation: Conversation) { const formattedMessages = ({ sep, start, end }) => const formattedMessages = ({ sep, start, end }: MessagesJoiner) => start + getMessages() .map(({ role, content }) => `{ "role": "${role}", "content": "${content}" }`) .join(sep) + end;
const formattedConfig = ({ sep, start, end }) => const formattedConfig = ({ sep, start, end }: MessagesJoiner) => start + Object.entries(conversation.config) .map(([key, val]) => `"${key}": ${val}`)1234567869810101112131415<script lang="ts"> import type { Conversation } from "$lib/types";
import { createEventDispatcher } from "svelte";
import CodeSnippets from "./InferencePlaygroundCodeSnippets.svelte"; import Message from "./InferencePlaygroundMessage.svelte"; import IconPlus from "../Icons/IconPlus.svelte"; import type { Conversation } from "$lib/types";
export let loading; export let conversation: Conversation; export let viewCode; export let loading: boolean; export let viewCode: boolean;
const dispatch = createEventDispatcher<{ addMessage: void;123456<script lang="ts"> import type { Conversation } from "$lib/types";
import { GENERATION_CONFIG_KEYS, GENERATION_CONFIG_KEYS_ADVANCED,11323456782 unmodified lines11121313141415161718<!-- Main modal --><script lang="ts"> import { createEventDispatcher, onDestroy, onMount } from "svelte"; import { browser } from "$app/environment";
import { createEventDispatcher, onDestroy, onMount } from "svelte";
import IconCross from "../Icons/IconCross.svelte";
let backdropEl: HTMLDivElement;2 unmodified lines const dispatch = createEventDispatcher<{ close: void }>();
function handleKeydown(event: KeyboardEvent) { // close on ESC if (event.key === "Escape") { const { key } = event; if (key === "Escape") { event.preventDefault(); dispatch("close"); }1223456<script lang="ts"> import { createEventDispatcher } from "svelte"; import { type ChatCompletionInputMessage } from "@huggingface/tasks"; import { createEventDispatcher } from "svelte";
export let message: ChatCompletionInputMessage; export let autofocus: boolean = false;122345614 unmodified lines2122232324252627<script lang="ts"> import type { Conversation, ModelEntryWithTokenizer } from "$lib/types"; import type { Conversation, ModelEntryWithTokenizer } from "./types";
import IconCaret from "../Icons/IconCaret.svelte"; import ModelSelectorModal from "./InferencePlaygroundModelSelectorModal.svelte";
14 unmodified lines return avatarUrl; }
function changeModel(modelId: string) { function changeModel(modelId: ModelEntryWithTokenizer["id"]) { const model = models.find(m => m.id === modelId); if (!model) { return;1223456784 unmodified lines13141514151617181920<script lang="ts"> import type { ModelEntryWithTokenizer } from "$lib/types"; import type { ModelEntryWithTokenizer } from "./types";
import { createEventDispatcher } from "svelte";
import IconSearch from "../Icons/IconSearch.svelte"; import IconStar from "../Icons/IconStar.svelte";
4 unmodified lines const dispatch = createEventDispatcher<{ modelSelected: string; close: void }>();
function handleKeydown(event: KeyboardEvent) { // close on ESC if (event.key === "Escape") { const { key } = event; if (key === "Escape") { event.preventDefault(); dispatch("close"); }12343567import { type ChatCompletionInputMessage } from "@huggingface/tasks";import type { Conversation, ModelEntryWithTokenizer } from "./types";
import { HfInference } from "@huggingface/inference";import type { Conversation, ModelEntryWithTokenizer } from "$lib/types";
export function createHfInference(token: string): HfInference { return new HfInference(token);1 unmodified line234556781 unmodified lineimport type { ModelEntry } from "@huggingface/hub";import type { ChatCompletionInputMessage } from "@huggingface/tasks";
type Conversation = {export type Conversation = { model: ModelEntryWithTokenizer; config: GenerationConfig; messages: ChatCompletionInputMessage[];1123411 unmodified lines1617181919202122import type { ModelEntryWithTokenizer } from "$lib/types";import type { ModelEntryWithTokenizer } from "$lib/components/InferencePlayground/types";import type { ModelEntry } from "@huggingface/hub";import type { PageServerLoad } from "./$types";import { env } from "$env/dynamic/private";11 unmodified lines compatibleModels.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase()));
const promises = compatibleModels.map(async model => { const configUrl = `https://huggingface.co/${model.modelId}/raw/main/tokenizer_config.json`; const configUrl = `https://huggingface.co/${model.id}/raw/main/tokenizer_config.json`; const res = await fetch(configUrl); if (!res.ok) { return null; // Ignore failed requests by returning nullsrc/lib/components/InferencePlayground/InferencePlayground.svelte+10-8
src/lib/components/InferencePlayground/InferencePlaygroundCodeSnippets.svelte+16-8
src/lib/components/InferencePlayground/InferencePlaygroundConversation.svelte+5-3
src/lib/components/InferencePlayground/InferencePlaygroundGenerationConfig.svelte+1
src/lib/components/InferencePlayground/InferencePlaygroundHFTokenModal.svelte+5-4
src/lib/components/InferencePlayground/InferencePlaygroundMessage.svelte+1-1
src/lib/components/InferencePlayground/InferencePlaygroundModelSelector.svelte+3-2
src/lib/components/InferencePlayground/InferencePlaygroundModelSelectorModal.svelte+5-3
src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts+2-1
src/lib/types/index.d.ts → src/lib/components/InferencePlayground/types.tsRenamed+1-1
src/routes/+page.server.ts+2-2