Lyte commited on
Commit
2673358
·
verified ·
1 Parent(s): 7f14ca3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer
3
+ from huggingface_hub import HfApi
4
+ from gradio_huggingfacehub_search import HuggingfaceHubSearch
5
+
6
+ def count_tokens(model_id, text):
7
+ try:
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ tokens = tokenizer.encode(text)
10
+
11
+ token_count = len(tokens)
12
+
13
+ return f"Number of tokens: {token_count}"
14
+ except Exception as e:
15
+ return f"Error: {str(e)}"
16
+
17
+ with gr.Blocks() as iface:
18
+ gr.Markdown("# Universal Tokenizer - Token Counter")
19
+ gr.Markdown("This app counts the number of tokens in the provided text using any tokenizer from a Hugging Face model.")
20
+
21
+ model_id = HuggingfaceHubSearch(
22
+ label="Select a model repo with a tokenizer",
23
+ placeholder="Search for a model on Hugging Face",
24
+ search_type="model",
25
+ )
26
+
27
+ text_input = gr.Textbox(lines=5, placeholder="Enter your text here...")
28
+
29
+ output = gr.Textbox(label="Result")
30
+
31
+ btn = gr.Button("Count Tokens")
32
+ btn.click(fn=count_tokens, inputs=[model_id, text_input], outputs=output)
33
+
34
+ iface.launch()