jiangjiechen commited on
Commit
ede8cb5
·
1 Parent(s): f7048ce
Files changed (2) hide show
  1. app.py +41 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tiktoken
3
+
4
+ def count_tokens(text):
5
+ """
6
+ Calculate the number of tokens in the input text using tiktoken.
7
+
8
+ Args:
9
+ text (str): The input text to be tokenized.
10
+
11
+ Returns:
12
+ int: The number of tokens in the input text.
13
+ """
14
+ # Choose the encoding based on the model you are targeting.
15
+ # Here, we use 'gpt-3.5-turbo' as an example.
16
+ encoding = tiktoken.encoding_for_model("gpt-4")
17
+
18
+ # Encode the input text to get the list of token IDs
19
+ tokens = encoding.encode(text)
20
+
21
+ # Return the number of tokens
22
+ return len(tokens)
23
+
24
+ # Define the Gradio interface
25
+ iface = gr.Interface(
26
+ fn=count_tokens, # The function to call
27
+ inputs=gr.Textbox(lines=10, placeholder="Enter your text here..."), # Input component
28
+ outputs="number", # Output component
29
+ title="Token Counter with tiktoken",
30
+ description="Enter text below to calculate the number of tokens using the tiktoken library.",
31
+ examples=[
32
+ ["Hello, how are you doing today?"],
33
+ ["Gradio makes it easy to create web apps for machine learning models."],
34
+ ["OpenAI's GPT models are powerful tools for natural language processing tasks."]
35
+ ],
36
+ theme="default"
37
+ )
38
+
39
+ # Launch the app
40
+ if __name__ == "__main__":
41
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ tiktoken
2
+ gradio