update git repo link
Browse files
app.py
CHANGED
|
@@ -1,126 +1,127 @@
|
|
| 1 |
-
#
|
| 2 |
-
# demo/app.py
|
| 3 |
-
#
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from gradio_tokenizertextbox import TokenizerTextBox
|
| 6 |
-
import json
|
| 7 |
-
|
| 8 |
-
# --- Data and Helper Functions ---
|
| 9 |
-
|
| 10 |
-
TOKENIZER_OPTIONS = {
|
| 11 |
-
"Xenova/clip-vit-large-patch14": "CLIP ViT-L/14",
|
| 12 |
-
"Xenova/gpt-4": "gpt-4 / gpt-3.5-turbo / text-embedding-ada-002",
|
| 13 |
-
"Xenova/text-davinci-003": "text-davinci-003 / text-davinci-002",
|
| 14 |
-
"Xenova/gpt-3": "gpt-3",
|
| 15 |
-
"Xenova/grok-1-tokenizer": "Grok-1",
|
| 16 |
-
"Xenova/claude-tokenizer": "Claude",
|
| 17 |
-
"Xenova/mistral-tokenizer-v3": "Mistral v3",
|
| 18 |
-
"Xenova/mistral-tokenizer-v1": "Mistral v1",
|
| 19 |
-
"Xenova/gemma-tokenizer": "Gemma",
|
| 20 |
-
"Xenova/llama-3-tokenizer": "Llama 3",
|
| 21 |
-
"Xenova/llama-tokenizer": "LLaMA / Llama 2",
|
| 22 |
-
"Xenova/c4ai-command-r-v01-tokenizer": "Cohere Command-R",
|
| 23 |
-
"Xenova/t5-small": "T5",
|
| 24 |
-
"Xenova/bert-base-cased": "bert-base-cased",
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
dropdown_choices = [
|
| 28 |
-
(display_name, model_name)
|
| 29 |
-
for model_name, display_name in TOKENIZER_OPTIONS.items()
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
def process_output(tokenization_data):
|
| 33 |
-
"""
|
| 34 |
-
This function receives the full dictionary from the component.
|
| 35 |
-
"""
|
| 36 |
-
if not tokenization_data:
|
| 37 |
-
return {"status": "Waiting for input..."}
|
| 38 |
-
return tokenization_data
|
| 39 |
-
|
| 40 |
-
# --- Gradio Application ---
|
| 41 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
-
# --- Header and Information ---
|
| 43 |
-
gr.Markdown("# TokenizerTextBox Component Demo")
|
| 44 |
-
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)")
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
demo.launch()
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# demo/app.py
|
| 3 |
+
#
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from gradio_tokenizertextbox import TokenizerTextBox
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# --- Data and Helper Functions ---
|
| 9 |
+
|
| 10 |
+
TOKENIZER_OPTIONS = {
|
| 11 |
+
"Xenova/clip-vit-large-patch14": "CLIP ViT-L/14",
|
| 12 |
+
"Xenova/gpt-4": "gpt-4 / gpt-3.5-turbo / text-embedding-ada-002",
|
| 13 |
+
"Xenova/text-davinci-003": "text-davinci-003 / text-davinci-002",
|
| 14 |
+
"Xenova/gpt-3": "gpt-3",
|
| 15 |
+
"Xenova/grok-1-tokenizer": "Grok-1",
|
| 16 |
+
"Xenova/claude-tokenizer": "Claude",
|
| 17 |
+
"Xenova/mistral-tokenizer-v3": "Mistral v3",
|
| 18 |
+
"Xenova/mistral-tokenizer-v1": "Mistral v1",
|
| 19 |
+
"Xenova/gemma-tokenizer": "Gemma",
|
| 20 |
+
"Xenova/llama-3-tokenizer": "Llama 3",
|
| 21 |
+
"Xenova/llama-tokenizer": "LLaMA / Llama 2",
|
| 22 |
+
"Xenova/c4ai-command-r-v01-tokenizer": "Cohere Command-R",
|
| 23 |
+
"Xenova/t5-small": "T5",
|
| 24 |
+
"Xenova/bert-base-cased": "bert-base-cased",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
dropdown_choices = [
|
| 28 |
+
(display_name, model_name)
|
| 29 |
+
for model_name, display_name in TOKENIZER_OPTIONS.items()
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
def process_output(tokenization_data):
|
| 33 |
+
"""
|
| 34 |
+
This function receives the full dictionary from the component.
|
| 35 |
+
"""
|
| 36 |
+
if not tokenization_data:
|
| 37 |
+
return {"status": "Waiting for input..."}
|
| 38 |
+
return tokenization_data
|
| 39 |
+
|
| 40 |
+
# --- Gradio Application ---
|
| 41 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
+
# --- Header and Information ---
|
| 43 |
+
gr.Markdown("# TokenizerTextBox Component Demo")
|
| 44 |
+
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)")
|
| 45 |
+
gr.Markdown("<span>💻 <a href='https://github.com/DEVAIEXP/gradio_component_tokenizertextbox'>Component GitHub Code</a></span>")
|
| 46 |
+
|
| 47 |
+
# --- Global Controls (affect both tabs) ---
|
| 48 |
+
with gr.Row():
|
| 49 |
+
model_selector = gr.Dropdown(
|
| 50 |
+
label="Select a Tokenizer",
|
| 51 |
+
choices=dropdown_choices,
|
| 52 |
+
value="Xenova/clip-vit-large-patch14",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
display_mode_radio = gr.Radio(
|
| 56 |
+
["text", "token_ids", "hidden"],
|
| 57 |
+
label="Display Mode",
|
| 58 |
+
value="text"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# --- Tabbed Interface for Different Modes ---
|
| 62 |
+
with gr.Tabs():
|
| 63 |
+
# --- Tab 1: Standalone Mode ---
|
| 64 |
+
with gr.TabItem("Standalone Mode"):
|
| 65 |
+
gr.Markdown("### In this mode, the component acts as its own interactive textbox.")
|
| 66 |
+
|
| 67 |
+
standalone_tokenizer = TokenizerTextBox(
|
| 68 |
+
label="Type your text here",
|
| 69 |
+
value="Gradio is an awesome tool for building ML demos!",
|
| 70 |
+
model="Xenova/clip-vit-large-patch14",
|
| 71 |
+
display_mode="text",
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
standalone_output = gr.JSON(label="Component Output")
|
| 75 |
+
standalone_tokenizer.change(process_output, standalone_tokenizer, standalone_output)
|
| 76 |
+
|
| 77 |
+
# --- Tab 2: Listener ("Push") Mode ---
|
| 78 |
+
with gr.TabItem("Listener Mode"):
|
| 79 |
+
gr.Markdown("### In this mode, the component is a read-only visualizer for other text inputs.")
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
prompt_1 = gr.Textbox(label="Prompt Part 1", value="A photorealistic image of an astronaut")
|
| 83 |
+
prompt_2 = gr.Textbox(label="Prompt Part 2", value="riding a horse on Mars")
|
| 84 |
+
|
| 85 |
+
visualizer = TokenizerTextBox(
|
| 86 |
+
label="Concatenated Prompt Visualization",
|
| 87 |
+
hide_input=True, # Hides the internal textbox
|
| 88 |
+
model="Xenova/clip-vit-large-patch14",
|
| 89 |
+
display_mode="text",
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
visualizer_output = gr.JSON(label="Visualizer Component Output")
|
| 93 |
+
|
| 94 |
+
# --- "Push" Logic ---
|
| 95 |
+
def update_visualizer_text(p1, p2):
|
| 96 |
+
concatenated_text = f"{p1}, {p2}"
|
| 97 |
+
# Return a new value for the visualizer.
|
| 98 |
+
# The postprocess method will correctly handle this string.
|
| 99 |
+
return gr.update(value=concatenated_text)
|
| 100 |
+
|
| 101 |
+
# Listen for changes on the source textboxes
|
| 102 |
+
prompt_1.change(update_visualizer_text, [prompt_1, prompt_2], visualizer)
|
| 103 |
+
prompt_2.change(update_visualizer_text, [prompt_1, prompt_2], visualizer)
|
| 104 |
+
|
| 105 |
+
# Also connect the visualizer to its own JSON output
|
| 106 |
+
visualizer.change(process_output, visualizer, visualizer_output)
|
| 107 |
+
|
| 108 |
+
# Run once on load to show the initial state
|
| 109 |
+
demo.load(update_visualizer_text, [prompt_1, prompt_2], visualizer)
|
| 110 |
+
|
| 111 |
+
# --- Link Global Controls to Both Components ---
|
| 112 |
+
# Create a list of all TokenizerTextBox components that need to be updated
|
| 113 |
+
all_tokenizers = [standalone_tokenizer, visualizer]
|
| 114 |
+
|
| 115 |
+
model_selector.change(
|
| 116 |
+
fn=lambda model: [gr.update(model=model) for _ in all_tokenizers],
|
| 117 |
+
inputs=model_selector,
|
| 118 |
+
outputs=all_tokenizers
|
| 119 |
+
)
|
| 120 |
+
display_mode_radio.change(
|
| 121 |
+
fn=lambda mode: [gr.update(display_mode=mode) for _ in all_tokenizers],
|
| 122 |
+
inputs=display_mode_radio,
|
| 123 |
+
outputs=all_tokenizers
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
if __name__ == '__main__':
|
| 127 |
demo.launch()
|