FYRGPT commited on
Commit
68aa57e
·
verified ·
1 Parent(s): 6b85b9e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +2045 -392
index.html CHANGED
@@ -1,622 +1,2275 @@
1
  <!DOCTYPE html>
 
2
  <html lang="en">
 
3
  <head>
 
4
  <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>FYR StudyAI</title>
7
-
8
- <style>
9
- body {
10
- margin: 0;
11
- font-family: Arial, sans-serif;
12
- background: #f5f6fa;
13
- color: #111827;
14
- }
15
 
16
- .container {
17
- max-width: 850px;
18
- margin: 40px auto;
19
- padding: 20px;
20
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- h1 {
23
- text-align: center;
24
- }
25
 
26
- .subtitle {
27
- text-align: center;
28
- color: #6b7280;
29
- }
30
 
31
- .status {
32
- margin: 20px 0;
33
- padding: 15px;
34
- background: white;
35
- border-radius: 12px;
36
- border: 1px solid #ddd;
37
- }
38
 
39
- #error {
40
- color: #dc2626;
41
- white-space: pre-wrap;
42
- font-size: 13px;
43
- }
44
 
45
- .chat {
46
- min-height: 300px;
47
- background: white;
48
- border-radius: 15px;
49
- padding: 20px;
50
- border: 1px solid #ddd;
51
- margin-bottom: 15px;
52
- }
53
 
54
- .message {
55
- padding: 12px;
56
- border-radius: 12px;
57
- margin: 10px 0;
58
- white-space: pre-wrap;
59
- }
60
 
61
- .user {
62
- background: #6366f1;
63
- color: white;
64
- margin-left: 20%;
65
- }
66
 
67
- .ai {
68
- background: #eef2ff;
69
- margin-right: 20%;
70
- }
71
 
72
- .controls {
73
- display: flex;
74
- gap: 10px;
75
- }
76
 
77
- textarea {
78
- flex: 1;
79
- padding: 12px;
80
- border-radius: 12px;
81
- border: 1px solid #ccc;
82
- resize: vertical;
83
- font: inherit;
84
- }
85
 
86
- button {
87
- padding: 12px 20px;
88
- border: none;
89
- border-radius: 12px;
90
- background: #6366f1;
91
- color: white;
92
- cursor: pointer;
93
- }
94
 
95
- button:disabled {
96
- opacity: 0.5;
97
- }
98
 
99
- select {
100
- padding: 10px;
101
- border-radius: 10px;
102
- margin-bottom: 10px;
103
- }
104
 
105
- .progress {
106
- font-size: 13px;
107
- color: #6b7280;
108
- }
109
- </style>
110
- </head>
111
 
112
- <body>
113
 
114
- <div class="container">
115
 
116
- <h1>📚 FYR StudyAI</h1>
117
- <p class="subtitle">
118
- Free AI running directly in your browser
119
- </p>
120
 
121
- <select id="subject">
122
- <option>General</option>
123
- <option>Mathematics</option>
124
- <option>Science</option>
125
- <option>English</option>
126
- <option>Chinese</option>
127
- <option>History</option>
128
- <option>Geography</option>
129
- </select>
130
-
131
- <select id="mode">
132
- <option>Ask anything</option>
133
- <option>Explain</option>
134
- <option>Quiz me</option>
135
- <option>Flashcards</option>
136
- <option>Study plan</option>
137
- <option>Check answer</option>
138
- </select>
139
-
140
- <div class="status">
141
- <strong>Status:</strong>
142
- <span id="status">Waiting for first question</span>
143
-
144
- <div class="progress" id="progress"></div>
145
-
146
- <div id="error"></div>
147
- </div>
148
 
149
- <div class="chat" id="chat">
150
- <div class="message ai">
151
- Hi! Ask me something to start the AI.
152
- </div>
153
- </div>
154
 
155
- <div class="controls">
156
 
157
- <textarea
158
- id="input"
159
- rows="2"
160
- placeholder="Ask FYR StudyAI something..."
161
- ></textarea>
162
 
163
- <button id="send">
164
- Send
165
- </button>
166
 
167
- </div>
168
 
169
- </div>
170
 
 
 
171
 
172
- <script type="module">
173
 
174
- import {
175
- pipeline,
176
- env
177
- } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1";
178
 
 
 
179
 
180
- /*
181
- Tell Transformers.js that models
182
- should come from Hugging Face.
183
- */
184
 
185
- env.allowLocalModels = false;
 
186
 
187
 
188
- const MODEL =
189
- "onnx-community/SmolLM2-135M-Instruct-ONNX-MHA";
190
 
 
191
 
192
- let generator = null;
193
 
194
 
195
- const status =
196
- document.getElementById("status");
197
 
198
- const progress =
199
- document.getElementById("progress");
200
 
201
- const errorBox =
202
- document.getElementById("error");
203
 
204
- const input =
205
- document.getElementById("input");
206
 
207
- const send =
208
- document.getElementById("send");
209
 
210
- const chat =
211
- document.getElementById("chat");
212
 
 
213
 
214
- function addMessage(text, type) {
215
 
216
- const div =
217
- document.createElement("div");
218
 
219
- div.className =
220
- "message " + type;
221
 
222
- div.textContent =
223
- text;
224
 
225
- chat.appendChild(div);
226
 
227
- chat.scrollTop =
228
- chat.scrollHeight;
229
 
230
- return div;
231
- }
 
 
 
 
232
 
233
 
234
- /*
235
- LOAD AI
236
- */
237
 
