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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Aged Guru Education Assistant</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Google Fonts for better typography -->
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
  <!-- Include necessary CSS styles -->
  <style>
    /* Reset and base styles */
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: 'Inter', sans-serif;
      background-color: #f9f9f9;
      display: flex;
      min-height: 100vh;
      color: #333;
      overflow: hidden;
    }

    /* Sidebar styling */
    .sidebar {
      width: 260px;
      background-color: #ffffff;
      box-shadow: 2px 0 5px rgba(0,0,0,0.1);
      flex-shrink: 0;
      display: flex;
      flex-direction: column;
      padding: 30px 20px;
      position: fixed;
      height: 100%;
      overflow-y: auto;
      transition: width 0.3s ease;
    }

    .sidebar h2 {
      text-align: center;
      margin-bottom: 40px;
      font-family: 'Roboto', sans-serif;
      font-weight: 600;
      font-size: 1.6em;
      color: #4A4A4A;
    }

    .sidebar a {
      color: #555555;
      padding: 12px 15px;
      text-decoration: none;
      font-size: 1em;
      border-radius: 8px;
      margin-bottom: 10px;
      transition: background-color 0.3s, color 0.3s, transform 0.2s;
      display: flex;
      align-items: center;
    }

    .sidebar a:hover {
      background-color: #f0f0f0;
      color: #333333;
      transform: translateX(5px);
    }

    .sidebar a.active {
      background-color: #e0f7fa;
      color: #00796B;
      font-weight: 500;
    }

    .sidebar a::before {
      content: '';
      display: inline-block;
      width: 6px;
      height: 6px;
      background-color: #00796B;
      border-radius: 50%;
      margin-right: 10px;
      opacity: 0;
      transition: opacity 0.3s;
    }

    .sidebar a.active::before {
      opacity: 1;
    }

    /* Main content styling */
    .main-content {
      margin-left: 260px;
      padding: 30px;
      flex: 1;
      background-color: #f9f9f9;
      overflow-y: auto;
      height: 100vh;
      transition: margin-left 0.3s ease;
    }

    /* Header */
    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 25px;
    }

    .header h1 {
      font-family: 'Roboto', sans-serif;
      font-size: 2em;
      color: #333333;
    }

    /* Card Styling */
    .card {
      background-color: #ffffff;
      border-radius: 12px;
      box-shadow: 0 4px 10px rgba(0,0,0,0.05);
      padding: 25px;
      margin-bottom: 25px;
      transition: transform 0.3s, box-shadow 0.3s;
    }

    .card:hover {
      transform: translateY(-5px);
      box-shadow: 0 6px 15px rgba(0,0,0,0.1);
    }

    /* Button Styling */
    button {
      background-color: #00796B;
      border: none;
      color: white;
      padding: 12px 20px;
      font-size: 1em;
      border-radius: 8px;
      cursor: pointer;
      transition: background-color 0.3s, transform 0.2s;
    }

    button:hover {
      background-color: #005D56;
      transform: translateY(-2px);
    }

    button:disabled {
      background-color: #A5D6A7;
      cursor: not-allowed;
    }

    /* Select Dropdown Styling */
    select {
      width: 100%;
      padding: 10px 15px;
      border: 1px solid #cccccc;
      border-radius: 8px;
      font-size: 1em;
      background-color: #ffffff;
      transition: border-color 0.3s;
      margin-bottom: 15px;
    }

    select:focus {
      border-color: #00796B;
      outline: none;
    }

    /* Chat Box Styling */
    #chat-box {
      height: 400px;
      border: 1px solid #e0e0e0;
      border-radius: 12px;
      overflow-y: auto;
      padding: 20px;
      background-color: #fafafa;
      margin-bottom: 20px;
      position: relative;
      box-shadow: inset 0 2px 4px rgba(0,0,0,0.05);
    }

    .message-container {
      margin-bottom: 20px;
      display: flex;
    }

    .message {
      padding: 12px 18px;
      border-radius: 20px;
      max-width: 75%;
      position: relative;
      word-wrap: break-word;
      box-shadow: 0 2px 8px rgba(0,0,0,0.05);
      font-size: 0.95em;
      line-height: 1.4;
    }

    .user {
      justify-content: flex-end;
    }

    .user .message {
      background-color: #DCF8C6;
      border-bottom-right-radius: 0;
    }

    .assistant {
      justify-content: flex-start;
    }

    .assistant .message {
      background-color: #FFFFFF;
      border-bottom-left-radius: 0;
    }

    /* Microphone Button Styling */
    #mic-container {
      text-align: center;
      margin-bottom: 20px;
    }

    /* Updated Start Button */
    #start_button {
      background-color: #00796B;
      border: none;
      color: white;
      padding: 15px;
      border-radius: 50%;
      cursor: pointer;
      transition: background-color 0.3s, transform 0.2s;
      width: 60px;
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      overflow: hidden;
    }

    #start_button::before {
      content: '';
      position: absolute;
      width: 200%;
      height: 200%;
      background: rgba(255, 255, 255, 0.3);
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) scale(0);
      border-radius: 50%;
      transition: transform 0.5s ease-out;
    }

    #start_button:active::before {
      transform: translate(-50%, -50%) scale(1);
    }

    #start_button:disabled {
      background-color: #A5D6A7;
      cursor: not-allowed;
    }

    #start_button:hover:not(:disabled) {
      background-color: #005D56;
      transform: scale(1.05);
    }

    /* Icon Styling */
    .mic-icon {
      width: 30px;
      height: 30px;
      transition: transform 0.3s;
    }

    /* Animation for Mic Icon */
    .mic-animate {
      animation: pulse 2s infinite;
    }

    @keyframes pulse {
      0% {
        transform: scale(1);
      }
      50% {
        transform: scale(1.2);
      }
      100% {
        transform: scale(1);
      }
    }

    /* Logs Styling */
    #logs {
      height: 120px;
      border: 1px solid #e0e0e0;
      border-radius: 12px;
      overflow-y: auto;
      padding: 15px;
      background-color: #f1f1f1;
      font-size: 0.85em;
      margin-bottom: 10px;
      box-shadow: inset 0 2px 4px rgba(0,0,0,0.05);
    }

    #clear-logs {
      background-color: #FF5252;
      border: none;
      color: white;
      padding: 10px 18px;
      font-size: 0.9em;
      cursor: pointer;
      border-radius: 8px;
      transition: background-color 0.3s, transform 0.2s;
    }

    #clear-logs:hover {
      background-color: #E53935;
      transform: scale(1.02);
    }

    /* Chat Stats */
    #chat-stats {
      font-size: 0.85em;
      color: #888888;
      text-align: center;
      margin-top: 10px;
    }

    /* Voice Selection Container */
    #tts-voice-container label {
      display: block;
      margin-bottom: 8px;
      color: #555555;
      font-weight: 500;
    }

    /* Hidden Class */
    .hidden {
      display: none;
    }

    /* Scrollbar Styling */
    #chat-box::-webkit-scrollbar,
    #logs::-webkit-scrollbar {
      width: 8px;
    }

    #chat-box::-webkit-scrollbar-track,
    #logs::-webkit-scrollbar-track {
      background: #f1f1f1;
      border-radius: 4px;
    }

    #chat-box::-webkit-scrollbar-thumb,
    #logs::-webkit-scrollbar-thumb {
      background: #cccccc;
      border-radius: 4px;
    }

    #chat-box::-webkit-scrollbar-thumb:hover,
    #logs::-webkit-scrollbar-thumb:hover {
      background: #a8a8a8;
    }

    /* Responsive Adjustments */
    @media (max-width: 768px) {
      .sidebar {
        width: 220px;
      }

      .main-content {
        margin-left: 220px;
      }
    }

    @media (max-width: 600px) {
      .sidebar {
        position: relative;
        width: 100%;
        height: auto;
        flex-direction: row;
        overflow-x: auto;
        padding: 15px 10px;
      }

      .sidebar h2 {
        display: none;
      }

      .sidebar a {
        margin-right: 10px;
        margin-bottom: 0;
        padding: 10px 12px;
        font-size: 0.9em;
      }

      .main-content {
        margin-left: 0;
        padding: 20px 15px;
      }

      .header {
        flex-direction: column;
        align-items: flex-start;
      }

      .header h1 {
        margin-bottom: 10px;
        font-size: 1.5em;
      }
    }

    /* Controls Container Styling */
    #controls {
      display: flex;
      gap: 10px;
      margin-bottom: 15px;
      flex-wrap: wrap;
    }

    /* Initialize Button Styling */
    #download {
      flex: 0 0 auto;
      background-color: #00796B;
      border: none;
      color: white;
      padding: 10px 20px;
      font-size: 1em;
      border-radius: 8px;
      cursor: pointer;
      transition: background-color 0.3s, transform 0.2s;
    }

    #download:hover {
      background-color: #005D56;
      transform: translateY(-2px);
    }

    #download:disabled {
      background-color: #A5D6A7;
      cursor: not-allowed;
    }

    /* Enhanced Layout for Digital Human Mentor Voice Section */
    #voice .card {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-gap: 20px;
    }

    /* Adjust controls and voice selection */
    #model-selection-container,
    #tts-voice-container {
      display: flex;
      flex-direction: column;
    }

    #model-selection-container label,
    #tts-voice-container label {
      margin-bottom: 5px;
      font-weight: 500;
      color: #555555;
    }

    /* Chat and Controls Layout */
    #chat-container {
      display: flex;
      flex-direction: column;
    }

    /* Ensure chat-box takes full width */
    #chat-box {
      width: 100%;
    }

    /* Adjust microphone container */
    #mic-container {
      display: flex;
      justify-content: center;
      margin-bottom: 20px;
    }

    /* Configuration Info */
    #configuration {
      margin-top: 15px;
      font-size: 0.9em;
      color: #555555;
    }

    /* Responsive Grid Adjustments */
    @media (max-width: 800px) {
      #voice .card {
        grid-template-columns: 1fr;
      }
    }
  </style>
