Spaces:
Runtime error
Runtime error
import gradio as gr | |
from anthropic import Anthropic | |
import tiktoken | |
anthropic = Anthropic() | |
gpt_35_encoder = tiktoken.get_encoding("cl100k_base") | |
gpt_4o_encoder = tiktoken.get_encoding("o200k_base") | |
def count_anthropic_tokens(text: str) -> int: | |
return anthropic.count_tokens(text) | |
def count_gpt_tokens_35(text: str) -> int: | |
return len(gpt_35_encoder.encode(text)) | |
def count_gpt_tokens_4o(text: str) -> int: | |
return len(gpt_4o_encoder.encode(text)) | |
def tokenize(text: str) -> tuple: | |
anthropic_count = count_anthropic_tokens(text) | |
gpt_tokens_count_35 = count_gpt_tokens_35(text) | |
gpt_tokens_count_4o = count_gpt_tokens_4o(text) | |
char_count = len(text) | |
return anthropic_count, gpt_tokens_count_35, gpt_tokens_count_4o, 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="OpenAI API (GPT-4o) Token Count", interactive=False), | |
gr.Number(label="Character Count", interactive=False) | |
], | |
live=True, | |
allow_flagging="never", | |
) | |
counter.launch(debug=True) | |