Spaces:
Running
Running
Gül Sena Altıntaş
commited on
Commit
·
3a08f05
1
Parent(s):
14ea3d3
Added additional tokenizers
Browse files- app.py +447 -92
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,138 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import tiktoken
|
| 3 |
from transformers import AutoTokenizer
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
# Model mappings
|
| 7 |
MODEL_MAP = {
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
}
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def tokenize_with_tiktoken(text, model):
|
| 16 |
-
encoding =
|
| 17 |
enc = tiktoken.get_encoding(encoding)
|
| 18 |
tokens = enc.encode(text)
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
|
|
|
| 28 |
def tokenize_with_hf(text, model):
|
| 29 |
try:
|
| 30 |
-
model_name = MODEL_MAP.get(model,
|
| 31 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
tokens = tokenizer.encode(text)
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
except Exception as e:
|
| 43 |
return {
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
if not text.strip():
|
| 52 |
-
return "Please enter some text to tokenize."
|
| 53 |
-
|
| 54 |
-
results =
|
| 55 |
-
|
| 56 |
for model in selected_models:
|
| 57 |
-
if model in [
|
| 58 |
-
|
| 59 |
else:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Blocks(
|
| 78 |
-
title="🔤 Tokenizer Comparison
|
| 79 |
-
theme=gr.themes.Soft()
|
| 80 |
) as demo:
|
| 81 |
-
|
| 82 |
gr.Markdown("""
|
| 83 |
-
# 🔤 Tokenizer Comparison Tool
|
| 84 |
|
| 85 |
-
Compare how different LLM tokenizers split text into tokens.
|
| 86 |
-
""")
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column(scale=2):
|
| 90 |
text_input = gr.Textbox(
|
| 91 |
label="Text to tokenize",
|
| 92 |
-
placeholder="
|
| 93 |
lines=4,
|
| 94 |
-
value="Hello world! This is a test with some subwords and punctuation."
|
| 95 |
)
|
| 96 |
-
|
| 97 |
with gr.Column(scale=1):
|
| 98 |
model_selector = gr.CheckboxGroup(
|
| 99 |
-
choices=[
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
gr.Markdown("""
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
- **
|
| 127 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
###
|
| 130 |
-
- **
|
| 131 |
-
- **
|
| 132 |
-
- **
|
| 133 |
-
- **
|
| 134 |
-
- **
|
| 135 |
""")
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
demo.launch()
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from collections import Counter
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import plotly.express as px
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
import tiktoken
|
| 10 |
from transformers import AutoTokenizer
|
|
|
|
| 11 |
|
| 12 |
# Model mappings
|
| 13 |
MODEL_MAP = {
|
| 14 |
+
"llama-2": "meta-llama/Llama-2-7b-hf",
|
| 15 |
+
"llama-3": "meta-llama/Llama-3.2-1B",
|
| 16 |
+
"gemma-2": "google/gemma-2-2b",
|
| 17 |
+
"qwen3": "Qwen/Qwen3-0.6B",
|
| 18 |
+
"qwen2.5": "Qwen/Qwen2.5-0.5B",
|
| 19 |
+
"bert": "bert-base-uncased",
|
| 20 |
+
"bloom": "bigscience/bloom-560m",
|
| 21 |
+
"aya-expanse": "CohereForAI/aya-expanse-8b",
|
| 22 |
+
"comma": "common-pile/comma-v0.1-2tgpt2",
|
| 23 |
+
"byte-level": "google/byt5-small",
|
| 24 |
+
"tokenmonster": "alasdairforsythe/tokenmonster",
|
| 25 |
}
|
| 26 |
|
| 27 |
+
TOKENIZER_INFO = {
|
| 28 |
+
"gpt-4": {"name": "GPT-4", "vocab_size": 100277, "encoding": "BPE"},
|
| 29 |
+
"gpt-2": {"name": "GPT-2", "vocab_size": 50257, "encoding": "BPE"},
|
| 30 |
+
"llama-2": {"name": "LLaMA-2", "vocab_size": 32000, "encoding": "SentencePiece"},
|
| 31 |
+
"llama-3": {"name": "LLaMA-3", "vocab_size": 128000, "encoding": "SentencePiece"},
|
| 32 |
+
"gemma-2": {"name": "Gemma-2", "vocab_size": 256000, "encoding": "SentencePiece"},
|
| 33 |
+
"qwen3": {"name": "Qwen3", "vocab_size": 151936, "encoding": "BPE"},
|
| 34 |
+
"qwen2.5": {"name": "Qwen2.5", "vocab_size": 151936, "encoding": "BPE"},
|
| 35 |
+
"bert": {"name": "BERT", "vocab_size": 30522, "encoding": "WordPiece"},
|
| 36 |
+
"bloom": {"name": "BLOOM", "vocab_size": 250680, "encoding": "BPE"},
|
| 37 |
+
"aya-expanse": {
|
| 38 |
+
"name": "Aya Expanse",
|
| 39 |
+
"vocab_size": 256000,
|
| 40 |
+
"encoding": "SentencePiece",
|
| 41 |
+
},
|
| 42 |
+
"comma": {"name": "Comma AI", "vocab_size": 50257, "encoding": ""},
|
| 43 |
+
"byte-level": {"name": "Byte-Level BPE", "vocab_size": 50000, "encoding": "BPE"},
|
| 44 |
+
"tokenmonster": {"name": "TokenMonster", "vocab_size": 32000, "encoding": ""},
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_token_type(token_text):
|
| 49 |
+
import re
|
| 50 |
+
|
| 51 |
+
if re.match(r"^\s+$", token_text):
|
| 52 |
+
return "whitespace"
|
| 53 |
+
elif re.match(r"^[a-zA-Z]+$", token_text):
|
| 54 |
+
return "word"
|
| 55 |
+
elif re.match(r"^\d+$", token_text):
|
| 56 |
+
return "number"
|
| 57 |
+
elif re.match(r"^[^\w\s]+$", token_text):
|
| 58 |
+
return "punctuation"
|
| 59 |
+
elif token_text.startswith("<") and token_text.endswith(">"):
|
| 60 |
+
return "special"
|
| 61 |
+
else:
|
| 62 |
+
return "mixed"
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def is_subword(token_text, model, is_first):
|
| 66 |
+
if model in ["llama-2", "llama-3", "qwen3"]:
|
| 67 |
+
return not token_text.startswith("▁") and not is_first
|
| 68 |
+
elif model == "bert":
|
| 69 |
+
return token_text.startswith("##")
|
| 70 |
+
else: # BPE models
|
| 71 |
+
return not token_text.startswith(" ") and not is_first and len(token_text) > 0
|
| 72 |
+
|
| 73 |
+
|
| 74 |
def tokenize_with_tiktoken(text, model):
|
| 75 |
+
encoding = "cl100k_base" if model == "gpt-4" else "gpt2"
|
| 76 |
enc = tiktoken.get_encoding(encoding)
|
| 77 |
tokens = enc.encode(text)
|
| 78 |
+
|
| 79 |
+
token_data = []
|
| 80 |
+
current_pos = 0
|
| 81 |
+
|
| 82 |
+
for i, token_id in enumerate(tokens):
|
| 83 |
+
token_text = enc.decode([token_id])
|
| 84 |
+
token_type = get_token_type(token_text)
|
| 85 |
+
subword = is_subword(token_text, model, i == 0)
|
| 86 |
+
|
| 87 |
+
token_data.append(
|
| 88 |
+
{
|
| 89 |
+
"text": token_text,
|
| 90 |
+
"id": int(token_id),
|
| 91 |
+
"type": token_type,
|
| 92 |
+
"is_subword": subword,
|
| 93 |
+
"bytes": len(token_text.encode("utf-8")),
|
| 94 |
+
"position": i,
|
| 95 |
+
}
|
| 96 |
+
)
|
| 97 |
+
current_pos += len(token_text)
|
| 98 |
+
|
| 99 |
return {
|
| 100 |
+
"model": TOKENIZER_INFO[model]["name"],
|
| 101 |
+
"token_count": len(tokens),
|
| 102 |
+
"tokens": token_data,
|
| 103 |
+
"compression_ratio": len(text) / len(tokens) if tokens else 0,
|
| 104 |
+
"encoding": TOKENIZER_INFO[model]["encoding"],
|
| 105 |
+
"vocab_size": TOKENIZER_INFO[model]["vocab_size"],
|
| 106 |
}
|
| 107 |
|
| 108 |
+
|
| 109 |
def tokenize_with_hf(text, model):
|
| 110 |
try:
|
| 111 |
+
model_name = MODEL_MAP.get(model, "gpt2")
|
| 112 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 113 |
+
model_name, token=os.getenv("HF_TOKEN"), trust_remote_code=True
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
tokens = tokenizer.encode(text)
|
| 117 |
+
token_data = []
|
| 118 |
+
|
| 119 |
+
for i, token_id in enumerate(tokens):
|
| 120 |
+
token_text = tokenizer.decode([token_id], skip_special_tokens=False)
|
| 121 |
+
token_type = get_token_type(token_text)
|
| 122 |
+
subword = is_subword(token_text, model, i == 0)
|
| 123 |
+
|
| 124 |
+
token_data.append(
|
| 125 |
+
{
|
| 126 |
+
"text": token_text,
|
| 127 |
+
"id": int(token_id),
|
| 128 |
+
"type": token_type,
|
| 129 |
+
"is_subword": subword,
|
| 130 |
+
"bytes": len(token_text.encode("utf-8")),
|
| 131 |
+
"position": i,
|
| 132 |
+
}
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
return {
|
| 136 |
+
"model": TOKENIZER_INFO[model]["name"],
|
| 137 |
+
"token_count": len(tokens),
|
| 138 |
+
"tokens": token_data,
|
| 139 |
+
"compression_ratio": len(text) / len(tokens) if tokens else 0,
|
| 140 |
+
"encoding": TOKENIZER_INFO[model]["encoding"],
|
| 141 |
+
"vocab_size": TOKENIZER_INFO[model]["vocab_size"],
|
| 142 |
}
|
| 143 |
except Exception as e:
|
| 144 |
return {
|
| 145 |
+
"model": TOKENIZER_INFO[model]["name"],
|
| 146 |
+
"token_count": 0,
|
| 147 |
+
"tokens": [],
|
| 148 |
+
"compression_ratio": 0,
|
| 149 |
+
"encoding": "Error",
|
| 150 |
+
"vocab_size": 0,
|
| 151 |
+
"error": str(e),
|
| 152 |
}
|
| 153 |
|
| 154 |
+
|
| 155 |
+
def compare_tokenizers(text, selected_models, show_details=False):
|
| 156 |
if not text.strip():
|
| 157 |
+
return "Please enter some text to tokenize.", "", None, None
|
| 158 |
+
|
| 159 |
+
results = {}
|
| 160 |
+
|
| 161 |
for model in selected_models:
|
| 162 |
+
if model in ["gpt-4", "gpt-2"]:
|
| 163 |
+
results[model] = tokenize_with_tiktoken(text, model)
|
| 164 |
else:
|
| 165 |
+
results[model] = tokenize_with_hf(text, model)
|
| 166 |
+
|
| 167 |
+
# Generate outputs
|
| 168 |
+
basic_output = generate_basic_comparison(results)
|
| 169 |
+
detailed_output = generate_detailed_analysis(results) if show_details else ""
|
| 170 |
+
efficiency_chart = create_efficiency_chart(results)
|
| 171 |
+
token_distribution_chart = create_token_distribution_chart(results)
|
| 172 |
+
|
| 173 |
+
return basic_output, detailed_output, efficiency_chart, token_distribution_chart
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def generate_basic_comparison(results):
|
| 177 |
+
if not results:
|
| 178 |
+
return "No results to display."
|
| 179 |
+
|
| 180 |
+
output = []
|
| 181 |
+
|
| 182 |
+
# Efficiency ranking
|
| 183 |
+
sorted_models = sorted(results.items(), key=lambda x: x[1]["token_count"])
|
| 184 |
+
|
| 185 |
+
output.append("## 🏆 Efficiency Ranking (Fewer tokens = more efficient)")
|
| 186 |
+
for i, (model, result) in enumerate(sorted_models):
|
| 187 |
+
if "error" in result:
|
| 188 |
+
output.append(
|
| 189 |
+
f"{i + 1}. **{result['model']}**: ❌ Error - {result['error']}"
|
| 190 |
+
)
|
| 191 |
+
else:
|
| 192 |
+
output.append(
|
| 193 |
+
f"{i + 1}. **{result['model']}**: {result['token_count']} tokens "
|
| 194 |
+
f"({result['compression_ratio']:.2f}x compression)"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
output.append("\n## 🔤 Tokenization Results")
|
| 198 |
+
|
| 199 |
+
for model, result in results.items():
|
| 200 |
+
if "error" in result:
|
| 201 |
+
output.append(f"\n### ❌ {result['model']} - Error: {result['error']}")
|
| 202 |
+
continue
|
| 203 |
+
|
| 204 |
+
output.append(f"\n### {result['model']}")
|
| 205 |
+
output.append(f"- **Tokens**: {result['token_count']}")
|
| 206 |
+
output.append(f"- **Vocab Size**: {result['vocab_size']:,}")
|
| 207 |
+
output.append(f"- **Encoding**: {result['encoding']}")
|
| 208 |
+
output.append(f"- **Compression**: {result['compression_ratio']:.2f}x")
|
| 209 |
+
|
| 210 |
+
# Show first 20 tokens with visual indicators
|
| 211 |
+
tokens_display = []
|
| 212 |
+
subword_count = 0
|
| 213 |
+
|
| 214 |
+
for token in result["tokens"][:20]:
|
| 215 |
+
token_text = token["text"]
|
| 216 |
+
if token_text == " ":
|
| 217 |
+
token_text = "·" # Space indicator
|
| 218 |
+
elif token_text.strip() == "":
|
| 219 |
+
token_text = "⎵" # Empty token indicator
|
| 220 |
+
|
| 221 |
+
# Add type indicators
|
| 222 |
+
if token["is_subword"]:
|
| 223 |
+
tokens_display.append(f"🔸`{token_text}`")
|
| 224 |
+
subword_count += 1
|
| 225 |
+
elif token["type"] == "word":
|
| 226 |
+
tokens_display.append(f"🔤`{token_text}`")
|
| 227 |
+
elif token["type"] == "number":
|
| 228 |
+
tokens_display.append(f"🔢`{token_text}`")
|
| 229 |
+
elif token["type"] == "punctuation":
|
| 230 |
+
tokens_display.append(f"❗`{token_text}`")
|
| 231 |
+
else:
|
| 232 |
+
tokens_display.append(f"`{token_text}`")
|
| 233 |
+
|
| 234 |
+
if len(result["tokens"]) > 20:
|
| 235 |
+
tokens_display.append(f"... (+{len(result['tokens']) - 20} more)")
|
| 236 |
+
|
| 237 |
+
output.append(f"- **Subwords**: {subword_count}/{len(result['tokens'][:20])}")
|
| 238 |
+
output.append(f"- **Tokens**: {' '.join(tokens_display)}")
|
| 239 |
+
|
| 240 |
+
return "\n".join(output)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def generate_detailed_analysis(results):
|
| 244 |
+
if not results or len(results) < 2:
|
| 245 |
+
return "Need at least 2 tokenizers for detailed analysis."
|
| 246 |
+
|
| 247 |
+
output = []
|
| 248 |
+
output.append("## 🔍 Detailed Analysis")
|
| 249 |
+
|
| 250 |
+
# Find common tokens
|
| 251 |
+
all_token_sets = []
|
| 252 |
+
for model, result in results.items():
|
| 253 |
+
if "error" not in result:
|
| 254 |
+
token_texts = {token["text"] for token in result["tokens"]}
|
| 255 |
+
all_token_sets.append(token_texts)
|
| 256 |
+
|
| 257 |
+
if all_token_sets:
|
| 258 |
+
common_tokens = set.intersection(*all_token_sets)
|
| 259 |
+
output.append(f"\n### Common Tokens ({len(common_tokens)})")
|
| 260 |
+
if common_tokens:
|
| 261 |
+
common_display = [
|
| 262 |
+
f"`{token}`" if token != " " else "`·`"
|
| 263 |
+
for token in list(common_tokens)[:15]
|
| 264 |
+
]
|
| 265 |
+
output.append(" ".join(common_display))
|
| 266 |
+
else:
|
| 267 |
+
output.append("No common tokens found.")
|
| 268 |
+
|
| 269 |
+
# Token type distribution
|
| 270 |
+
output.append("\n### Token Type Distribution")
|
| 271 |
+
for model, result in results.items():
|
| 272 |
+
if "error" not in result:
|
| 273 |
+
type_counts = Counter(token["type"] for token in result["tokens"])
|
| 274 |
+
type_display = [f"{type_}: {count}" for type_, count in type_counts.items()]
|
| 275 |
+
output.append(f"**{result['model']}**: {', '.join(type_display)}")
|
| 276 |
|
| 277 |
+
# Subword analysis
|
| 278 |
+
output.append("\n### Subword Analysis")
|
| 279 |
+
for model, result in results.items():
|
| 280 |
+
if "error" not in result:
|
| 281 |
+
subwords = [token for token in result["tokens"] if token["is_subword"]]
|
| 282 |
+
subword_ratio = (
|
| 283 |
+
len(subwords) / len(result["tokens"]) * 100 if result["tokens"] else 0
|
| 284 |
+
)
|
| 285 |
+
output.append(
|
| 286 |
+
f"**{result['model']}**: {len(subwords)} subwords ({subword_ratio:.1f}%)"
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
return "\n".join(output)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def create_efficiency_chart(results):
|
| 293 |
+
if not results:
|
| 294 |
+
return None
|
| 295 |
+
|
| 296 |
+
models = []
|
| 297 |
+
token_counts = []
|
| 298 |
+
compression_ratios = []
|
| 299 |
+
|
| 300 |
+
for model, result in results.items():
|
| 301 |
+
if "error" not in result:
|
| 302 |
+
models.append(result["model"])
|
| 303 |
+
token_counts.append(result["token_count"])
|
| 304 |
+
compression_ratios.append(result["compression_ratio"])
|
| 305 |
+
|
| 306 |
+
if not models:
|
| 307 |
+
return None
|
| 308 |
+
|
| 309 |
+
fig = go.Figure()
|
| 310 |
+
|
| 311 |
+
# Add token count bars
|
| 312 |
+
fig.add_trace(
|
| 313 |
+
go.Bar(
|
| 314 |
+
x=models,
|
| 315 |
+
y=token_counts,
|
| 316 |
+
name="Token Count",
|
| 317 |
+
marker_color="lightblue",
|
| 318 |
+
text=token_counts,
|
| 319 |
+
textposition="auto",
|
| 320 |
+
)
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
fig.update_layout(
|
| 324 |
+
title="Token Count Comparison (Lower = More Efficient)",
|
| 325 |
+
xaxis_title="Tokenizer",
|
| 326 |
+
yaxis_title="Number of Tokens",
|
| 327 |
+
template="plotly_white",
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
return fig
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
def create_token_distribution_chart(results):
|
| 334 |
+
if not results:
|
| 335 |
+
return None
|
| 336 |
+
|
| 337 |
+
all_data = []
|
| 338 |
+
|
| 339 |
+
for model, result in results.items():
|
| 340 |
+
if "error" not in result:
|
| 341 |
+
type_counts = Counter(token["type"] for token in result["tokens"])
|
| 342 |
+
for token_type, count in type_counts.items():
|
| 343 |
+
all_data.append(
|
| 344 |
+
{
|
| 345 |
+
"Tokenizer": result["model"],
|
| 346 |
+
"Token Type": token_type,
|
| 347 |
+
"Count": count,
|
| 348 |
+
}
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
if not all_data:
|
| 352 |
+
return None
|
| 353 |
+
|
| 354 |
+
df = pd.DataFrame(all_data)
|
| 355 |
+
|
| 356 |
+
fig = px.bar(
|
| 357 |
+
df,
|
| 358 |
+
x="Tokenizer",
|
| 359 |
+
y="Count",
|
| 360 |
+
color="Token Type",
|
| 361 |
+
title="Token Type Distribution by Tokenizer",
|
| 362 |
+
template="plotly_white",
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
return fig
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
# Custom CSS for better styling
|
| 369 |
+
css = """
|
| 370 |
+
.gradio-container {
|
| 371 |
+
font-family: 'Inter', sans-serif;
|
| 372 |
+
}
|
| 373 |
+
.token-display {
|
| 374 |
+
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
| 375 |
+
background: #f8f9fa;
|
| 376 |
+
padding: 8px;
|
| 377 |
+
border-radius: 4px;
|
| 378 |
+
font-size: 0.9em;
|
| 379 |
+
}
|
| 380 |
+
"""
|
| 381 |
+
|
| 382 |
+
# Create the Gradio interface
|
| 383 |
with gr.Blocks(
|
| 384 |
+
title="🔤 Advanced Tokenizer Comparison", theme=gr.themes.Soft(), css=css
|
|
|
|
| 385 |
) as demo:
|
|
|
|
| 386 |
gr.Markdown("""
|
| 387 |
+
# 🔤 Advanced Tokenizer Comparison Tool
|
| 388 |
|
| 389 |
+
Compare how different LLM tokenizers split text into tokens. Analyze efficiency, subwords, and token types.
|
|
|
|
| 390 |
|
| 391 |
+
**Legend**: 🔤 Word | 🔢 Number | ❗ Punctuation | 🔸 Subword | · Space
|
| 392 |
+
""")
|
| 393 |
+
|
| 394 |
with gr.Row():
|
| 395 |
with gr.Column(scale=2):
|
| 396 |
text_input = gr.Textbox(
|
| 397 |
label="Text to tokenize",
|
| 398 |
+
placeholder="Enter your text here...",
|
| 399 |
lines=4,
|
| 400 |
+
value="Hello world! This is a test with some subwords and punctuation.",
|
| 401 |
)
|
| 402 |
+
|
| 403 |
with gr.Column(scale=1):
|
| 404 |
model_selector = gr.CheckboxGroup(
|
| 405 |
+
choices=[
|
| 406 |
+
"gpt-4",
|
| 407 |
+
"gpt-2",
|
| 408 |
+
"llama-2",
|
| 409 |
+
"llama-3",
|
| 410 |
+
"gemma-2",
|
| 411 |
+
"qwen3",
|
| 412 |
+
"qwen2.5",
|
| 413 |
+
"bert",
|
| 414 |
+
"bloom",
|
| 415 |
+
"aya-expanse",
|
| 416 |
+
"comma",
|
| 417 |
+
"byte-level",
|
| 418 |
+
"tokenmonster",
|
| 419 |
+
],
|
| 420 |
+
value=["gpt-4", "llama-3", "gpt-2"],
|
| 421 |
+
label="Select tokenizers to compare",
|
| 422 |
)
|
| 423 |
+
|
| 424 |
+
show_details = gr.Checkbox(label="Show detailed analysis", value=False)
|
| 425 |
+
|
| 426 |
+
with gr.Row():
|
| 427 |
+
with gr.Column():
|
| 428 |
+
basic_output = gr.Markdown(
|
| 429 |
+
label="Comparison Results",
|
| 430 |
+
value="Enter text above to see tokenization results...",
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
with gr.Row():
|
| 434 |
+
with gr.Column():
|
| 435 |
+
detailed_output = gr.Markdown(label="Detailed Analysis", visible=False)
|
| 436 |
+
|
| 437 |
+
with gr.Row():
|
| 438 |
+
with gr.Column():
|
| 439 |
+
efficiency_chart = gr.Plot(label="Efficiency Comparison")
|
| 440 |
+
with gr.Column():
|
| 441 |
+
distribution_chart = gr.Plot(label="Token Type Distribution")
|
| 442 |
+
|
| 443 |
+
# Update visibility of detailed analysis
|
| 444 |
+
def toggle_details(show_details):
|
| 445 |
+
return gr.update(visible=show_details)
|
| 446 |
+
|
| 447 |
+
show_details.change(fn=toggle_details, inputs=show_details, outputs=detailed_output)
|
| 448 |
+
|
| 449 |
+
# Main comparison function
|
| 450 |
+
def update_comparison(text, models, details):
|
| 451 |
+
basic, detailed, eff_chart, dist_chart = compare_tokenizers(
|
| 452 |
+
text, models, details
|
| 453 |
+
)
|
| 454 |
+
return basic, detailed, eff_chart, dist_chart
|
| 455 |
+
|
| 456 |
+
# Auto-update on changes
|
| 457 |
+
for component in [text_input, model_selector, show_details]:
|
| 458 |
+
component.change(
|
| 459 |
+
fn=update_comparison,
|
| 460 |
+
inputs=[text_input, model_selector, show_details],
|
| 461 |
+
outputs=[
|
| 462 |
+
basic_output,
|
| 463 |
+
detailed_output,
|
| 464 |
+
efficiency_chart,
|
| 465 |
+
distribution_chart,
|
| 466 |
+
],
|
| 467 |
+
)
|
| 468 |
+
|
| 469 |
gr.Markdown("""
|
| 470 |
+
---
|
| 471 |
+
### About the Models
|
| 472 |
+
|
| 473 |
+
- **GPT-4/GPT-2**: OpenAI's tokenizers using BPE (Byte-Pair Encoding)
|
| 474 |
+
- **LLaMA-2/3**: Meta's models using SentencePiece
|
| 475 |
+
- **Gemma-2**: Google's model with SentencePiece
|
| 476 |
+
- **Qwen3/2.5**: Alibaba's models with BPE
|
| 477 |
+
- **BERT**: Google's BERT with WordPiece
|
| 478 |
+
- **BLOOM**: BigScience's multilingual model with BPE
|
| 479 |
+
- **Aya Expanse**: Cohere's multilingual model with SentencePiece
|
| 480 |
+
- **Comma AI**: Comma AI's model with BPE
|
| 481 |
+
- **Byte-Level**: Byte-level BPE tokenizer
|
| 482 |
+
- **TokenMonster**: Optimized tokenizer with BPE
|
| 483 |
|
| 484 |
+
### Features
|
| 485 |
+
- **Efficiency Ranking**: Compare token counts across models
|
| 486 |
+
- **Subword Analysis**: See how models handle subwords
|
| 487 |
+
- **Token Types**: Classification of word/number/punctuation tokens
|
| 488 |
+
- **Visual Charts**: Interactive plots for comparison
|
| 489 |
+
- **Detailed Analysis**: Common tokens and distribution stats
|
| 490 |
""")
|
| 491 |
|
| 492 |
if __name__ == "__main__":
|
| 493 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
tiktoken
|
| 3 |
transformers
|
| 4 |
-
torch
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
tiktoken
|
| 3 |
transformers
|
| 4 |
+
torch
|
| 5 |
+
pandas
|
| 6 |
+
plotly
|