Spaces:
Sleeping
Sleeping
File size: 2,852 Bytes
66448b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | """Token counter β count tokens for text across multiple model families.
Uses agentfit's pluggable counter. Falls back to char-based estimates when
exact tokenizers aren't available.
"""
import gradio as gr
from agentfit import count
MODELS = [
"claude-sonnet-4-6",
"claude-haiku-4-5",
"gpt-5",
"gpt-4.1",
"default",
]
def count_tokens(text: str, comparison: str):
"""Count tokens for the given text across selected models."""
if not text.strip():
return "_Enter some text to count tokens._"
models = [m.strip() for m in comparison.split(",") if m.strip()] if comparison else MODELS
rows = ["| Model | Tokens | Chars/token |", "|---|---:|---:|"]
char_count = len(text)
for m in models:
try:
n = count([{"role": "user", "content": text}], model=m)
ratio = f"{char_count / n:.2f}" if n else "β"
rows.append(f"| `{m}` | {n} | {ratio} |")
except Exception as e:
rows.append(f"| `{m}` | β | error: {e} |")
return "\n".join(rows) + f"\n\n**Input:** {char_count} chars Β· {len(text.split())} words"
with gr.Blocks(title="Token Counter β across model families", theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# Token Counter
Paste any text and see how it tokenizes across Claude, GPT, and other model families.
Powered by [`agentfit`](https://pypi.org/project/agentfit-py/) β pure Python, no API calls.
π‘ Useful for: budgeting context windows, comparing tokenizer efficiency for non-English text, sanity-checking your own counter.
"""
)
with gr.Row():
with gr.Column():
txt = gr.Textbox(
value="The quick brown fox jumps over the lazy dog.",
label="Text",
lines=10,
placeholder="Paste text here...",
)
models_in = gr.Textbox(
value=", ".join(MODELS),
label="Models (comma-separated)",
)
btn = gr.Button("Count", variant="primary")
out = gr.Markdown()
btn.click(count_tokens, inputs=[txt, models_in], outputs=out)
gr.Examples(
examples=[
["Hello, world!", "claude-sonnet-4-6, gpt-5, default"],
["δ½ ε₯½δΈη γγγ«γ‘γ― μλ
νμΈμ", "claude-sonnet-4-6, gpt-5, default"],
["πππ€β¨", "claude-sonnet-4-6, gpt-5, default"],
["function add(a, b) {\n return a + b;\n}", "claude-sonnet-4-6, gpt-5"],
],
inputs=[txt, models_in],
)
gr.Markdown(
"""
---
Part of [The Agent Reliability Stack](https://mukundakatta.github.io/agent-stack/) Β· MIT licensed
"""
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)
|