Upload source code (#3)
Browse files- Upload source code (5199a7f85023acc5bc243308dc599fa76042be48)
- Delete assets (b974d200c52d7ea1d64deba2ce72c18d2267fd2b)
- Update README.md (434f73c9dc70c1237345429b049c454bb2c9afbb)
- Delete banner.png (51f51c190925fafaa1e00280276267b199bcf680)
- Delete logo.png (c9e5c6fcea399fb0ff125b2ab031982a84214316)
- .gitattributes +2 -0
- README.md +50 -9
- assets/index-1aYx7_gh.js +0 -0
- assets/index-B8DcjZ2h.css +0 -1
- assets/worker-Cu04qZVZ.js +0 -0
- eslint.config.js +38 -0
- index.html +1 -2
- package.json +34 -0
- postcss.config.js +6 -0
- banner.png → public/banner.png +0 -0
- logo.png → public/logo.png +0 -0
- src/App.jsx +394 -0
- src/components/Chat.css +129 -0
- src/components/Chat.jsx +67 -0
- src/components/Progress.jsx +22 -0
- src/components/icons/ArrowRightIcon.jsx +19 -0
- src/components/icons/BotIcon.jsx +23 -0
- src/components/icons/StopIcon.jsx +22 -0
- src/components/icons/UserIcon.jsx +19 -0
- src/index.css +32 -0
- src/main.jsx +10 -0
- src/worker.js +166 -0
- tailwind.config.js +8 -0
- vite.config.js +7 -0
.gitattributes
CHANGED
@@ -35,3 +35,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
banner.png filter=lfs diff=lfs merge=lfs -text
|
37 |
logo.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
banner.png filter=lfs diff=lfs merge=lfs -text
|
37 |
logo.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
public/banner.png filter=lfs diff=lfs merge=lfs -text
|
39 |
+
public/logo.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,16 +1,57 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: static
|
7 |
pinned: false
|
8 |
license: apache-2.0
|
9 |
-
short_description: A blazingly fast & powerful AI chatbot that runs in-browser!
|
10 |
-
thumbnail: >-
|
11 |
-
https://huggingface.co/spaces/HuggingFaceTB/SmolLM2-1.7B-Instruct-WebGPU/resolve/main/banner.png
|
12 |
models:
|
13 |
-
- HuggingFaceTB/
|
|
|
|
|
|
|
|
|
14 |
---
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: SmolLM WebGPU
|
3 |
+
emoji: 🤏
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: indigo
|
6 |
sdk: static
|
7 |
pinned: false
|
8 |
license: apache-2.0
|
|
|
|
|
|
|
9 |
models:
|
10 |
+
- HuggingFaceTB/SmolLM-360M-Instruct
|
11 |
+
short_description: A powerful AI chatbot that runs locally in your browser
|
12 |
+
app_build_command: npm run build
|
13 |
+
app_file: dist/index.html
|
14 |
+
thumbnail: https://huggingface.co/spaces/webml-community/smollm-webgpu/resolve/main/public/banner.png
|
15 |
---
|
16 |
|
17 |
+
# SmolLM WebGPU
|
18 |
+
|
19 |
+
A simple React + Vite application for running [SmolLM-360M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM-360M-Instruct), a powerful small language model, locally in the browser using Transformers.js and WebGPU-acceleration.
|
20 |
+
|
21 |
+
## Getting Started
|
22 |
+
|
23 |
+
Follow the steps below to set up and run the application.
|
24 |
+
|
25 |
+
### 1. Clone the Repository
|
26 |
+
|
27 |
+
Clone the examples repository from GitHub:
|
28 |
+
|
29 |
+
```sh
|
30 |
+
git clone https://github.com/huggingface/transformers.js-examples.git
|
31 |
+
```
|
32 |
+
|
33 |
+
### 2. Navigate to the Project Directory
|
34 |
+
|
35 |
+
Change your working directory to the `smollm-webgpu` folder:
|
36 |
+
|
37 |
+
```sh
|
38 |
+
cd transformers.js-examples/smollm-webgpu
|
39 |
+
```
|
40 |
+
|
41 |
+
### 3. Install Dependencies
|
42 |
+
|
43 |
+
Install the necessary dependencies using npm:
|
44 |
+
|
45 |
+
```sh
|
46 |
+
npm i
|
47 |
+
```
|
48 |
+
|
49 |
+
### 4. Run the Development Server
|
50 |
+
|
51 |
+
Start the development server:
|
52 |
+
|
53 |
+
```sh
|
54 |
+
npm run dev
|
55 |
+
```
|
56 |
+
|
57 |
+
The application should now be running locally. Open your browser and go to `http://localhost:5173` to see it in action.
|
assets/index-1aYx7_gh.js
DELETED
The diff for this file is too large to render.
See raw diff
|
|
assets/index-B8DcjZ2h.css
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
@scope (.markdown){pre{margin:.5rem 0;white-space:break-spaces}code{padding:.2em .4em;border-radius:4px;font-family:Consolas,Monaco,Andale Mono,Ubuntu Mono,monospace;font-size:.9em}pre,code{background-color:#f2f2f2}@media (prefers-color-scheme: dark){pre,code{background-color:#333}}pre:has(code){padding:1rem .5rem}pre>code{padding:0}h1,h2,h3,h4,h5,h6{font-weight:600;line-height:1.2}h1{font-size:2em;margin:1rem 0}h2{font-size:1.5em;margin:.83rem 0}h3{font-size:1.25em;margin:.67rem 0}h4{font-size:1em;margin:.5rem 0}h5{font-size:.875em;margin:.33rem 0}h6{font-size:.75em;margin:.25rem 0}h1,h2,h3,h4,h5,h6:first-child{margin-top:0}ul{list-style-type:disc;margin-left:1.5rem}ol{list-style-type:decimal;margin-left:1.5rem}li{margin:.25rem 0}p:not(:first-child){margin-top:.75rem}p:not(:last-child){margin-bottom:.75rem}ul>li{margin-left:1rem}table,th,td{border:1px solid lightgray;padding:.25rem}@media (prefers-color-scheme: dark){table,th,td{border:1px solid #f2f2f2}}}*,:before,:after{box-sizing:border-box;border-width:0;border-style:solid;border-color:#e5e7eb}:before,:after{--tw-content: ""}html,:host{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji";font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,samp,pre{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:transparent;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:baseline}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dl,dd,h1,h2,h3,h4,h5,h6,hr,figure,p,pre{margin:0}fieldset{margin:0;padding:0}legend{padding:0}ol,ul,menu{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}button,[role=button]{cursor:pointer}:disabled{cursor:default}img,svg,video,canvas,audio,iframe,embed,object{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,:before,:after{--tw-border-spacing-x: 0;--tw-border-spacing-y: 0;--tw-translate-x: 0;--tw-translate-y: 0;--tw-rotate: 0;--tw-skew-x: 0;--tw-skew-y: 0;--tw-scale-x: 1;--tw-scale-y: 1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness: proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width: 0px;--tw-ring-offset-color: #fff;--tw-ring-color: rgb(59 130 246 / .5);--tw-ring-offset-shadow: 0 0 #0000;--tw-ring-shadow: 0 0 #0000;--tw-shadow: 0 0 #0000;--tw-shadow-colored: 0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }::backdrop{--tw-border-spacing-x: 0;--tw-border-spacing-y: 0;--tw-translate-x: 0;--tw-translate-y: 0;--tw-rotate: 0;--tw-skew-x: 0;--tw-skew-y: 0;--tw-scale-x: 1;--tw-scale-y: 1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness: proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width: 0px;--tw-ring-offset-color: #fff;--tw-ring-color: rgb(59 130 246 / .5);--tw-ring-offset-shadow: 0 0 #0000;--tw-ring-shadow: 0 0 #0000;--tw-shadow: 0 0 #0000;--tw-shadow-colored: 0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.static{position:static}.fixed{position:fixed}.absolute{position:absolute}.relative{position:relative}.bottom-0{bottom:0}.bottom-3{bottom:.75rem}.right-3{right:.75rem}.z-10{z-index:10}.m-1{margin:.25rem}.mx-auto{margin-left:auto;margin-right:auto}.my-3{margin-top:.75rem;margin-bottom:.75rem}.mb-0\.5{margin-bottom:.125rem}.mb-1{margin-bottom:.25rem}.mb-2{margin-bottom:.5rem}.mb-3{margin-bottom:.75rem}.mb-4{margin-bottom:1rem}.mr-1{margin-right:.25rem}.mt-2{margin-top:.5rem}.mt-auto{margin-top:auto}.block{display:block}.flex{display:flex}.h-2\.5{height:.625rem}.h-6{height:1.5rem}.h-8{height:2rem}.h-full{height:100%}.h-screen{height:100vh}.max-h-\[200px\]{max-height:200px}.min-h-6{min-height:1.5rem}.w-2\.5{width:.625rem}.w-6{width:1.5rem}.w-8{width:2rem}.w-\[550px\]{width:550px}.w-\[600px\]{width:600px}.w-full{width:100%}.w-screen{width:100vw}.min-w-6{min-width:1.5rem}.max-w-\[320px\]{max-width:320px}.max-w-\[480px\]{max-width:480px}.max-w-\[500px\]{max-width:500px}.max-w-\[80\%\]{max-width:80%}.max-w-\[960px\]{max-width:960px}.flex-1{flex:1 1 0%}@keyframes pulse{50%{opacity:.5}}.animate-pulse{animation:pulse 2s cubic-bezier(.4,0,.6,1) infinite}.cursor-pointer{cursor:pointer}.select-none{-webkit-user-select:none;-moz-user-select:none;user-select:none}.resize-none{resize:none}.flex-col{flex-direction:column}.items-start{align-items:flex-start}.items-center{align-items:center}.justify-end{justify-content:flex-end}.justify-center{justify-content:center}.gap-1{gap:.25rem}.space-x-4>:not([hidden])~:not([hidden]){--tw-space-x-reverse: 0;margin-right:calc(1rem * var(--tw-space-x-reverse));margin-left:calc(1rem * calc(1 - var(--tw-space-x-reverse)))}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse: 0;margin-top:calc(1rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem * var(--tw-space-y-reverse))}.overflow-auto{overflow:auto}.overflow-hidden{overflow:hidden}.overflow-y-auto{overflow-y:auto}.whitespace-nowrap{white-space:nowrap}.rounded-full{border-radius:9999px}.rounded-lg{border-radius:.5rem}.rounded-md{border-radius:.375rem}.border{border-width:1px}.border-none{border-style:none}.bg-black{--tw-bg-opacity: 1;background-color:rgb(0 0 0 / var(--tw-bg-opacity))}.bg-blue-400{--tw-bg-opacity: 1;background-color:rgb(96 165 250 / var(--tw-bg-opacity))}.bg-blue-500{--tw-bg-opacity: 1;background-color:rgb(59 130 246 / var(--tw-bg-opacity))}.bg-gray-100{--tw-bg-opacity: 1;background-color:rgb(243 244 246 / var(--tw-bg-opacity))}.bg-gray-200{--tw-bg-opacity: 1;background-color:rgb(229 231 235 / var(--tw-bg-opacity))}.bg-gray-600{--tw-bg-opacity: 1;background-color:rgb(75 85 99 / var(--tw-bg-opacity))}.bg-gray-800{--tw-bg-opacity: 1;background-color:rgb(31 41 55 / var(--tw-bg-opacity))}.bg-transparent{background-color:transparent}.bg-white{--tw-bg-opacity: 1;background-color:rgb(255 255 255 / var(--tw-bg-opacity))}.bg-opacity-\[92\%\]{--tw-bg-opacity: 92%}.p-1{padding:.25rem}.p-2{padding:.5rem}.p-4{padding:1rem}.p-6{padding:1.5rem}.px-1{padding-left:.25rem;padding-right:.25rem}.px-3{padding-left:.75rem;padding-right:.75rem}.px-4{padding-left:1rem;padding-right:1rem}.py-2{padding-top:.5rem;padding-bottom:.5rem}.py-4{padding-top:1rem;padding-bottom:1rem}.text-left{text-align:left}.text-center{text-align:center}.text-2xl{font-size:1.5rem;line-height:2rem}.text-4xl{font-size:2.25rem;line-height:2.5rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.text-xs{font-size:.75rem;line-height:1rem}.font-bold{font-weight:700}.font-medium{font-weight:500}.font-semibold{font-weight:600}.text-black{--tw-text-opacity: 1;color:rgb(0 0 0 / var(--tw-text-opacity))}.text-gray-400{--tw-text-opacity: 1;color:rgb(156 163 175 / var(--tw-text-opacity))}.text-gray-50{--tw-text-opacity: 1;color:rgb(249 250 251 / var(--tw-text-opacity))}.text-gray-500{--tw-text-opacity: 1;color:rgb(107 114 128 / var(--tw-text-opacity))}.text-gray-800{--tw-text-opacity: 1;color:rgb(31 41 55 / var(--tw-text-opacity))}.text-red-500{--tw-text-opacity: 1;color:rgb(239 68 68 / var(--tw-text-opacity))}.text-white{--tw-text-opacity: 1;color:rgb(255 255 255 / var(--tw-text-opacity))}.underline{text-decoration-line:underline}.placeholder-gray-500::-moz-placeholder{--tw-placeholder-opacity: 1;color:rgb(107 114 128 / var(--tw-placeholder-opacity))}.placeholder-gray-500::placeholder{--tw-placeholder-opacity: 1;color:rgb(107 114 128 / var(--tw-placeholder-opacity))}.outline-none{outline:2px solid transparent;outline-offset:2px}.filter{filter:var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow)}.scrollbar-thin::-webkit-scrollbar{width:.5rem}.scrollbar-thin::-webkit-scrollbar-track{border-radius:9999px;--tw-bg-opacity: 1;background-color:rgb(243 244 246 / var(--tw-bg-opacity))}@media (prefers-color-scheme: dark){.scrollbar-thin::-webkit-scrollbar-track{--tw-bg-opacity: 1;background-color:rgb(55 65 81 / var(--tw-bg-opacity))}}.scrollbar-thin::-webkit-scrollbar-thumb{border-radius:9999px;--tw-bg-opacity: 1;background-color:rgb(209 213 219 / var(--tw-bg-opacity))}@media (prefers-color-scheme: dark){.scrollbar-thin::-webkit-scrollbar-thumb{--tw-bg-opacity: 1;background-color:rgb(75 85 99 / var(--tw-bg-opacity))}}.scrollbar-thin::-webkit-scrollbar-thumb:hover{--tw-bg-opacity: 1;background-color:rgb(107 114 128 / var(--tw-bg-opacity))}.animation-delay-200{animation-delay:.2s}.animation-delay-400{animation-delay:.4s}.overflow-wrap-anywhere{overflow-wrap:anywhere}.hover\:bg-blue-500:hover{--tw-bg-opacity: 1;background-color:rgb(59 130 246 / var(--tw-bg-opacity))}.disabled\:cursor-not-allowed:disabled{cursor:not-allowed}.disabled\:bg-blue-100:disabled{--tw-bg-opacity: 1;background-color:rgb(219 234 254 / var(--tw-bg-opacity))}.disabled\:text-gray-400:disabled{--tw-text-opacity: 1;color:rgb(156 163 175 / var(--tw-text-opacity))}.disabled\:placeholder-gray-200:disabled::-moz-placeholder{--tw-placeholder-opacity: 1;color:rgb(229 231 235 / var(--tw-placeholder-opacity))}.disabled\:placeholder-gray-200:disabled::placeholder{--tw-placeholder-opacity: 1;color:rgb(229 231 235 / var(--tw-placeholder-opacity))}@media (prefers-color-scheme: dark){.dark\:border-gray-600{--tw-border-opacity: 1;border-color:rgb(75 85 99 / var(--tw-border-opacity))}.dark\:bg-gray-100{--tw-bg-opacity: 1;background-color:rgb(243 244 246 / var(--tw-bg-opacity))}.dark\:bg-gray-300{--tw-bg-opacity: 1;background-color:rgb(209 213 219 / var(--tw-bg-opacity))}.dark\:bg-gray-600{--tw-bg-opacity: 1;background-color:rgb(75 85 99 / var(--tw-bg-opacity))}.dark\:bg-gray-700{--tw-bg-opacity: 1;background-color:rgb(55 65 81 / var(--tw-bg-opacity))}.dark\:bg-gray-900{--tw-bg-opacity: 1;background-color:rgb(17 24 39 / var(--tw-bg-opacity))}.dark\:text-black{--tw-text-opacity: 1;color:rgb(0 0 0 / var(--tw-text-opacity))}.dark\:text-gray-100{--tw-text-opacity: 1;color:rgb(243 244 246 / var(--tw-text-opacity))}.dark\:text-gray-200{--tw-text-opacity: 1;color:rgb(229 231 235 / var(--tw-text-opacity))}.dark\:text-gray-300{--tw-text-opacity: 1;color:rgb(209 213 219 / var(--tw-text-opacity))}.dark\:text-gray-800{--tw-text-opacity: 1;color:rgb(31 41 55 / var(--tw-text-opacity))}.dark\:text-white{--tw-text-opacity: 1;color:rgb(255 255 255 / var(--tw-text-opacity))}.dark\:placeholder-gray-400::-moz-placeholder{--tw-placeholder-opacity: 1;color:rgb(156 163 175 / var(--tw-placeholder-opacity))}.dark\:placeholder-gray-400::placeholder{--tw-placeholder-opacity: 1;color:rgb(156 163 175 / var(--tw-placeholder-opacity))}}
|
|
|
|
assets/worker-Cu04qZVZ.js
DELETED
The diff for this file is too large to render.
See raw diff
|
|
eslint.config.js
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import js from "@eslint/js";
|
2 |
+
import globals from "globals";
|
3 |
+
import react from "eslint-plugin-react";
|
4 |
+
import reactHooks from "eslint-plugin-react-hooks";
|
5 |
+
import reactRefresh from "eslint-plugin-react-refresh";
|
6 |
+
|
7 |
+
export default [
|
8 |
+
{ ignores: ["dist"] },
|
9 |
+
{
|
10 |
+
files: ["**/*.{js,jsx}"],
|
11 |
+
languageOptions: {
|
12 |
+
ecmaVersion: 2020,
|
13 |
+
globals: globals.browser,
|
14 |
+
parserOptions: {
|
15 |
+
ecmaVersion: "latest",
|
16 |
+
ecmaFeatures: { jsx: true },
|
17 |
+
sourceType: "module",
|
18 |
+
},
|
19 |
+
},
|
20 |
+
settings: { react: { version: "18.3" } },
|
21 |
+
plugins: {
|
22 |
+
react,
|
23 |
+
"react-hooks": reactHooks,
|
24 |
+
"react-refresh": reactRefresh,
|
25 |
+
},
|
26 |
+
rules: {
|
27 |
+
...js.configs.recommended.rules,
|
28 |
+
...react.configs.recommended.rules,
|
29 |
+
...react.configs["jsx-runtime"].rules,
|
30 |
+
...reactHooks.configs.recommended.rules,
|
31 |
+
"react/jsx-no-target-blank": "off",
|
32 |
+
"react-refresh/only-export-components": [
|
33 |
+
"warn",
|
34 |
+
{ allowConstantExport: true },
|
35 |
+
],
|
36 |
+
},
|
37 |
+
},
|
38 |
+
];
|
index.html
CHANGED
@@ -5,8 +5,6 @@
|
|
5 |
<link rel="icon" type="image/png" href="/logo.png" />
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
7 |
<title>SmolLM2 WebGPU</title>
|
8 |
-
<script type="module" crossorigin src="/assets/index-1aYx7_gh.js"></script>
|
9 |
-
<link rel="stylesheet" crossorigin href="/assets/index-B8DcjZ2h.css">
|
10 |
</head>
|
11 |
|
12 |
<body>
|
@@ -29,5 +27,6 @@
|
|
29 |
id="MathJax-script"
|
30 |
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
|
31 |
></script>
|
|
|
32 |
</body>
|
33 |
</html>
|
|
|
5 |
<link rel="icon" type="image/png" href="/logo.png" />
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
7 |
<title>SmolLM2 WebGPU</title>
|
|
|
|
|
8 |
</head>
|
9 |
|
10 |
<body>
|
|
|
27 |
id="MathJax-script"
|
28 |
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
|
29 |
></script>
|
30 |
+
<script type="module" src="/src/main.jsx"></script>
|
31 |
</body>
|
32 |
</html>
|
package.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "smollm-webgpu",
|
3 |
+
"private": true,
|
4 |
+
"version": "0.0.0",
|
5 |
+
"type": "module",
|
6 |
+
"scripts": {
|
7 |
+
"dev": "vite",
|
8 |
+
"build": "vite build",
|
9 |
+
"lint": "eslint .",
|
10 |
+
"preview": "vite preview"
|
11 |
+
},
|
12 |
+
"dependencies": {
|
13 |
+
"@huggingface/transformers": "3.7.1",
|
14 |
+
"dompurify": "^3.1.2",
|
15 |
+
"marked": "^12.0.2",
|
16 |
+
"react": "^18.3.1",
|
17 |
+
"react-dom": "^18.3.1"
|
18 |
+
},
|
19 |
+
"devDependencies": {
|
20 |
+
"@eslint/js": "^9.9.0",
|
21 |
+
"@types/react": "^18.3.3",
|
22 |
+
"@types/react-dom": "^18.3.0",
|
23 |
+
"@vitejs/plugin-react": "^4.3.1",
|
24 |
+
"autoprefixer": "^10.4.20",
|
25 |
+
"eslint": "^9.9.0",
|
26 |
+
"eslint-plugin-react": "^7.35.0",
|
27 |
+
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
|
28 |
+
"eslint-plugin-react-refresh": "^0.4.9",
|
29 |
+
"globals": "^15.9.0",
|
30 |
+
"postcss": "^8.4.41",
|
31 |
+
"tailwindcss": "^3.4.10",
|
32 |
+
"vite": "^6.2.0"
|
33 |
+
}
|
34 |
+
}
|
postcss.config.js
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export default {
|
2 |
+
plugins: {
|
3 |
+
tailwindcss: {},
|
4 |
+
autoprefixer: {},
|
5 |
+
},
|
6 |
+
};
|
banner.png → public/banner.png
RENAMED
File without changes
|
logo.png → public/logo.png
RENAMED
File without changes
|
src/App.jsx
ADDED
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { useEffect, useState, useRef } from "react";
|
2 |
+
|
3 |
+
import Chat from "./components/Chat";
|
4 |
+
import ArrowRightIcon from "./components/icons/ArrowRightIcon";
|
5 |
+
import StopIcon from "./components/icons/StopIcon";
|
6 |
+
import Progress from "./components/Progress";
|
7 |
+
|
8 |
+
const IS_WEBGPU_AVAILABLE = !!navigator.gpu;
|
9 |
+
const STICKY_SCROLL_THRESHOLD = 120;
|
10 |
+
const EXAMPLES = [
|
11 |
+
"Give me some tips to improve my time management skills.",
|
12 |
+
"What is the difference between AI and ML?",
|
13 |
+
"Write python code to compute the nth fibonacci number.",
|
14 |
+
];
|
15 |
+
|
16 |
+
function App() {
|
17 |
+
// Create a reference to the worker object.
|
18 |
+
const worker = useRef(null);
|
19 |
+
|
20 |
+
const textareaRef = useRef(null);
|
21 |
+
const chatContainerRef = useRef(null);
|
22 |
+
|
23 |
+
// Model loading and progress
|
24 |
+
const [status, setStatus] = useState(null);
|
25 |
+
const [error, setError] = useState(null);
|
26 |
+
const [loadingMessage, setLoadingMessage] = useState("");
|
27 |
+
const [progressItems, setProgressItems] = useState([]);
|
28 |
+
const [isRunning, setIsRunning] = useState(false);
|
29 |
+
|
30 |
+
// Inputs and outputs
|
31 |
+
const [input, setInput] = useState("");
|
32 |
+
const [messages, setMessages] = useState([]);
|
33 |
+
const [tps, setTps] = useState(null);
|
34 |
+
const [numTokens, setNumTokens] = useState(null);
|
35 |
+
|
36 |
+
function onEnter(message) {
|
37 |
+
setMessages((prev) => [...prev, { role: "user", content: message }]);
|
38 |
+
setTps(null);
|
39 |
+
setIsRunning(true);
|
40 |
+
setInput("");
|
41 |
+
}
|
42 |
+
|
43 |
+
function onInterrupt() {
|
44 |
+
// NOTE: We do not set isRunning to false here because the worker
|
45 |
+
// will send a 'complete' message when it is done.
|
46 |
+
worker.current.postMessage({ type: "interrupt" });
|
47 |
+
}
|
48 |
+
|
49 |
+
useEffect(() => {
|
50 |
+
resizeInput();
|
51 |
+
}, [input]);
|
52 |
+
|
53 |
+
function resizeInput() {
|
54 |
+
if (!textareaRef.current) return;
|
55 |
+
|
56 |
+
const target = textareaRef.current;
|
57 |
+
target.style.height = "auto";
|
58 |
+
const newHeight = Math.min(Math.max(target.scrollHeight, 24), 200);
|
59 |
+
target.style.height = `${newHeight}px`;
|
60 |
+
}
|
61 |
+
|
62 |
+
// We use the `useEffect` hook to setup the worker as soon as the `App` component is mounted.
|
63 |
+
useEffect(() => {
|
64 |
+
// Create the worker if it does not yet exist.
|
65 |
+
if (!worker.current) {
|
66 |
+
worker.current = new Worker(new URL("./worker.js", import.meta.url), {
|
67 |
+
type: "module",
|
68 |
+
});
|
69 |
+
worker.current.postMessage({ type: "check" }); // Do a feature check
|
70 |
+
}
|
71 |
+
|
72 |
+
// Create a callback function for messages from the worker thread.
|
73 |
+
const onMessageReceived = (e) => {
|
74 |
+
switch (e.data.status) {
|
75 |
+
case "loading":
|
76 |
+
// Model file start load: add a new progress item to the list.
|
77 |
+
setStatus("loading");
|
78 |
+
setLoadingMessage(e.data.data);
|
79 |
+
break;
|
80 |
+
|
81 |
+
case "initiate":
|
82 |
+
setProgressItems((prev) => [...prev, e.data]);
|
83 |
+
break;
|
84 |
+
|
85 |
+
case "progress":
|
86 |
+
// Model file progress: update one of the progress items.
|
87 |
+
setProgressItems((prev) =>
|
88 |
+
prev.map((item) => {
|
89 |
+
if (item.file === e.data.file) {
|
90 |
+
return { ...item, ...e.data };
|
91 |
+
}
|
92 |
+
return item;
|
93 |
+
}),
|
94 |
+
);
|
95 |
+
break;
|
96 |
+
|
97 |
+
case "done":
|
98 |
+
// Model file loaded: remove the progress item from the list.
|
99 |
+
setProgressItems((prev) =>
|
100 |
+
prev.filter((item) => item.file !== e.data.file),
|
101 |
+
);
|
102 |
+
break;
|
103 |
+
|
104 |
+
case "ready":
|
105 |
+
// Pipeline ready: the worker is ready to accept messages.
|
106 |
+
setStatus("ready");
|
107 |
+
break;
|
108 |
+
|
109 |
+
case "start":
|
110 |
+
{
|
111 |
+
// Start generation
|
112 |
+
setMessages((prev) => [
|
113 |
+
...prev,
|
114 |
+
{ role: "assistant", content: "" },
|
115 |
+
]);
|
116 |
+
}
|
117 |
+
break;
|
118 |
+
|
119 |
+
case "update":
|
120 |
+
{
|
121 |
+
// Generation update: update the output text.
|
122 |
+
// Parse messages
|
123 |
+
const { output, tps, numTokens } = e.data;
|
124 |
+
setTps(tps);
|
125 |
+
setNumTokens(numTokens);
|
126 |
+
setMessages((prev) => {
|
127 |
+
const cloned = [...prev];
|
128 |
+
const last = cloned.at(-1);
|
129 |
+
cloned[cloned.length - 1] = {
|
130 |
+
...last,
|
131 |
+
content: last.content + output,
|
132 |
+
};
|
133 |
+
return cloned;
|
134 |
+
});
|
135 |
+
}
|
136 |
+
break;
|
137 |
+
|
138 |
+
case "complete":
|
139 |
+
// Generation complete: re-enable the "Generate" button
|
140 |
+
setIsRunning(false);
|
141 |
+
break;
|
142 |
+
|
143 |
+
case "error":
|
144 |
+
setError(e.data.data);
|
145 |
+
break;
|
146 |
+
}
|
147 |
+
};
|
148 |
+
|
149 |
+
const onErrorReceived = (e) => {
|
150 |
+
console.error("Worker error:", e);
|
151 |
+
};
|
152 |
+
|
153 |
+
// Attach the callback function as an event listener.
|
154 |
+
worker.current.addEventListener("message", onMessageReceived);
|
155 |
+
worker.current.addEventListener("error", onErrorReceived);
|
156 |
+
|
157 |
+
// Define a cleanup function for when the component is unmounted.
|
158 |
+
return () => {
|
159 |
+
worker.current.removeEventListener("message", onMessageReceived);
|
160 |
+
worker.current.removeEventListener("error", onErrorReceived);
|
161 |
+
};
|
162 |
+
}, []);
|
163 |
+
|
164 |
+
// Send the messages to the worker thread whenever the `messages` state changes.
|
165 |
+
useEffect(() => {
|
166 |
+
if (messages.filter((x) => x.role === "user").length === 0) {
|
167 |
+
// No user messages yet: do nothing.
|
168 |
+
return;
|
169 |
+
}
|
170 |
+
if (messages.at(-1).role === "assistant") {
|
171 |
+
// Do not update if the last message is from the assistant
|
172 |
+
return;
|
173 |
+
}
|
174 |
+
setTps(null);
|
175 |
+
worker.current.postMessage({ type: "generate", data: messages });
|
176 |
+
}, [messages, isRunning]);
|
177 |
+
|
178 |
+
useEffect(() => {
|
179 |
+
if (!chatContainerRef.current || !isRunning) return;
|
180 |
+
const element = chatContainerRef.current;
|
181 |
+
if (
|
182 |
+
element.scrollHeight - element.scrollTop - element.clientHeight <
|
183 |
+
STICKY_SCROLL_THRESHOLD
|
184 |
+
) {
|
185 |
+
element.scrollTop = element.scrollHeight;
|
186 |
+
}
|
187 |
+
}, [messages, isRunning]);
|
188 |
+
|
189 |
+
return IS_WEBGPU_AVAILABLE ? (
|
190 |
+
<div className="flex flex-col h-screen mx-auto items justify-end text-gray-800 dark:text-gray-200 bg-white dark:bg-gray-900">
|
191 |
+
{status === null && messages.length === 0 && (
|
192 |
+
<div className="h-full overflow-auto scrollbar-thin flex justify-center items-center flex-col relative">
|
193 |
+
<div className="flex flex-col items-center mb-1 max-w-[320px] text-center">
|
194 |
+
<img
|
195 |
+
src="logo.png"
|
196 |
+
width="80%"
|
197 |
+
height="auto"
|
198 |
+
className="block"
|
199 |
+
></img>
|
200 |
+
<h1 className="text-4xl font-bold mb-1">SmolLM2 WebGPU</h1>
|
201 |
+
<h2 className="font-semibold">
|
202 |
+
A blazingly fast and powerful AI chatbot that runs locally in your
|
203 |
+
browser.
|
204 |
+
</h2>
|
205 |
+
</div>
|
206 |
+
|
207 |
+
<div className="flex flex-col items-center px-4">
|
208 |
+
<p className="max-w-[480px] mb-4">
|
209 |
+
<br />
|
210 |
+
You are about to load{" "}
|
211 |
+
<a
|
212 |
+
href="https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
213 |
+
target="_blank"
|
214 |
+
rel="noreferrer"
|
215 |
+
className="font-medium underline"
|
216 |
+
>
|
217 |
+
SmolLM2-1.7B-Instruct
|
218 |
+
</a>
|
219 |
+
, a 1.7B parameter LLM optimized for in-browser inference.
|
220 |
+
Everything runs entirely in your browser with{" "}
|
221 |
+
<a
|
222 |
+
href="https://huggingface.co/docs/transformers.js"
|
223 |
+
target="_blank"
|
224 |
+
rel="noreferrer"
|
225 |
+
className="underline"
|
226 |
+
>
|
227 |
+
🤗 Transformers.js
|
228 |
+
</a>{" "}
|
229 |
+
and ONNX Runtime Web, meaning no data is sent to a server. Once
|
230 |
+
loaded, it can even be used offline. The source code for the demo
|
231 |
+
is available on{" "}
|
232 |
+
<a
|
233 |
+
href="https://github.com/huggingface/transformers.js-examples/tree/main/smollm-webgpu"
|
234 |
+
target="_blank"
|
235 |
+
rel="noreferrer"
|
236 |
+
className="font-medium underline"
|
237 |
+
>
|
238 |
+
GitHub
|
239 |
+
</a>
|
240 |
+
.
|
241 |
+
</p>
|
242 |
+
|
243 |
+
{error && (
|
244 |
+
<div className="text-red-500 text-center mb-2">
|
245 |
+
<p className="mb-1">
|
246 |
+
Unable to load model due to the following error:
|
247 |
+
</p>
|
248 |
+
<p className="text-sm">{error}</p>
|
249 |
+
</div>
|
250 |
+
)}
|
251 |
+
|
252 |
+
<button
|
253 |
+
className="border px-4 py-2 rounded-lg bg-blue-400 text-white hover:bg-blue-500 disabled:bg-blue-100 disabled:cursor-not-allowed select-none"
|
254 |
+
onClick={() => {
|
255 |
+
worker.current.postMessage({ type: "load" });
|
256 |
+
setStatus("loading");
|
257 |
+
}}
|
258 |
+
disabled={status !== null || error !== null}
|
259 |
+
>
|
260 |
+
Load model
|
261 |
+
</button>
|
262 |
+
</div>
|
263 |
+
</div>
|
264 |
+
)}
|
265 |
+
{status === "loading" && (
|
266 |
+
<>
|
267 |
+
<div className="w-full max-w-[500px] text-left mx-auto p-4 bottom-0 mt-auto">
|
268 |
+
<p className="text-center mb-1">{loadingMessage}</p>
|
269 |
+
{progressItems.map(({ file, progress, total }, i) => (
|
270 |
+
<Progress
|
271 |
+
key={i}
|
272 |
+
text={file}
|
273 |
+
percentage={progress}
|
274 |
+
total={total}
|
275 |
+
/>
|
276 |
+
))}
|
277 |
+
</div>
|
278 |
+
</>
|
279 |
+
)}
|
280 |
+
|
281 |
+
{status === "ready" && (
|
282 |
+
<div
|
283 |
+
ref={chatContainerRef}
|
284 |
+
className="overflow-y-auto scrollbar-thin w-full flex flex-col items-center h-full"
|
285 |
+
>
|
286 |
+
<Chat messages={messages} />
|
287 |
+
{messages.length === 0 && (
|
288 |
+
<div>
|
289 |
+
{EXAMPLES.map((msg, i) => (
|
290 |
+
<div
|
291 |
+
key={i}
|
292 |
+
className="m-1 border dark:border-gray-600 rounded-md p-2 bg-gray-100 dark:bg-gray-700 cursor-pointer"
|
293 |
+
onClick={() => onEnter(msg)}
|
294 |
+
>
|
295 |
+
{msg}
|
296 |
+
</div>
|
297 |
+
))}
|
298 |
+
</div>
|
299 |
+
)}
|
300 |
+
<p className="text-center text-sm min-h-6 text-gray-500 dark:text-gray-300">
|
301 |
+
{tps && messages.length > 0 && (
|
302 |
+
<>
|
303 |
+
{!isRunning && (
|
304 |
+
<span>
|
305 |
+
Generated {numTokens} tokens in{" "}
|
306 |
+
{(numTokens / tps).toFixed(2)} seconds (
|
307 |
+
</span>
|
308 |
+
)}
|
309 |
+
{
|
310 |
+
<>
|
311 |
+
<span className="font-medium text-center mr-1 text-black dark:text-white">
|
312 |
+
{tps.toFixed(2)}
|
313 |
+
</span>
|
314 |
+
<span className="text-gray-500 dark:text-gray-300">
|
315 |
+
tokens/second
|
316 |
+
</span>
|
317 |
+
</>
|
318 |
+
}
|
319 |
+
{!isRunning && (
|
320 |
+
<>
|
321 |
+
<span className="mr-1">).</span>
|
322 |
+
<span
|
323 |
+
className="underline cursor-pointer"
|
324 |
+
onClick={() => {
|
325 |
+
worker.current.postMessage({ type: "reset" });
|
326 |
+
setMessages([]);
|
327 |
+
}}
|
328 |
+
>
|
329 |
+
Reset
|
330 |
+
</span>
|
331 |
+
</>
|
332 |
+
)}
|
333 |
+
</>
|
334 |
+
)}
|
335 |
+
</p>
|
336 |
+
</div>
|
337 |
+
)}
|
338 |
+
|
339 |
+
<div className="mt-2 border dark:bg-gray-700 rounded-lg w-[600px] max-w-[80%] max-h-[200px] mx-auto relative mb-3 flex">
|
340 |
+
<textarea
|
341 |
+
ref={textareaRef}
|
342 |
+
className="scrollbar-thin w-[550px] dark:bg-gray-700 px-3 py-4 rounded-lg bg-transparent border-none outline-none text-gray-800 disabled:text-gray-400 dark:text-gray-200 placeholder-gray-500 dark:placeholder-gray-400 disabled:placeholder-gray-200 resize-none disabled:cursor-not-allowed"
|
343 |
+
placeholder="Type your message..."
|
344 |
+
type="text"
|
345 |
+
rows={1}
|
346 |
+
value={input}
|
347 |
+
disabled={status !== "ready"}
|
348 |
+
title={status === "ready" ? "Model is ready" : "Model not loaded yet"}
|
349 |
+
onKeyDown={(e) => {
|
350 |
+
if (
|
351 |
+
input.length > 0 &&
|
352 |
+
!isRunning &&
|
353 |
+
e.key === "Enter" &&
|
354 |
+
!e.shiftKey
|
355 |
+
) {
|
356 |
+
e.preventDefault(); // Prevent default behavior of Enter key
|
357 |
+
onEnter(input);
|
358 |
+
}
|
359 |
+
}}
|
360 |
+
onInput={(e) => setInput(e.target.value)}
|
361 |
+
/>
|
362 |
+
{isRunning ? (
|
363 |
+
<div className="cursor-pointer" onClick={onInterrupt}>
|
364 |
+
<StopIcon className="h-8 w-8 p-1 rounded-md text-gray-800 dark:text-gray-100 absolute right-3 bottom-3" />
|
365 |
+
</div>
|
366 |
+
) : input.length > 0 ? (
|
367 |
+
<div className="cursor-pointer" onClick={() => onEnter(input)}>
|
368 |
+
<ArrowRightIcon
|
369 |
+
className={`h-8 w-8 p-1 bg-gray-800 dark:bg-gray-100 text-white dark:text-black rounded-md absolute right-3 bottom-3`}
|
370 |
+
/>
|
371 |
+
</div>
|
372 |
+
) : (
|
373 |
+
<div>
|
374 |
+
<ArrowRightIcon
|
375 |
+
className={`h-8 w-8 p-1 bg-gray-200 dark:bg-gray-600 text-gray-50 dark:text-gray-800 rounded-md absolute right-3 bottom-3`}
|
376 |
+
/>
|
377 |
+
</div>
|
378 |
+
)}
|
379 |
+
</div>
|
380 |
+
|
381 |
+
<p className="text-xs text-gray-400 text-center mb-3">
|
382 |
+
Disclaimer: Generated content may be inaccurate or false.
|
383 |
+
</p>
|
384 |
+
</div>
|
385 |
+
) : (
|
386 |
+
<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">
|
387 |
+
WebGPU is not supported
|
388 |
+
<br />
|
389 |
+
by this browser :(
|
390 |
+
</div>
|
391 |
+
);
|
392 |
+
}
|
393 |
+
|
394 |
+
export default App;
|
src/components/Chat.css
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@scope (.markdown) {
|
2 |
+
/* Code blocks */
|
3 |
+
pre {
|
4 |
+
margin: 0.5rem 0;
|
5 |
+
white-space: break-spaces;
|
6 |
+
}
|
7 |
+
|
8 |
+
code {
|
9 |
+
padding: 0.2em 0.4em;
|
10 |
+
border-radius: 4px;
|
11 |
+
font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
|
12 |
+
font-size: 0.9em;
|
13 |
+
}
|
14 |
+
|
15 |
+
pre,
|
16 |
+
code {
|
17 |
+
background-color: #f2f2f2;
|
18 |
+
}
|
19 |
+
|
20 |
+
@media (prefers-color-scheme: dark) {
|
21 |
+
pre,
|
22 |
+
code {
|
23 |
+
background-color: #333;
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
pre:has(code) {
|
28 |
+
padding: 1rem 0.5rem;
|
29 |
+
}
|
30 |
+
|
31 |
+
pre > code {
|
32 |
+
padding: 0;
|
33 |
+
}
|
34 |
+
|
35 |
+
/* Headings */
|
36 |
+
h1,
|
37 |
+
h2,
|
38 |
+
h3,
|
39 |
+
h4,
|
40 |
+
h5,
|
41 |
+
h6 {
|
42 |
+
font-weight: 600;
|
43 |
+
line-height: 1.2;
|
44 |
+
}
|
45 |
+
|
46 |
+
h1 {
|
47 |
+
font-size: 2em;
|
48 |
+
margin: 1rem 0;
|
49 |
+
}
|
50 |
+
|
51 |
+
h2 {
|
52 |
+
font-size: 1.5em;
|
53 |
+
margin: 0.83rem 0;
|
54 |
+
}
|
55 |
+
|
56 |
+
h3 {
|
57 |
+
font-size: 1.25em;
|
58 |
+
margin: 0.67rem 0;
|
59 |
+
}
|
60 |
+
|
61 |
+
h4 {
|
62 |
+
font-size: 1em;
|
63 |
+
margin: 0.5rem 0;
|
64 |
+
}
|
65 |
+
|
66 |
+
h5 {
|
67 |
+
font-size: 0.875em;
|
68 |
+
margin: 0.33rem 0;
|
69 |
+
}
|
70 |
+
|
71 |
+
h6 {
|
72 |
+
font-size: 0.75em;
|
73 |
+
margin: 0.25rem 0;
|
74 |
+
}
|
75 |
+
|
76 |
+
h1,
|
77 |
+
h2,
|
78 |
+
h3,
|
79 |
+
h4,
|
80 |
+
h5,
|
81 |
+
h6:first-child {
|
82 |
+
margin-top: 0;
|
83 |
+
}
|
84 |
+
|
85 |
+
/* Unordered List */
|
86 |
+
ul {
|
87 |
+
list-style-type: disc;
|
88 |
+
margin-left: 1.5rem;
|
89 |
+
}
|
90 |
+
|
91 |
+
/* Ordered List */
|
92 |
+
ol {
|
93 |
+
list-style-type: decimal;
|
94 |
+
margin-left: 1.5rem;
|
95 |
+
}
|
96 |
+
|
97 |
+
/* List Items */
|
98 |
+
li {
|
99 |
+
margin: 0.25rem 0;
|
100 |
+
}
|
101 |
+
|
102 |
+
p:not(:first-child) {
|
103 |
+
margin-top: 0.75rem;
|
104 |
+
}
|
105 |
+
|
106 |
+
p:not(:last-child) {
|
107 |
+
margin-bottom: 0.75rem;
|
108 |
+
}
|
109 |
+
|
110 |
+
ul > li {
|
111 |
+
margin-left: 1rem;
|
112 |
+
}
|
113 |
+
|
114 |
+
/* Table */
|
115 |
+
table,
|
116 |
+
th,
|
117 |
+
td {
|
118 |
+
border: 1px solid lightgray;
|
119 |
+
padding: 0.25rem;
|
120 |
+
}
|
121 |
+
|
122 |
+
@media (prefers-color-scheme: dark) {
|
123 |
+
table,
|
124 |
+
th,
|
125 |
+
td {
|
126 |
+
border: 1px solid #f2f2f2;
|
127 |
+
}
|
128 |
+
}
|
129 |
+
}
|
src/components/Chat.jsx
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { marked } from "marked";
|
2 |
+
import DOMPurify from "dompurify";
|
3 |
+
|
4 |
+
import BotIcon from "./icons/BotIcon";
|
5 |
+
import UserIcon from "./icons/UserIcon";
|
6 |
+
|
7 |
+
import "./Chat.css";
|
8 |
+
import { useEffect } from "react";
|
9 |
+
|
10 |
+
function render(text) {
|
11 |
+
return DOMPurify.sanitize(marked.parse(text));
|
12 |
+
}
|
13 |
+
|
14 |
+
export default function Chat({ messages }) {
|
15 |
+
const empty = messages.length === 0;
|
16 |
+
|
17 |
+
useEffect(() => {
|
18 |
+
window.MathJax.typeset();
|
19 |
+
}, [messages]);
|
20 |
+
|
21 |
+
return (
|
22 |
+
<div
|
23 |
+
className={`flex-1 p-6 max-w-[960px] w-full ${empty ? "flex flex-col items-center justify-end" : "space-y-4"}`}
|
24 |
+
>
|
25 |
+
{empty ? (
|
26 |
+
<div className="text-xl">Ready!</div>
|
27 |
+
) : (
|
28 |
+
messages.map((msg, i) => (
|
29 |
+
<div key={`message-${i}`} className="flex items-start space-x-4">
|
30 |
+
{msg.role === "assistant" ? (
|
31 |
+
<>
|
32 |
+
<BotIcon className="h-6 w-6 min-h-6 min-w-6 my-3 text-gray-500 dark:text-gray-300" />
|
33 |
+
<div className="bg-gray-200 dark:bg-gray-700 rounded-lg p-4">
|
34 |
+
<p className="min-h-6 text-gray-800 dark:text-gray-200 overflow-wrap-anywhere">
|
35 |
+
{msg.content.length > 0 ? (
|
36 |
+
<span
|
37 |
+
className="markdown"
|
38 |
+
dangerouslySetInnerHTML={{
|
39 |
+
__html: render(msg.content),
|
40 |
+
}}
|
41 |
+
/>
|
42 |
+
) : (
|
43 |
+
<span className="h-6 flex items-center gap-1">
|
44 |
+
<span className="w-2.5 h-2.5 bg-gray-600 dark:bg-gray-300 rounded-full animate-pulse"></span>
|
45 |
+
<span className="w-2.5 h-2.5 bg-gray-600 dark:bg-gray-300 rounded-full animate-pulse animation-delay-200"></span>
|
46 |
+
<span className="w-2.5 h-2.5 bg-gray-600 dark:bg-gray-300 rounded-full animate-pulse animation-delay-400"></span>
|
47 |
+
</span>
|
48 |
+
)}
|
49 |
+
</p>
|
50 |
+
</div>
|
51 |
+
</>
|
52 |
+
) : (
|
53 |
+
<>
|
54 |
+
<UserIcon className="h-6 w-6 min-h-6 min-w-6 my-3 text-gray-500 dark:text-gray-300" />
|
55 |
+
<div className="bg-blue-500 text-white rounded-lg p-4">
|
56 |
+
<p className="min-h-6 overflow-wrap-anywhere">
|
57 |
+
{msg.content}
|
58 |
+
</p>
|
59 |
+
</div>
|
60 |
+
</>
|
61 |
+
)}
|
62 |
+
</div>
|
63 |
+
))
|
64 |
+
)}
|
65 |
+
</div>
|
66 |
+
);
|
67 |
+
}
|
src/components/Progress.jsx
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function formatBytes(size) {
|
2 |
+
const i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
3 |
+
return (
|
4 |
+
+(size / Math.pow(1024, i)).toFixed(2) * 1 +
|
5 |
+
["B", "kB", "MB", "GB", "TB"][i]
|
6 |
+
);
|
7 |
+
}
|
8 |
+
|
9 |
+
export default function Progress({ text, percentage, total }) {
|
10 |
+
percentage ??= 0;
|
11 |
+
return (
|
12 |
+
<div className="w-full bg-gray-100 dark:bg-gray-700 text-left rounded-lg overflow-hidden mb-0.5">
|
13 |
+
<div
|
14 |
+
className="bg-blue-400 whitespace-nowrap px-1 text-sm"
|
15 |
+
style={{ width: `${percentage}%` }}
|
16 |
+
>
|
17 |
+
{text} ({percentage.toFixed(2)}%
|
18 |
+
{isNaN(total) ? "" : ` of ${formatBytes(total)}`})
|
19 |
+
</div>
|
20 |
+
</div>
|
21 |
+
);
|
22 |
+
}
|
src/components/icons/ArrowRightIcon.jsx
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export default function ArrowRightIcon(props) {
|
2 |
+
return (
|
3 |
+
<svg
|
4 |
+
{...props}
|
5 |
+
xmlns="http://www.w3.org/2000/svg"
|
6 |
+
width="24"
|
7 |
+
height="24"
|
8 |
+
viewBox="0 0 24 24"
|
9 |
+
fill="none"
|
10 |
+
stroke="currentColor"
|
11 |
+
strokeWidth="2"
|
12 |
+
strokeLinecap="round"
|
13 |
+
strokeLinejoin="round"
|
14 |
+
>
|
15 |
+
<path d="M5 12h14" />
|
16 |
+
<path d="m12 5 7 7-7 7" />
|
17 |
+
</svg>
|
18 |
+
);
|
19 |
+
}
|
src/components/icons/BotIcon.jsx
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export default function BotIcon(props) {
|
2 |
+
return (
|
3 |
+
<svg
|
4 |
+
{...props}
|
5 |
+
xmlns="http://www.w3.org/2000/svg"
|
6 |
+
width="24"
|
7 |
+
height="24"
|
8 |
+
viewBox="0 0 24 24"
|
9 |
+
fill="none"
|
10 |
+
stroke="currentColor"
|
11 |
+
strokeWidth="2"
|
12 |
+
strokeLinecap="round"
|
13 |
+
strokeLinejoin="round"
|
14 |
+
>
|
15 |
+
<path d="M12 8V4H8" />
|
16 |
+
<rect width="16" height="12" x="4" y="8" rx="2" />
|
17 |
+
<path d="M2 14h2" />
|
18 |
+
<path d="M20 14h2" />
|
19 |
+
<path d="M15 13v2" />
|
20 |
+
<path d="M9 13v2" />
|
21 |
+
</svg>
|
22 |
+
);
|
23 |
+
}
|
src/components/icons/StopIcon.jsx
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export default function StopIcon(props) {
|
2 |
+
return (
|
3 |
+
<svg
|
4 |
+
{...props}
|
5 |
+
xmlns="http://www.w3.org/2000/svg"
|
6 |
+
width="24"
|
7 |
+
height="24"
|
8 |
+
viewBox="0 0 24 24"
|
9 |
+
fill="none"
|
10 |
+
stroke="currentColor"
|
11 |
+
strokeWidth="2"
|
12 |
+
strokeLinecap="round"
|
13 |
+
strokeLinejoin="round"
|
14 |
+
>
|
15 |
+
<path d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
|
16 |
+
<path
|
17 |
+
fill="currentColor"
|
18 |
+
d="M9 9.563C9 9.252 9.252 9 9.563 9h4.874c.311 0 .563.252.563.563v4.874c0 .311-.252.563-.563.563H9.564A.562.562 0 0 1 9 14.437V9.564Z"
|
19 |
+
/>
|
20 |
+
</svg>
|
21 |
+
);
|
22 |
+
}
|
src/components/icons/UserIcon.jsx
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export default function UserIcon(props) {
|
2 |
+
return (
|
3 |
+
<svg
|
4 |
+
{...props}
|
5 |
+
xmlns="http://www.w3.org/2000/svg"
|
6 |
+
width="24"
|
7 |
+
height="24"
|
8 |
+
viewBox="0 0 24 24"
|
9 |
+
fill="none"
|
10 |
+
stroke="currentColor"
|
11 |
+
strokeWidth="2"
|
12 |
+
strokeLinecap="round"
|
13 |
+
strokeLinejoin="round"
|
14 |
+
>
|
15 |
+
<path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2" />
|
16 |
+
<circle cx="12" cy="7" r="4" />
|
17 |
+
</svg>
|
18 |
+
);
|
19 |
+
}
|
src/index.css
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@tailwind base;
|
2 |
+
@tailwind components;
|
3 |
+
@tailwind utilities;
|
4 |
+
|
5 |
+
@layer utilities {
|
6 |
+
.scrollbar-thin::-webkit-scrollbar {
|
7 |
+
@apply w-2;
|
8 |
+
}
|
9 |
+
|
10 |
+
.scrollbar-thin::-webkit-scrollbar-track {
|
11 |
+
@apply rounded-full bg-gray-100 dark:bg-gray-700;
|
12 |
+
}
|
13 |
+
|
14 |
+
.scrollbar-thin::-webkit-scrollbar-thumb {
|
15 |
+
@apply rounded-full bg-gray-300 dark:bg-gray-600;
|
16 |
+
}
|
17 |
+
|
18 |
+
.scrollbar-thin::-webkit-scrollbar-thumb:hover {
|
19 |
+
@apply bg-gray-500;
|
20 |
+
}
|
21 |
+
|
22 |
+
.animation-delay-200 {
|
23 |
+
animation-delay: 200ms;
|
24 |
+
}
|
25 |
+
.animation-delay-400 {
|
26 |
+
animation-delay: 400ms;
|
27 |
+
}
|
28 |
+
|
29 |
+
.overflow-wrap-anywhere {
|
30 |
+
overflow-wrap: anywhere;
|
31 |
+
}
|
32 |
+
}
|
src/main.jsx
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React from "react";
|
2 |
+
import ReactDOM from "react-dom/client";
|
3 |
+
import App from "./App.jsx";
|
4 |
+
import "./index.css";
|
5 |
+
|
6 |
+
ReactDOM.createRoot(document.getElementById("root")).render(
|
7 |
+
<React.StrictMode>
|
8 |
+
<App />
|
9 |
+
</React.StrictMode>,
|
10 |
+
);
|
src/worker.js
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import {
|
2 |
+
AutoTokenizer,
|
3 |
+
AutoModelForCausalLM,
|
4 |
+
TextStreamer,
|
5 |
+
InterruptableStoppingCriteria,
|
6 |
+
} from "@huggingface/transformers";
|
7 |
+
|
8 |
+
/**
|
9 |
+
* Helper function to perform feature detection for WebGPU
|
10 |
+
*/
|
11 |
+
// let fp16_supported = false;
|
12 |
+
async function check() {
|
13 |
+
try {
|
14 |
+
const adapter = await navigator.gpu.requestAdapter();
|
15 |
+
if (!adapter) {
|
16 |
+
throw new Error("WebGPU is not supported (no adapter found)");
|
17 |
+
}
|
18 |
+
// fp16_supported = adapter.features.has("shader-f16")
|
19 |
+
} catch (e) {
|
20 |
+
self.postMessage({
|
21 |
+
status: "error",
|
22 |
+
data: e.toString(),
|
23 |
+
});
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
/**
|
28 |
+
* This class uses the Singleton pattern to enable lazy-loading of the pipeline
|
29 |
+
*/
|
30 |
+
class TextGenerationPipeline {
|
31 |
+
static model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct";
|
32 |
+
|
33 |
+
static async getInstance(progress_callback = null) {
|
34 |
+
this.tokenizer ??= AutoTokenizer.from_pretrained(this.model_id, {
|
35 |
+
progress_callback,
|
36 |
+
});
|
37 |
+
|
38 |
+
this.model ??= AutoModelForCausalLM.from_pretrained(this.model_id, {
|
39 |
+
dtype: "q4f16",
|
40 |
+
device: "webgpu",
|
41 |
+
progress_callback,
|
42 |
+
});
|
43 |
+
|
44 |
+
return Promise.all([this.tokenizer, this.model]);
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
const stopping_criteria = new InterruptableStoppingCriteria();
|
49 |
+
|
50 |
+
let past_key_values_cache = null;
|
51 |
+
async function generate(messages) {
|
52 |
+
// Retrieve the text-generation pipeline.
|
53 |
+
const [tokenizer, model] = await TextGenerationPipeline.getInstance();
|
54 |
+
|
55 |
+
const inputs = tokenizer.apply_chat_template(messages, {
|
56 |
+
add_generation_prompt: true,
|
57 |
+
return_dict: true,
|
58 |
+
});
|
59 |
+
|
60 |
+
let startTime;
|
61 |
+
let numTokens = 0;
|
62 |
+
let tps;
|
63 |
+
const token_callback_function = () => {
|
64 |
+
startTime ??= performance.now();
|
65 |
+
|
66 |
+
if (numTokens++ > 0) {
|
67 |
+
tps = (numTokens / (performance.now() - startTime)) * 1000;
|
68 |
+
}
|
69 |
+
};
|
70 |
+
const callback_function = (output) => {
|
71 |
+
self.postMessage({
|
72 |
+
status: "update",
|
73 |
+
output,
|
74 |
+
tps,
|
75 |
+
numTokens,
|
76 |
+
});
|
77 |
+
};
|
78 |
+
|
79 |
+
const streamer = new TextStreamer(tokenizer, {
|
80 |
+
skip_prompt: true,
|
81 |
+
skip_special_tokens: true,
|
82 |
+
callback_function,
|
83 |
+
token_callback_function,
|
84 |
+
});
|
85 |
+
|
86 |
+
// Tell the main thread we are starting
|
87 |
+
self.postMessage({ status: "start" });
|
88 |
+
|
89 |
+
const { past_key_values, sequences } = await model.generate({
|
90 |
+
...inputs,
|
91 |
+
past_key_values: past_key_values_cache,
|
92 |
+
|
93 |
+
// Sampling
|
94 |
+
// do_sample: true,
|
95 |
+
// top_k: 3,
|
96 |
+
// temperature: 0.2,
|
97 |
+
|
98 |
+
max_new_tokens: 1024,
|
99 |
+
streamer,
|
100 |
+
stopping_criteria,
|
101 |
+
return_dict_in_generate: true,
|
102 |
+
});
|
103 |
+
past_key_values_cache = past_key_values;
|
104 |
+
|
105 |
+
const decoded = tokenizer.batch_decode(sequences, {
|
106 |
+
skip_special_tokens: true,
|
107 |
+
});
|
108 |
+
|
109 |
+
// Send the output back to the main thread
|
110 |
+
self.postMessage({
|
111 |
+
status: "complete",
|
112 |
+
output: decoded,
|
113 |
+
});
|
114 |
+
}
|
115 |
+
|
116 |
+
async function load() {
|
117 |
+
self.postMessage({
|
118 |
+
status: "loading",
|
119 |
+
data: "Loading model...",
|
120 |
+
});
|
121 |
+
|
122 |
+
// Load the pipeline and save it for future use.
|
123 |
+
const [tokenizer, model] = await TextGenerationPipeline.getInstance((x) => {
|
124 |
+
// We also add a progress callback to the pipeline so that we can
|
125 |
+
// track model loading.
|
126 |
+
self.postMessage(x);
|
127 |
+
});
|
128 |
+
|
129 |
+
self.postMessage({
|
130 |
+
status: "loading",
|
131 |
+
data: "Compiling shaders and warming up model...",
|
132 |
+
});
|
133 |
+
|
134 |
+
// Run model with dummy input to compile shaders
|
135 |
+
const inputs = tokenizer("a");
|
136 |
+
await model.generate({ ...inputs, max_new_tokens: 1 });
|
137 |
+
self.postMessage({ status: "ready" });
|
138 |
+
}
|
139 |
+
// Listen for messages from the main thread
|
140 |
+
self.addEventListener("message", async (e) => {
|
141 |
+
const { type, data } = e.data;
|
142 |
+
|
143 |
+
switch (type) {
|
144 |
+
case "check":
|
145 |
+
check();
|
146 |
+
break;
|
147 |
+
|
148 |
+
case "load":
|
149 |
+
load();
|
150 |
+
break;
|
151 |
+
|
152 |
+
case "generate":
|
153 |
+
stopping_criteria.reset();
|
154 |
+
generate(data);
|
155 |
+
break;
|
156 |
+
|
157 |
+
case "interrupt":
|
158 |
+
stopping_criteria.interrupt();
|
159 |
+
break;
|
160 |
+
|
161 |
+
case "reset":
|
162 |
+
past_key_values_cache = null;
|
163 |
+
stopping_criteria.reset();
|
164 |
+
break;
|
165 |
+
}
|
166 |
+
});
|
tailwind.config.js
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/** @type {import('tailwindcss').Config} */
|
2 |
+
export default {
|
3 |
+
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
|
4 |
+
theme: {
|
5 |
+
extend: {},
|
6 |
+
},
|
7 |
+
plugins: [],
|
8 |
+
};
|
vite.config.js
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { defineConfig } from "vite";
|
2 |
+
import react from "@vitejs/plugin-react";
|
3 |
+
|
4 |
+
// https://vitejs.dev/config/
|
5 |
+
export default defineConfig({
|
6 |
+
plugins: [react()],
|
7 |
+
});
|