File size: 586 Bytes
f6bdf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc4091
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import gradio as gr
import tiktoken

encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
def num_tokens_from_string(string: str) -> int:
    encoding = tiktoken.get_encoding("cl100k_base")
    num_tokens = len(encoding.encode(string))
    return num_tokens

gr.Interface(
    fn=num_tokens_from_string,
    inputs=gr.inputs.Textbox(),
    outputs="text",
    live=True,
    title="GPT-3.5 Token Counter",
    description="Enter text and see the token usage.",
    article="To use the GPT-3.5/GPT-4 token counter, simply enter your text in the input box above.",
).launch(share=True)