File size: 12,835 Bytes
ac53e27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ffa44e
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
358
359
360
361
362
363
364
365
366
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>dispatchAI — Mobile AI in Your Browser</title>
    <style>
        :root {
            --ink: #0A0F1A;
            --off-white: #F5F7FA;
            --electric-blue: #2E6BFF;
            --cyan: #1FE0E6;
        }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace;
            background: var(--ink);
            color: var(--off-white);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .logo {
            font-size: 2.5em;
            font-weight: 800;
            background: linear-gradient(135deg, var(--electric-blue), var(--cyan));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: -1px;
        }
        .tagline {
            color: #8892a6;
            font-size: 0.9em;
            margin-top: 5px;
        }
        .badge {
            display: inline-block;
            background: rgba(46, 107, 255, 0.15);
            border: 1px solid var(--electric-blue);
            color: var(--electric-blue);
            padding: 3px 10px;
            border-radius: 12px;
            font-size: 0.75em;
            margin-top: 8px;
        }
        .chat-container {
            width: 100%;
            max-width: 700px;
            background: #111827;
            border: 1px solid #1a2332;
            border-radius: 16px;
            overflow: hidden;
        }
        .chat-header {
            background: linear-gradient(135deg, rgba(46, 107, 255, 0.1), rgba(31, 224, 230, 0.1));
            padding: 15px 20px;
            border-bottom: 1px solid #1a2332;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .model-name {
            font-size: 0.85em;
            color: var(--cyan);
        }
        .status {
            font-size: 0.75em;
            padding: 4px 10px;
            border-radius: 8px;
            background: #1a2332;
            color: #8892a6;
        }
        .status.loaded { background: rgba(31, 224, 230, 0.15); color: var(--cyan); }
        .status.loading { background: rgba(46, 107, 255, 0.15); color: var(--electric-blue); }
        .status.error { background: rgba(255, 80, 80, 0.15); color: #ff5050; }
        .messages {
            height: 400px;
            overflow-y: auto;
            padding: 20px;
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
        .messages::-webkit-scrollbar { width: 6px; }
        .messages::-webkit-scrollbar-thumb { background: #1a2332; border-radius: 3px; }
        .msg {
            max-width: 85%;
            padding: 10px 14px;
            border-radius: 12px;
            font-size: 0.9em;
            line-height: 1.5;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .msg.user {
            background: var(--electric-blue);
            color: white;
            align-self: flex-end;
        }
        .msg.ai {
            background: #1a2332;
            color: var(--off-white);
            align-self: flex-start;
            border: 1px solid #233;
        }
        .msg.system {
            background: transparent;
            color: #555;
            font-size: 0.8em;
            align-self: center;
            font-style: italic;
        }
        .input-area {
            padding: 15px 20px;
            border-top: 1px solid #1a2332;
            display: flex;
            gap: 10px;
        }
        .input-area input {
            flex: 1;
            background: #0d1421;
            border: 1px solid #1a2332;
            color: var(--off-white);
            padding: 12px 16px;
            border-radius: 10px;
            font-family: inherit;
            font-size: 0.9em;
            outline: none;
            transition: border-color 0.2s;
        }
        .input-area input:focus { border-color: var(--electric-blue); }
        .input-area input:disabled { opacity: 0.5; }
        .input-area button {
            background: linear-gradient(135deg, var(--electric-blue), var(--cyan));
            color: var(--ink);
            border: none;
            padding: 12px 24px;
            border-radius: 10px;
            font-family: inherit;
            font-weight: 700;
            font-size: 0.9em;
            cursor: pointer;
            transition: opacity 0.2s;
        }
        .input-area button:disabled { opacity: 0.5; cursor: not-allowed; }
        .stats {
            width: 100%;
            max-width: 700px;
            margin-top: 15px;
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        .stat {
            background: #111827;
            border: 1px solid #1a2332;
            border-radius: 8px;
            padding: 8px 14px;
            font-size: 0.75em;
            color: #8892a6;
        }
        .stat span { color: var(--cyan); font-weight: 700; }
        .footer {
            margin-top: 30px;
            text-align: center;
            color: #555;
            font-size: 0.75em;
        }
        .footer a { color: var(--electric-blue); text-decoration: none; }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">dispatchAI</div>
        <div class="tagline">Mobile AI running entirely in your browser. No server. No cloud. Zero cost.</div>
        <div class="badge">100% on-device | WebGPU powered</div>
    </div>

    <div class="chat-container">
        <div class="chat-header">
            <span class="model-name" id="modelName">SmolLM2-360M-Instruct</span>
            <span class="status" id="status">Click Initialize</span>
        </div>
        <div class="messages" id="messages">
            <div class="msg system">This model runs entirely in your browser using WebGPU. No data leaves your device.</div>
        </div>
        <div class="input-area">
            <input type="text" id="input" placeholder="Ask anything..." disabled autocomplete="off">
            <button id="sendBtn" disabled>Send</button>
        </div>
    </div>

    <div class="stats">
        <div class="stat">Model: <span id="statModel">SmolLM2-360M</span></div>
        <div class="stat">Size: <span>~200MB</span></div>
        <div class="stat">Backend: <span id="statBackend">WebGPU</span></div>
        <div class="stat">Tokens/s: <span id="statTps"></span></div>
        <div class="stat">Load time: <span id="statLoad"></span></div>
    </div>

    <div class="footer">
        Powered by <a href="https://huggingface.co/dispatchAI" target="_blank">dispatchAI</a> |
        Model: <a href="https://huggingface.co/dispatchAI/SmolLM2-360M-Instruct-mobile" target="_blank">SmolLM2-360M-Instruct-mobile</a> |
        Built with <a href="https://github.com/xenova/transformers.js" target="_blank">transformers.js</a>
    </div>

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

        // Configure for browser inference
        env.allowLocalModels = false;
        env.useBrowserCache = true;

        let generator = null;
        let isGenerating = false;

        const statusEl = document.getElementById('status');
        const modelNameEl = document.getElementById('modelName');
        const messagesEl = document.getElementById('messages');
        const inputEl = document.getElementById('input');
        const sendBtn = document.getElementById('sendBtn');
        const statTps = document.getElementById('statTps');
        const statLoad = document.getElementById('statLoad');
        const statBackend = document.getElementById('statBackend');

        const MODEL_ID = 'onnx-community/SmolLM2-360M-Instruct-ONNX';

        // Check WebGPU support
        async function checkWebGPU() {
            if (!navigator.gpu) {
                statBackend.textContent = 'WASM (no WebGPU)';
                return 'wasm';
            }
            try {
                const adapter = await navigator.gpu.requestAdapter();
                if (adapter) {
                    statBackend.textContent = 'WebGPU';
                    return 'webgpu';
                }
            } catch (e) {}
            statBackend.textContent = 'WASM fallback';
            return 'wasm';
        }

        function addMessage(text, role) {
            const div = document.createElement('div');
            div.className = `msg ${role}`;
            div.textContent = text;
            messagesEl.appendChild(div);
            messagesEl.scrollTop = messagesEl.scrollHeight;
            return div;
        }

        async function initModel() {
            statusEl.textContent = 'Initializing...';
            statusEl.className = 'status loading';

            const backend = await checkWebGPU();
            const device = backend === 'webgpu' ? 'webgpu' : 'wasm';

            try {
                const t0 = performance.now();
                statusEl.textContent = 'Downloading model...';

                generator = await pipeline('text-generation', MODEL_ID, {
                    device: device,
                    dtype: backend === 'webgpu' ? 'q4f16' : 'q8',
                });

                const loadTime = ((performance.now() - t0) / 1000).toFixed(1);
                statLoad.textContent = `${loadTime}s`;
                statusEl.textContent = 'Ready';
                statusEl.className = 'status loaded';
                inputEl.disabled = false;
                sendBtn.disabled = false;
                inputEl.focus();

                addMessage(`Model loaded in ${loadTime}s on ${backend.toUpperCase()}. Ask me anything!`, 'system');
            } catch (e) {
                statusEl.textContent = 'Failed to load';
                statusEl.className = 'status error';
                addMessage(`Error: ${e.message}. Try refreshing the page.`, 'system');
                console.error(e);
            }
        }

        async function generate() {
            if (isGenerating || !generator) return;
            const text = inputEl.value.trim();
            if (!text) return;

            inputEl.value = '';
            inputEl.disabled = true;
            sendBtn.disabled = true;
            sendBtn.textContent = '...';
            isGenerating = true;

            addMessage(text, 'user');
            const aiMsg = addMessage('', 'ai');

            const messages = [
                { role: 'system', content: 'You are a helpful assistant. Keep responses concise.' },
                { role: 'user', content: text },
            ];

            try {
                const t0 = performance.now();
                let tokenCount = 0;

                // Streaming generation
                const stream = await generator(messages, {
                    max_new_tokens: 128,
                    do_sample: true,
                    temperature: 0.7,
                    streaming: true,
                    callback_function: (data) => {
                        tokenCount++;
                    },
                });

                const elapsed = (performance.now() - t0) / 1000;
                const output = stream[0].generated_text[2].content;

                // Type out the response
                let i = 0;
                const typeInterval = setInterval(() => {
                    if (i < output.length) {
                        aiMsg.textContent = output.slice(0, i + 1);
                        messagesEl.scrollTop = messagesEl.scrollHeight;
                        i++;
                    } else {
                        clearInterval(typeInterval);
                        const tps = (tokenCount / elapsed).toFixed(1);
                        statTps.textContent = `${tps}`;
                    }
                }, 10);

            } catch (e) {
                aiMsg.textContent = `Error: ${e.message}`;
                console.error(e);
            } finally {
                inputEl.disabled = false;
                sendBtn.disabled = false;
                sendBtn.textContent = 'Send';
                isGenerating = false;
                inputEl.focus();
            }
        }

        sendBtn.addEventListener('click', generate);
        inputEl.addEventListener('keydown', (e) => {
            if (e.key === 'Enter') generate();
        });

        // Auto-init on page load
        initModel();
    </script>
</body>
</html>