-
Notifications
You must be signed in to change notification settings - Fork 2
/
playground.html
357 lines (291 loc) · 12.4 KB
/
playground.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>LLM.js - Playground</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/water.css@2/out/light.css"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
</head>
<style>
#llm_list {
display: grid;
}
#llm_list > div {
}
</style>
<body>
<div class="container">
<h1><a href="https://rahuldshetty.github.io/llm.js/#/" target="_blank">LLM.js</a> - Playground</h1>
<p>
Run <b>Large-Language Models (LLMs)</b> 🚀 directly in your browser 🌐!
</p>
<p>
Learn More: 📜 <a href="https://rahuldshetty.github.io/llm.js/#/api_guide" target="_blank">API Reference</a>
Guidance: <a href="https://rahuldshetty.github.io/llm.js/#/guidance" target="_blank">API Reference</a>
</p>
<p>
Developed By: ✒️ <a href="https://rahuldshetty.github.io/" target="_blank">RDS </a>
</p>
<h2>Demo</h2>
<p>
This web demo enables you to run LLM models from Hugging Face (GGUF based) directly in your browser.
</p>
<form onsubmit="event.preventDefault();">
<div>
<span>Pick a Model:</span>
<progress id="progress" hidden></progress>
<div id="llm_list">
</div>
<button id="loadBtn">Load Model</button>
</div>
<label for="textInput">Craft your <i>prompt</i>📄:</label>
<textarea id="textInput" name="textInput" rows="10"></textarea>
<div>
<span>Use <a href="https://rahuldshetty.github.io/llm.js/#/guidance">Guidance</a>:</span>
<input type="radio" id="none" name="structure_llm" value="none" checked>
<label for="none">None</label>
<input type="radio" id="json-radio" name="structure_llm" value="json">
<label for="json-radio">JSON</label>
<input type="radio" id="cfg-radio" name="structure_llm" value="cfg">
<label for="cfg-radio">CFG</label>
<textarea style="display: none;" id="guidanceInput" name="textInput" rows="3" placeholder="Enter Grammar/JSON Schema"></textarea>
</div>
<button id="submitBtn" disabled>Run</button>
</form>
<div id="result">
<h3>Result</h3>
<pre><code id="output" style="white-space: pre-wrap;"></code></pre>
</div>
</div>
<script type="module">
import {LLM} from "./llm.js/llm.js";
let LLMEngine;
let progress = document.getElementById('progress');
let loadButton = document.getElementById('loadBtn');
let submitButton = document.getElementById('submitBtn');
const radioButtons = document.querySelectorAll('input[name="structure_llm"]');
let inputElement = document.getElementById('textInput');
let outputElement = document.getElementById('output');
let guidanceInput = document.getElementById('guidanceInput');
let selectedOption = "none";
let jsonGrammar = '{"type": "array","items": {"type": "string"},"minItems": 3,"maxItems": 9}';
let cgfGrammar = `root ::= item+
item ::= "- " ([a-zA-Z]+){1,10} "\\n"
`;
const available_llms = [
{
"id": 0,
"name": "Qwen2-0.5B-Instruct (353 MB)",
"link": "https://huggingface.co/Qwen/Qwen2-0.5B-Instruct-GGUF",
"model": "https://huggingface.co/Qwen/Qwen2-0.5B-Instruct-GGUF/resolve/main/qwen2-0_5b-instruct-q4_0.gguf",
"prompt": "<|im_start|>user\nList the 9 planets in Solar System<|im_end|>\n<|im_start|>assistant\n",
"type": "GGUF_CPU"
},
{
"id": 1,
"name": "TinyMistral-248M-SFT-v4 (264 MB)",
"link": "https://huggingface.co/Felladrin/TinyMistral-248M-SFT-v4",
"model": "https://huggingface.co/rahuldshetty/llm.js/resolve/main/TinyMistral-248M-SFT-v4.Q8_0.gguf",
"prompt": "<|im_start|>user\nWho is Sherlock Holmes?<|im_end|>\n<|im_start|>assistant\n",
"type": "GGUF_CPU"
},
{
"id": 2,
"name": "LLaMa Lite (289 MB)",
"link": "https://huggingface.co/Felladrin/llama2_xs_460M_experimental_evol_instruct",
"model": "https://huggingface.co/rahuldshetty/llm.js/resolve/main/llama2_xs_460m_experimental_evol_instruct.q4_k_m.gguf",
"prompt": "### Instruction:\n{prompt}\n\n### Response:\n",
"type": "GGUF_CPU"
},
{
"id": 3,
"name": "TinyLLama 1.5T (482 MB)",
"link": "https://huggingface.co/Corianas/tiny-llama-miniguanaco-1.5T",
"model": "https://huggingface.co/rahuldshetty/llm.js/resolve/main/tiny-llama-miniguanaco-1.5t.q2_k.gguf",
"prompt": "{prompt}\n",
"type": "GGUF_CPU"
},
{
"id": 4,
"name": "TinyMistral-248M-Alpaca (156 MB)",
"link": "https://huggingface.co/Felladrin/TinyMistral-248M-Evol-Instruct",
"model": "https://huggingface.co/afrideva/TinyMistral-248M-Alpaca-GGUF/resolve/main/tinymistral-248m-alpaca.q4_k_m.gguf",
"prompt": "### Instruction:\n{prompt}\n\n### Response:\n",
"type": "GGUF_CPU"
}
]
// Show Available LLMs
const populate_llm_list = () => {
const head = document.getElementById('llm_list');
let div = document.createElement('div');
for(const llm of available_llms){
// Radio Button
let top = document.createElement('div');
let item = document.createElement('input');
item.type = 'radio';
item.id = llm.id;
item.name = 'llm'
item.value = llm.name
if(llm.id == "0"){
item.checked = true;
inputElement.textContent = available_llms[0].prompt;
}
// Label
let label = document.createElement('label');
item.for = llm.id;
// Link
let anchor = document.createElement('a');
anchor.href = llm.link;
anchor.target = "_blank";
anchor.appendChild(document.createTextNode(llm.name))
label.appendChild(
anchor
);
top.appendChild(item);
top.appendChild(label);
head.appendChild(top);
head.appendChild(document.createElement('br'));
// Event handler: Selecting Radio Button
item.addEventListener('change', (e)=>{
if(e.target.id)
inputElement.textContent = available_llms[Number.parseInt(e.target.id)].prompt;
submitButton.disabled = true;
div.hidden = true;
})
}
// Custom
let top = document.createElement('div');
let item = document.createElement('input');
item.type = 'radio';
item.id = -1;
item.name = 'llm'
item.value = 'Custom'
let label = document.createElement('label');
item.for = -1;
label.appendChild(document.createTextNode('Custom '));
// Input Text
div.hidden = true;
let url_input = document.createElement('input');
url_input.type = 'text';
url_input.id = 'customURL';
url_input.value = "https://huggingface.co/rahuldshetty/llm.js/resolve/main/TinyMistral-248M-SFT-v4.Q8_0.gguf"
url_input.placeholder = "URL"
url_input.size = 60
div.appendChild(url_input);
let type_input = document.createElement('input');
type_input.type = 'text';
type_input.id = 'customType';
type_input.value = "GGUF_CPU"
type_input.placeholder = "TYPE"
div.appendChild(type_input);
top.appendChild(item);
top.appendChild(label);
head.appendChild(top);
head.appendChild(div);
// Event handler: Selecting Radio Button
item.addEventListener('change', (e)=>{
inputElement.textContent = "";
submitButton.disabled = true;
div.hidden = false;
})
}
// Event Listener Functions
const on_loaded = () => {
loadButton.disabled = false;
submitButton.disabled = false;
progress.hidden = true;
}
const write_result = (line) => {
outputElement.textContent += line + "\n";
}
const run_complete = () => {
submitButton.disabled = false;
}
const load_llm_from_url = (link, type) => {
progress.hidden = false;
loadButton.disabled = true;
LLMEngine = new LLM(
type,
link,
on_loaded,
write_result,
run_complete
);
LLMEngine.load_worker();
}
// Actions
const load_model = (e) => {
const selected = document.querySelector('input[name="llm"]:checked').id;
let llm;
if(selected != -1)
{
llm = available_llms[Number.parseInt(selected)];
} else {
llm = {
model: document.getElementById("customURL").value,
type: document.getElementById("customType").value,
}
}
load_llm_from_url(llm.model, llm.type);
}
const run_model = (e) => {
const text = inputElement.value;
submitButton.disabled = true;
outputElement.textContent = ""; // clead old content
let params = {
prompt: text,
max_token_len: 100,
top_k: 1
}
if (selectedOption == "cfg"){
params['grammar'] = cgfGrammar;
} else if(selectedOption == "json"){
params['json_schema'] = jsonGrammar;
}
LLMEngine.run(params);
}
const onTextareaInput = (event) => {
const text = event.target.value;
if (selectedOption == "cfg"){
cgfGrammar = text;
} else if (selectedOption == "json"){
jsonGrammar = text;
}
}
const onRadioButtonSelected = (e) =>{
selectedOption = e.target.value;
// Hide/Unhide guidance input
if (selectedOption != "none"){
guidanceInput.style.display = "block";
} else{
guidanceInput.style.display = "none";
}
// Set Event listeners
if (selectedOption == 'cfg'){
guidanceInput.value = cgfGrammar;
} else if (selectedOption == 'json'){
guidanceInput.value = jsonGrammar;
}
}
// Loader Scripts
populate_llm_list();
loadButton.addEventListener("click", load_model);
submitButton.addEventListener("click", run_model);
guidanceInput.addEventListener("input", onTextareaInput);
// Add event listeners to each radio button
radioButtons.forEach(radio => {
radio.addEventListener('change', onRadioButtonSelected);
});
</script>
</body>
</html>