File size: 7,567 Bytes
751936e
 
 
 
 
d10ecd7
a173fe5
 
 
 
 
 
 
 
 
d10ecd7
751936e
 
 
 
 
 
428b731
 
 
 
751936e
 
 
 
 
 
d10ecd7
751936e
 
d10ecd7
 
751936e
428b731
 
751936e
428b731
 
1ee0570
428b731
d10ecd7
 
428b731
 
751936e
428b731
d10ecd7
 
428b731
751936e
 
d10ecd7
428b731
751936e
428b731
 
 
751936e
428b731
751936e
d10ecd7
 
 
 
 
 
 
 
 
 
 
751936e
 
 
428b731
 
 
751936e
d10ecd7
 
 
 
751936e
 
428b731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10ecd7
 
 
428b731
 
 
d10ecd7
 
428b731
 
 
d10ecd7
 
 
 
 
428b731
d10ecd7
428b731
 
 
 
 
 
 
 
 
 
 
 
 
 
d10ecd7
 
 
428b731
 
 
d10ecd7
 
 
 
 
 
 
428b731
 
 
 
751936e
 
 
 
428b731
751936e
 
 
 
 
428b731
751936e
 
 
 
428b731
 
d10ecd7
 
428b731
 
 
d10ecd7
 
428b731
 
d10ecd7
 
 
 
 
 
428b731
 
 
d10ecd7
 
 
 
 
 
 
 
 
 
 
 
428b731
751936e
d10ecd7
a173fe5
751936e
 
 
d10ecd7
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# coding=utf-8
# author: xusong
# time: 2022/8/23 16:06

"""
## TODO:
- http get方式获取参数,
- 自启动
- iter_vocab 的 warmup
- add_special_token 开关
- theme 开关 light/dark
- token_id/tokens/bytes 开关
- 通过 javascript 添加 hover_text
-



plots

table

## related demo
- [](http://text-processing.com/demo/tokenize/)
- [gpt-tokenizer](https://gpt-tokenizer.dev/)
- [llama-tokenizer-js](https://belladoreai.github.io/llama-tokenizer-js/example-demo/build/)
- [](https://huggingface.co/spaces/Xenova/the-tokenizer-playground)

## 可视化

[ The, 2, QUICK, Brown, Foxes, jumped, over, the, lazy, dog's, bone ]
"""


import gradio as gr

from vocab import all_tokenizers
from util import *

example_text = """Replace this text in the input field to see how tokenization works
华为智能音箱发布:华为Sound X"""

# llama chatglm_6b gpt_nexo_20b baichuan  baichuan_7b
examples = [
    # ["空格测试:  2个空格        8个空格", "llama", "chatglm_6b"],  # chatglm 有blank_n,
    ["标点测试:,。!?;", "baichuan_7b", "llama"],
    ["符号测试:🦙❤❥웃유♋☮✊☏☢☚✔☑♚▢♪✈✞÷↑↓▤▥⊙■□▣▽¿─│♥❣▬▫☿Ⓐ ✋✉☣☤", "baichuan_7b", "llama"],
    ["中文简体:宽带,繁体:樂來", "baichuan_7b", "llama"],
    ["数字测试:(10086 + 98) = 100184", "baichuan_7b", "llama"],
]


def example_fn(example_idx):
    return examples[example_idx]



