mishig HF staff commited on
Commit
c02e352
1 Parent(s): 79d3242

Feature: snippets tab for python & curl working (#17)

Browse files
Files changed (1) hide show
  1. src/lib/components/CodeSnippets.svelte +124 -20
src/lib/components/CodeSnippets.svelte CHANGED
@@ -5,19 +5,34 @@
5
  import bash from 'highlight.js/lib/languages/bash';
6
  import type { Conversation } from '$lib/types';
7
 
 
 
 
 
8
  export let conversation: Conversation;
9
 
10
- type Langauge = 'javascript' | 'python' | 'bash';
 
 
 
 
 
 
 
11
  interface Snippet {
12
  label: string;
13
  code: string;
14
  }
15
 
16
- hljs.registerLanguage('javascript', javascript);
17
- hljs.registerLanguage('python', python);
18
- hljs.registerLanguage('bash', bash);
 
 
 
 
19
 
20
- function highlight(code: string, language: Langauge) {
21
  return hljs.highlight(code, { language }).value;
22
  }
23
 
@@ -34,6 +49,98 @@ const hf = new HfInference('your access token')`
34
  label: 'Streaming API',
35
  code: `let out = "";
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  for await (const chunk of hf.chatCompletionStream({
38
  model: "${conversation.model}",
39
  messages: [
@@ -73,24 +180,21 @@ for await (const chunk of hf.chatCompletionStream({
73
  class="border-b border-gray-200 text-center text-sm font-medium text-gray-500 dark:border-gray-700 dark:text-gray-400"
74
  >
75
  <ul class="-mb-px flex flex-wrap">
76
- <li>
77
- <a
78
- href="#"
79
- class="active inline-block rounded-t-lg border-b-2 border-black p-4 text-black dark:border-blue-500 dark:text-blue-500"
80
- aria-current="page">Huggingface.js</a
81
- >
82
- </li>
83
- <li>
84
- <a
85
- href="#"
86
- class="inline-block rounded-t-lg border-b-2 border-transparent p-4 hover:border-gray-300 hover:text-gray-600 dark:hover:text-gray-300"
87
- >Curl</a
88
- >
89
- </li>
90
  </ul>
91
  </div>
92
 
93
- {#each getJavascriptSnippets() as { label, code }}
94
  <div class="px-4 pb-4 pt-6">
95
  <h2 class="font-semibold">{label}</h2>
96
  </div>
 
5
  import bash from 'highlight.js/lib/languages/bash';
6
  import type { Conversation } from '$lib/types';
7
 
8
+ hljs.registerLanguage('javascript', javascript);
9
+ hljs.registerLanguage('python', python);
10
+ hljs.registerLanguage('bash', bash);
11
+
12
  export let conversation: Conversation;
13
 
14
+ const lanuages = ['javascript', 'python', 'bash'];
15
+ type Language = (typeof lanuages)[number];
16
+ const labelsByLanguage: Record<Language, string> = {
17
+ javascript: 'JavaScript',
18
+ python: 'Python',
19
+ bash: 'Curl'
20
+ };
21
+
22
  interface Snippet {
23
  label: string;
24
  code: string;
25
  }
26
 
27
+ const snippetsByLanguage: Record<Language, Snippet[]> = {
28
+ javascript: getJavascriptSnippets(),
29
+ python: getPythonSnippets(),
30
+ bash: getBashSnippets()
31
+ };
32
+
33
+ let selectedLanguage: Language = 'javascript';
34
 
35
+ function highlight(code: string, language: Language) {
36
  return hljs.highlight(code, { language }).value;
37
  }
38
 
 
49
  label: 'Streaming API',
50
  code: `let out = "";
51
 
52
+ for await (const chunk of hf.chatCompletionStream({
53
+ model: "${conversation.model}",
54
+ messages: [
55
+ { role: "user", content: "Complete the equation 1+1= ,just the answer" },
56
+ ],
57
+ max_tokens: ${conversation.config.maxTokens},
58
+ temperature: ${conversation.config.temperature},
59
+ seed: 0,
60
+ })) {
61
+ if (chunk.choices && chunk.choices.length > 0) {
62
+ out += chunk.choices[0].delta.content;
63
+ }
64
+ }`
65
+ });
66
+ } else {
67
+ // non-streaming
68
+ snippets.push({
69
+ label: 'Non-Streaming API',
70
+ code: `await hf.chatCompletion({
71
+ model: "${conversation.model}",
72
+ messages: [
73
+ { role: "user", content: "Complete the this sentence with words one plus one is equal " }
74
+ ],
75
+ max_tokens: ${conversation.config.maxTokens},
76
+ temperature: ${conversation.config.temperature},
77
+ seed: 0,
78
+ });`
79
+ });
80
+ }
81
+
82
+ return snippets;
83
+ }
84
+
85
+ function getPythonSnippets() {
86
+ const snippets: Snippet[] = [];
87
+ snippets.push({
88
+ label: 'Install',
89
+ code: `import { HfInference } from '@huggingface/inference'
90
+
91
+ const hf = new HfInference('your access token')`
92
+ });
93
+ if (conversation.config.streaming) {
94
+ snippets.push({
95
+ label: 'Streaming API',
96
+ code: `let out = "";
97
+
98
+ for await (const chunk of hf.chatCompletionStream({
99
+ model: "${conversation.model}",
100
+ messages: [
101
+ { role: "user", content: "Complete the equation 1+1= ,just the answer" },
102
+ ],
103
+ max_tokens: ${conversation.config.maxTokens},
104
+ temperature: ${conversation.config.temperature},
105
+ seed: 0,
106
+ })) {
107
+ if (chunk.choices && chunk.choices.length > 0) {
108
+ out += chunk.choices[0].delta.content;
109
+ }
110
+ }`
111
+ });
112
+ } else {
113
+ // non-streaming
114
+ snippets.push({
115
+ label: 'Non-Streaming API',
116
+ code: `await hf.chatCompletion({
117
+ model: "${conversation.model}",
118
+ messages: [
119
+ { role: "user", content: "Complete the this sentence with words one plus one is equal " }
120
+ ],
121
+ max_tokens: ${conversation.config.maxTokens},
122
+ temperature: ${conversation.config.temperature},
123
+ seed: 0,
124
+ });`
125
+ });
126
+ }
127
+
128
+ return snippets;
129
+ }
130
+
131
+ function getBashSnippets() {
132
+ const snippets: Snippet[] = [];
133
+ snippets.push({
134
+ label: 'Install',
135
+ code: `import { HfInference } from '@huggingface/inference'
136
+
137
+ const hf = new HfInference('your access token')`
138
+ });
139
+ if (conversation.config.streaming) {
140
+ snippets.push({
141
+ label: 'Streaming API',
142
+ code: `let out = "";
143
+
144
  for await (const chunk of hf.chatCompletionStream({
145
  model: "${conversation.model}",
146
  messages: [
 
180
  class="border-b border-gray-200 text-center text-sm font-medium text-gray-500 dark:border-gray-700 dark:text-gray-400"
181
  >
182
  <ul class="-mb-px flex flex-wrap">
183
+ {#each Object.entries(labelsByLanguage) as [language, label]}
184
+ <li>
185
+ <button
186
+ on:click={() => (selectedLanguage = language)}
187
+ class="inline-block rounded-t-lg border-b-2 p-4 {language === selectedLanguage
188
+ ? 'border-black text-black dark:border-blue-500 dark:text-blue-500'
189
+ : 'border-transparent hover:border-gray-300 hover:text-gray-600 dark:hover:text-gray-300'}"
190
+ aria-current="page">{label}</button
191
+ >
192
+ </li>
193
+ {/each}
 
 
 
194
  </ul>
195
  </div>
196
 
197
+ {#each snippetsByLanguage[selectedLanguage] as { label, code }}
198
  <div class="px-4 pb-4 pt-6">
199
  <h2 class="font-semibold">{label}</h2>
200
  </div>