jfrery-zama commited on
Commit
552a6d2
·
unverified ·
1 Parent(s): 79953d6

validate token count

Browse files
Files changed (1) hide show
  1. wasm-demo.js +21 -1
wasm-demo.js CHANGED
@@ -227,24 +227,29 @@ $('tokenInput').addEventListener('input', () => {
227
  $('estimatedTime').textContent = timeText;
228
 
229
  if (tokenCount > TOKEN_LIMIT) {
230
- $('encStatus').textContent = `⚠️ ${tokenCount}/${TOKEN_LIMIT} tokens - text too long`;
231
  $('encStatus').style.color = '#d32f2f';
 
232
  } else if (tokenCount < 10) {
233
  $('encStatus').textContent = `⚠️ ${tokenCount}/${TOKEN_LIMIT} tokens - low reliability`;
234
  $('encStatus').style.color = '#f57c00';
 
235
  } else {
236
  $('encStatus').textContent = `${tokenCount}/${TOKEN_LIMIT} tokens`;
237
  $('encStatus').style.color = '';
 
238
  }
239
  } catch (e) {
240
  // Tokenizer might not be ready yet
241
  $('encStatus').textContent = '';
242
  currentTokenCount = 0;
 
243
  }
244
  } else {
245
  $('encStatus').textContent = '';
246
  $('encStatus').style.color = '';
247
  currentTokenCount = 0;
 
248
  }
249
  });
250
 
@@ -261,6 +266,21 @@ $('btnEncrypt').onclick = async () => {
261
  return;
262
  }
263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  show('encryptSpin', true);
265
  show('encIcon', false);
266
  enable('btnEncrypt', false);
 
227
  $('estimatedTime').textContent = timeText;
228
 
229
  if (tokenCount > TOKEN_LIMIT) {
230
+ $('encStatus').textContent = `⚠️ ${tokenCount}/${TOKEN_LIMIT} tokens - exceeds limit, encryption disabled`;
231
  $('encStatus').style.color = '#d32f2f';
232
+ enable('btnEncrypt', false);
233
  } else if (tokenCount < 10) {
234
  $('encStatus').textContent = `⚠️ ${tokenCount}/${TOKEN_LIMIT} tokens - low reliability`;
235
  $('encStatus').style.color = '#f57c00';
236
+ enable('btnEncrypt', true);
237
  } else {
238
  $('encStatus').textContent = `${tokenCount}/${TOKEN_LIMIT} tokens`;
239
  $('encStatus').style.color = '';
240
+ enable('btnEncrypt', true);
241
  }
242
  } catch (e) {
243
  // Tokenizer might not be ready yet
244
  $('encStatus').textContent = '';
245
  currentTokenCount = 0;
246
+ enable('btnEncrypt', true);
247
  }
248
  } else {
249
  $('encStatus').textContent = '';
250
  $('encStatus').style.color = '';
251
  currentTokenCount = 0;
252
+ enable('btnEncrypt', true);
253
  }
254
  });
255
 
 
266
  return;
267
  }
268
 
269
+ // Validate token limit before proceeding
270
+ try {
271
+ const tokenIds = llama3Tokenizer.encode(text);
272
+ const TOKEN_LIMIT = 16;
273
+ if (tokenIds.length > TOKEN_LIMIT) {
274
+ console.error(`[Main] Token limit exceeded: ${tokenIds.length}/${TOKEN_LIMIT} tokens`);
275
+ alert(`Text is too long. Maximum ${TOKEN_LIMIT} tokens allowed, but your text has ${tokenIds.length} tokens. Please shorten your text.`);
276
+ return;
277
+ }
278
+ } catch (error) {
279
+ console.error('[Main] Token validation error:', error);
280
+ alert(`Error validating text: ${error.message}`);
281
+ return;
282
+ }
283
+
284
  show('encryptSpin', true);
285
  show('encIcon', false);
286
  enable('btnEncrypt', false);