238
- async function loadModel() {
 
 
239
 
240
- if (generator) {
241
  return;
 
242
  }
243
 
244
- status.textContent =
245
- "Loading AI...";
246
 
247
- errorBox.textContent =
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  "";
249
 
250
 
251
- /*
252
- Check whether this browser supports WebGPU.
253
- */
254
 
255
- const webGPU =
256
- "gpu" in navigator;
257
 
 
 
258
 
259
- if (webGPU) {
260
 
261
- progress.textContent =
262
- "WebGPU detected ✅ Downloading model...";
263
 
264
- }
 
265
 
266
- else {
267
 
268
- progress.textContent =
269
- "WebGPU unavailable. Using CPU mode.";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
 
271
- }
272
 
273
 
274
  try {
275
 
276
- const settings = {
277
 
278
- dtype: webGPU
279
- ? "q4f16"
280
- : "q4",
281
 
282
- progress_callback: (x) => {
283
 
284
- console.log(x);
285
 
286
- if (
287
- x.status === "progress" &&
288
- typeof x.progress === "number"
289
- ) {
290
 
291
- progress.textContent =
292
- "Downloading AI: " +
293
- Math.round(x.progress) +
294
- "%";
295
 
296
- }
297
 
298
- else if (x.file) {
 
 
 
 
299
 
300
- progress.textContent =
301
- "Loading: " +
302
- x.file;
303
 
304
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
306
  }
307
 
308
- };
309
 
 
 
 
 
 
 
 
 
310
 
311
- if (webGPU) {
312
 
313
- settings.device =
314
- "webgpu";
 
 
315
 
316
  }
317
 
318
 
319
- generator =
320
- await pipeline(
321
 
322
- "text-generation",
323
 
324
- MODEL,
325
 
326
- settings
327
 
328
- );
 
 
 
 
 
 
329
 
330
 
331
- status.textContent =
332
- "AI Ready ✅";
333
 
334
- progress.textContent =
335
- "Model loaded successfully.";
 
 
 
 
 
 
336
 
337
  }
338
 
339
- catch (error) {
340
 
341
- console.error(error);
342
 
343
- status.textContent =
344
- "AI could not start ❌";
345
 
346
- errorBox.textContent =
347
- "ERROR:\n" +
348
- (error.stack ||
349
- error.message ||
350
- String(error));
351
 
352
- throw error;
 
353
 
354
  }
355
 
356
  }
357
 
358
 
359
- /*
360
- MAKE STUDY PROMPT
361
- */
362
 
363
- function buildPrompt(question) {
364
 
365
- const subject =
366
- document.getElementById("subject").value;
367
 
368
- const mode =
369
- document.getElementById("mode").value;
370
 
 
371
 
372
- if (mode === "Explain") {
373
 
374
- return `
375
- You are a helpful secondary school tutor.
376
 
377
- Subject: ${subject}
378
 
379
- Explain this clearly:
380
 
381
- ${question}
382
 
383
- Give:
384
- 1. Simple explanation
385
- 2. Important ideas
386
- 3. Example
387
- 4. Short recap
388
- `;
389
 
390
- }
391
 
392
 
393
- if (mode === "Quiz me") {
394
 
395
- return `
396
- You are a tutor.
397
 
398
- Subject: ${subject}
 
 
399
 
400
- Create five questions about:
401
 
402
- ${question}
403
 
404
- Do not reveal the answers yet.
405
- `;
 
 
 
 
406
 
407
  }
408
 
 
409
 
410
- if (mode === "Flashcards") {
411
 
412
- return `
413
- Subject: ${subject}
414
 
415
- Make 6 flashcards about:
416
 
417
- ${question}
418
 
419
- Format:
420
 
421
- Q: question
422
- A: answer
423
- `;
424
 
425
- }
 
426
 
427
 
428
- if (mode === "Study plan") {
429
 
430
- return `
431
- Make a realistic study plan.
432
 
433
- Subject: ${subject}
434
 
435
- Student request:
436
 
437
- ${question}
438
 
439
- Include revision,
440
- practice questions,
441
- active recall,
442
- breaks and final review.
443
- `;
444
 
445
  }
446
 
 
447
 
448
- if (mode === "Check answer") {
449
 
450
- return `
451
- Subject: ${subject}
452
 
453
- Check this student's work:
454
 
455
- ${question}
456
 
457
- Explain what is correct,
458
- what needs improvement,
459
- and give a better answer.
460
- `;
 
461
 
462
- }
463
 
464
 
465
- return `
466
- You are FYR StudyAI.
467
 
468
- You are a friendly tutor for a secondary school student.
469
 
470
- Subject: ${subject}
471
 
472
- Question:
473
 
474
- ${question}
 
 
 
 
475
 
476
- Explain clearly.
477
- Give steps and examples when helpful.
478
- `;
479
 
480
- }
 
 
 
 
481
 
 
482
 
483
- /*
484
- SEND QUESTION
485
- */
486
 
487
- async function askAI() {
488
 
489
- const question =
490
- input.value.trim();
 
 
 
491
 
492
- if (!question) {
493
- return;
494
- }
495
 
496
 
497
- addMessage(
498
- question,
499
- "user"
500
- );
501
 
502
- input.value =
503
- "";
504
 
505
- send.disabled =
506
- true;
507
 
 
 
 
 
 
 
 
 
508
 
509
- const answerBubble =
510
- addMessage(
511
- "Starting AI...",
512
- "ai"
513
  );
514
 
 
515
 
516
- try {
517
 
518
- await loadModel();
519
 
520
 
521
- answerBubble.textContent =
522
- "Thinking...";
523
 
524
 
525
- const prompt =
526
- buildPrompt(question);
 
 
 
527
 
 
528
 
529
- /*
530
- Keep this simple:
531
- use plain text instead of chat-message arrays.
532
- */
533
 
534
- const result =
535
- await generator(
536
 
537
- prompt,
538
 
539
- {
540
- max_new_tokens: 150,
541
- do_sample: true,
542
- temperature: 0.7,
543
- top_p: 0.9,
544
- repetition_penalty: 1.1
545
- }
546
 
547
- );
548
 
 
 
 
 
 
549
 
550
- let generated =
551
- result[0].generated_text;
552
 
 
 
 
 
 
553
 
554
- /*
555
- Remove the original prompt
556
- if the model repeats it.
557
- */
558
 
559
- if (
560
- typeof generated === "string" &&
561
- generated.startsWith(prompt)
562
- ) {
563
 
564
- generated =
565
- generated.slice(
566
- prompt.length
 
 
567
  );
568
 
569
- }
570
 
571
 
572
- answerBubble.textContent =
573
- generated.trim();
574
 
575
 
576
- status.textContent =
577
- "AI Ready ✅";
578
 
579
- }
 
 
 
 
 
580
 
581
- catch (error) {
582
 
583
- answerBubble.textContent =
584
- "AI couldn't start. Look at the red ERROR box above.";
585
 
586
  }
587
 
 
588
 
589
- send.disabled =
590
- false;
591
 
592
- }
593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
 
595
- send.addEventListener(
596
- "click",
597
- askAI
598
  );
599
 
600
 
601
- input.addEventListener(
602
- "keydown",
603
- (event) => {
604
 
605
- if (
606
- event.key === "Enter" &&
607
- !event.shiftKey
608
- ) {
609
 
610
- event.preventDefault();
611
 
612
- askAI();
 
 
 
 
 
 
 
 
613
 
614
- }
 
 
 
 
 
615
 
616
  }
 
617
  );
618
 
 
619
  </script>
620
 
 
621
  </body>
 
622
  </html>
 
1
  <!DOCTYPE html>
2
+
3
  <html lang="en">
4
+
5
  <head>
6
+
7
  <meta charset="UTF-8">
 
 
 
 
 
 
 
 
 
 
8
 
9
+ <meta
10
+ name="viewport"
11
+ content="width=device-width, initial-scale=1.0"
12
+ >
13
+
14
+ <title>
15
+ FYR StudyAI
16
+ </title>
17
+
18
+ <link
19
+ rel="stylesheet"
20
+ href="style.css"
21
+ >
22
+
23
+ </head>
24
+
25
+
26
+ <body>
27
+
28
+
29
+ <!-- MODEL CHOICE -->
30
+
31
+ <div
32
+ id="modelPicker"
33
+ class="model-picker"
34
+ >
35
+
36
+ <div class="model-window">
37
+
38
+
39
+ <h1>
40
+ 📚 Welcome to FYR StudyAI
41
+ </h1>
42
+
43
+
44
+ <p>
45
+ Choose how powerful you want the AI to be.
46
+ </p>
47
+
48
+
49
+
50
+ <div class="model-grid">
51
+
52
+
53
+ <!-- 135M -->
54
+
55
+ <div class="model-card">
56
+
57
+
58
+ <div class="model-icon">
59
+ ⚡
60
+ </div>
61
+
62
+
63
+ <h2>
64
+ SmolLM2 135M
65
+ </h2>
66
+
67
+
68
+ <span class="tag">
69
+ FASTEST
70
+ </span>
71
+
72
+
73
+ <p>
74
+ Best for quick and simple questions.
75
+ </p>
76
+
77
+
78
+ <div class="model-details">
79
+
80
+ ✅ Smallest model
81
+
82
+ <br>
83
+
84
+ ✅ Fastest startup
85
+
86
+ <br>
87
+
88
+ ✅ Lowest memory use
89
+
90
+ <br>
91
+
92
+ ✅ Best for slower devices
93
+
94
+ <br><br>
95
+
96
+ ❌ Weakest answers
97
+
98
+ <br>
99
+
100
+ ❌ Can misunderstand questions
101
+
102
+ <br>
103
+
104
+ ❌ Not ideal for difficult schoolwork
105
+
106
+ </div>
107
+
108
+
109
+ <button
110
+ class="model-button"
111
+ data-model="smol135"
112
+ >
113
+
114
+ Choose Fast
115
+
116
+ </button>
117
+
118
+
119
+ </div>
120
+
121
+
122
+
123
+ <!-- 360M -->
124
+
125
+ <div class="model-card">
126
+
127
+
128
+ <div class="model-icon">
129
+ ⚖️
130
+ </div>
131
+
132
+
133
+ <h2>
134
+ SmolLM2 360M
135
+ </h2>
136
+
137
+
138
+ <span class="tag">
139
+ BALANCED
140
+ </span>
141
+
142
+
143
+ <p>
144
+ A middle option between speed and intelligence.
145
+ </p>
146
+
147
+
148
+ <div class="model-details">
149
+
150
+ ✅ Better answers than 135M
151
+
152
+ <br>
153
+
154
+ ✅ Still reasonably lightweight
155
+
156
+ <br>
157
+
158
+ ✅ Better instruction following
159
+
160
+ <br>
161
+
162
+ ✅ Good everyday option
163
+
164
+ <br><br>
165
+
166
+ ❌ Slower than 135M
167
+
168
+ <br>
169
+
170
+ ❌ Still weaker than Qwen
171
+
172
+ </div>
173
+
174
+
175
+ <button
176
+ class="model-button balanced"
177
+ data-model="smol360"
178
+ >
179
+
180
+ Choose Balanced
181
+
182
+ </button>
183
+
184
+
185
+ </div>
186
+
187
+
188
+
189
+ <!-- QWEN -->
190
+
191
+ <div class="model-card recommended">
192
+
193
+
194
+ <div class="badge">
195
+ RECOMMENDED
196
+ </div>
197
+
198
+
199
+ <div class="model-icon">
200
+ 🧠
201
+ </div>
202
+
203
+
204
+ <h2>
205
+ Qwen2.5 0.5B
206
+ </h2>
207
+
208
+
209
+ <span class="tag">
210
+ SMARTEST
211
+ </span>
212
+
213
+
214
+ <p>
215
+ Best choice for explanations and studying.
216
+ </p>
217
+
218
+
219
+ <div class="model-details">
220
+
221
+ ✅ Best explanations
222
+
223
+ <br>
224
+
225
+ ✅ Better reasoning
226
+
227
+ <br>
228
+
229
+ ✅ Better instruction following
230
+
231
+ <br>
232
+
233
+ ✅ Best for school questions
234
+
235
+ <br><br>
236
+
237
+ ❌ Largest download
238
+
239
+ <br>
240
+
241
+ ❌ Uses more memory
242
+
243
+ <br>
244
+
245
+ ❌ Slower startup
246
+
247
+ </div>
248
+
249
+
250
+ <button
251
+ class="model-button qwen"
252
+ data-model="qwen"
253
+ >
254
+
255
+ Choose Smart
256
+
257
+ </button>
258
+
259
+
260
+ </div>
261
+
262
+
263
+ </div>
264
+
265
+
266
+
267
+ <div class="free-note">
268
+
269
+ 💚 All 3 models run directly on your device.
270
+
271
+ <br><br>
272
+
273
+ No OpenAI key
274
+
275
+ <br>
276
+
277
+ No AI API
278
+
279
+ <br>
280
+
281
+ No pay-per-message charge
282
+
283
+ </div>
284
+
285
+
286
+ </div>
287
+
288
+ </div>
289
+
290
+
291
+
292
+ <!-- APP -->
293
+
294
+ <div class="app">
295
+
296
+
297
+ <aside class="sidebar">
298
+
299
+
300
+ <div class="brand">
301
+
302
+ 📚 FYR StudyAI
303
+
304
+ </div>
305
+
306
+
307
+
308
+ <button
309
+ id="newChat"
310
+ class="new-chat"
311
+ >
312
+
313
+ + New Chat
314
+
315
+ </button>
316
+
317
+
318
+
319
+ <button
320
+ id="changeModel"
321
+ class="change-model"
322
+ >
323
+
324
+ 🧠 Change Model
325
+
326
+ </button>
327
+
328
+
329
+
330
+ <div class="sidebar-title">
331
+
332
+ Subjects
333
+
334
+ </div>
335
+
336
+
337
+
338
+ <button
339
+ class="option subject active"
340
+ data-subject="General"
341
+ >
342
+
343
+ 📚 General
344
+
345
+ </button>
346
+
347
+
348
+
349
+ <button
350
+ class="option subject"
351
+ data-subject="Mathematics"
352
+ >
353
+
354
+ ➗ Mathematics
355
+
356
+ </button>
357
+
358
+
359
+
360
+ <button
361
+ class="option subject"
362
+ data-subject="Science"
363
+ >
364
+
365
+ 🔬 Science
366
+
367
+ </button>
368
+
369
+
370
+
371
+ <button
372
+ class="option subject"
373
+ data-subject="English"
374
+ >
375
+
376
+ ✍️ English
377
+
378
+ </button>
379
+
380
+
381
+
382
+ <button
383
+ class="option subject"
384
+ data-subject="Chinese"
385
+ >
386
+
387
+ 🀄 Chinese
388
+
389
+ </button>
390
+
391
+
392
+
393
+ <button
394
+ class="option subject"
395
+ data-subject="History"
396
+ >
397
+
398
+ 🏛️ History
399
+
400
+ </button>
401
+
402
+
403
+
404
+ <button
405
+ class="option subject"
406
+ data-subject="Geography"
407
+ >
408
+
409
+ 🌏 Geography
410
+
411
+ </button>
412
+
413
+
414
+
415
+ <div class="sidebar-title">
416
+
417
+ Study Tools
418
+
419
+ </div>
420
+
421
+
422
+
423
+ <button
424
+ class="option mode active"
425
+ data-mode="Ask anything"
426
+ >
427
+
428
+ 💬 Ask anything
429
+
430
+ </button>
431
+
432
+
433
+
434
+ <button
435
+ class="option mode"
436
+ data-mode="Explain"
437
+ >
438
+
439
+ 🧠 Explain
440
+
441
+ </button>
442
+
443
+
444
+
445
+ <button
446
+ class="option mode"
447
+ data-mode="Quiz"
448
+ >
449
+
450
+ 📝 Quiz me
451
+
452
+ </button>
453
+
454
+
455
+
456
+ <button
457
+ class="option mode"
458
+ data-mode="Flashcards"
459
+ >
460
+
461
+ 🗂️ Flashcards
462
+
463
+ </button>
464
+
465
+
466
+
467
+ <button
468
+ class="option mode"
469
+ data-mode="Study plan"
470
+ >
471
+
472
+ 📅 Study plan
473
+
474
+ </button>
475
+
476
+
477
+
478
+ <button
479
+ class="option mode"
480
+ data-mode="Check answer"
481
+ >
482
+
483
+ ✅ Check answer
484
+
485
+ </button>
486
+
487
+
488
+ </aside>
489
+
490
+
491
+
492
+ <main class="main">
493
+
494
+
495
+ <header class="topbar">
496
+
497
+
498
+ <div>
499
+
500
+
501
+ <h2 id="modeTitle">
502
+
503
+ Ask anything
504
+
505
+ </h2>
506
+
507
+
508
+ <small id="subjectTitle">
509
+
510
+ General
511
+
512
+ </small>
513
+
514
+
515
+ </div>
516
+
517
+
518
+
519
+ <div
520
+ id="status"
521
+ class="status"
522
+ >
523
+
524
+ Choose an AI model
525
+
526
+ </div>
527
+
528
+
529
+ </header>
530
+
531
+
532
+
533
+ <section
534
+ id="chat"
535
+ class="chat"
536
+ >
537
+
538
+
539
+ <div
540
+ id="welcome"
541
+ class="welcome"
542
+ >
543
+
544
+
545
+ <div class="logo">
546
+
547
+ S
548
+
549
+ </div>
550
+
551
+
552
+ <h1>
553
+
554
+ FYR StudyAI
555
+
556
+ </h1>
557
+
558
+
559
+ <p>
560
+
561
+ Choose an AI model and start studying.
562
+
563
+ </p>
564
+
565
+
566
+
567
+ <div class="examples">
568
+
569
+
570
+ <button
571
+ class="example"
572
+ data-example="Explain photosynthesis simply"
573
+ >
574
+
575
+ 🔬 Explain photosynthesis
576
+
577
+ </button>
578
+
579
+
580
+
581
+ <button
582
+ class="example"
583
+ data-example="Quiz me on algebra"
584
+ >
585
+
586
+ ➗ Quiz me on algebra
587
+
588
+ </button>
589
+
590
+
591
+
592
+ <button
593
+ class="example"
594
+ data-example="Make flashcards about the fall of Singapore"
595
+ >
596
+
597
+ 🗂️ Make flashcards
598
+
599
+ </button>
600
+
601
+
602
+
603
+ <button
604
+ class="example"
605
+ data-example="Make me a 1 hour study plan"
606
+ >
607
+
608
+ 📅 Make a study plan
609
+
610
+ </button>
611
+
612
+
613
+ </div>
614
+
615
+
616
+ </div>
617
+
618
+
619
+ </section>
620
+
621
+
622
+
623
+ <div class="input-area">
624
+
625
+
626
+ <div class="input-box">
627
+
628
+
629
+ <textarea
630
+ id="message"
631
+ rows="1"
632
+ placeholder="Ask FYR StudyAI something..."
633
+ ></textarea>
634
+
635
+
636
+ <button id="send">
637
+
638
+ ➤
639
+
640
+ </button>
641
+
642
+
643
+ </div>
644
+
645
+
646
+
647
+ <div
648
+ id="progress"
649
+ class="progress"
650
+ ></div>
651
+
652
+
653
+
654
+ <div
655
+ id="errorBox"
656
+ class="error"
657
+ ></div>
658
+
659
+
660
+
661
+ <div class="warning">
662
+
663
+ FYR StudyAI can make mistakes.
664
+
665
+ Check important information with your
666
+ notes, textbook or teacher.
667
+
668
+ </div>
669
+
670
+
671
+ </div>
672
+
673
+
674
+ </main>
675
+
676
+
677
+ </div>
678
+
679
+
680
+
681
+ <script type="module">
682
+
683
+
684
+ import {
685
+
686
+ pipeline,
687
+ env
688
+
689
+ }
690
+
691
+ from
692
+
693
+ "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1";
694
+
695
+
696
+
697
+ env.allowLocalModels =
698
+ false;
699
+
700
+
701
+ env.useBrowserCache =
702
+ true;
703
+
704
+
705
+
706
+ /* MODELS */
707
+
708
+
709
+ const MODELS = {
710
+
711
+
712
+ smol135: {
713
+
714
+ id:
715
+ "onnx-community/SmolLM2-135M-Instruct-ONNX-MHA",
716
+
717
+ name:
718
+ "SmolLM2 135M",
719
+
720
+ tokens:
721
+ 180
722
+
723
+ },
724
+
725
+
726
+ smol360: {
727
+
728
+ id:
729
+ "onnx-community/SmolLM2-360M-Instruct-ONNX",
730
+
731
+ name:
732
+ "SmolLM2 360M",
733
+
734
+ tokens:
735
+ 240
736
+
737
+ },
738
+
739
+
740
+ qwen: {
741
+
742
+ id:
743
+ "onnx-community/Qwen2.5-0.5B-Instruct",
744
+
745
+ name:
746
+ "Qwen2.5 0.5B",
747
+
748
+ tokens:
749
+ 320
750
+
751
+ }
752
+
753
+
754
+ };
755
+
756
+
757
+
758
+ let selectedModel =
759
+ null;
760
+
761
+
762
+ let generator =
763
+ null;
764
+
765
+
766
+ let loadingPromise =
767
+ null;
768
+
769
+
770
+ let subject =
771
+ "General";
772
+
773
+
774
+ let mode =
775
+ "Ask anything";
776
+
777
+
778
+ let history =
779
+ [];
780
+
781
+
782
+
783
+ const modelPicker =
784
+ document.getElementById(
785
+ "modelPicker"
786
+ );
787
+
788
+
789
+ const chat =
790
+ document.getElementById(
791
+ "chat"
792
+ );
793
+
794
+
795
+ const input =
796
+ document.getElementById(
797
+ "message"
798
+ );
799
+
800
+
801
+ const sendButton =
802
+ document.getElementById(
803
+ "send"
804
+ );
805
+
806
+
807
+ const status =
808
+ document.getElementById(
809
+ "status"
810
+ );
811
+
812
+
813
+ const progress =
814
+ document.getElementById(
815
+ "progress"
816
+ );
817
+
818
+
819
+ const errorBox =
820
+ document.getElementById(
821
+ "errorBox"
822
+ );
823
+
824
+
825
+
826
+ function setStatus(
827
+ text,
828
+ color = "#6366f1"
829
+ ) {
830
+
831
+ status.textContent =
832
+ text;
833
+
834
+
835
+ status.style.color =
836
+ color;
837
+
838
+ }
839
+
840
+
841
+
842
+ function addMessage(
843
+ text,
844
+ type
845
+ ) {
846
+
847
+ const row =
848
+ document.createElement(
849
+ "div"
850
+ );
851
+
852
+
853
+ row.className =
854
+ "message-row " +
855
+ type;
856
+
857
+
858
+
859
+ const bubble =
860
+ document.createElement(
861
+ "div"
862
+ );
863
+
864
+
865
+ bubble.className =
866
+ "bubble " +
867
+ type;
868
+
869
+
870
+ bubble.textContent =
871
+ text;
872
+
873
+
874
+
875
+ row.appendChild(
876
+ bubble
877
+ );
878
+
879
+
880
+ chat.appendChild(
881
+ row
882
+ );
883
+
884
+
885
+ chat.scrollTop =
886
+ chat.scrollHeight;
887
+
888
+
889
+ return bubble;
890
+
891
+ }
892
+
893
+
894
+
895
+ function resetChat() {
896
+
897
+ history =
898
+ [];
899
+
900
+
901
+ chat.innerHTML =
902
+ `
903
+
904
+ <div
905
+ id="welcome"
906
+ class="welcome"
907
+ >
908
+
909
+ <div class="logo">
910
+
911
+ S
912
+
913
+ </div>
914
+
915
+ <h1>
916
+
917
+ FYR StudyAI
918
+
919
+ </h1>
920
+
921
+ <p>
922
+
923
+ ${
924
+ selectedModel
925
+ ?
926
+ selectedModel.name +
927
+ " selected."
928
+ :
929
+ "Choose an AI model."
930
+ }
931
+
932
+ </p>
933
+
934
+ </div>
935
+
936
+ `;
937
+
938
+ }
939
+
940
+
941
+
942
+ /* MODEL SELECTION */
943
+
944
+
945
+ document
946
+ .querySelectorAll(
947
+ "[data-model]"
948
+ )
949
+ .forEach(
950
+
951
+ button => {
952
+
953
+
954
+ button.addEventListener(
955
+
956
+ "click",
957
+
958
+ () => {
959
+
960
+
961
+ chooseModel(
962
+ button.dataset.model
963
+ );
964
+
965
+ }
966
+
967
+ );
968
+
969
+ }
970
+
971
+ );
972
+
973
+
974
+
975
+ function chooseModel(
976
+ key
977
+ ) {
978
+
979
+
980
+ selectedModel =
981
+ MODELS[key];
982
+
983
+
984
+ generator =
985
+ null;
986
+
987
+
988
+ loadingPromise =
989
+ null;
990
+
991
+
992
+ history =
993
+ [];
994
+
995
+
996
+ modelPicker.style.display =
997
+ "none";
998
+
999
+
1000
+ progress.textContent =
1001
+ "";
1002
+
1003
+
1004
+ errorBox.textContent =
1005
+ "";
1006
+
1007
+
1008
+ setStatus(
1009
+
1010
+ selectedModel.name +
1011
+ " selected"
1012
+
1013
+ );
1014
+
1015
+
1016
+ resetChat();
1017
+
1018
+ }
1019
+
1020
+
1021
+
1022
+ document
1023
+ .getElementById(
1024
+ "changeModel"
1025
+ )
1026
+ .addEventListener(
1027
+
1028
+ "click",
1029
+
1030
+ () => {
1031
+
1032
+
1033
+ modelPicker.style.display =
1034
+ "flex";
1035
+
1036
+ }
1037
+
1038
+ );
1039
+
1040
+
1041
+
1042
+ /* LOAD MODEL */
1043
+
1044
+
1045
+ async function loadAI() {
1046
+
1047
+
1048
+ if (
1049
+ !selectedModel
1050
+ ) {
1051
+
1052
+
1053
+ modelPicker.style.display =
1054
+ "flex";
1055
+
1056
+
1057
+ throw new Error(
1058
+
1059
+ "Choose an AI model first."
1060
+
1061
+ );
1062
+
1063
+ }
1064
+
1065
+
1066
+
1067
+ if (
1068
+ generator
1069
+ ) {
1070
+
1071
+
1072
+ return generator;
1073
+
1074
+ }
1075
+
1076
+
1077
+
1078
+ if (
1079
+ loadingPromise
1080
+ ) {
1081
+
1082
+
1083
+ return loadingPromise;
1084
+
1085
+ }
1086
+
1087
+
1088
+
1089
+ loadingPromise =
1090
+ (
1091
+
1092
+ async () => {
1093
+
1094
+
1095
+ const webGPU =
1096
+ "gpu" in navigator;
1097
+
1098
+
1099
+
1100
+ setStatus(
1101
+
1102
+ "Loading " +
1103
+ selectedModel.name +
1104
+ "..."
1105
+
1106
+ );
1107
+
1108
+
1109
+
1110
+ progress.textContent =
1111
+
1112
+ webGPU
1113
+
1114
+ ?
1115
+
1116
+ "WebGPU detected. Downloading AI..."
1117
+
1118
+ :
1119
+
1120
+ "CPU mode. Downloading AI...";
1121
+
1122
+
1123
+
1124
+ errorBox.textContent =
1125
+ "";
1126
+
1127
+
1128
+
1129
+ try {
1130
+
1131
+
1132
+ const options = {
1133
+
1134
+
1135
+ dtype:
1136
+
1137
+ webGPU
1138
+
1139
+ ?
1140
+
1141
+ "q4f16"
1142
+
1143
+ :
1144
+
1145
+ "q4",
1146
+
1147
+
1148
+
1149
+ progress_callback:
1150
+
1151
+ data => {
1152
+
1153
+
1154
+ if (
1155
+
1156
+ data.status ===
1157
+ "progress"
1158
+
1159
+ &&
1160
+
1161
+ typeof
1162
+ data.progress ===
1163
+ "number"
1164
+
1165
+ ) {
1166
+
1167
+
1168
+ progress.textContent =
1169
+
1170
+ "Downloading AI: "
1171
+
1172
+ +
1173
+
1174
+ Math.round(
1175
+ data.progress
1176
+ )
1177
+
1178
+ +
1179
+
1180
+ "%";
1181
+
1182
+ }
1183
+
1184
+
1185
+ else if (
1186
+ data.file
1187
+ ) {
1188
+
1189
+
1190
+ progress.textContent =
1191
+
1192
+ "Loading: "
1193
+
1194
+ +
1195
+
1196
+ data.file;
1197
+
1198
+ }
1199
+
1200
+ }
1201
+
1202
+ };
1203
+
1204
+
1205
+
1206
+ if (
1207
+ webGPU
1208
+ ) {
1209
+
1210
+
1211
+ options.device =
1212
+ "webgpu";
1213
+
1214
+ }
1215
+
1216
+
1217
+
1218
+ generator =
1219
+ await pipeline(
1220
+
1221
+ "text-generation",
1222
+
1223
+ selectedModel.id,
1224
+
1225
+ options
1226
+
1227
+ );
1228
+
1229
+
1230
+
1231
+ setStatus(
1232
+
1233
+ selectedModel.name +
1234
+ " ready ✅",
1235
+
1236
+ "#16a34a"
1237
+
1238
+ );
1239
+
1240
+
1241
+
1242
+ progress.textContent =
1243
+ "AI loaded successfully.";
1244
+
1245
+
1246
+
1247
+ return generator;
1248
+
1249
+ }
1250
+
1251
+
1252
+
1253
+ catch (
1254
+ error
1255
+ ) {
1256
+
1257
+
1258
+ console.error(
1259
+ error
1260
+ );
1261
+
1262
+
1263
+ generator =
1264
+ null;
1265
+
1266
+
1267
+
1268
+ setStatus(
1269
+
1270
+ "AI failed to load ❌",
1271
+
1272
+ "#dc2626"
1273
+
1274
+ );
1275
+
1276
+
1277
+
1278
+ errorBox.textContent =
1279
+
1280
+ "ERROR:\n"
1281
+
1282
+ +
1283
+
1284
+ (
1285
+ error?.stack
1286
+
1287
+ ||
1288
+
1289
+ error?.message
1290
+
1291
+ ||
1292
+
1293
+ String(error)
1294
+ );
1295
+
1296
+
1297
+
1298
+ throw error;
1299
+
1300
+ }
1301
+
1302
+
1303
+
1304
+ finally {
1305
+
1306
+
1307
+ loadingPromise =
1308
+ null;
1309
+
1310
+ }
1311
+
1312
+
1313
+ }
1314
+
1315
+ )();
1316
+
1317
+
1318
+
1319
+ return loadingPromise;
1320
+
1321
+ }
1322
+
1323
+
1324
+
1325
+ /* STUDY PROMPTS */
1326
+
1327
+
1328
+ function createPrompt(
1329
+ text
1330
+ ) {
1331
+
1332
+
1333
+ if (
1334
+ mode ===
1335
+ "Explain"
1336
+ ) {
1337
+
1338
+
1339
+ return `
1340
+
1341
+ Subject: ${subject}
1342
+
1343
+ Explain this topic:
1344
+
1345
+ ${text}
1346
+
1347
+ Use:
1348
+
1349
+ 1. Simple explanation
1350
+
1351
+ 2. Important ideas
1352
+
1353
+ 3. Example
1354
+
1355
+ 4. Short recap
1356
+
1357
+ `;
1358
+
1359
+ }
1360
+
1361
+
1362
+
1363
+ if (
1364
+ mode ===
1365
+ "Quiz"
1366
+ ) {
1367
+
1368
+
1369
+ return `
1370
+
1371
+ Subject: ${subject}
1372
+
1373
+ Create a five-question quiz about:
1374
+
1375
+ ${text}
1376
+
1377
+ Do not give the answers yet.
1378
+
1379
+ `;
1380
+
1381
+ }
1382
+
1383
+
1384
+
1385
+ if (
1386
+ mode ===
1387
+ "Flashcards"
1388
+ ) {
1389
+
1390
+
1391
+ return `
1392
+
1393
+ Subject: ${subject}
1394
+
1395
+ Create eight flashcards about:
1396
+
1397
+ ${text}
1398
+
1399
+ Format:
1400
+
1401
+ Q: question
1402
+
1403
+ A: answer
1404
+
1405
+ `;
1406
+
1407
+ }
1408
+
1409
+
1410
+
1411
+ if (
1412
+ mode ===
1413
+ "Study plan"
1414
+ ) {
1415
+
1416
+
1417
+ return `
1418
+
1419
+ Create a realistic study plan.
1420
+
1421
+ Subject:
1422
+
1423
+ ${subject}
1424
+
1425
+ Request:
1426
+
1427
+ ${text}
1428
+
1429
+ Include:
1430
+
1431
+ revision,
1432
+ active recall,
1433
+ practice,
1434
+ breaks,
1435
+ and final review.
1436
+
1437
+ `;
1438
+
1439
+ }
1440
+
1441
+
1442
+
1443
+ if (
1444
+ mode ===
1445
+ "Check answer"
1446
+ ) {
1447
+
1448
+
1449
+ return `
1450
+
1451
+ Subject: ${subject}
1452
+
1453
+ Check this student's answer:
1454
+
1455
+ ${text}
1456
+
1457
+ Explain:
1458
+
1459
+ 1. What is correct
1460
+
1461
+ 2. What needs improvement
1462
+
1463
+ 3. A better answer
1464
+
1465
+ 4. One tip
1466
+
1467
+ `;
1468
+
1469
+ }
1470
+
1471
+
1472
+
1473
+ return `
1474
+
1475
+ Subject: ${subject}
1476
+
1477
+ Student question:
1478
+
1479
+ ${text}
1480
+
1481
+ Answer clearly.
1482
+
1483
+ Explain the idea instead of
1484
+ only giving the final answer.
1485
+
1486
+ Use examples when useful.
1487
+
1488
+ `;
1489
+
1490
+ }
1491
+
1492
+
1493
+
1494
+ function systemPrompt() {
1495
+
1496
+
1497
+ return `
1498
+
1499
+ You are FYR StudyAI.
1500
+
1501
+ You are a friendly tutor
1502
+ for secondary-school students.
1503
+
1504
+ Explain ideas clearly.
1505
+
1506
+ Break difficult ideas
1507
+ into simple steps.
1508
+
1509
+ For mathematics,
1510
+ show working.
1511
+
1512
+ Give examples when useful.
1513
+
1514
+ For quizzes,
1515
+ do not reveal answers
1516
+ until the student attempts them.
1517
+
1518
+ Do not invent facts.
1519
+
1520
+ If unsure,
1521
+ say you are unsure.
1522
+
1523
+ `;
1524
+
1525
+ }
1526
+
1527
+
1528
+
1529
+ /* EXTRACT RESULT */
1530
+
1531
+
1532
+ function extractAnswer(
1533
+ result,
1534
+ prompt
1535
+ ) {
1536
+
1537
+
1538
+ const generated =
1539
+ result?.[0]
1540
+ ?.generated_text;
1541
+
1542
+
1543
+
1544
+ if (
1545
+ Array.isArray(
1546
+ generated
1547
+ )
1548
+ ) {
1549
+
1550
+
1551
+ const last =
1552
+ generated[
1553
+ generated.length - 1
1554
+ ];
1555
+
1556
+
1557
+
1558
+ if (
1559
+
1560
+ last
1561
+
1562
+ &&
1563
+
1564
+ typeof
1565
+ last.content ===
1566
+ "string"
1567
+
1568
+ ) {
1569
+
1570
+
1571
+ return last.content.trim();
1572
+
1573
+ }
1574
+
1575
+ }
1576
+
1577
+
1578
+
1579
+ if (
1580
+ typeof generated ===
1581
+ "string"
1582
+ ) {
1583
+
1584
+
1585
+ let answer =
1586
+ generated;
1587
+
1588
+
1589
+
1590
+ if (
1591
+ answer.startsWith(
1592
+ prompt
1593
+ )
1594
+ ) {
1595
+
1596
+
1597
+ answer =
1598
+ answer.slice(
1599
+ prompt.length
1600
+ );
1601
+
1602
+ }
1603
+
1604
+
1605
+
1606
+ return answer.trim();
1607
+
1608
+ }
1609
+
1610
+
1611
+
1612
+ return "";
1613
+
1614
+ }
1615
+
1616
+
1617
+
1618
+ /* GENERATION */
1619
+
1620
+
1621
+ async function generateAnswer(
1622
+ prompt
1623
+ ) {
1624
+
1625
+
1626
+ const messages = [
1627
+
1628
+
1629
+ {
1630
+
1631
+ role:
1632
+ "system",
1633
+
1634
+ content:
1635
+ systemPrompt()
1636
+
1637
+ },
1638
+
1639
+
1640
+ ...history.slice(
1641
+ -4
1642
+ ),
1643
+
1644
+
1645
+ {
1646
+
1647
+ role:
1648
+ "user",
1649
+
1650
+ content:
1651
+ prompt
1652
+
1653
+ }
1654
+
1655
+
1656
+ ];
1657
+
1658
+
1659
+
1660
+ try {
1661
+
1662
+
1663
+ const result =
1664
+ await generator(
1665
+
1666
+ messages,
1667
+
1668
+ {
1669
+
1670
+ max_new_tokens:
1671
+ selectedModel.tokens,
1672
+
1673
+
1674
+ do_sample:
1675
+ false,
1676
+
1677
+
1678
+ repetition_penalty:
1679
+ 1.08
1680
+
1681
+ }
1682
+
1683
+ );
1684
+
1685
+
1686
+
1687
+ const answer =
1688
+ extractAnswer(
1689
+
1690
+ result,
1691
+
1692
+ prompt
1693
+
1694
+ );
1695
+
1696
+
1697
+
1698
+ if (
1699
+ answer
1700
+ ) {
1701
+
1702
 
1703
+ return answer;
 
 
1704
 
1705
+ }
 
 
 
1706
 
1707
+ }
 
 
 
 
 
 
1708
 
 
 
 
 
 
1709
 
 
 
 
 
 
 
 
 
1710
 
1711
+ catch (
1712
+ error
1713
+ ) {
 
 
 
1714
 
 
 
 
 
 
1715
 
1716
+ console.warn(
 
 
 
1717
 
1718
+ "Using text fallback",
 
 
 
1719
 
1720
+ error
 
 
 
 
 
 
 
1721
 
1722
+ );
 
 
 
 
 
 
 
1723
 
1724
+ }
 
 
1725
 
 
 
 
 
 
1726
 
 
 
 
 
 
 
1727
 
1728
+ const textPrompt =
1729
 
1730
+ systemPrompt()
1731
 
1732
+ +
 
 
 
1733
 
1734
+ "\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1735
 
1736
+ +
 
 
 
 
1737
 
1738
+ prompt
1739
 
1740
+ +
 
 
 
 
1741
 
1742
+ "\n\nTutor answer:";
 
 
1743
 
 
1744
 
 
1745
 
1746
+ const result =
1747
+ await generator(
1748
 
1749
+ textPrompt,
1750
 
1751
+ {
 
 
 
1752
 
1753
+ max_new_tokens:
1754
+ selectedModel.tokens,
1755
 
 
 
 
 
1756
 
1757
+ do_sample:
1758
+ false,
1759
 
1760
 
1761
+ repetition_penalty:
1762
+ 1.08
1763
 
1764
+ }
1765
 
1766
+ );
1767
 
1768
 
 
 
1769
 
1770
+ return (
 
1771
 
1772
+ extractAnswer(
 
1773
 
1774
+ result,
 
1775
 
1776
+ textPrompt
 
1777
 
1778
+ )
 
1779
 
1780
+ ||
1781
 
1782
+ "I couldn't generate an answer. Try asking the question differently."
1783
 
1784
+ );
 
1785
 
1786
+ }
 
1787
 
 
 
1788
 
 
1789
 
1790
+ /* SEND */
 
1791
 
1792
+
1793
+ async function sendMessage() {
1794
+
1795
+
1796
+ const text =
1797
+ input.value.trim();
1798
 
1799
 
 
 
 
1800
 
1801
+ if (
1802
+ !text
1803
+ ) {
1804
 
 
1805
  return;
1806
+
1807
  }
1808
 
 
 
1809
 
1810
+
1811
+ if (
1812
+ !selectedModel
1813
+ ) {
1814
+
1815
+
1816
+ modelPicker.style.display =
1817
+ "flex";
1818
+
1819
+
1820
+ return;
1821
+
1822
+ }
1823
+
1824
+
1825
+
1826
+ document
1827
+ .getElementById(
1828
+ "welcome"
1829
+ )
1830
+ ?.remove();
1831
+
1832
+
1833
+
1834
+ addMessage(
1835
+
1836
+ text,
1837
+
1838
+ "user"
1839
+
1840
+ );
1841
+
1842
+
1843
+
1844
+ input.value =
1845
  "";
1846
 
1847
 
1848
+ input.style.height =
1849
+ "auto";
 
1850
 
 
 
1851
 
1852
+ sendButton.disabled =
1853
+ true;
1854
 
 
1855
 
 
 
1856
 
1857
+ const bubble =
1858
+ addMessage(
1859
 
1860
+ generator
1861
 
1862
+ ?
1863
+
1864
+ "Thinking..."
1865
+
1866
+ :
1867
+
1868
+ "Starting "
1869
+
1870
+ +
1871
+
1872
+ selectedModel.name
1873
+
1874
+ +
1875
+
1876
+ "...",
1877
+
1878
+ "ai"
1879
+
1880
+ );
1881
 
 
1882
 
1883
 
1884
  try {
1885
 
 
1886
 
1887
+ await loadAI();
 
 
1888
 
 
1889
 
 
1890
 
1891
+ bubble.textContent =
1892
+ "Thinking...";
 
 
1893
 
 
 
 
 
1894
 
 
1895
 
1896
+ const prompt =
1897
+ createPrompt(
1898
+ text
1899
+ );
1900
+
1901
 
 
 
 
1902
 
1903
+ const answer =
1904
+ await generateAnswer(
1905
+ prompt
1906
+ );
1907
+
1908
+
1909
+
1910
+ bubble.textContent =
1911
+ answer;
1912
+
1913
+
1914
+
1915
+ history.push(
1916
+
1917
+
1918
+ {
1919
+
1920
+ role:
1921
+ "user",
1922
+
1923
+ content:
1924
+ prompt
1925
+
1926
+ },
1927
+
1928
+
1929
+ {
1930
+
1931
+ role:
1932
+ "assistant",
1933
+
1934
+ content:
1935
+ answer
1936
 
1937
  }
1938
 
 
1939
 
1940
+ );
1941
+
1942
+
1943
+
1944
+ if (
1945
+ history.length >
1946
+ 8
1947
+ ) {
1948
 
 
1949
 
1950
+ history =
1951
+ history.slice(
1952
+ -8
1953
+ );
1954
 
1955
  }
1956
 
1957
 
 
 
1958
 
1959
+ setStatus(
1960
 
1961
+ selectedModel.name
1962
 
1963
+ +
1964
 
1965
+ " ready ✅",
1966
+
1967
+ "#16a34a"
1968
+
1969
+ );
1970
+
1971
+ }
1972
 
1973
 
 
 
1974
 
1975
+ catch (
1976
+ error
1977
+ ) {
1978
+
1979
+
1980
+ bubble.textContent =
1981
+
1982
+ "The AI couldn't start. Check the error below or try a smaller model.";
1983
 
1984
  }
1985
 
 
1986
 
 
1987
 
1988
+ finally {
 
1989
 
 
 
 
 
 
1990
 
1991
+ sendButton.disabled =
1992
+ false;
1993
 
1994
  }
1995
 
1996
  }
1997
 
1998
 
 
 
 
1999
 
2000
+ /* SEND BUTTON */
2001
 
 
 
2002
 
2003
+ sendButton.addEventListener(
 
2004
 
2005
+ "click",
2006
 
2007
+ sendMessage
2008
 
2009
+ );
 
2010
 
 
2011
 
 
2012
 
2013
+ input.addEventListener(
2014
 
2015
+ "keydown",
 
 
 
 
 
2016
 
2017
+ event => {
2018
 
2019
 
2020
+ if (
2021
 
2022
+ event.key ===
2023
+ "Enter"
2024
 
2025
+ &&
2026
+
2027
+ !event.shiftKey
2028
 
2029
+ ) {
2030
 
 
2031
 
2032
+ event.preventDefault();
2033
+
2034
+
2035
+ sendMessage();
2036
+
2037
+ }
2038
 
2039
  }
2040
 
2041
+ );
2042
 
 
2043
 
 
 
2044
 
2045
+ input.addEventListener(
2046
 
2047
+ "input",
2048
 
2049
+ () => {
2050
 
 
 
 
2051
 
2052
+ input.style.height =
2053
+ "auto";
2054
 
2055
 
2056
+ input.style.height =
2057
 
2058
+ Math.min(
 
2059
 
2060
+ input.scrollHeight,
2061
 
2062
+ 150
2063
 
2064
+ )
2065
 
2066
+ +
2067
+
2068
+ "px";
 
 
2069
 
2070
  }
2071
 
2072
+ );
2073
 
 
2074
 
 
 
2075
 
2076
+ /* SUBJECTS */
2077
 
 
2078
 
2079
+ document
2080
+ .querySelectorAll(
2081
+ ".subject"
2082
+ )
2083
+ .forEach(
2084
 
2085
+ button => {
2086
 
2087
 
2088
+ button.addEventListener(
 
2089
 
2090
+ "click",
2091
 
2092
+ () => {
2093
 
 
2094
 
2095
+ document
2096
+ .querySelectorAll(
2097
+ ".subject"
2098
+ )
2099
+ .forEach(
2100
 
2101
+ item =>
 
 
2102
 
2103
+ item
2104
+ .classList
2105
+ .remove(
2106
+ "active"
2107
+ )
2108
 
2109
+ );
2110
 
 
 
 
2111
 
 
2112
 
2113
+ button
2114
+ .classList
2115
+ .add(
2116
+ "active"
2117
+ );
2118
 
 
 
 
2119
 
2120
 
2121
+ subject =
2122
+ button.dataset.subject;
 
 
2123
 
 
 
2124
 
 
 
2125
 
2126
+ document
2127
+ .getElementById(
2128
+ "subjectTitle"
2129
+ )
2130
+ .textContent =
2131
+ subject;
2132
+
2133
+ }
2134
 
 
 
 
 
2135
  );
2136
 
2137
+ }
2138
 
2139
+ );
2140
 
 
2141
 
2142
 
2143
+ /* MODES */
 
2144
 
2145
 
2146
+ document
2147
+ .querySelectorAll(
2148
+ ".mode"
2149
+ )
2150
+ .forEach(
2151
 
2152
+ button => {
2153
 
 
 
 
 
2154
 
2155
+ button.addEventListener(
 
2156
 
2157
+ "click",
2158
 
2159
+ () => {
 
 
 
 
 
 
2160
 
 
2161
 
2162
+ document
2163
+ .querySelectorAll(
2164
+ ".mode"
2165
+ )
2166
+ .forEach(
2167
 
2168
+ item =>
 
2169
 
2170
+ item
2171
+ .classList
2172
+ .remove(
2173
+ "active"
2174
+ )
2175
 
2176
+ );
 
 
 
2177
 
 
 
 
 
2178
 
2179
+
2180
+ button
2181
+ .classList
2182
+ .add(
2183
+ "active"
2184
  );
2185
 
 
2186
 
2187
 
2188
+ mode =
2189
+ button.dataset.mode;
2190
 
2191
 
 
 
2192
 
2193
+ document
2194
+ .getElementById(
2195
+ "modeTitle"
2196
+ )
2197
+ .textContent =
2198
+ mode;
2199
 
2200
+ }
2201
 
2202
+ );
 
2203
 
2204
  }
2205
 
2206
+ );
2207
 
 
 
2208
 
 
2209
 
2210
+ /* EXAMPLES */
2211
+
2212
+
2213
+ document
2214
+ .querySelectorAll(
2215
+ ".example"
2216
+ )
2217
+ .forEach(
2218
+
2219
+ button => {
2220
+
2221
+
2222
+ button.addEventListener(
2223
+
2224
+ "click",
2225
+
2226
+ () => {
2227
+
2228
+
2229
+ input.value =
2230
+ button.dataset.example;
2231
+
2232
+
2233
+ input.focus();
2234
+
2235
+ }
2236
+
2237
+ );
2238
+
2239
+ }
2240
 
 
 
 
2241
  );
2242
 
2243
 
 
 
 
2244
 
2245
+ /* NEW CHAT */
 
 
 
2246
 
 
2247
 
2248
+ document
2249
+ .getElementById(
2250
+ "newChat"
2251
+ )
2252
+ .addEventListener(
2253
+
2254
+ "click",
2255
+
2256
+ () => {
2257
 
2258
+
2259
+ resetChat();
2260
+
2261
+
2262
+ errorBox.textContent =
2263
+ "";
2264
 
2265
  }
2266
+
2267
  );
2268
 
2269
+
2270
  </script>
2271
 
2272
+
2273
  </body>
2274
+
2275
  </html>