File size: 2,366 Bytes
68a7b59
 
dfd4a9f
c254aed
 
 
68a7b59
dfd4a9f
68a7b59
dfd4a9f
 
 
 
68a7b59
 
 
 
 
 
 
dfd4a9f
5451ef9
c254aed
dfd4a9f
 
 
 
 
 
68a7b59
dfd4a9f
68a7b59
dfd4a9f
 
 
68a7b59
 
dfd4a9f
 
 
68a7b59
 
 
 
 
 
 
 
dfd4a9f
68a7b59
 
 
dfd4a9f
 
68a7b59
 
 
dfd4a9f
68a7b59
 
dfd4a9f
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from transformers import AutoTokenizer
from huggingface_hub import HfApi, login

api = HfApi()

# Define a function to calculate tokens
def count_tokens(llm_name, input_text, api_token):
    try:
        # Login using the API token if provided
        if api_token:
            login(api_token)
        
        # Load the tokenizer for the selected transformer-based model
        tokenizer = AutoTokenizer.from_pretrained(llm_name)
        tokens = tokenizer.encode(input_text)
        return f"Number of tokens: {len(tokens)}"
    except Exception as e:
        return f"Error: {str(e)}"

# Fetch model details including metadata (like tags)
models = list(api.list_models(task="text-generation"))

# Filter models that have the 'text-generation-inference' tag and 'text-generation' pipeline_tag
filtered_models = []
for model in models:
    model_info = api.model_info(model.modelId)
    if 'text-generation-inference' in model_info.tags and model_info.pipeline_tag == 'text-generation':
        filtered_models.append(model.modelId)

# Define custom CSS for a bluish theme and cursor pointer
custom_css = """
.gr-dropdown {
    cursor: pointer;
}
"""

# Set the default model to the first filtered model, or "gpt2" if there are no filtered models
default_model = filtered_models[0] if filtered_models else "gpt2"

# Create the Gradio interface
with gr.Blocks(css=custom_css) as demo:
    gr.HTML("<h1 style='text-align: center; color: #0078d7;'>Token Counter for Transformer-Based Models</h1>")
    gr.Markdown(
        "This app allows you to count the number of tokens in the input text "
        "using selected transformer-based models from Hugging Face."
    )
    with gr.Row():
        llm_dropdown = gr.Dropdown(choices=filtered_models, label="Select Transformer Model", value=default_model)
    with gr.Row():
        input_text = gr.Textbox(label="Enter your text")
        output = gr.Textbox(label="Token Count", interactive=False)
    with gr.Row():
        api_token_input = gr.Textbox(label="Enter Hugging Face API Token (if needed)", type="password", placeholder="Your API Token", interactive=True)
    with gr.Row():
        submit_btn = gr.Button("Calculate Tokens")

    submit_btn.click(count_tokens, inputs=[llm_dropdown, input_text, api_token_input], outputs=output)

# Launch the app
demo.launch(share=True, debug=True)