Spaces:
Runtime error
Runtime error
yuta_hayashi
commited on
Commit
·
b19f17e
1
Parent(s):
253d3cb
first commit
Browse files- app.py +34 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from anthropic import Anthropic
|
| 3 |
+
import tiktoken
|
| 4 |
+
|
| 5 |
+
anthropic = Anthropic()
|
| 6 |
+
gpt_35_encoder = tiktoken.get_encoding("cl100k_base")
|
| 7 |
+
|
| 8 |
+
def count_anthropic_tokens(text: str) -> int:
|
| 9 |
+
return anthropic.count_tokens(text)
|
| 10 |
+
|
| 11 |
+
def count_gpt_tokens(text: str) -> int:
|
| 12 |
+
return len(gpt_35_encoder.encode(text))
|
| 13 |
+
|
| 14 |
+
def tokenize(text: str) -> tuple:
|
| 15 |
+
anthropic_count = count_anthropic_tokens(text)
|
| 16 |
+
gpt_tokens_count = count_gpt_tokens(text)
|
| 17 |
+
char_count = len(text)
|
| 18 |
+
|
| 19 |
+
return anthropic_count, gpt_tokens_count, char_count
|
| 20 |
+
|
| 21 |
+
counter = gr.Interface(
|
| 22 |
+
title="Token and Character Counter",
|
| 23 |
+
fn=tokenize,
|
| 24 |
+
inputs=gr.Textbox(lines=10, placeholder="Input Text"),
|
| 25 |
+
outputs=[
|
| 26 |
+
gr.Number(label="Anthropic API (Claude) Token Count", interactive=False),
|
| 27 |
+
gr.Number(label="OpenAI API (GPT-3.5 / GPT-4) Token Count", interactive=False),
|
| 28 |
+
gr.Number(label="Character Count", interactive=False)
|
| 29 |
+
],
|
| 30 |
+
live=True,
|
| 31 |
+
allow_flagging="never",
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
counter.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
anthropic
|
| 3 |
+
tiktoken
|