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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
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
import { useEffect, useState, useRef } from "react";

import Chat from "./components/Chat";
import ArrowRightIcon from "./components/icons/ArrowRightIcon";
import StopIcon from "./components/icons/StopIcon";
import Progress from "./components/Progress";

const IS_WEBGPU_AVAILABLE = !!navigator.gpu;
const STICKY_SCROLL_THRESHOLD = 120;
const EXAMPLES = [
  "Find all complex solutions of z^3 + 2z^2 - z - 2 = 0",
  "A train travels at 60 km/h for the first half of its journey and at 40 km/h for the second half. If the total journey is 300 km, how long does it take?",
  "Write a Python function to check if a given string is a palindrome, ignoring spaces and punctuation.",
];

// Add gradient animation classes
const GRADIENT_CLASSES = "animate-gradient bg-gradient-to-r from-blue-500 via-purple-500 to-pink-500 bg-[length:200%_auto]";
const CARD_HOVER_CLASSES = "hover:scale-105 hover:shadow-xl hover:shadow-blue-500/20 dark:hover:shadow-blue-800/20";

function App() {
  // Create a reference to the worker object.
  const worker = useRef(null);

  const textareaRef = useRef(null);
  const chatContainerRef = useRef(null);

  // Model loading and progress
  const [status, setStatus] = useState(null);
  const [error, setError] = useState(null);
  const [loadingMessage, setLoadingMessage] = useState("");
  const [progressItems, setProgressItems] = useState([]);
  const [isRunning, setIsRunning] = useState(false);

  // Inputs and outputs
  const [input, setInput] = useState("");
  const [messages, setMessages] = useState([]);
  const [tps, setTps] = useState(null);
  const [numTokens, setNumTokens] = useState(null);

  function onEnter(message) {
    setMessages((prev) => [...prev, { role: "user", content: message }]);
    setTps(null);
    setIsRunning(true);
    setInput("");
  }

  function onInterrupt() {
    // NOTE: We do not set isRunning to false here because the worker
    // will send a 'complete' message when it is done.
    worker.current.postMessage({ type: "interrupt" });
  }

  useEffect(() => {
    resizeInput();
  }, [input]);

  function resizeInput() {
    if (!textareaRef.current) return;

    const target = textareaRef.current;
    target.style.height = "auto";
    const newHeight = Math.min(Math.max(target.scrollHeight, 24), 200);
    target.style.height = `${newHeight}px`;
  }

  // We use the `useEffect` hook to setup the worker as soon as the `App` component is mounted.
  useEffect(() => {
    // Create the worker if it does not yet exist.
    if (!worker.current) {
      worker.current = new Worker(new URL("./worker.js", import.meta.url), {
        type: "module",
      });
      worker.current.postMessage({ type: "check" }); // Do a feature check
    }

    // Create a callback function for messages from the worker thread.
    const onMessageReceived = (e) => {
      switch (e.data.status) {
        case "loading":
          // Model file start load: add a new progress item to the list.
          setStatus("loading");
          setLoadingMessage(e.data.data);
          break;

        case "initiate":
          setProgressItems((prev) => [...prev, e.data]);
          break;

        case "progress":
          // Model file progress: update one of the progress items.
          setProgressItems((prev) =>
            prev.map((item) => {
              if (item.file === e.data.file) {
                return { ...item, ...e.data };
              }
              return item;
            }),
          );
          break;

        case "done":
          // Model file loaded: remove the progress item from the list.
          setProgressItems((prev) =>
            prev.filter((item) => item.file !== e.data.file),
          );
          break;

        case "ready":
          // Pipeline ready: the worker is ready to accept messages.
          setStatus("ready");
          break;

        case "start":
          {
            // Start generation
            setMessages((prev) => [
              ...prev,
              { role: "assistant", content: "" },
            ]);
          }
          break;

        case "update":
          {
            // Generation update: update the output text.
            // Parse messages
            const { output, tps, numTokens, state } = e.data;
            setTps(tps);
            setNumTokens(numTokens);
            setMessages((prev) => {
              const cloned = [...prev];
              const last = cloned.at(-1);
              const data = {
                ...last,
                content: last.content + output,
              };
              if (data.answerIndex === undefined && state === "answering") {
                // When state changes to answering, we set the answerIndex
                data.answerIndex = last.content.length;
              }
              cloned[cloned.length - 1] = data;
              return cloned;
            });
          }
          break;

        case "complete":
          // Generation complete: re-enable the "Generate" button
          setIsRunning(false);
          break;

        case "error":
          setError(e.data.data);
          break;
      }
    };

    const onErrorReceived = (e) => {
      console.error("Worker error:", e);
    };

    // Attach the callback function as an event listener.
    worker.current.addEventListener("message", onMessageReceived);
    worker.current.addEventListener("error", onErrorReceived);

    // Define a cleanup function for when the component is unmounted.
    return () => {
      worker.current.removeEventListener("message", onMessageReceived);
      worker.current.removeEventListener("error", onErrorReceived);
    };
  }, []);

  // Send the messages to the worker thread whenever the `messages` state changes.
  useEffect(() => {
    if (messages.filter((x) => x.role === "user").length === 0) {
      // No user messages yet: do nothing.
      return;
    }
    if (messages.at(-1).role === "assistant") {
      // Do not update if the last message is from the assistant
      return;
    }
    setTps(null);
    worker.current.postMessage({ type: "generate", data: messages });
  }, [messages, isRunning]);

  useEffect(() => {
    if (!chatContainerRef.current || !isRunning) return;
    const element = chatContainerRef.current;
    if (
      element.scrollHeight - element.scrollTop - element.clientHeight <
      STICKY_SCROLL_THRESHOLD
    ) {
      element.scrollTop = element.scrollHeight;
    }
  }, [messages, isRunning]);

  return IS_WEBGPU_AVAILABLE ? (
    <div className="flex flex-col h-screen mx-auto items justify-end text-slate-800 dark:text-slate-200 bg-gradient-to-b from-slate-50 via-white to-blue-50 dark:from-gray-900 dark:via-gray-800 dark:to-blue-900/20 transition-colors duration-300">
      {status === null && messages.length === 0 && (
        <div className="h-full overflow-auto scrollbar-thin flex justify-center items-center flex-col relative bg-white/50 dark:bg-gray-900/50 backdrop-blur-sm">
          <div className="flex flex-col items-center mb-1 max-w-[400px] text-center animate-fade-in">
            <div className="text-7xl mb-4 flex items-center gap-3 animate-bounce-slow">
              <span className="animate-pulse">πŸ€”</span>
              <span className="animate-pulse animation-delay-200">πŸ’­</span>
              <span className="animate-pulse animation-delay-400">🧠</span>
            </div>
            <h1 className={`text-5xl font-bold mb-3 ${GRADIENT_CLASSES} bg-clip-text text-transparent transition-all duration-500`}>
              DeepSeek-R1 WebGPU
            </h1>
            <div className="text-8xl mb-4 group perspective">
              <div className="relative transform-gpu transition-all duration-500 animate-float group-hover:scale-110">
                <span className="absolute inset-0 animate-shimmer-fast bg-gradient-to-r from-transparent via-white/30 to-transparent rounded-full blur-sm"></span>
                <span className="relative inline-block animate-shimmer bg-gradient-to-r from-blue-400 via-cyan-300 to-blue-400 bg-clip-text text-transparent filter drop-shadow-glow hover:drop-shadow-glow-strong">
                  🐳
                </span>
                <div className="absolute inset-0 bg-gradient-to-r from-blue-500/20 via-cyan-400/20 to-blue-500/20 rounded-full blur-md animate-pulse-slow"></div>
              </div>
            </div>
            <h2 className="font-semibold text-slate-700 dark:text-slate-300">
              A next-generation reasoning model that runs locally in your browser with WebGPU acceleration.
            </h2>
            <p className="text-sm mt-2 text-slate-500 dark:text-slate-400">
              Forked from{" "}
              <a
                href="https://github.com/huggingface/transformers.js-examples/tree/main/deepseek-r1-webgpu"
                target="_blank"
                rel="noreferrer"
                className="font-medium text-blue-500 dark:text-blue-400 hover:text-blue-600 dark:hover:text-blue-300 transition-colors"
              >
                Xenova & Deepseek-R1
              </a>
              {" "}- Frontend by{" "}
              <a
                href="https://github.com/Reidmen"
                target="_blank"
                rel="noreferrer"
                className="font-medium text-blue-500 dark:text-blue-400 hover:text-blue-600 dark:hover:text-blue-300 transition-colors"
              >
                Reidmen
              </a>
            </p>
          </div>

          <div className="flex flex-col items-center px-4 animate-fade-in-up">
            <p className="max-w-[510px] mb-4 text-slate-600 dark:text-slate-400">
              <br />
              You are about to load{" "}
              <a
                href="https://huggingface.co/onnx-community/DeepSeek-R1-Distill-Qwen-1.5B-ONNX"
                target="_blank"
                rel="noreferrer"
                className="font-medium underline"
              >
                DeepSeek-R1-Distill-Qwen-1.5B
              </a>
              , a 1.5B parameter reasoning LLM. 
              Everything runs entirely in your browser with{" "}
       
            </p>

            {error && (
              <div className="text-red-500 text-center mb-4 p-4 bg-red-50 dark:bg-red-900/20 rounded-xl animate-fade-in">
                <p className="font-semibold mb-2">
                  Unable to load model due to the following error:
                </p>
                <p className="text-sm mb-3 font-mono bg-red-100/50 dark:bg-red-900/40 p-2 rounded">{error}</p>
                
                <div className="text-sm text-red-600 dark:text-red-300">
                  {error.includes("WebGPU") ? (
                    <p>
                      πŸ’‘ This app requires WebGPU support. Please:
                      <ul className="list-disc list-inside mt-1">
                        <li>Enable WebGPU flags in your browser settings</li>
                      </ul>
                    </p>
                  ) : error.includes("memory") ? (
                    <p>
                      πŸ’‘ Memory issue detected. Try:
                      <ul className="list-disc list-inside mt-1">
                        <li>Closing other browser tabs</li>
                        <li>Refreshing the page</li>
                        <li>Using a device with more available memory</li>
                      </ul>
                    </p>
                  ) : error.includes("fetch") ? (
                    <p>
                      πŸ’‘ Network error detected. Please:
                      <ul className="list-disc list-inside mt-1">
                        <li>Check your internet connection</li>
                        <li>Verify if Hugging Face is accessible</li>
                        <li>Try refreshing the page</li>
                      </ul>
                    </p>
                  ) : (
                    <p>
                      πŸ’‘ Try refreshing the page or check the browser console for more details.
                      If the issue persists, please report it on our{" "}
                      <a 
                        href="https://github.com/huggingface/transformers.js-examples/issues"
                        target="_blank"
                        rel="noreferrer"
                        className="underline hover:text-red-400"
                      >
                        GitHub issues
                      </a>.
                    </p>
                  )}
                </div>
              </div>
            )}

            <button
              className={`border px-8 py-3 rounded-2xl ${GRADIENT_CLASSES} text-white font-medium shadow-lg shadow-blue-500/30 dark:shadow-blue-800/30 hover:shadow-xl hover:shadow-blue-500/40 dark:hover:shadow-blue-800/40 disabled:from-slate-400 disabled:to-slate-500 disabled:shadow-none transition-all duration-300 transform hover:scale-105 disabled:hover:scale-100`}
              onClick={() => {
                worker.current.postMessage({ type: "load" });
                setStatus("loading");
              }}
              disabled={status !== null || error !== null}
            >
              Load model
            </button>
          </div>
        </div>
      )}
      {status === "loading" && (
        <div className="w-full max-w-[500px] text-left mx-auto p-6 bottom-0 mt-auto bg-white/90 dark:bg-gray-800/90 backdrop-blur-md rounded-2xl shadow-xl animate-fade-in">
          <p className="text-center mb-1 text-slate-700 dark:text-slate-300">{loadingMessage}</p>
          {progressItems.map(({ file, progress, total }, i) => (
            <Progress
              key={i}
              text={file}
              percentage={progress}
              total={total}
              className="bg-gradient-to-r from-blue-500 to-indigo-500"
            />
          ))}
        </div>
      )}

      {status === "ready" && (
        <div
          ref={chatContainerRef}
          className="overflow-y-auto scrollbar-thin w-full flex flex-col items-center h-full bg-gradient-to-b from-transparent to-blue-50/30 dark:to-blue-900/20"
        >
          <Chat messages={messages} />
          {messages.length === 0 && (
            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 p-6 animate-fade-in-up">
              {EXAMPLES.map((msg, i) => (
                <div
                  key={i}
                  className={`border border-gray-200 dark:border-gray-700 rounded-xl p-4 bg-white/80 dark:bg-gray-800/80 backdrop-blur-sm cursor-pointer ${CARD_HOVER_CLASSES} transition-all duration-300`}
                  onClick={() => onEnter(msg)}
                >
                  {msg}
                </div>
              ))}
            </div>
          )}
          <p className="text-center text-sm min-h-6 text-gray-500 dark:text-gray-300">
            {tps && messages.length > 0 && (
              <>
                {!isRunning && (
                  <span>
                    Generated {numTokens} tokens in{" "}
                    {(numTokens / tps).toFixed(2)} seconds&nbsp;&#40;
                  </span>
                )}
                {
                  <>
                    <span className="font-medium text-center mr-1 text-black dark:text-white">
                      {tps.toFixed(2)}
                    </span>
                    <span className="text-gray-500 dark:text-gray-300">
                      tokens/second
                    </span>
                  </>
                }
                {!isRunning && (
                  <>
                    <span className="mr-1">&#41;.</span>
                    <span
                      className="underline cursor-pointer"
                      onClick={() => {
                        worker.current.postMessage({ type: "reset" });
                        setMessages([]);
                      }}
                    >
                      Reset
                    </span>
                  </>
                )}
              </>
            )}
          </p>
        </div>
      )}

      <div className="mt-4 border-0 rounded-2xl w-[700px] max-w-[90%] mx-auto relative mb-4 bg-white/90 dark:bg-gray-800/90 backdrop-blur-md shadow-lg hover:shadow-xl transition-all duration-300">
        <div className="flex items-center w-full p-2">
          <textarea
            ref={textareaRef}
            className="w-full px-4 py-3 rounded-xl bg-transparent border-none outline-none text-slate-800 dark:text-slate-200 placeholder-slate-400 dark:placeholder-slate-500 disabled:text-slate-400 disabled:placeholder-slate-300 resize-none disabled:cursor-not-allowed focus:ring-2 focus:ring-blue-500/50 transition-all duration-300"
            placeholder="Type your message..."
            type="text"
            rows={1}
            value={input}
            disabled={status !== "ready"}
            title={status === "ready" ? "Model is ready" : "Model not loaded yet"}
            onKeyDown={(e) => {
              if (input.length > 0 && !isRunning && e.key === "Enter" && !e.shiftKey) {
                e.preventDefault();
                onEnter(input);
              }
            }}
            onInput={(e) => setInput(e.target.value)}
          />
          <div className="flex-shrink-0 px-2">
            {isRunning ? (
              <button 
                onClick={onInterrupt}
                className="p-2 rounded-lg hover:bg-red-50 dark:hover:bg-red-900/30 text-red-500 dark:text-red-400 transition-all duration-300 hover:scale-110"
                title="Stop generating"
              >
                <StopIcon className="h-5 w-5" />
              </button>
            ) : (
              <button
                onClick={() => input.length > 0 && onEnter(input)}
                disabled={input.length === 0}
                className={`p-2 rounded-lg transition-all duration-300 ${
                  input.length > 0
                    ? `${GRADIENT_CLASSES} text-white shadow-md shadow-blue-500/30 dark:shadow-blue-800/30 hover:shadow-lg hover:scale-110`
                    : "bg-gray-100 dark:bg-gray-700 text-gray-400 dark:text-gray-600 cursor-not-allowed"
                }`}
                title={input.length > 0 ? "Send message" : "Please type a message"}
              >
                <ArrowRightIcon className="h-5 w-5" />
              </button>
            )}
          </div>
        </div>
      </div>

      <p className="text-xs text-slate-400 dark:text-slate-500 text-center mb-3 animate-pulse">
        Disclaimer: Generated content may be inaccurate or false.
      </p>
    </div>
  ) : (
    <div className="fixed w-screen h-screen bg-black z-10 bg-opacity-[92%] text-white text-2xl font-semibold flex justify-center items-center text-center">
      WebGPU is not supported
      <br />
      by this browser :&#40;
    </div>
  );
}

export default App;