File size: 10,089 Bytes
7c73423
 
 
 
 
 
 
a4208a2
 
 
 
7c73423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8167cc2
7c73423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8167cc2
7c73423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8167cc2
7c73423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# coding=utf-8
# author: xusong
# time: 2022/8/23 16:06

import gradio as gr
from vocab import tokenizer_factory
from playground_examples import example_types, example_fn
from playground_util import (tokenize,
                             tokenize_pair, basic_count,
                             get_overlap_token_size, on_load,
                             default_user_input, default_tokenizer_name_1, default_tokenizer_name_2)

get_window_url_params = """

    function(url_params) {

        const params = new URLSearchParams(window.location.search);

        url_params = JSON.stringify(Object.fromEntries(params));

        return url_params;

        }

    """

all_tokenizer_name = [(config.name_display, config.name_or_path) for config in tokenizer_factory.all_tokenizer_configs]

with gr.Blocks() as demo:
    # links: https://www.coderstool.com/utf8-encoding-decoding
    # 功能:输入文本,进行分词
    # 分词器:常见的分词器有集中,
    # 背景:方便分词、看词粒度、对比

    with gr.Row():
        gr.Markdown("## Input Text")
        dropdown_examples = gr.Dropdown(
            example_types,
            value="Examples",
            type="index",
            allow_custom_value=True,
            show_label=False,
            container=False,
            scale=0,
            elem_classes="example-style"
        )
    user_input = gr.Textbox(
        # value=default_user_input,
        label="Input Text",
        lines=5,
        show_label=False,
    )
    gr.Markdown("## Tokenization")

    # compress rate setting TODO: 将 这个模块调整到下面
    # with gr.Accordion("Compress Rate Setting", open=True):
    #     gr.Markdown(
    #         "Please select corpus and unit of compress rate, get more details at [github](https://github.com/xu-song/tokenizer-arena/). ")
    #     with gr.Row():
    #         compress_rate_corpus = gr.CheckboxGroup(
    #             common_corpuses,  # , "code"
    #             value=["cc100-en", "cc100-zh-Hans"],
    #             label="corpus",
    #             # info=""
    #         )
    #         compress_rate_unit = gr.Radio(
    #             common_units,
    #             value="b_tokens/g_bytes",
    #             label="unit",
    #         )
    # TODO: Token Setting
    # with gr.Accordion("Token Filter Setting", open=False):
    #     gr.Markdown(
    #         "Get total number of tokens which contain the following character)")
    #     gr.Radio(
    #         ["zh-Hans", "", "number", "space"],
    #         value="zh",
    #     )

    with gr.Row():
        with gr.Column(scale=6):
            with gr.Group():
                tokenizer_name_1 = gr.Dropdown(
                    all_tokenizer_name,
                    label="Tokenizer 1",
                    # value=default_tokenizer_name_1,
                )
                with gr.Group():
                    with gr.Row():
                        organization_1 = gr.TextArea(
                            label="Organization",
                            lines=1,
                            elem_classes="statistics",
                        )
                        stats_vocab_size_1 = gr.TextArea(
                            label="Vocab Size",
                            lines=1,
                            elem_classes="statistics"
                        )
                        # stats_zh_token_size_1 = gr.TextArea(
                        #     label="ZH char/word",
                        #     lines=1,
                        #     elem_classes="statistics",
                        # )
                        # stats_compress_rate_1 = gr.TextArea(
                        #     label="Compress Rate",
                        #     lines=1,
                        #     elem_classes="statistics",
                        # )
                        stats_overlap_token_size_1 = gr.TextArea(
                            # value=default_stats_overlap_token_size,
                            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,
                 show_share_button=False)
        with gr.Column(scale=6):
            with gr.Group():
                tokenizer_name_2 = gr.Dropdown(
                    all_tokenizer_name,
                    label="Tokenizer 2",
                    # value=default_tokenizer_name_2
                )
                with gr.Group():
                    with gr.Row():
                        organization_2 = gr.TextArea(
                            label="Organization",
                            lines=1,
                            elem_classes="statistics",
                        )
                        stats_vocab_size_2 = gr.TextArea(
                            label="Vocab Size",
                            lines=1,
                            elem_classes="statistics"
                        )
                        # stats_zh_token_size_2 = gr.TextArea(
                        #     label="ZH char/word",  # 中文字/词
                        #     lines=1,
                        #     elem_classes="statistics",
                        # )
                        # stats_compress_rate_2 = gr.TextArea(
                        #     label="Compress Rate",
                        #     lines=1,
                        #     elem_classes="statistics"
                        # )
                        stats_filtered_token_2 = gr.TextArea(
                            label="filtered tokens",
                            lines=1,
                            elem_classes="statistics",
                            visible=False
                        )
                        stats_overlap_token_size_2 = gr.TextArea(
                            label="Overlap Tokens",
                            lines=1,
                            elem_classes="statistics"
                        )

    # TODO: 图 表 压缩率
    with gr.Row():
        # dynamic change label
        with gr.Column():
            output_text_1 = gr.Highlightedtext(
                show_legend=False,
                show_inline_category=False,
                elem_classes="space-show"
            )
        with gr.Column():
            output_text_2 = gr.Highlightedtext(
                show_legend=False,
                show_inline_category=False,
                elem_classes="space-show"
            )

    with gr.Row():
        output_table_1 = gr.Dataframe()
        output_table_2 = gr.Dataframe()

    # setting
    # compress_rate_unit.change(compress_rate_unit_change, [compress_rate_unit],
    #                             [stats_compress_rate_1, stats_compress_rate_2])

    tokenizer_name_1.change(tokenize, [user_input, tokenizer_name_1],
                            [output_text_1, output_table_1])
    tokenizer_name_1.change(basic_count, [tokenizer_name_1], [stats_vocab_size_1, organization_1])
    tokenizer_name_1.change(get_overlap_token_size, [tokenizer_name_1, tokenizer_name_2],
                            [stats_overlap_token_size_1, stats_overlap_token_size_2])
    # tokenizer_type_1.change(get_compress_rate, [tokenizer_type_1, compress_rate_corpus, compress_rate_unit],
    #                         [stats_compress_rate_1])

    # TODO: every=3
    user_input.change(tokenize_pair,
                      [user_input, tokenizer_name_1, tokenizer_name_2],
                      [output_text_1, output_table_1, output_text_2, output_table_2])  # , pass_request=1

    tokenizer_name_2.change(tokenize, [user_input, tokenizer_name_2],
                            [output_text_2, output_table_2])
    tokenizer_name_2.change(basic_count, [tokenizer_name_2], [stats_vocab_size_2, organization_2])
    tokenizer_name_2.change(get_overlap_token_size, [tokenizer_name_1, tokenizer_name_2],
                            [stats_overlap_token_size_1, stats_overlap_token_size_2])
    # tokenizer_type_2.change(get_compress_rate,
    #                         [tokenizer_type_2, compress_rate_corpus, compress_rate_unit],
    #                         [stats_compress_rate_2])
    #
    # compress_rate_unit.change(get_compress_rate,
    #                           [tokenizer_type_1, compress_rate_corpus, compress_rate_unit],
    #                           [stats_compress_rate_1])
    # compress_rate_unit.change(get_compress_rate,
    #                           [tokenizer_type_2, compress_rate_corpus, compress_rate_unit],
    #                           [stats_compress_rate_2])
    # compress_rate_corpus.change(get_compress_rate,
    #                             [tokenizer_type_1, compress_rate_corpus, compress_rate_unit],
    #                             [stats_compress_rate_1])
    # compress_rate_corpus.change(get_compress_rate,
    #                             [tokenizer_type_2, compress_rate_corpus, compress_rate_unit],
    #                             [stats_compress_rate_2])

    dropdown_examples.change(
        example_fn,
        dropdown_examples,
        [user_input, tokenizer_name_1, tokenizer_name_2]
    )

    demo.load(
        fn=on_load,
        inputs=[user_input],  # 这里只需要传个空object即可。
        outputs=[user_input, tokenizer_name_1, tokenizer_name_2],
        js=get_window_url_params
    )

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