File size: 1,108 Bytes
2673358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gradio as gr
from transformers import AutoTokenizer
from huggingface_hub import HfApi
from gradio_huggingfacehub_search import HuggingfaceHubSearch

def count_tokens(model_id, text):
    try:
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        tokens = tokenizer.encode(text)
    
        token_count = len(tokens)
    
        return f"Number of tokens: {token_count}"
    except Exception as e:
        return f"Error: {str(e)}"

with gr.Blocks() as iface:
    gr.Markdown("# Universal Tokenizer - Token Counter")
    gr.Markdown("This app counts the number of tokens in the provided text using any tokenizer from a Hugging Face model.")
    
    model_id = HuggingfaceHubSearch(
        label="Select a model repo with a tokenizer",
        placeholder="Search for a model on Hugging Face",
        search_type="model",
    )

    text_input = gr.Textbox(lines=5, placeholder="Enter your text here...")
    
    output = gr.Textbox(label="Result")
    
    btn = gr.Button("Count Tokens")
    btn.click(fn=count_tokens, inputs=[model_id, text_input], outputs=output)

iface.launch()