Skip to main content

tags: [gradio-custom-component, TextBox, textbox] title: gradio_tokenizertextbox short_description: Textbox tokenizer colorFrom: blue colorTo: yellow sdk: gradio pinned: false app_file: space.py

gradio_tokenizertextbox

PyPI - Version

Textbox tokenizer

Installation

pip install gradio_tokenizertextbox

Usage

#
# demo/app.py
#
import gradio as gr
from gradio_tokenizertextbox import TokenizerTextBox 
import json

# --- Data and Helper Functions ---

TOKENIZER_OPTIONS = {
    "Xenova/clip-vit-large-patch14": "CLIP ViT-L/14",
    "Xenova/gpt-4": "gpt-4 / gpt-3.5-turbo / text-embedding-ada-002",
    "Xenova/text-davinci-003": "text-davinci-003 / text-davinci-002",
    "Xenova/gpt-3": "gpt-3",
    "Xenova/grok-1-tokenizer": "Grok-1",
    "Xenova/claude-tokenizer": "Claude",
    "Xenova/mistral-tokenizer-v3": "Mistral v3",
    "Xenova/mistral-tokenizer-v1": "Mistral v1",
    "Xenova/gemma-tokenizer": "Gemma",
    "Xenova/llama-3-tokenizer": "Llama 3",
    "Xenova/llama-tokenizer": "LLaMA / Llama 2",
    "Xenova/c4ai-command-r-v01-tokenizer": "Cohere Command-R",
    "Xenova/t5-small": "T5",
    "Xenova/bert-base-cased": "bert-base-cased",
}

dropdown_choices = [
    (display_name, model_name) 
    for model_name, display_name in TOKENIZER_OPTIONS.items()
]

def process_output(tokenization_data):
    """
    This function receives the full dictionary from the component.
    """
    if not tokenization_data:
        return {"status": "Waiting for input..."}
    return tokenization_data

# --- Gradio Application ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    # --- Header and Information ---
    gr.Markdown("# TokenizerTextBox Component Demo")
    gr.Markdown("Component idea taken from the original example application on [Xenova Tokenizer Playground](https://github.com/huggingface/transformers.js-examples/tree/main/the-tokenizer-playground)")
    
    # --- Global Controls (affect both tabs) ---
    with gr.Row():
        model_selector = gr.Dropdown(
            label="Select a Tokenizer",
            choices=dropdown_choices,
            value="Xenova/clip-vit-large-patch14",
        )
        
        display_mode_radio = gr.Radio(
            ["text", "token_ids", "hidden"],
            label="Display Mode",
            value="text"
        )

    # --- Tabbed Interface for Different Modes ---
    with gr.Tabs():
        # --- Tab 1: Standalone Mode ---
        with gr.TabItem("Standalone Mode"):
            gr.Markdown("### In this mode, the component acts as its own interactive textbox.")
            gr.Markdown("<span>💻 <a href='https://github.com/DEVAIEXP/gradio_component_tokenizertextbox'>GitHub Code</a></span>")
            
            standalone_tokenizer = TokenizerTextBox(
                label="Type your text here",
                value="Gradio is an awesome tool for building ML demos!",
                model="Xenova/clip-vit-large-patch14",                
                display_mode="text",
                preview_tokens=True
            )
            
            standalone_output = gr.JSON(label="Component Output")
            standalone_tokenizer.change(process_output, standalone_tokenizer, standalone_output)

        # --- Tab 2: Listener ("Push") Mode ---
        with gr.TabItem("Listener Mode"):
            gr.Markdown("### In this mode, the component is a read-only visualizer for other text inputs.")
            
            with gr.Row():
                prompt_1 = gr.Textbox(label="Prompt Part 1", value="A photorealistic image of an astronaut")
                prompt_2 = gr.Textbox(label="Prompt Part 2", value="riding a horse on Mars")

            visualizer = TokenizerTextBox(
                label="Concatenated Prompt Visualization",
                hide_input=True, # Hides the internal textbox
                model="Xenova/clip-vit-large-patch14",
                display_mode="text",
            )
            
            visualizer_output = gr.JSON(label="Visualizer Component Output")

            # --- "Push" Logic ---
            def update_visualizer_text(p1, p2):
                concatenated_text = f"{p1}, {p2}"
                # Return a new value for the visualizer.
                # The postprocess method will correctly handle this string.
                return gr.update(value=concatenated_text)

            # Listen for changes on the source textboxes
            prompt_1.change(update_visualizer_text, [prompt_1, prompt_2], visualizer)
            prompt_2.change(update_visualizer_text, [prompt_1, prompt_2], visualizer)

            # Also connect the visualizer to its own JSON output
            visualizer.change(process_output, visualizer, visualizer_output)

            # Run once on load to show the initial state
            demo.load(update_visualizer_text, [prompt_1, prompt_2], visualizer)

    # --- Link Global Controls to Both Components ---
    # Create a list of all TokenizerTextBox components that need to be updated
    all_tokenizers = [standalone_tokenizer, visualizer]

    model_selector.change(
        fn=lambda model: [gr.update(model=model) for _ in all_tokenizers],
        inputs=model_selector,
        outputs=all_tokenizers
    )
    display_mode_radio.change(
        fn=lambda mode: [gr.update(display_mode=mode) for _ in all_tokenizers],
        inputs=display_mode_radio,
        outputs=all_tokenizers
    )