with gr.Blocks(css="style.css") as demo:
    gr.HTML("""<h1 align="center">Tokenizer Arena ⚔️</h1>""")
    # links: https://www.coderstool.com/utf8-encoding-decoding
    # 功能:输入文本,进行分词
    # 分词器:常见的分词器有集中,
    # 背景:方便分词、看词粒度、对比
    #
    # Byte: 表示分词

    with gr.Row():
        gr.Markdown("## Input Text")
        dropdown_examples = gr.Dropdown(
            ["Example1", "Example2", "Example3"],
            value="Examples",
            type="index",
            show_label=False,
            container=False,
            scale=0,
            elem_classes="example-style"
        )

    user_input = gr.Textbox(
        value=example_text,
        label="Input Text",
        lines=5,
        show_label=False,
    )  # placeholder="Enter sentence here..."
    # gr.Examples(
    #     examples,
    #     None,
    # )


    gr.Markdown("## Tokenization")

    with gr.Row():
        with gr.Column(scale=6):
            with gr.Group():
                tokenizer_type_1 = gr.Dropdown(
                    all_tokenizers,
                    value="llama",
                    label="Tokenizer 1",
                )
                with gr.Group():
                    """
                    <div class="stat"><div class="stat-value">69</div><div class="stat-label">Characters</div></div>
                    """
                    with gr.Row():
                        stats_vocab_size_1 = gr.TextArea(
                            label="VocabSize",
                            lines=1,
                            elem_classes="statistics"
                        )
                        stats_zh_token_size_1 = gr.TextArea(
                            # value="1252/1455",
                            label="ZH char/word",
                            lines=1,
                            elem_classes="statistics"
                        )
                        stats_overlap_token_size_1 = gr.TextArea(
                            label="Overlap Tokens",
                            lines=1,
                            elem_classes="statistics"
                        )
                        # stats_3 = gr.TextArea(
                        #     label="Compress Rate",
                        #     lines=1,
                        #     elem_classes="statistics"
                        # )
        # https://www.onlinewebfonts.com/icon/418591
        gr.Image("images/VS.svg", scale=1, show_label=False, show_download_button=False, container=False)  # height=10,
        with gr.Column(scale=6):
            with gr.Group():
                tokenizer_type_2 = gr.Dropdown(
                    all_tokenizers,
                    value="baichuan_7b",
                    label="Tokenizer 2",
                )
                with gr.Group():
                    with gr.Row():
                        stats_vocab_size_2 = gr.TextArea(
                            label="VocabSize",
                            lines=1,
                            elem_classes="statistics"
                        )
                        stats_zh_token_size_2 = gr.TextArea(  # 中文单子数,
                            # value="12/45",
                            label="ZH char/word",
                            lines=1,
                            elem_classes="statistics"
                        )
                        # stats_6 = gr.TextArea(
                        #     label="Compress Rate",
                        #     lines=1,
                        #     elem_classes="statistics"
                        # )
                        stats_overlap_token_size_2 = gr.TextArea(
                            label="Overlap Tokens",
                            lines=1,
                            elem_classes="statistics"
                        )

    # TODO: 图 表 压缩率
    with gr.Row():
        with gr.Column():
            output_text_1 = gr.Highlightedtext(
                label="Tokens 1",
                show_legend=True,
                elem_classes="space-show"
            )
        with gr.Column():
            output_text_2 = gr.Highlightedtext(
                label="Tokens 2",
                show_legend=True,
                elem_classes="space-show"
            )

    with gr.Row():
        output_table_1 = gr.Dataframe(
            headers=["TokenID", "Byte", "Text"],
            datatype=["str", "str", "str"],
            # elem_classes="space-show",   # 给整个Dataframe加这个css不起作用,因此直接修改cell-wrap
        )
        output_table_2 = gr.Dataframe(
            headers=["TokenID", "Token", "Text"],
            datatype=["str", "str", "str"],
        )

    tokenizer_type_1.change(tokenize, [user_input, tokenizer_type_1],
                            [output_text_1, output_table_1])
    # 下面两个好像可以合并
    tokenizer_type_1.change(basic_count, [tokenizer_type_1], [stats_vocab_size_1, stats_zh_token_size_1])
    tokenizer_type_1.change(get_overlap_token_size, [tokenizer_type_1, tokenizer_type_2],
                            [stats_overlap_token_size_1, stats_overlap_token_size_2])

    user_input.change(tokenize_pair,
                      [user_input, tokenizer_type_1, tokenizer_type_2],
                      [output_text_1, output_table_1, output_text_2, output_table_2])

    tokenizer_type_2.change(tokenize, [user_input, tokenizer_type_2],
                            [output_text_2, output_table_2])
    tokenizer_type_2.change(basic_count, [tokenizer_type_2], [stats_vocab_size_2, stats_zh_token_size_2])
    tokenizer_type_2.change(get_overlap_token_size, [tokenizer_type_1, tokenizer_type_2],
                            [stats_overlap_token_size_1, stats_overlap_token_size_2])

    dropdown_examples.change(
        example_fn,
        dropdown_examples,
        [user_input, tokenizer_type_1, tokenizer_type_2]
    )

    # start up 初始化
    # user_input.update(user_input.value + "___")


if __name__ == "__main__":
    demo.queue(max_size=20).launch()
    # demo.launch()