File size: 786 Bytes
4f34e0f
 
 
 
 
 
 
b92686c
4f34e0f
b92686c
4f34e0f
 
 
 
 
 
 
 
 
 
 
 
 
b92686c
e51daad
 
b92686c
c18b36b
4f34e0f
e51daad
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
import gradio as gr
import tiktoken

encodings = tiktoken.list_encoding_names()
encodings.reverse()


def calc(input: str, encoding: str) -> (int, int):
    tokens = tiktoken.get_encoding(encoding).encode(input)
    return len(input), len(tokens)


gr.Interface(
    calc,
    inputs=[
        gr.Text(label="Input", lines=6, placeholder="Enter text here..."),
        gr.Dropdown(
            label="Encoding",
            choices=encodings,
            value="cl100k_base",
            info="The encoding to use. (GPT-3.5 and GPT-4 use cl100k_base as their encoding.)",
        ),
    ],
    outputs=[
        gr.Number(label="Character Count"),
        gr.Number(label="Token Count"),
    ],
    title="Token and Character Calculator Counter",
    allow_flagging="never",
).launch()