File size: 2,085 Bytes
8dc674a
 
 
 
da0f624
 
8dc674a
da0f624
 
 
 
 
8dc674a
 
 
da0f624
 
 
 
 
 
 
 
 
 
8dc674a
 
95586bb
0a68cc2
 
 
 
 
 
 
 
 
 
95586bb
0a68cc2
 
 
95586bb
da0f624
4dc118c
0a68cc2
 
 
 
 
 
 
 
 
 
 
8dc674a
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>GPT-2 Text Predictor</title>
  <style>
    body { font-family: system-ui, sans-serif; max-width: 650px; margin: 40px auto; padding: 0 20px; }
    textarea { width: 100%; height: 120px; font-size: 16px; padding: 10px; box-sizing: border-box; }
    button { padding: 12px 24px; font-size: 16px; margin-top: 10px; cursor: pointer; }
    #output { margin-top: 20px; padding: 15px; background: #f0f0f0; border-radius: 6px; white-space: pre-wrap; font-size: 16px; }
    .status { color: #666; font-style: italic; }
  </style>
</head>
<body>
  <h2>GPT-2 Text Prediction (Static SDK)</h2>
  <p>Enter a prompt below to generate predictions client-side:</p>

  <textarea id="prompt" placeholder="Type starting text here...">The future of artificial intelligence is</textarea>
  <br>
  <button id="predict-btn">Predict Next Words</button>

  <div id="output">
    <span class="status">Click "Predict Next Words" to run the model directly in your browser.</span>
  </div>

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

  const btn = document.getElementById('predict-btn');
  const output = document.getElementById('output');
  let generator = null;

  btn.addEventListener('click', async () => {
    const text = document.getElementById('prompt').value.trim();
    if (!text) return;

    btn.disabled = true;
    output.innerHTML = '<span class="status">Loading ONNX model into browser...</span>';

    try {
      if (!generator) {
        generator = await pipeline('text-generation', 'Xenova/gpt2');
      }

      output.innerHTML = '<span class="status">Generating prediction...</span>';
      const result = await generator(text, { max_new_tokens: 25, temperature: 0.7 });

      output.innerText = result[0].generated_text;
    } catch (err) {
      output.innerText = 'Error: ' + err.message;
    } finally {
      btn.disabled = false;
    }
  });
</script>
</body>
</html>