Spaces:
Running

HubertRNA commited on
Commit
72dac32
·
verified ·
1 Parent(s): 4d64d58

Pierwsza wersja

Browse files
Files changed (2) hide show
  1. README.md +18 -7
  2. index.html +621 -17
README.md CHANGED
@@ -1,12 +1,23 @@
1
  ---
2
- title: Tokenizator
3
- emoji: 🦀
4
- colorFrom: blue
5
- colorTo: green
6
  sdk: static
7
  pinned: false
8
- license: apache-2.0
9
- short_description: Simple tokenizators showroom
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Tokenizator ReceptaNaAI
3
+ emoji: 🧩
4
+ colorFrom: green
5
+ colorTo: yellow
6
  sdk: static
7
  pinned: false
8
+ short_description: Zobacz, jak modele AI dzielą tekst na tokeny
 
9
  ---
10
 
11
+ # Tokenizator
12
+
13
+ Interaktywne narzędzie edukacyjne z bloga [ReceptaNaAI.pl](https://receptanaai.pl). Pokazuje, jak tokenizery różnych modeli językowych dzielą tekst na tokeny.
14
+
15
+ - Tokenizacja działa w całości w przeglądarce (Transformers.js). Wpisany tekst nie jest nigdzie wysyłany.
16
+ - Modele: GPT-4, Llama 3, Mistral v3, Gemma, Claude (tokenizer nieoficjalny, wartości orientacyjne).
17
+ - Plik `index.html` jest samodzielny, nie wymaga budowania.
18
+
19
+ ## Licencje i podziękowania
20
+
21
+ - Biblioteka Transformers.js (Hugging Face): Apache 2.0.
22
+ - Inspiracja: "The Tokenizer Playground" (Xenova, Hugging Face), przykład z repozytorium `huggingface/transformers.js-examples` (Apache 2.0).
23
+ - Tokenizery są pobierane z Hugging Face Hub z repozytoriów społeczności Xenova i podlegają licencjom modeli, z których pochodzą.
index.html CHANGED
@@ -1,19 +1,623 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="pl">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Tokenizator - ReceptaNaAI.pl</title>
7
+ <meta name="description" content="Zobacz, jak modele językowe dzielą tekst na tokeny. Działa w przeglądarce, tekst nigdzie nie jest wysyłany.">
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
+ <link href="https://fonts.googleapis.com/css2?family=Alfa+Slab+One&family=Atkinson+Hyperlegible:wght@400;700&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet">
11
+ <style>
12
+ :root {
13
+ color-scheme: light;
14
+ --paper: #F3E7C9;
15
+ --paper-deep: #E9D9AE;
16
+ --ink: #17181C;
17
+ --muted: #4A4F5C;
18
+ --navy: #1D3557;
19
+ --teal: #17A398;
20
+ --teal-l: #A5DDD5;
21
+ --coral: #E4583A;
22
+ --coral-l: #F8B09C;
23
+ --mustard-l: #F7D98B;
24
+ --peri: #BAC8E8;
25
+ --brass: #8A5D16;
26
+ --radius: 7px;
27
+ --font-head: "Alfa Slab One", "Rockwell", "Georgia", serif;
28
+ --font-body: "Atkinson Hyperlegible", "Segoe UI", system-ui, sans-serif;
29
+ --font-mono: "JetBrains Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
30
+ }
31
+ * { box-sizing: border-box; }
32
+ html { -webkit-text-size-adjust: 100%; }
33
+ body {
34
+ margin: 0;
35
+ background: var(--paper);
36
+ color: var(--ink);
37
+ font-family: var(--font-body);
38
+ font-size: 1.0625rem;
39
+ line-height: 1.55;
40
+ }
41
+ a { color: var(--navy); text-underline-offset: 3px; }
42
+ a:hover { color: var(--coral); }
43
+ :focus-visible { outline: 3px solid var(--navy); outline-offset: 3px; }
44
+
45
+ .wrap { max-width: 1000px; margin: 0 auto; padding: 0 16px; }
46
+
47
+ /* ---------- hero ---------- */
48
+ .hero { position: relative; padding: 28px 0 30px; border-bottom: 3px solid var(--ink); overflow: hidden; }
49
+ .hero::before {
50
+ content: ""; position: absolute; inset: 0; pointer-events: none;
51
+ background-image: radial-gradient(var(--teal) 1.7px, transparent 2px);
52
+ background-size: 11px 11px; opacity: .38;
53
+ -webkit-mask-image: linear-gradient(to left, #000 0%, transparent 62%);
54
+ mask-image: linear-gradient(to left, #000 0%, transparent 62%);
55
+ }
56
+ .hero .wrap { position: relative; }
57
+ .brand { font-weight: 700; color: var(--navy); text-decoration: none; }
58
+ h1 { margin: 14px 0 6px; font-family: var(--font-head); font-weight: 400; font-size: clamp(2.3rem, 8vw, 4.6rem); line-height: 1.12; display: flex; flex-wrap: wrap; gap: 8px; }
59
+ h1 .tok { font-family: var(--font-head); font-size: inherit; padding: 0 .22em .06em; border-width: 3px; border-radius: 10px; box-shadow: 4px 4px 0 var(--ink); }
60
+ .hero-sub { margin: 14px 0 0; font-size: clamp(1.15rem, 3vw, 1.45rem); font-weight: 700; color: var(--navy); }
61
+ .lead { max-width: 60ch; margin: 8px 0 0; }
62
+
63
+ /* ---------- sections ---------- */
64
+ main { padding-bottom: 40px; }
65
+ section { margin-top: 34px; }
66
+ h2 { margin: 0 0 10px; font-family: var(--font-head); font-weight: 400; font-size: 1.5rem; color: var(--navy); line-height: 1.2; }
67
+ .hint { color: var(--muted); margin: 0 0 12px; max-width: 64ch; }
68
+
69
+ .privacy {
70
+ margin-top: 22px; padding: 12px 16px; background: var(--coral-l);
71
+ border: 2px solid var(--ink); border-left-width: 10px; border-radius: var(--radius);
72
+ max-width: 78ch;
73
+ }
74
+ .privacy strong { font-weight: 700; }
75
+
76
+ /* ---------- token tiles ---------- */
77
+ .tok {
78
+ display: inline-block; border: 2px solid var(--ink); border-radius: 6px;
79
+ padding: 1px 7px 2px; font-family: var(--font-mono); font-weight: 500; font-size: 1.05rem;
80
+ line-height: 1.45; white-space: pre-wrap; word-break: break-all; color: var(--ink);
81
+ box-shadow: 2px 2px 0 var(--ink); position: relative;
82
+ }
83
+ .c0 { background: var(--teal-l); }
84
+ .c1 { background: var(--coral-l); }
85
+ .c2 { background: var(--mustard-l); }
86
+ .c3 { background: var(--peri); }
87
+ .c4 { background: #CFE6B8; }
88
+ .tok.frag {
89
+ background-image: repeating-linear-gradient(45deg, rgba(23,24,28,.22) 0 3px, transparent 3px 7px);
90
+ }
91
+ .sp, .nl { opacity: .45; }
92
+ .tid { display: block; font-size: .72rem; font-weight: 700; line-height: 1.1; text-align: center; color: var(--navy); margin-top: 1px; }
93
+
94
+ /* ---------- model radios ---------- */
95
+ fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
96
+ legend { padding: 0; margin-bottom: 10px; font-family: var(--font-head); font-size: 1.5rem; color: var(--navy); line-height: 1.2; }
97
+ .chips { display: flex; flex-wrap: wrap; gap: 10px; }
98
+ .chip { position: relative; }
99
+ .chip input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
100
+ .chip span {
101
+ display: inline-flex; align-items: center; gap: 8px; min-height: 46px; padding: 6px 16px;
102
+ border: 2px solid var(--ink); border-radius: 999px; background: #FBF4DE;
103
+ font-weight: 700; box-shadow: 2px 2px 0 var(--ink); transition: transform .08s, box-shadow .08s;
104
+ }
105
+ .chip input:hover + span { background: var(--mustard-l); }
106
+ .chip input:checked + span { background: var(--navy); color: #FBF4DE; box-shadow: 0 0 0 var(--ink); transform: translate(2px, 2px); }
107
+ .chip input:focus-visible + span { outline: 3px solid var(--navy); outline-offset: 3px; }
108
+ .chip small { font-weight: 400; font-size: .8rem; }
109
+ .model-note { min-height: 1.6em; margin: 12px 0 0; color: var(--muted); font-size: .95rem; max-width: 70ch; }
110
+
111
+ /* ---------- input ---------- */
112
+ label.field { display: block; font-weight: 700; margin-bottom: 6px; }
113
+ textarea {
114
+ width: 100%; min-height: 130px; resize: vertical; padding: 12px 14px;
115
+ font-family: var(--font-mono); font-size: 1.05rem; line-height: 1.5; color: var(--ink);
116
+ background: #FBF4DE; border: 2px solid var(--ink); border-radius: var(--radius); box-shadow: 3px 3px 0 var(--ink);
117
+ }
118
+ textarea::placeholder { color: #6b6a62; }
119
+ .examples { margin-top: 14px; }
120
+ .examples p { margin: 0 0 8px; font-weight: 700; }
121
+ .ex-list { display: flex; flex-wrap: wrap; gap: 8px; }
122
+ button {
123
+ font: inherit; cursor: pointer; color: var(--ink);
124
+ }
125
+ .btn-ex, .btn {
126
+ min-height: 40px; padding: 4px 14px; font-weight: 700; background: transparent;
127
+ border: 2px solid var(--ink); border-radius: 999px;
128
+ }
129
+ .btn-ex:hover { background: var(--mustard-l); }
130
+ .btn { background: var(--teal-l); box-shadow: 2px 2px 0 var(--ink); min-height: 46px; padding: 6px 18px; }
131
+ .btn:hover { background: #8fd2c9; }
132
+ .btn:active { transform: translate(2px, 2px); box-shadow: none; }
133
+ .btn[disabled] { opacity: .6; cursor: progress; }
134
+
135
+ /* ---------- stats ---------- */
136
+ .stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0; border-top: 3px solid var(--ink); border-bottom: 3px solid var(--ink); }
137
+ .stat { padding: 12px 14px; border-right: 2px solid var(--ink); }
138
+ .stat:last-child { border-right: 0; }
139
+ .stat .v { display: block; font-family: var(--font-head); font-size: clamp(1.7rem, 5vw, 2.5rem); line-height: 1.1; color: var(--navy); }
140
+ .stat .k { display: block; font-size: .92rem; color: var(--muted); }
141
+
142
+ /* ---------- output ---------- */
143
+ .out-tools { display: flex; flex-wrap: wrap; gap: 10px 22px; margin: 4px 0 14px; }
144
+ .toggle { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; cursor: pointer; min-height: 32px; }
145
+ .toggle input { width: 20px; height: 20px; accent-color: var(--navy); margin: 0; }
146
+ #tokens { display: flex; flex-wrap: wrap; gap: 8px 6px; align-items: flex-start; min-height: 120px; padding: 16px; background: #FBF4DE; border: 2px solid var(--ink); border-radius: var(--radius); box-shadow: 3px 3px 0 var(--ink); }
147
+ #tokens .empty { color: var(--muted); margin: 0; align-self: center; }
148
+ #tokens .err { margin: 0; color: var(--ink); width: 100%; }
149
+ #tokens .err .btn { margin-top: 10px; display: block; }
150
+ .legend { margin: 10px 0 0; color: var(--muted); font-size: .95rem; max-width: 72ch; }
151
+ .legend .tok { font-size: .85rem; padding: 0 6px; box-shadow: none; vertical-align: 1px; }
152
+
153
+ /* pop-in once, on first render */
154
+ @media (prefers-reduced-motion: no-preference) {
155
+ .first .tok { animation: pop .35s cubic-bezier(.3,1.6,.5,1) both; animation-delay: calc(var(--i, 0) * 28ms); }
156
+ @keyframes pop { from { transform: scale(.5) rotate(-4deg); opacity: 0; } to { transform: none; opacity: 1; } }
157
+ }
158
+
159
+ /* ---------- compare ---------- */
160
+ .compare-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 18px; }
161
+ .progress { color: var(--muted); font-size: .95rem; }
162
+ #bars { list-style: none; margin: 16px 0 0; padding: 0; display: grid; gap: 10px; }
163
+ .bar-row { display: grid; grid-template-columns: 8.5rem 1fr; gap: 12px; align-items: center; }
164
+ .bar-row button { text-align: left; background: none; border: 0; padding: 4px 0; font-weight: 700; min-height: 40px; }
165
+ .bar-row button[aria-current="true"] { color: var(--coral); }
166
+ .bar-row button[aria-current="true"]::after { content: " (wybrany)"; font-weight: 400; font-size: .8rem; color: var(--muted); }
167
+ .bar-track { display: flex; align-items: center; gap: 10px; }
168
+ .bar { height: 30px; min-width: 6px; border: 2px solid var(--ink); border-radius: 6px; box-shadow: 2px 2px 0 var(--ink); transition: width .25s; }
169
+ .bar-val { font-family: var(--font-mono); font-weight: 700; white-space: nowrap; }
170
+ .bar-val small { font-family: var(--font-body); font-weight: 400; color: var(--muted); }
171
+ .bar-na { color: var(--muted); }
172
+ .insight { margin: 14px 0 0; font-weight: 700; color: var(--navy); max-width: 70ch; }
173
+
174
+ /* ---------- notes ---------- */
175
+ .notes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 34px; padding: 0; margin: 0; list-style: none; }
176
+ .notes li { padding: 12px 0; border-top: 2px solid var(--ink); }
177
+ .notes b { color: var(--navy); }
178
+
179
+ /* ---------- footer ---------- */
180
+ footer { border-top: 3px solid var(--ink); background: var(--paper-deep); padding: 22px 0 34px; font-size: .92rem; color: var(--muted); }
181
+ footer p { margin: 0 0 8px; max-width: 90ch; }
182
+ footer strong { color: var(--ink); }
183
+
184
+ .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
185
+
186
+ @media (max-width: 720px) {
187
+ .stats { grid-template-columns: repeat(2, 1fr); }
188
+ .stat:nth-child(2) { border-right: 0; }
189
+ .stat:nth-child(-n+2) { border-bottom: 2px solid var(--ink); }
190
+ .notes { grid-template-columns: 1fr; }
191
+ .bar-row { grid-template-columns: 1fr; gap: 0; }
192
+ .bar-row button { min-height: 36px; }
193
+ h1 { gap: 6px; }
194
+ }
195
+ </style>
196
+ </head>
197
+ <body>
198
+
199
+ <header class="hero">
200
+ <div class="wrap">
201
+ <a class="brand" href="https://receptanaai.pl" target="_blank" rel="noopener">ReceptaNaAI.pl</a>
202
+ <h1 aria-label="Tokenizator"><span class="tok c0" aria-hidden="true">Token</span><span class="tok c1" aria-hidden="true">iza</span><span class="tok c2" aria-hidden="true">tor</span></h1>
203
+ <p class="hero-sub">Jak model językowy czyta Twój tekst?</p>
204
+ <p class="lead">Model nie widzi liter ani słów. Widzi tokeny: kawałki tekstu, na które tnie go tokenizer. Wpisz zdanie i zobacz, jak potnie je wybrany model.</p>
205
+ <p class="privacy"><strong>Tekst jest przetwarzany w Twojej przeglądarce i nigdzie nie jest wysyłany.</strong> Mimo to nie wpisuj tu danych pacjentów ani innych danych wrażliwych.</p>
206
+ </div>
207
+ </header>
208
+
209
+ <main class="wrap">
210
+
211
+ <section aria-labelledby="lg-model">
212
+ <fieldset>
213
+ <legend id="lg-model">1. Wybierz model</legend>
214
+ <div class="chips" id="chips"></div>
215
+ </fieldset>
216
+ <p class="model-note" id="model-note" aria-live="polite"></p>
217
+ </section>
218
+
219
+ <section aria-labelledby="h-text">
220
+ <h2 id="h-text">2. Wpisz tekst</h2>
221
+ <label class="field" for="text">Tekst do podzielenia na tokeny</label>
222
+ <textarea id="text" spellcheck="false" placeholder="Wpisz zdanie albo wybierz przykład poniżej"></textarea>
223
+ <div class="examples">
224
+ <p>Przykłady do porównania</p>
225
+ <div class="ex-list" id="examples"></div>
226
+ </div>
227
+ </section>
228
+
229
+ <section aria-labelledby="h-out">
230
+ <h2 id="h-out">3. Zobacz tokeny</h2>
231
+ <div class="stats" id="stats" aria-live="polite">
232
+ <div class="stat"><span class="v" id="s-tok">0</span><span class="k">tokenów</span></div>
233
+ <div class="stat"><span class="v" id="s-chr">0</span><span class="k">znaków</span></div>
234
+ <div class="stat"><span class="v" id="s-wrd">0</span><span class="k">słów</span></div>
235
+ <div class="stat"><span class="v" id="s-tpw">-</span><span class="k">tokenów na słowo</span></div>
236
+ </div>
237
+ <div class="out-tools" style="margin-top:16px">
238
+ <label class="toggle"><input type="checkbox" id="opt-sp" checked> Pokaż spacje</label>
239
+ <label class="toggle"><input type="checkbox" id="opt-id"> Pokaż numery tokenów</label>
240
+ </div>
241
+ <div id="tokens" role="region" aria-label="Tokeny" aria-live="polite"></div>
242
+ <p class="legend" id="legend" hidden><span class="tok frag" aria-hidden="true">&#xFFFD;</span> Kratka oznacza fragment znaku zapisanego w kilku bajtach. Tak bywają cięte rzadsze litery, np. polskie znaki diakrytyczne.</p>
243
+ </section>
244
+
245
+ <section aria-labelledby="h-cmp">
246
+ <h2 id="h-cmp">4. Porównaj modele</h2>
247
+ <p class="hint">Ten sam tekst, różne tokenizery. Porównanie pobiera pliki wszystkich tokenizerów, więc za pierwszym razem może chwilę potrwać.</p>
248
+ <div class="compare-head">
249
+ <button class="btn" id="btn-cmp" type="button">Porównaj wszystkie modele</button>
250
+ <span class="progress" id="cmp-progress" aria-live="polite"></span>
251
+ </div>
252
+ <ul id="bars" aria-label="Liczba tokenów w poszczególnych modelach"></ul>
253
+ <p class="insight" id="insight" aria-live="polite"></p>
254
+ </section>
255
+
256
+ <section aria-labelledby="h-notes">
257
+ <h2 id="h-notes">Co warto zauważyć</h2>
258
+ <ul class="notes">
259
+ <li><b>Token to nie słowo.</b> Krótkie, częste słowa zwykle zajmują jeden token, a długie lub rzadkie dzielą się na kilka kawałków. Sprawdź to na nazwach leków.</li>
260
+ <li><b>Ten sam sens, inna liczba tokenów.</b> Porównaj zdanie po polsku z tym samym zdaniem po angielsku i zobacz, czy któryś model potrzebuje więcej tokenów.</li>
261
+ <li><b>Tokeny to waluta modeli.</b> Okno kontekstu i koszt zapytań w API liczy się w tokenach, a nie w znakach.</li>
262
+ <li><b>Każdy model ma własny tokenizer.</b> Liczba tokenów tego samego tekstu zależy od modelu, więc używaj porównania.</li>
263
+ </ul>
264
+ </section>
265
+ </main>
266
+
267
+ <footer>
268
+ <div class="wrap">
269
+ <p><strong>Skąd to się bierze.</strong> Tokenizacja działa lokalnie dzięki bibliotece Transformers.js (Hugging Face, licencja Apache 2.0). Przeglądarka pobiera bibliotekę z serwisu jsDelivr, pliki tokenizerów z Hugging Face Hub i czcionki z Google Fonts. Twój tekst nie jest do nich wysyłany.</p>
270
+ <p><strong>Licencje.</strong> Strona powstała z inspiracji projektem „The Tokenizer Playground” (Xenova, Hugging Face, Apache 2.0). Tokenizery pochodzą z repozytoriów społeczności Xenova i podlegają licencjom modeli, z których wywodzą się. Tokenizer oznaczony jako Claude jest nieoficjalny.</p>
271
+ <p><strong>Uwaga.</strong> Liczby tokenów służą celom edukacyjnym. Rzeczywiste zużycie tokenów w usługach komercyjnych może się różnić, np. przez znaczniki systemowe. Tu nie liczymy znaczników specjalnych.</p>
272
+ <p>© Recepta Na AI sp. z o.o. · <a href="https://receptanaai.pl" target="_blank" rel="noopener">ReceptaNaAI.pl</a></p>
273
+ </div>
274
+ </footer>
275
+
276
+ <script type="module">
277
+ /* ============================================================
278
+ Konfiguracja: modele (tokenizery z Hugging Face Hub)
279
+ Aby dodać model, dopisz wpis do tablicy MODELS.
280
+ ============================================================ */
281
+ const LIB_URL = "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.1";
282
+
283
+ const MODELS = [
284
+ { id: "Xenova/gpt-4", label: "GPT-4", note: "Tokenizer rodziny GPT-4 i GPT-3.5. Nowsze modele OpenAI mogą używać innych tokenizerów." },
285
+ { id: "Xenova/llama-3-tokenizer", label: "Llama 3", note: "Tokenizer rodziny Llama 3 (Meta)." },
286
+ { id: "Xenova/mistral-tokenizer-v3", label: "Mistral v3", note: "Tokenizer Mistral v3." },
287
+ { id: "Xenova/gemma-tokenizer", label: "Gemma", note: "Tokenizer rodziny Gemma (Google)." },
288
+ { id: "Xenova/claude-tokenizer", label: "Claude", badge: "nieoficjalny", note: "Tokenizer społecznościowy (Xenova), nieoficjalny. Liczby są orientacyjne i mogą różnić się od tych w faktycznych modelach Claude." },
289
+ ];
290
+
291
+ const EXAMPLES = [
292
+ { label: "Zdanie po polsku", text: "Pacjent z kardiomiopatią przerostową otrzymał bisoprolol i amiodaron." },
293
+ { label: "To samo po angielsku", text: "The patient with hypertrophic cardiomyopathy received bisoprolol and amiodarone." },
294
+ { label: "Skróty z kardiologii", text: "EKG: SR 72/min, LVEF 55%, NT-proBNP 340 pg/ml, hs-TnT 12 ng/l." },
295
+ { label: "Nazwy leków", text: "metoprolol, rywaroksaban, atorwastatyna, ezetymib, sakubitryl/walsartan" },
296
+ { label: "Łacina kliniczna", text: "Fibrillatio atriorum paroxysmalis, status post ablationem." },
297
+ { label: "Zdanie bazowe", text: "Ala ma kota, a kot ma Alę." },
298
+ ];
299
+
300
+ const MAX_TILES = 1500; // tyle kafelków maksymalnie rysujemy
301
+ const DEBOUNCE_MS = 150;
302
+
303
+ /* ============================================================
304
+ Pomocnicze
305
+ ============================================================ */
306
+ const $ = (id) => document.getElementById(id);
307
+ const nf = new Intl.NumberFormat("pl-PL");
308
+ const nf1 = new Intl.NumberFormat("pl-PL", { minimumFractionDigits: 1, maximumFractionDigits: 1 });
309
+ const nf2 = new Intl.NumberFormat("pl-PL", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
310
+
311
+ function plTokeny(n) {
312
+ if (n === 1) return "token";
313
+ const m10 = n % 10, m100 = n % 100;
314
+ return (m10 >= 2 && m10 <= 4 && !(m100 >= 12 && m100 <= 14)) ? "tokeny" : "tokenów";
315
+ }
316
+ const countChars = (s) => Array.from(s).length;
317
+ const countWords = (s) => s.trim() ? s.trim().split(/\s+/).length : 0;
318
+ const modelById = (id) => MODELS.find((m) => m.id === id);
319
+
320
+ /* ============================================================
321
+ Stan
322
+ ============================================================ */
323
+ const params = new URLSearchParams(location.search);
324
+ let currentId = modelById(params.get("model")) ? params.get("model") : MODELS[0].id;
325
+ let runId = 0;
326
+ let firstRender = true;
327
+ let compareOn = false;
328
+ const loaded = new Map(); // id -> tokenizer (gotowy)
329
+ const loading = new Map(); // id -> Promise<tokenizer>
330
+ const failed = new Set(); // id, którym nie udało się załadować
331
+ let lib = null;
332
+
333
+ /* ============================================================
334
+ Ładowanie biblioteki i tokenizerów
335
+ ============================================================ */
336
+ async function getLib() {
337
+ if (lib) return lib;
338
+ lib = await import(LIB_URL);
339
+ lib.env.allowLocalModels = false;
340
+ return lib;
341
+ }
342
+
343
+ function fixDecoder(tok) {
344
+ // Tokenizery typu Llama/Mistral kończą dekoder krokiem StripDecoder, który obcina wiodącą spację tokenu.
345
+ // Zdejmujemy go, żeby przy pojedynczych tokenach było widać, do którego tokenu należy spacja.
346
+ try {
347
+ const list = tok.decoder?.decoders;
348
+ if (Array.isArray(list) && list.at(-1)?.constructor?.name === "StripDecoder") list.pop();
349
+ } catch (_) { /* wygląd nie jest krytyczny */ }
350
+ return tok;
351
+ }
352
+
353
+ function getTokenizer(id) {
354
+ if (loaded.has(id)) return Promise.resolve(loaded.get(id));
355
+ if (loading.has(id)) return loading.get(id);
356
+ const p = (async () => {
357
+ const { AutoTokenizer } = await getLib();
358
+ const tok = fixDecoder(await AutoTokenizer.from_pretrained(id));
359
+ loaded.set(id, tok);
360
+ failed.delete(id);
361
+ return tok;
362
+ })();
363
+ loading.set(id, p);
364
+ p.catch(() => { failed.add(id); }).finally(() => loading.delete(id));
365
+ return p;
366
+ }
367
+
368
+ function encodeIds(tok, text) {
369
+ // Bez znaczników specjalnych (np. BOS), żeby liczby były porównywalne między modelami.
370
+ const ids = tok.encode(text, { add_special_tokens: false });
371
+ return Array.from(ids, Number);
372
+ }
373
+
374
+ /* ============================================================
375
+ Interfejs: wybór modelu
376
+ ============================================================ */
377
+ function buildChips() {
378
+ const box = $("chips");
379
+ MODELS.forEach((m) => {
380
+ const lab = document.createElement("label");
381
+ lab.className = "chip";
382
+ const inp = document.createElement("input");
383
+ inp.type = "radio"; inp.name = "model"; inp.value = m.id; inp.checked = m.id === currentId;
384
+ inp.addEventListener("change", () => setModel(m.id));
385
+ const span = document.createElement("span");
386
+ span.append(m.label);
387
+ if (m.badge) { const s = document.createElement("small"); s.textContent = "(" + m.badge + ")"; span.append(s); }
388
+ lab.append(inp, span);
389
+ box.append(lab);
390
+ });
391
+ updateNote();
392
+ }
393
+
394
+ function updateNote() {
395
+ const m = modelById(currentId);
396
+ $("model-note").textContent = m ? m.note : "";
397
+ }
398
+
399
+ function setModel(id) {
400
+ currentId = id;
401
+ document.querySelectorAll('input[name="model"]').forEach((r) => { r.checked = r.value === id; });
402
+ updateNote();
403
+ render();
404
+ }
405
+
406
+ function buildExamples() {
407
+ const box = $("examples");
408
+ EXAMPLES.forEach((ex) => {
409
+ const b = document.createElement("button");
410
+ b.type = "button"; b.className = "btn-ex"; b.textContent = ex.label;
411
+ b.addEventListener("click", () => { $("text").value = ex.text; render(); });
412
+ box.append(b);
413
+ });
414
+ }
415
+
416
+ /* ============================================================
417
+ Rysowanie kafelków (tekst użytkownika wstawiamy wyłącznie przez textContent)
418
+ ============================================================ */
419
+ function fillTileText(el, text, showSpaces) {
420
+ if (!showSpaces) { el.textContent = text; return; }
421
+ let buf = "";
422
+ const flush = () => { if (buf) { el.append(document.createTextNode(buf)); buf = ""; } };
423
+ for (const ch of text) {
424
+ if (ch === " ") { flush(); const s = document.createElement("span"); s.className = "sp"; s.textContent = "·"; el.append(s); }
425
+ else if (ch === "\n") { flush(); const s = document.createElement("span"); s.className = "nl"; s.textContent = "↵"; el.append(s); el.append(document.createTextNode("\n")); }
426
+ else if (ch === "\t") { flush(); const s = document.createElement("span"); s.className = "sp"; s.textContent = "⇥"; el.append(s); }
427
+ else buf += ch;
428
+ }
429
+ flush();
430
+ }
431
+
432
+ function drawTokens(ids, decoded) {
433
+ const box = $("tokens");
434
+ box.replaceChildren();
435
+ box.classList.toggle("first", firstRender);
436
+ const showSp = $("opt-sp").checked, showId = $("opt-id").checked;
437
+ const n = Math.min(ids.length, MAX_TILES);
438
+ let hasFrag = false;
439
+ const frag = document.createDocumentFragment();
440
+ for (let i = 0; i < n; i++) {
441
+ const t = document.createElement("span");
442
+ const bad = decoded[i].includes("�");
443
+ if (bad) hasFrag = true;
444
+ t.className = "tok c" + (i % 5) + (bad ? " frag" : "");
445
+ t.style.setProperty("--i", Math.min(i, 40));
446
+ fillTileText(t, decoded[i], showSp);
447
+ if (showId) { const d = document.createElement("span"); d.className = "tid"; d.textContent = ids[i]; t.append(d); }
448
+ frag.append(t);
449
+ }
450
+ box.append(frag);
451
+ if (ids.length > MAX_TILES) {
452
+ const p = document.createElement("p");
453
+ p.className = "empty"; p.style.width = "100%";
454
+ p.textContent = "Pokazano pierwsze " + nf.format(MAX_TILES) + " tokenów z " + nf.format(ids.length) + ".";
455
+ box.append(p);
456
+ }
457
+ $("legend").hidden = !hasFrag;
458
+ firstRender = false;
459
+ }
460
+
461
+ function showMessage(text, retry) {
462
+ const box = $("tokens");
463
+ box.replaceChildren();
464
+ const p = document.createElement("p");
465
+ p.className = retry ? "err" : "empty";
466
+ p.textContent = text;
467
+ if (retry) {
468
+ const b = document.createElement("button");
469
+ b.type = "button"; b.className = "btn"; b.textContent = "Spróbuj ponownie";
470
+ b.addEventListener("click", retry);
471
+ p.append(b);
472
+ }
473
+ box.append(p);
474
+ $("legend").hidden = true;
475
+ }
476
+
477
+ function setStats(tokens, text) {
478
+ const chars = countChars(text), words = countWords(text);
479
+ $("s-tok").textContent = tokens == null ? "-" : nf.format(tokens);
480
+ $("s-chr").textContent = nf.format(chars);
481
+ $("s-wrd").textContent = nf.format(words);
482
+ $("s-tpw").textContent = (tokens != null && words > 0) ? nf1.format(tokens / words) : "-";
483
+ }
484
+
485
+ /* ============================================================
486
+ Główny przebieg
487
+ ============================================================ */
488
+ async function render() {
489
+ const my = ++runId;
490
+ const text = $("text").value;
491
+ const m = modelById(currentId);
492
+
493
+ if (!text) {
494
+ setStats(0, "");
495
+ showMessage("Wpisz tekst albo wybierz przykład, a tutaj pojawią się tokeny.");
496
+ updateBars(text);
497
+ return;
498
+ }
499
+ setStats(null, text);
500
+
501
+ let tok;
502
+ if (!loaded.has(currentId)) showMessage("Pobieram tokenizer " + m.label + "…");
503
+ try {
504
+ tok = await getTokenizer(currentId);
505
+ } catch (e) {
506
+ if (my !== runId) return;
507
+ console.error(e);
508
+ showMessage("Nie udało się pobrać tokenizera " + m.label + ". Sprawdź połączenie z internetem i spróbuj ponownie.", () => render());
509
+ updateBars(text);
510
+ return;
511
+ }
512
+ if (my !== runId) return;
513
+
514
+ let ids, decoded;
515
+ try {
516
+ ids = encodeIds(tok, text);
517
+ decoded = ids.slice(0, MAX_TILES).map((id) => tok.decode([id]));
518
+ // Tokenizery typu Llama dodają sztuczną spację na początku tekstu; nie pokazujemy jej, jeśli użytkownik jej nie wpisał.
519
+ if (decoded.length && decoded[0].startsWith(" ") && !text.startsWith(" ")) decoded[0] = decoded[0].slice(1);
520
+ } catch (e) {
521
+ console.error(e);
522
+ showMessage("Ten tokenizer nie poradził sobie z podanym tekstem.");
523
+ return;
524
+ }
525
+ setStats(ids.length, text);
526
+ drawTokens(ids, decoded);
527
+ updateBars(text);
528
+ }
529
+
530
+ /* ============================================================
531
+ Porównanie modeli
532
+ ============================================================ */
533
+ function updateBars(text) {
534
+ const list = $("bars");
535
+ if (!compareOn) { list.replaceChildren(); $("insight").textContent = ""; return; }
536
+ const rows = MODELS.map((m, i) => {
537
+ const tok = loaded.get(m.id);
538
+ let n = null;
539
+ if (tok && text) { try { n = encodeIds(tok, text).length; } catch (_) { n = null; } }
540
+ return { m, i, n };
541
+ });
542
+ const max = Math.max(1, ...rows.map((r) => r.n ?? 0));
543
+ const words = countWords(text);
544
+ list.replaceChildren();
545
+ rows.forEach(({ m, i, n }) => {
546
+ const li = document.createElement("li");
547
+ li.className = "bar-row";
548
+ const b = document.createElement("button");
549
+ b.type = "button"; b.textContent = m.label;
550
+ if (m.id === currentId) b.setAttribute("aria-current", "true");
551
+ b.title = "Pokaż tokeny tego modelu";
552
+ b.addEventListener("click", () => setModel(m.id));
553
+ const track = document.createElement("div");
554
+ track.className = "bar-track";
555
+ if (n == null) {
556
+ const s = document.createElement("span");
557
+ s.className = "bar-na";
558
+ s.textContent = failed.has(m.id) ? "brak danych (nie udało się pobrać tokenizera)" : (text ? "ładowanie…" : "-");
559
+ track.append(s);
560
+ } else {
561
+ const bar = document.createElement("div");
562
+ bar.className = "bar c" + (i % 5);
563
+ bar.style.width = "calc(" + (n / max * 100).toFixed(1) + "% - 9rem)";
564
+ bar.style.minWidth = "6px";
565
+ const v = document.createElement("span");
566
+ v.className = "bar-val";
567
+ v.append(nf.format(n) + " " + plTokeny(n));
568
+ if (words > 0) { const sm = document.createElement("small"); sm.textContent = " (" + nf1.format(n / words) + " na słowo)"; v.append(sm); }
569
+ track.append(bar, v);
570
+ }
571
+ li.append(b, track);
572
+ list.append(li);
573
+ });
574
+
575
+ const have = rows.filter((r) => r.n != null);
576
+ if (have.length >= 2) {
577
+ const lo = have.reduce((a, b) => (b.n < a.n ? b : a));
578
+ const hi = have.reduce((a, b) => (b.n > a.n ? b : a));
579
+ $("insight").textContent = lo.n === hi.n
580
+ ? "Wszystkie modele podzieliły ten tekst na tyle samo tokenów."
581
+ : "Najmniej tokenów: " + lo.m.label + " (" + nf.format(lo.n) + "). Najwięcej: " + hi.m.label + " (" + nf.format(hi.n) + "). Stosunek największej do najmniejszej liczby: " + nf2.format(hi.n / lo.n) + "×.";
582
+ } else {
583
+ $("insight").textContent = "";
584
+ }
585
+ }
586
+
587
+ async function runCompare() {
588
+ const btn = $("btn-cmp"), prog = $("cmp-progress");
589
+ compareOn = true;
590
+ btn.disabled = true;
591
+ const todo = MODELS.filter((m) => !loaded.has(m.id));
592
+ let done = 0;
593
+ updateBars($("text").value);
594
+ for (const m of todo) {
595
+ prog.textContent = "Pobieram tokenizer " + m.label + " (" + (done + 1) + " z " + todo.length + ")…";
596
+ try { await getTokenizer(m.id); } catch (e) { console.error(e); }
597
+ done++;
598
+ updateBars($("text").value);
599
+ }
600
+ prog.textContent = failed.size ? "Część tokenizerów nie została pobrana." : "";
601
+ btn.disabled = false;
602
+ btn.textContent = "Odśwież porównanie";
603
+ if (failed.size) btn.textContent = "Spróbuj ponownie pobrać brakujące";
604
+ }
605
+
606
+ /* ============================================================
607
+ Start
608
+ ============================================================ */
609
+ buildChips();
610
+ buildExamples();
611
+
612
+ let timer = null;
613
+ $("text").addEventListener("input", () => { clearTimeout(timer); timer = setTimeout(render, DEBOUNCE_MS); });
614
+ $("opt-sp").addEventListener("change", render);
615
+ $("opt-id").addEventListener("change", render);
616
+ $("btn-cmp").addEventListener("click", runCompare);
617
+
618
+ $("text").value = params.get("text") ?? EXAMPLES[0].text;
619
+ render();
620
+ </script>
621
+ <noscript><div class="wrap"><p class="privacy">Do działania tokenizatora potrzebny jest JavaScript.</p></div></noscript>
622
+ </body>
623
  </html>