Spaces:
Runtime error
Runtime error
snippets "showToken" feature
Browse filesFiles changed (3)
195 unmodified lines196197198199200201202195 unmodified lines {conversation} index={0} {viewCode} {hfToken} on:addMessage={addMessage} on:deleteMessage={e => deleteMessage(e.detail)} />14 unmodified lines151617181920217 unmodified lines293031323334352 unmodified lines383940414243444546474041424849505152454653545556575859606120 unmodified lines828384738586878817 unmodified lines1061071081091101119911211311411513 unmodified lines1291301311321331341211351361371388 unmodified lines14714814913615015115215317 unmodified lines17117217317417517616217717817918011 unmodified lines19219319419519619719818319920020120210 unmodified lines21321421520021621721821922022122213 unmodified lines2362372382392402212412422432446 unmodified lines25125225325425523525625725825932 unmodified lines29229329427429529629729829930030130230330430530630711 unmodified lines31932032132232332432514 unmodified lines hljs.registerLanguage("http", http);
export let conversation: Conversation; export let hfToken: string;
const lanuages = ["javascript", "python", "http"]; type Language = (typeof lanuages)[number];7 unmodified lines label: string; code: string; language?: Language; needsToken?: boolean; }
interface MessagesJoiner {2 unmodified lines end: string; }
let selectedLanguage: Language = "javascript"; let timeout: ReturnType<typeof setTimeout>; let showToken = false;
$: tokenStr = getTokenStr(showToken);
$: snippetsByLanguage = { javascript: getJavascriptSnippets(conversation), python: getPythonSnippets(conversation), http: getHttpSnippets(conversation), javascript: getJavascriptSnippets(conversation, tokenStr), python: getPythonSnippets(conversation, tokenStr), http: getHttpSnippets(conversation, tokenStr), };
let selectedLanguage: Language = "javascript"; let timeout: ReturnType<typeof setTimeout>; function getTokenStr(showToken: boolean){ if(hfToken && showToken){ return hfToken; } return "YOUR_HF_TOKEN"; }
function getMessages() { const placeholder = [{ role: "user", content: "Tell me a story" }];20 unmodified lines return hljs.highlight(code, { language }).value; }
function getJavascriptSnippets(conversation: Conversation) { function getJavascriptSnippets(conversation: Conversation, tokenStr: string) { const formattedMessages = ({ sep, start, end }: MessagesJoiner) => start + getMessages()17 unmodified lines if (conversation.streaming) { snippets.push({ label: "Streaming API", needsToken: true, code: `import { HfInference } from "@huggingface/inference"
const inference = new HfInference("YOUR_HF_TOKEN")const inference = new HfInference("${tokenStr}")
let out = "";
13 unmodified lines // non-streaming snippets.push({ label: "Non-Streaming API", needsToken: true, code: `import { HfInference } from '@huggingface/inference'
const inference = new HfInference("YOUR_HF_TOKEN")const inference = new HfInference("${tokenStr}")
const out = await inference.chatCompletion({ model: "${conversation.model.id}",8 unmodified lines return snippets; }
function getPythonSnippets(conversation: Conversation) { function getPythonSnippets(conversation: Conversation, tokenStr: string) { const formattedMessages = ({ sep, start, end }: MessagesJoiner) => start + getMessages()17 unmodified lines if (conversation.streaming) { snippets.push({ label: "Streaming API", needsToken: true, code: `from huggingface_hub import InferenceClient
client = InferenceClient(api_key="YOUR_HF_TOKEN")client = InferenceClient(api_key="${tokenStr}")
messages = ${formattedMessages({ sep: ",\n\t", start: `[\n\t`, end: `\n]` })}
11 unmodified lines // non-streaming snippets.push({ label: "Non-Streaming API", needsToken: true, code: `from huggingface_hub import InferenceClient
model_id="${conversation.model.id}"client = InferenceClient(api_key="YOUR_HF_TOKEN")client = InferenceClient(api_key="${tokenStr}")
messages = ${formattedMessages({ sep: ",\n\t", start: `[\n\t`, end: `\n]` })}
10 unmodified lines return snippets; }
function getHttpSnippets(conversation: Conversation) { function getHttpSnippets(conversation: Conversation, tokenStr: string) { if(tokenStr === "YOUR_HF_TOKEN"){ tokenStr = "{YOUR_HF_TOKEN}"; } const formattedMessages = ({ sep, start, end }: MessagesJoiner) => start + getMessages()13 unmodified lines if (conversation.streaming) { snippets.push({ label: "Streaming API", needsToken: true, code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\--header "Authorization: Bearer {YOUR_HF_TOKEN}" \\--header "Authorization: Bearer ${tokenStr}" \\--header 'Content-Type: application/json' \\--data '{ "model": "${conversation.model.id}",6 unmodified lines // non-streaming snippets.push({ label: "Non-Streaming API", needsToken: true, code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\--header "Authorization: Bearer {YOUR_HF_TOKEN}" \\--header "Authorization: Bearer ${tokenStr}" \\--header 'Content-Type: application/json' \\--data '{ "model": "${conversation.model.id}",32 unmodified lines </ul> </div>
{#each snippetsByLanguage[selectedLanguage] as { label, code, language }} {#each snippetsByLanguage[selectedLanguage] as { label, code, language, needsToken }} <div class="flex items-center justify-between px-2 md:px-4 pb-4 pt-6"> <h2 class="font-semibold">{label}</h2> <div class="flex items-center gap-x-4"> {#if needsToken && hfToken} <label class="flex items-center gap-x-1.5"> <input type="checkbox" bind:checked={showToken}> <p class="leading-none">show token</p> </label> {/if} <button class="flex items-center gap-x-1.5 rounded-md bg-gray-200 px-1.5 py-0.5 text-sm transition dark:bg-gray-950/80" on:click={e => {11 unmodified lines <IconCopyCode /> Copy code </button> </div> </div> <pre class="overflow-x-auto rounded-lg border border-gray-200/80 bg-white px-4 py-6 text-sm shadow-sm dark:border-gray-800 dark:bg-gray-800/50">{@html highlight( code,9 unmodified lines1011121314151641 unmodified lines585960606162639 unmodified lines export let conversation: Conversation; export let loading: boolean; export let viewCode: boolean; export let hfToken: string;
const dispatch = createEventDispatcher<{ addMessage: void;41 unmodified lines </div> </button> {:else} <CodeSnippets {conversation} /> <CodeSnippets {conversation} {hfToken} /> {/if}</div>src/lib/components/InferencePlayground/InferencePlayground.svelte+1
src/lib/components/InferencePlayground/InferencePlaygroundCodeSnippets.svelte+44-15
src/lib/components/InferencePlayground/InferencePlaygroundConversation.svelte+2-1