djstrong commited on
Commit
ca14ddc
·
1 Parent(s): b3505c0

Update .env.example to include TOKEN for gated Hugging Face repos and modify app.py to support TOKEN or HF_TOKEN for loading tokenizers.

Browse files
Files changed (2) hide show
  1. .env.example +3 -2
  2. app.py +11 -2
.env.example CHANGED
@@ -1,2 +1,3 @@
1
- HF_TOKEN=your_huggingface_token_here
2
-
 
 
1
+ # For gated Hugging Face repos (e.g. some LLaMA, Bielik). Use one of:
2
+ TOKEN=your_huggingface_token_here
3
+ # HF_TOKEN=your_huggingface_token_here
app.py CHANGED
@@ -3,10 +3,14 @@ Tokenizer Playground — inspect how text is tokenized, view config and chat tem
3
  Runs locally and on Hugging Face Spaces.
4
  """
5
  import html
 
6
  import random
7
  import gradio as gr
8
  from transformers import AutoTokenizer
9
 
 
 
 
10
 
11
  def _token_color(index: int, total: int) -> str:
12
  """Distinct hue per token, pastel background (HSL)."""
@@ -99,11 +103,16 @@ def get_tokenizer(model_id: str, use_fast: bool = True):
99
  key = (model_id or DEFAULT_MODEL, use_fast)
100
  if key not in _tokenizer_cache:
101
  try:
102
- _tokenizer_cache[key] = AutoTokenizer.from_pretrained(
103
- model_id or DEFAULT_MODEL,
104
  trust_remote_code=True,
105
  use_fast=use_fast,
106
  )
 
 
 
 
 
 
107
  except Exception as e:
108
  raise RuntimeError(f"Failed to load tokenizer for '{model_id}': {e}") from e
109
  return _tokenizer_cache[key]
 
3
  Runs locally and on Hugging Face Spaces.
4
  """
5
  import html
6
+ import os
7
  import random
8
  import gradio as gr
9
  from transformers import AutoTokenizer
10
 
11
+ # For gated repos: set TOKEN or HF_TOKEN (e.g. in .env or Hugging Face Space secrets)
12
+ HF_TOKEN = os.getenv("TOKEN") or os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN") or None
13
+
14
 
15
  def _token_color(index: int, total: int) -> str:
16
  """Distinct hue per token, pastel background (HSL)."""
 
103
  key = (model_id or DEFAULT_MODEL, use_fast)
104
  if key not in _tokenizer_cache:
105
  try:
106
+ kwargs = dict(
 
107
  trust_remote_code=True,
108
  use_fast=use_fast,
109
  )
110
+ if HF_TOKEN:
111
+ kwargs["token"] = HF_TOKEN
112
+ _tokenizer_cache[key] = AutoTokenizer.from_pretrained(
113
+ model_id or DEFAULT_MODEL,
114
+ **kwargs,
115
+ )
116
  except Exception as e:
117
  raise RuntimeError(f"Failed to load tokenizer for '{model_id}': {e}") from e
118
  return _tokenizer_cache[key]