if __name__ == '__main__':
    demo.launch()

TokenizerTextBox

Initialization

name type default description
value
typing.Union[str, dict, typing.Callable, NoneType][
    str, dict, Callable, None
]
None The initial value. Can be a string to initialize the text, or a dictionary for full state. If a function is provided, it will be called when the app loads to set the initial value.
model
str
"Xenova/gpt-3" The name of a Hugging Face tokenizer to use (must be compatible with Transformers.js). Defaults to "Xenova/gpt-2".
display_mode
"text" | "token_ids" | "hidden"
"text" Controls the content of the token visualization panel. Can be 'text' (default), 'token_ids', or 'hidden'.
hide_input
bool
False If True, the component's own textbox is hidden, turning it into a read-only visualizer. Defaults to False.
model_max_length
int | None
None The maximum number of tokens for the model. If the token count exceeds this, the counter will turn red. If not provided, the component will try to detect it from the loaded tokenizer.
preview_tokens
bool
False If True, the component displays the formatted tokens.
lines
int
2 The minimum number of line rows for the textarea.
max_lines
int | None
None The maximum number of line rows for the textarea.
placeholder
str | None
None A placeholder hint to display in the textarea when it is empty.
autofocus
bool
False If True, will focus on the textbox when the page loads.
autoscroll
bool
True If True, will automatically scroll to the bottom of the textbox when the value changes.
text_align
typing.Optional[typing.Literal["left", "right"]][
    "left" | "right", None
]
None How to align the text in the textbox, can be: "left" or "right".
rtl
bool
False If True, sets the direction of the text to right-to-left.
show_copy_button
bool
False If True, a copy button will be shown.
max_length
int | None
None The maximum number of characters allowed in the textbox.
label
str | None
None The label for this component, displayed above the component.
info
str | None
None Additional component description, displayed below the label.
every
float | None
None If `value` is a callable, this sets a timer to run the function repeatedly.
show_label
bool
True If False, the label is not displayed.
container
bool
True If False, the component will not be wrapped in a container.
scale
int | None
None The relative size of the component compared to others in a `gr.Row` or `gr.Column`.
min_width
int
160 The minimum-width of the component in pixels.
interactive
bool | None
None If False, the user will not be able to edit the text.
visible
bool
True If False, the component will be hidden.
elem_id
str | None
None An optional string that is assigned as the id of this component in the HTML DOM.
elem_classes
list[str] | str | None
None An optional list of strings that are assigned as the classes of this component in the HTML DOM.

Events

name description
change Triggered when the value of the TokenizerTextBox changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See .input() for a listener that is only triggered by user input.
input This listener is triggered when the user changes the value of the TokenizerTextBox.
submit This listener is triggered when the user presses the Enter key while the TokenizerTextBox is focused.
blur This listener is triggered when the TokenizerTextBox is unfocused/blurred.
select Event listener for when the user selects or deselects the TokenizerTextBox. Uses event data gradio.SelectData to carry value referring to the label of the TokenizerTextBox, and selected to refer to state of the TokenizerTextBox. See EventData documentation on how to use this event data

User function

The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).

  • When used as an Input, the component only impacts the input signature of the user function.
  • When used as an output, the component only impacts the return signature of the user function.

The code snippet below is accurate in cases where the component is used as both an input and an output.

  • As output: Is passed, a dictionary enriched with 'char_count' and 'token_count'.
  • As input: Should return, the value to set for the component, can be a string or a dictionary.
def predict(
    value: dict | None
) -> str | dict | None:
    return value

Metadata

Release files for gradio-tokenizertextbox 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gradio-tokenizertextbox 0.0.5
File Size Uploaded Provenance Actions
gradio_tokenizertextbox-0.0.5.tar.gz 2.3 MB Without provenance Details for gradio_tokenizertextbox-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gradio-tokenizertextbox 0.0.5
File Interpreter ABI Platform Actions
gradio_tokenizertextbox-0.0.5-py3-none-any.whl Python 3 none any Details for gradio_tokenizertextbox-0.0.5-py3-none-any.whl

Total release size: 4.4 MB

Release files / gradio_tokenizertextbox-0.0.5.tar.gz

Download URL gradio_tokenizertextbox-0.0.5.tar.gz
Size 2.3 MB
Tags Source
SHA-256 checksum
How to use checksums
3e0119e038702d0d24973e300e82bc5f6c11c67596e655a9fc29a80829400c36
BLAKE2b-256 checksum
How to use checksums
dc6222288d42f08e861b3dbde25aeab4471a9ed9a341ca51a15e2824388e41b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Release files / gradio_tokenizertextbox-0.0.5-py3-none-any.whl

Download URL gradio_tokenizertextbox-0.0.5-py3-none-any.whl
Size 2.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
25a96f5cd95eba271a8c4caa7058793f24dc8955a3fdce0c3048e224de8e47d1
BLAKE2b-256 checksum
How to use checksums
0a564b17aecbad9839cf25e051257149b40e086af383b7128ffae5c04ddd120e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.11

Security for gradio-tokenizertextbox 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page