Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tiktoken
|
3 |
+
|
4 |
+
encodings = tiktoken.list_encoding_names()
|
5 |
+
encodings.reverse()
|
6 |
+
|
7 |
+
def function(input, encoding):
|
8 |
+
tokens = tiktoken.get_encoding(encoding).encode(input)
|
9 |
+
return len(tokens)
|
10 |
+
|
11 |
+
value1 = gr.Textbox(lines=6, label="Input", placeholder="Enter text here...")
|
12 |
+
value2 = gr.Dropdown(
|
13 |
+
label="Encoding",
|
14 |
+
choices=encodings,
|
15 |
+
value="cl100k_base",
|
16 |
+
info="The encoding to use. (GPT-3.5 and GPT-4 use cl100k_base as their encoding.)"
|
17 |
+
)
|
18 |
+
value3 = gr.Number(label="Output") # Don't include lines and placeholder
|
19 |
+
|
20 |
+
examples = [
|
21 |
+
["The only way to do great work is to love what you do. - Steve Jobs", "cl100k_base"],
|
22 |
+
["In the end, we will remember not the words of our enemies, but the silence of our friends. - Martin Luther King Jr.", "cl100k_base"],
|
23 |
+
["Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill", "cl100k_base"],
|
24 |
+
["The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela", "cl100k_base"],
|
25 |
+
["The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Helen Keller", "cl100k_base"]
|
26 |
+
]
|
27 |
+
|
28 |
+
demo = gr.Interface(
|
29 |
+
fn=function,
|
30 |
+
inputs=[value1, value2],
|
31 |
+
outputs=value3,
|
32 |
+
title="ChatGPT Token Calculator",
|
33 |
+
examples=examples,
|
34 |
+
description="Calculate the number of tokens in a text string."
|
35 |
+
)
|
36 |
+
|
37 |
+
demo.launch(debug=True)
|