File size: 4,179 Bytes
dd452b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import gradio as gr
from html import escape
import spaces

from tokenizer import tokenize


CONTEXT_WINDOW = 4096


def create_token_visualization(token_data):
    html = """
    <div style="
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        width: 100%;
        box-sizing: border-box;
    ">
    """

    for item in token_data:
        token = item["token"]
        token_id = item["token_id"]

        # Preserve spaces when displaying tokens in HTML
        display_token = escape(token).replace(" ", "&nbsp;")

        html += f"""
        <div style="
            border: 1px solid #555;
            border-radius: 8px;
            padding: 12px 16px;
            min-width: 100px;
            text-align: center;
            box-sizing: border-box;
        ">
            <div style="
                font-size: 16px;
                font-weight: bold;
                margin-bottom: 8px;
            ">
                {display_token}
            </div>

            <div style="
                font-size: 13px;
                opacity: 0.8;
            ">
                ID: {token_id}
            </div>
        </div>
        """

    html += "</div>"

    return html


def create_context_visualization(token_count):
    remaining = CONTEXT_WINDOW - token_count
    # Context window is a fixed limit for each inference request.
    # The tokens included in that request are checked against this limit.

    usage_percentage = (token_count / CONTEXT_WINDOW) * 100

    if remaining < 0:
        status = "⚠️ Context window exceeded"
    else:
        status = "✅ Within context window"

    return f"""
    <div style="
        padding: 15px;
        border: 1px solid #555;
        border-radius: 10px;
    ">
        <h3>Context Window</h3>

        <p>
            <strong>Limit:</strong> {CONTEXT_WINDOW} tokens
        </p>

        <p>
            <strong>Used:</strong> {token_count} tokens
        </p>

        <p>
            <strong>Remaining:</strong> {max(remaining, 0)} tokens
        </p>

        <p>
            <strong>Usage:</strong> {usage_percentage:.2f}%
        </p>

        <p>{status}</p>
    </div>
    """

@spaces.GPU
def process_text(text):

    # Treat empty or whitespace-only input as empty
    if not text.strip():
        return (
            "",
            0,
            """
            <div style="padding: 15px;">
                <h3>Context Window</h3>
                <p>No text entered.</p>
            </div>
            """
        )

    token_data = []

    token_ids, tokens, token_count = tokenize(text)

    for token_id, token in zip(token_ids, tokens):
        token_data.append({
            "token": token,
            "token_id": token_id
        })

    token_visualization = create_token_visualization(token_data)

    context_visualization = create_context_visualization(token_count)

    return (
        token_visualization,
        token_count,
        context_visualization
    )


with gr.Blocks(title="Tokenizer Visualizer") as app:

    gr.Markdown(
        """
        # Tokenizer Visualizer

        Understand how text is converted into tokens and token IDs.
        """
    )

    with gr.Row():

        # LEFT SIDE — INPUT
        with gr.Column():

            text_input = gr.Textbox(
                label="Enter text",
                placeholder="Type some text here...",
                lines=5
            )

            tokenize_button = gr.Button(
                "Tokenize",
                variant="primary"
            )

        # RIGHT SIDE — OUTPUT
        with gr.Column():

            token_visualization = gr.HTML(
                label="Token Visualization"
            )

            token_count = gr.Number(
                label="Token Count",
                value=0,
                interactive=False
            )

            context_visualization = gr.HTML(
                label="Context Window"
            )

    tokenize_button.click(
        fn=process_text,
        inputs=text_input,
        outputs=[
            token_visualization,
            token_count,
            context_visualization
        ]
    )


app.launch()