File size: 2,706 Bytes
a6c95b6
 
 
db627b7
 
 
 
 
 
 
 
8301a40
 
db627b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8301a40
db627b7
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Transformers.js Demo</title>
  <style>
    #chat-box { border: 1px solid #ccc; height: 200px; overflow-y: auto; padding: 5px; }
    .msg { margin: 5px 0; }
    .user-message { color: blue; }
    .ai-message { color: green; }
  </style>
</head>
<body>
  <div id="status">Initializing...</div>
  <div id="chat-box"></div>
  <input id="chat-input" placeholder="Type here..." disabled />
  <button id="main-btn" disabled>Loading...</button>

  <script type="module">
    import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@xenova/transformers";

    // CONFIG
    env.allowRemoteModels = true;
    env.allowLocalModels = false;
    env.backends.onnx.wasm.proxy = true;   // safer for laptops
    env.backends.onnx.wasm.numThreads = 2; // laptops can handle more

    const status = document.getElementById('status');
    const btn = document.getElementById('main-btn');
    const input = document.getElementById('chat-input');
    const chatBox = document.getElementById('chat-box');
    let generator = null;

    async function init() {
      try {
        btn.disabled = true;
        status.textContent = "Downloading Model (Stay on page)...";

        generator = await pipeline(
          'text-generation',
          'Xenova/distilbert-base-uncased', // stable ONNX model
          {
            device: 'wasm',
            progress_callback: (d) => {
              if (d.status === 'progress') {
                status.textContent = `Loading: ${Math.round(d.progress)}%`;
              }
            }
          }
        );

        status.textContent = "Ready!";
        btn.textContent = "Send";
        input.disabled = false;
        btn.disabled = false;

        btn.onclick = async () => {
          const userText = input.value.trim();
          if (!userText) return;

          addMessage('user', userText);
          input.value = '';
          status.textContent = "AI is thinking...";

          const output = await generator(userText, {
            max_new_tokens: 30,
            temperature: 0.7
          });

          addMessage('ai', output[0].generated_text.replace(userText, '').trim());
          status.textContent = "Ready!";
        };
      } catch (e) {
        status.textContent = "Error: " + e.message;
        console.error("Pipeline init failed:", e);
      }
    }

    function addMessage(sender, text) {
      const msgDiv = document.createElement('div');
      msgDiv.className = `msg ${sender}-message`;
      msgDiv.textContent = text;
      chatBox.appendChild(msgDiv);
      chatBox.scrollTop = chatBox.scrollHeight;
    }

    // Start
    init();
  </script>
</body>
</html>