</head>
<body>

<!-- Sidebar -->
<div class="sidebar">
  <h2>Aged Guru</h2>
  <a href="#" class="active" data-content="voice">Digital Human Mentor Voice</a>
  <a href="#" data-content="video">Digital Human Mentor Video</a>
  <a href="#" data-content="math-assistant">Virtual Math Assistant</a>
  <a href="#" data-content="advanced-math">Advanced Mathematics & Problem Solving</a>
  <a href="#" data-content="interdisciplinary">Interdisciplinary Studies Assistant</a>
  <a href="#" data-content="document-insights">Document Insights</a>
  <a href="#" data-content="3d-explorer">3D Explorer</a>
  <a href="#" data-content="knowledge-base">Knowledge Base</a>
  <a href="#" data-content="">Digital Twin</a>
  <a href="#" data-content="">Student</a>
  <a href="#" data-content="">Teacher</a>
</div>

<!-- Main Content -->
<div class="main-content" id="main-content">
  <div class="header">
    <h1>Aged Guru Education Assistant</h1>
    <!-- You can add user profile or settings here -->
  </div>
  <!-- Content will be loaded here -->
  <div id="content-area">
    <!-- Digital Human Mentor Voice Content Starts -->
    <div id="voice" class="content-section">
      <div class="card">
        <!-- Model Selection Section -->
        <div id="model-selection-container">
          <label for="model-selection">Select Language Model:</label>
          <select id="model-selection"></select>
          <button id="download">Initialize</button>
        </div>

        <!-- Voice Selection Section -->
        <div id="tts-voice-container">
          <label for="voices">Select Voice:</label>
          <select id="voices"></select>
        </div>
      </div>

      <div id="chat-container">
        <div id="configuration" class="hidden">
          <div id="model-info">
            <strong>LLM:</strong> <span id="selected-model">Not Initialized</span>
          </div>
        </div>
        <div id="chat-box"></div>

        <!-- Microphone button -->
        <div id="mic-container">
          <button id="start_button" disabled>
            <svg class="mic-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12 14a3 3 0 003-3V5a3 3 0 00-6 0v6a3 3 0 003 3zm5-3a5 5 0 01-10 0H5a7 7 0 0014 0h-2z"/>
            </svg>
          </button>
        </div>

        <div id="chat-stats" class="hidden"></div>
      </div>

      <h3>Logs</h3>
      <div id="logs">
        <div id="download-status" class="hidden"></div>
      </div>
      <button id="clear-logs">Clear Logs</button>
    </div>
    <!-- Digital Human Mentor Voice Content Ends -->

    <!-- Placeholder for other sections -->
    <div id="video" class="content-section hidden">
      <div class="card">
        <h2>Interactive Digital Human Mentor Video</h2>
        <p>Content for Video Mentor will go here.</p>
      </div>
    </div>

    <div id="math-assistant" class="content-section hidden">
      <div class="card">
        <h2>Virtual Math Assistant</h2>
        <p>Content for Virtual Math Assistant will go here.</p>
      </div>
    </div>

    <div id="advanced-math" class="content-section hidden">
      <div class="card">
        <h2>Advanced Mathematics & Problem Solving</h2>
        <p>Content for Advanced Mathematics & Problem Solving will go here.</p>
      </div>
    </div>

    <div id="interdisciplinary" class="content-section hidden">
      <div class="card">
        <h2>Interdisciplinary Studies Assistant</h2>
        <p>Content for Interdisciplinary Studies Assistant will go here.</p>
      </div>
    </div>

    <div id="document-insights" class="content-section hidden">
      <div class="card">
        <h2>Document Insights</h2>
        <p>Content for Document Insights will go here.</p>
      </div>
    </div>

    <div id="3d-explorer" class="content-section hidden">
      <div class="card">
        <h2>3D Explorer</h2>
        <p>Content for 3D Explorer will go here.</p>
      </div>
    </div>

    <div id="knowledge-base" class="content-section hidden">
      <div class="card">
        <h2>Knowledge Base</h2>
        <p>Content for Knowledge Base will go here.</p>
      </div>
    </div>
  </div>
