Spaces:
Runtime error
Runtime error
snippets "showToken" feature
Browse filesFiles changed (3)
195 unmodified lines196197198199200201195 unmodified lines{conversation}index={0}{viewCode}on:addMessage={addMessage}on:deleteMessage={e => deleteMessage(e.detail)}/>195 unmodified lines196197198199200201202195 unmodified lines{conversation}index={0}{viewCode}{hfToken}on:addMessage={addMessage}on:deleteMessage={e => deleteMessage(e.detail)}/>
14 unmodified lines1516171819207 unmodified lines2829303132332 unmodified lines363738394041424344454647484920 unmodified lines7071727374757617 unmodified lines94959697989910010110213 unmodified lines1161171181191201211221231248 unmodified lines13313413513613713813917 unmodified lines15715815916016116216316416511 unmodified lines17717817918018118218318418518610 unmodified lines19719819920020120220313 unmodified lines2172182192202212222232246 unmodified lines23123223323423523623723832 unmodified lines27127227327427527627727827911 unmodified lines29129229329429529614 unmodified lineshljs.registerLanguage("http", http);export let conversation: Conversation;const lanuages = ["javascript", "python", "http"];type Language = (typeof lanuages)[number];7 unmodified lineslabel: string;code: string;language?: Language;}interface MessagesJoiner {2 unmodified linesend: string;}$: snippetsByLanguage = {javascript: getJavascriptSnippets(conversation),python: getPythonSnippets(conversation),http: getHttpSnippets(conversation),};let selectedLanguage: Language = "javascript";let timeout: ReturnType<typeof setTimeout>;function getMessages() {const placeholder = [{ role: "user", content: "Tell me a story" }];20 unmodified linesreturn hljs.highlight(code, { language }).value;}function getJavascriptSnippets(conversation: Conversation) {const formattedMessages = ({ sep, start, end }: MessagesJoiner) =>start +getMessages()17 unmodified linesif (conversation.streaming) {snippets.push({label: "Streaming API",code: `import { HfInference } from "@huggingface/inference"const inference = new HfInference("YOUR_HF_TOKEN")let out = "";13 unmodified lines// non-streamingsnippets.push({label: "Non-Streaming API",code: `import { HfInference } from '@huggingface/inference'const inference = new HfInference("YOUR_HF_TOKEN")const out = await inference.chatCompletion({model: "${conversation.model.id}",8 unmodified linesreturn snippets;}function getPythonSnippets(conversation: Conversation) {const formattedMessages = ({ sep, start, end }: MessagesJoiner) =>start +getMessages()17 unmodified linesif (conversation.streaming) {snippets.push({label: "Streaming API",code: `from huggingface_hub import InferenceClientclient = InferenceClient(api_key="YOUR_HF_TOKEN")messages = ${formattedMessages({ sep: ",\n\t", start: `[\n\t`, end: `\n]` })}11 unmodified lines// non-streamingsnippets.push({label: "Non-Streaming API",code: `from huggingface_hub import InferenceClientmodel_id="${conversation.model.id}"client = InferenceClient(api_key="YOUR_HF_TOKEN")messages = ${formattedMessages({ sep: ",\n\t", start: `[\n\t`, end: `\n]` })}10 unmodified linesreturn snippets;}function getHttpSnippets(conversation: Conversation) {const formattedMessages = ({ sep, start, end }: MessagesJoiner) =>start +getMessages()13 unmodified linesif (conversation.streaming) {snippets.push({label: "Streaming API",code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\--header "Authorization: Bearer {YOUR_HF_TOKEN}" \\--header 'Content-Type: application/json' \\--data '{"model": "${conversation.model.id}",6 unmodified lines// non-streamingsnippets.push({label: "Non-Streaming API",code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\--header "Authorization: Bearer {YOUR_HF_TOKEN}" \\--header 'Content-Type: application/json' \\--data '{"model": "${conversation.model.id}",32 unmodified lines</ul></div>{#each snippetsByLanguage[selectedLanguage] as { label, code, language }}<div class="flex items-center justify-between px-2 md:px-4 pb-4 pt-6"><h2 class="font-semibold">{label}</h2><buttonclass="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><preclass="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,14 unmodified lines151617181920217 unmodified lines293031323334352 unmodified lines38394041424344454647484950515253545556575859606120 unmodified lines8283848586878817 unmodified lines10610710810911011111211311411513 unmodified lines1291301311321331341351361371388 unmodified lines14714814915015115215317 unmodified lines17117217317417517617717817918011 unmodified lines19219319419519619719819920020120210 unmodified lines21321421521621721821922022122213 unmodified lines2362372382392402412422432446 unmodified lines25125225325425525625725825932 unmodified lines29229329429529629729829930030130230330430530630711 unmodified lines31932032132232332432514 unmodified lineshljs.registerLanguage("http", http);export let conversation: Conversation;export let hfToken: string;const lanuages = ["javascript", "python", "http"];type Language = (typeof lanuages)[number];7 unmodified lineslabel: string;code: string;language?: Language;needsToken?: boolean;}interface MessagesJoiner {2 unmodified linesend: string;}let selectedLanguage: Language = "javascript";let timeout: ReturnType<typeof setTimeout>;let showToken = false;$: tokenStr = getTokenStr(showToken);$: snippetsByLanguage = {javascript: getJavascriptSnippets(conversation, tokenStr),python: getPythonSnippets(conversation, tokenStr),http: getHttpSnippets(conversation, tokenStr),};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 linesreturn hljs.highlight(code, { language }).value;}function getJavascriptSnippets(conversation: Conversation, tokenStr: string) {const formattedMessages = ({ sep, start, end }: MessagesJoiner) =>start +getMessages()17 unmodified linesif (conversation.streaming) {snippets.push({label: "Streaming API",needsToken: true,code: `import { HfInference } from "@huggingface/inference"const inference = new HfInference("${tokenStr}")let out = "";13 unmodified lines// non-streamingsnippets.push({label: "Non-Streaming API",needsToken: true,code: `import { HfInference } from '@huggingface/inference'const inference = new HfInference("${tokenStr}")const out = await inference.chatCompletion({model: "${conversation.model.id}",8 unmodified linesreturn snippets;}function getPythonSnippets(conversation: Conversation, tokenStr: string) {const formattedMessages = ({ sep, start, end }: MessagesJoiner) =>start +getMessages()17 unmodified linesif (conversation.streaming) {snippets.push({label: "Streaming API",needsToken: true,code: `from huggingface_hub import InferenceClientclient = InferenceClient(api_key="${tokenStr}")messages = ${formattedMessages({ sep: ",\n\t", start: `[\n\t`, end: `\n]` })}11 unmodified lines// non-streamingsnippets.push({label: "Non-Streaming API",needsToken: true,code: `from huggingface_hub import InferenceClientmodel_id="${conversation.model.id}"client = InferenceClient(api_key="${tokenStr}")messages = ${formattedMessages({ sep: ",\n\t", start: `[\n\t`, end: `\n]` })}10 unmodified linesreturn snippets;}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 linesif (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 ${tokenStr}" \\--header 'Content-Type: application/json' \\--data '{"model": "${conversation.model.id}",6 unmodified lines// non-streamingsnippets.push({label: "Non-Streaming API",needsToken: true,code: `curl 'https://api-inference.huggingface.co/models/${conversation.model.id}/v1/chat/completions' \\--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, 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}<buttonclass="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><preclass="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 lines10111213141541 unmodified lines5758596061629 unmodified linesexport let conversation: Conversation;export let loading: boolean;export let viewCode: boolean;const dispatch = createEventDispatcher<{addMessage: void;41 unmodified lines</div></button>{:else}<CodeSnippets {conversation} />{/if}</div>9 unmodified lines1011121314151641 unmodified lines5859606162639 unmodified linesexport 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} {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