Spaces:
Running
Running
Update index.html
Browse files- index.html +24 -7
index.html
CHANGED
|
@@ -148,15 +148,33 @@
|
|
| 148 |
|
| 149 |
status('Tokenizing…');
|
| 150 |
try {
|
| 151 |
-
//
|
| 152 |
-
const
|
| 153 |
-
|
| 154 |
-
// Get IDs (encode() may return different field names across versions)
|
| 155 |
-
const enc = await tokenizer.encode(text);
|
| 156 |
const ids =
|
| 157 |
(enc && (enc.ids ?? enc.input_ids ?? enc.inputIds)) || [];
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
state.tokens = Array.isArray(tokens) ? tokens : [];
|
| 161 |
state.ids = Array.isArray(ids) ? ids : [];
|
| 162 |
|
|
@@ -169,7 +187,6 @@
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
-
|
| 173 |
function render(){
|
| 174 |
const tokens = Array.isArray(state.tokens) ? state.tokens : [];
|
| 175 |
const ids = Array.isArray(state.ids) ? state.ids : [];
|
|
|
|
| 148 |
|
| 149 |
status('Tokenizing…');
|
| 150 |
try {
|
| 151 |
+
// 1) Always get IDs from encode
|
| 152 |
+
const enc = await tokenizer.encode(text, { add_special_tokens: false });
|
|
|
|
|
|
|
|
|
|
| 153 |
const ids =
|
| 154 |
(enc && (enc.ids ?? enc.input_ids ?? enc.inputIds)) || [];
|
| 155 |
|
| 156 |
+
// 2) Derive token strings in a version-tolerant way
|
| 157 |
+
let tokens = [];
|
| 158 |
+
|
| 159 |
+
// a) Preferred: convert_ids_to_tokens exists on many tokenizers
|
| 160 |
+
if (typeof tokenizer.convert_ids_to_tokens === 'function') {
|
| 161 |
+
tokens = tokenizer.convert_ids_to_tokens(ids);
|
| 162 |
+
}
|
| 163 |
+
// b) Fallback: id_to_token per-id
|
| 164 |
+
else if (typeof tokenizer.id_to_token === 'function') {
|
| 165 |
+
tokens = ids.map(id => tokenizer.id_to_token(id));
|
| 166 |
+
}
|
| 167 |
+
// c) Some builds include enc.tokens
|
| 168 |
+
else if (Array.isArray(enc.tokens)) {
|
| 169 |
+
tokens = enc.tokens;
|
| 170 |
+
}
|
| 171 |
+
// d) Last resort: stringify IDs (shouldn’t happen, but keeps UI stable)
|
| 172 |
+
else {
|
| 173 |
+
tokens = ids.map(String);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
if (myRun !== runId) return; // drop stale results
|
| 177 |
+
|
| 178 |
state.tokens = Array.isArray(tokens) ? tokens : [];
|
| 179 |
state.ids = Array.isArray(ids) ? ids : [];
|
| 180 |
|
|
|
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
|
|
|
| 190 |
function render(){
|
| 191 |
const tokens = Array.isArray(state.tokens) ? state.tokens : [];
|
| 192 |
const ids = Array.isArray(state.ids) ? state.ids : [];
|