File size: 1,004 Bytes
b19f17e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from anthropic import Anthropic
import tiktoken

anthropic = Anthropic()
gpt_35_encoder = tiktoken.get_encoding("cl100k_base")

def count_anthropic_tokens(text: str) -> int:
    return anthropic.count_tokens(text)

def count_gpt_tokens(text: str) -> int:
    return len(gpt_35_encoder.encode(text))

def tokenize(text: str) -> tuple:
    anthropic_count = count_anthropic_tokens(text)
    gpt_tokens_count = count_gpt_tokens(text)
    char_count = len(text)

    return anthropic_count, gpt_tokens_count, char_count

counter = gr.Interface(
    title="Token and Character Counter",
    fn=tokenize,
    inputs=gr.Textbox(lines=10, placeholder="Input Text"),
    outputs=[
        gr.Number(label="Anthropic API (Claude) Token Count", interactive=False),
        gr.Number(label="OpenAI API (GPT-3.5 / GPT-4) Token Count", interactive=False),
        gr.Number(label="Character Count", interactive=False)
    ],
    live=True,
    allow_flagging="never",
)

counter.launch(debug=True)