</div>

<!-- Include the necessary scripts -->
<script type="module">
  import * as webllm from "https://esm.run/@mlc-ai/web-llm";

  // Sidebar navigation
  const sidebarLinks = document.querySelectorAll('.sidebar a');
  const contentSections = document.querySelectorAll('.content-section');

  sidebarLinks.forEach(link => {
    link.addEventListener('click', (e) => {
      e.preventDefault();
      // Remove active class from all links
      sidebarLinks.forEach(l => l.classList.remove('active'));
      // Add active class to the clicked link
      link.classList.add('active');

      // Hide all content sections
      contentSections.forEach(section => section.classList.add('hidden'));

      // Show the selected content section
      const target = link.getAttribute('data-content');
      document.getElementById(target).classList.remove('hidden');
    });
  });

  // Initialize the Digital Human Mentor Voice section
  const messages = [
    {
      content: "You are Chatbot, a friendly, helpful robot called Aged Guru. Respond to what the user said in a creative and helpful way, but keep your responses brief.\n" +
        "Keep responses brief and legible. Your output will be converted to audio.\n" +
        "Your responses will converted to audio. Please do not include any special characters in your response other than '!' or '?'.",
      role: "system"
    }
  ];

  const availableModels = webllm.prebuiltAppConfig.model_list.map(
    (m) => m.model_id
  );
  let selectedModel = "TinyLlama-1.1B-Chat-v0.4-q4f32_1-MLC-1k";

  function updateEngineInitProgressCallback(report) {
    console.log("initialize", report.progress);
    document.getElementById("download-status").textContent = report.text;
  }

  const engine = new webllm.MLCEngine();
  engine.setInitProgressCallback(updateEngineInitProgressCallback);

  async function initializeWebLLMEngine() {
    document.getElementById("download-status").classList.remove("hidden");
    selectedModel = document.getElementById("model-selection").value;
    const config = {
      temperature: 1.0,
      top_p: 1
    };
    await engine.reload(selectedModel, config);
    document.getElementById("selected-model").textContent = selectedModel;
    document.getElementById("start_button").disabled = false;
    document.getElementById("configuration").classList.remove("hidden");
  }

  async function streamingGenerating(messages, onUpdate, onFinish, onError) {
    try {
      let curMessage = "";
      const completion = await engine.chat.completions.create({
        stream: true,
        messages
      });
      for await (const chunk of completion) {
        const curDelta = chunk.choices[0].delta.content;
        if (curDelta) {
          curMessage += curDelta;
        }
        onUpdate(curMessage);
      }
      const finalMessage = await engine.getMessage();
      onFinish(finalMessage);
    } catch (err) {
      onError(err);
    }
  }

  function appendMessage(message) {
    const chatBox = document.getElementById("chat-box");
    const container = document.createElement("div");
    container.classList.add("message-container");
    const newMessage = document.createElement("div");
    newMessage.classList.add("message");
    newMessage.textContent = message.content;

    if (message.role === "user") {
      container.classList.add("user");
    } else {
      container.classList.add("assistant");
    }

    container.appendChild(newMessage);
    chatBox.appendChild(container);
    chatBox.scrollTop = chatBox.scrollHeight;

    // If the message is from the assistant and not a placeholder, trigger TTS
    if (message.role === "assistant" && message.content !== "typing...") {
      speak(message.content);
    }
  }

  function updateLastMessage(content) {
    const messageDoms = document.getElementById("chat-box").querySelectorAll(".message");
    const lastMessageDom = messageDoms[messageDoms.length - 1];
    lastMessageDom.textContent = content;
  }

  function onSpeechRecognized(transcript) {
    const input = transcript.trim();
    const message = {
      content: input,
      role: "user"
    };
    if (input.length === 0) {
      return;
    }
    document.getElementById("start_button").disabled = true;

    messages.push(message);
    appendMessage(message);

    // Append "typing..." placeholder
    const aiPlaceholder = {
      content: "typing...",
      role: "assistant"
    };
    appendMessage(aiPlaceholder);

    const onFinishGenerating = (finalMessage) => {
      // Remove the "typing..." placeholder
      const chatBox = document.getElementById("chat-box");
      const lastMessageContainer = chatBox.lastElementChild;
      if (lastMessageContainer && lastMessageContainer.querySelector(".message").textContent === "typing...") {
        chatBox.removeChild(lastMessageContainer);
      }

      // Append the final message
      const aiMessage = {
        content: finalMessage,
        role: "assistant"
      };
      appendMessage(aiMessage);

      document.getElementById("start_button").disabled = false;
      engine.runtimeStatsText().then((statsText) => {
        document.getElementById("chat-stats").classList.remove("hidden");
        document.getElementById("chat-stats").textContent = statsText;
      });
    };

    streamingGenerating(
      messages,
      updateLastMessage,
      onFinishGenerating,
      console.error
    );
  }

  // Speech recognition code
  var recognizing = false;
  var ignore_onend;
  var final_transcript = '';
  var recognition;

  function startButton(event) {
    if (recognizing) {
      recognition.stop();
      return;
    }
    final_transcript = '';
    recognition.lang = 'en-US';
    recognition.start();
    ignore_onend = false;
    document.getElementById("start_button").classList.add("mic-animate");
  }

  if (!('webkitSpeechRecognition' in window)) {
    alert("Web Speech API is not supported by this browser.");
  } else {
    recognition = new webkitSpeechRecognition();
    recognition.continuous = false; // Non-continuous recognition
    recognition.interimResults = false; // Get only final results

    recognition.onstart = function() {
      recognizing = true;
    };

    recognition.onerror = function(event) {
      if (event.error == 'no-speech') {
        document.getElementById("start_button").classList.remove("mic-animate");
        alert('No speech was detected.');
        ignore_onend = true;
      }
      if (event.error == 'audio-capture') {
        document.getElementById("start_button").classList.remove("mic-animate");
        alert('No microphone was found.');
        ignore_onend = true;
      }
      if (event.error == 'not-allowed') {
        alert('Permission to use microphone was denied.');
        ignore_onend = true;
      }
    };

    recognition.onend = function() {
      recognizing = false;
      document.getElementById("start_button").classList.remove("mic-animate");
      if (ignore_onend) {
        return;
      }
      if (!final_transcript) {
        return;
      }
      // Process the final transcript
      onSpeechRecognized(final_transcript);
    };

    recognition.onresult = function(event) {
      for (var i = event.resultIndex; i < event.results.length; ++i) {
        if (event.results[i].isFinal) {
          final_transcript += event.results[i][0].transcript;
        }
      }
      final_transcript = final_transcript.trim();
    };
  }

  document.getElementById("start_button").addEventListener("click", function(event) {
    startButton(event);
  });

  // Initialize model selection
  availableModels.forEach((modelId) => {
    const option = document.createElement("option");
    option.value = modelId;
    option.textContent = modelId;
    document.getElementById("model-selection").appendChild(option);
  });
  document.getElementById("model-selection").value = selectedModel;
  document.getElementById("download").addEventListener("click", function () {
    initializeWebLLMEngine().then(() => {
      document.getElementById("start_button").disabled = false;
    });
  });

  document.getElementById("clear-logs").addEventListener("click", function () {
    document.getElementById("logs").innerHTML = '';
  });

  // ===== TTS Integration =====

  // Initialize Speech Synthesis
  let speech = new SpeechSynthesisUtterance();
  speech.lang = "en";

  let voices = [];
  window.speechSynthesis.onvoiceschanged = () => {
    voices = window.speechSynthesis.getVoices();
    populateVoices();
  };

  function populateVoices() {
    const voiceSelect = document.getElementById("voices");
    voiceSelect.innerHTML = ''; // Clear existing options
    voices.forEach((voice, i) => {
      const option = new Option(voice.name, i);
      voiceSelect.appendChild(option);
    });
    if (voices.length > 0) {
      speech.voice = voices[0];
    }
  }

  // Voice Selection Event Listener
  document.getElementById("voices").addEventListener("change", () => {
    const selectedVoiceIndex = document.getElementById("voices").value;
    speech.voice = voices[selectedVoiceIndex];
  });

  // Function to speak text
  function speak(text) {
    if (window.speechSynthesis.speaking) {
      window.speechSynthesis.cancel(); // Cancel previous speech
    }
    speech.text = text;
    window.speechSynthesis.speak(speech);
  }
</script>

</body>
</html>