Spaces:
Running
Running
Suchinthana
commited on
Commit
·
86f4b94
1
Parent(s):
0ef92cd
Added code
Browse files- app.py +117 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from huggingface_hub import login, logout
|
| 5 |
+
from markupsafe import escape
|
| 6 |
+
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
| 7 |
+
from fractions import Fraction
|
| 8 |
+
|
| 9 |
+
def random_light_color():
|
| 10 |
+
"""Generates a random light color with black text."""
|
| 11 |
+
return f"hsl({random.randint(0, 360)}, 100%, 80%)"
|
| 12 |
+
|
| 13 |
+
def utf8_tokens(tokens):
|
| 14 |
+
"""Generates UTF-8 token representations with valid Unicode for each token."""
|
| 15 |
+
utf8_representation = []
|
| 16 |
+
for token in tokens:
|
| 17 |
+
try:
|
| 18 |
+
utf8_bytes = token.encode('utf-8')
|
| 19 |
+
utf8_hex = " ".join([f"<0x{byte:02X}>" for byte in utf8_bytes])
|
| 20 |
+
unicode_token = utf8_bytes.decode('utf-8')
|
| 21 |
+
utf8_representation.append(
|
| 22 |
+
f'<span style="background-color:{random_light_color()}; color: black;">{escape(unicode_token)} {utf8_hex}</span>'
|
| 23 |
+
)
|
| 24 |
+
except UnicodeDecodeError:
|
| 25 |
+
utf8_representation.append(
|
| 26 |
+
f'<span style="background-color:{random_light_color()}; color: brown;">{escape(token)} {utf8_hex}</span>'
|
| 27 |
+
)
|
| 28 |
+
return " ".join(utf8_representation)
|
| 29 |
+
|
| 30 |
+
def tokenize_text(tokenizer_name_1, tokenizer_name_2, text, hf_token=None):
|
| 31 |
+
def tokenize_with_model(tokenizer_name):
|
| 32 |
+
try:
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name, use_auth_token=hf_token)
|
| 34 |
+
tokens = tokenizer.tokenize(text)
|
| 35 |
+
word_count = len(text.split())
|
| 36 |
+
token_count = len(tokens)
|
| 37 |
+
ratio_simplified = f"{Fraction(word_count, token_count).numerator}/{Fraction(word_count, token_count).denominator}" if token_count > 0 else "N/A"
|
| 38 |
+
colored_tokens = [
|
| 39 |
+
f'<span style="background-color:{random_light_color()}; color: black;">{escape(token)}</span>' for token in tokens
|
| 40 |
+
]
|
| 41 |
+
tokenized_text = " ".join(colored_tokens)
|
| 42 |
+
utf8_representation = utf8_tokens(tokens)
|
| 43 |
+
return tokenized_text, token_count, word_count, ratio_simplified, utf8_representation
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error loading tokenizer {tokenizer_name}: {str(e)}", 0, 0, "N/A", ""
|
| 46 |
+
|
| 47 |
+
if hf_token:
|
| 48 |
+
login(hf_token)
|
| 49 |
+
|
| 50 |
+
tokenizer_1_output = tokenize_with_model(tokenizer_name_1)
|
| 51 |
+
tokenizer_2_output = tokenize_with_model(tokenizer_name_2)
|
| 52 |
+
|
| 53 |
+
if hf_token:
|
| 54 |
+
logout()
|
| 55 |
+
|
| 56 |
+
return (
|
| 57 |
+
f"<p><strong>Tokenizer 1:</strong><br>{tokenizer_1_output[0]}</p>",
|
| 58 |
+
f"Tokenizer 1 - Total tokens: {tokenizer_1_output[1]}, Total words: {tokenizer_1_output[2]}, Word/Token ratio: {tokenizer_1_output[3]}",
|
| 59 |
+
f"<p>{tokenizer_1_output[4]}</p>",
|
| 60 |
+
f"<p><strong>Tokenizer 2:</strong><br>{tokenizer_2_output[0]}</p>",
|
| 61 |
+
f"Tokenizer 2 - Total tokens: {tokenizer_2_output[1]}, Total words: {tokenizer_2_output[2]}, Word/Token ratio: {tokenizer_2_output[3]}",
|
| 62 |
+
f"<p>{tokenizer_2_output[4]}</p>"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def fill_example_text(example_text):
|
| 66 |
+
"""Fills the textbox with the selected example."""
|
| 67 |
+
return example_text
|
| 68 |
+
|
| 69 |
+
examples = {
|
| 70 |
+
"Example 1 (en)": "Hugging Face's tokenizers are really cool!",
|
| 71 |
+
"Example 2 (en)": "Gradio makes building UIs so easy and intuitive.",
|
| 72 |
+
"Example 3 (en)": "Machine learning models often require extensive training data.",
|
| 73 |
+
"Example 4 (ta)": "விரைவு பழுப்பு நரி சோம்பேறி நாய் மீது குதிக்கிறது",
|
| 74 |
+
"Example 5 (si)": "ඉක්මන් දුඹුරු නරියා කම්මැලි බල්ලා උඩින් පනියි"
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
with gr.Blocks() as demo:
|
| 78 |
+
tokenizer_search_1 = HuggingfaceHubSearch(
|
| 79 |
+
label="Search Huggingface Hub for Tokenizer 1",
|
| 80 |
+
placeholder="Search for Tokenizer 1",
|
| 81 |
+
search_type="model"
|
| 82 |
+
)
|
| 83 |
+
tokenizer_search_2 = HuggingfaceHubSearch(
|
| 84 |
+
label="Search Huggingface Hub for Tokenizer 2",
|
| 85 |
+
placeholder="Search for Tokenizer 2",
|
| 86 |
+
search_type="model"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
example_dropdown = gr.Dropdown(label="Select Example", choices=list(examples.keys()), value="Example 1")
|
| 90 |
+
input_text = gr.Textbox(label="Input Text", lines=5)
|
| 91 |
+
|
| 92 |
+
with gr.Accordion("Hugging Face Token (Optional)", open=False):
|
| 93 |
+
hf_token = gr.Textbox(label="Hugging Face Token", placeholder="Enter HF token if needed for private tokenizers")
|
| 94 |
+
|
| 95 |
+
tokenized_output_1 = gr.HTML(label="Tokenizer 1 - Tokenized Text")
|
| 96 |
+
token_count_label_1 = gr.Label(label="Tokenizer 1 - Token Count and Word Count")
|
| 97 |
+
utf8_output_1 = gr.HTML(label="Tokenizer 1 - UTF-8 Decoded Text")
|
| 98 |
+
|
| 99 |
+
tokenized_output_2 = gr.HTML(label="Tokenizer 2 - Tokenized Text")
|
| 100 |
+
token_count_label_2 = gr.Label(label="Tokenizer 2 - Token Count and Word Count")
|
| 101 |
+
utf8_output_2 = gr.HTML(label="Tokenizer 2 - UTF-8 Decoded Text")
|
| 102 |
+
|
| 103 |
+
example_dropdown.change(fn=lambda x: fill_example_text(examples[x]), inputs=example_dropdown, outputs=input_text)
|
| 104 |
+
|
| 105 |
+
input_text.change(tokenize_text,
|
| 106 |
+
inputs=[tokenizer_search_1, tokenizer_search_2, input_text, hf_token],
|
| 107 |
+
outputs=[tokenized_output_1, token_count_label_1, utf8_output_1, tokenized_output_2, token_count_label_2, utf8_output_2])
|
| 108 |
+
|
| 109 |
+
tokenizer_search_1.change(tokenize_text,
|
| 110 |
+
inputs=[tokenizer_search_1, tokenizer_search_2, input_text, hf_token],
|
| 111 |
+
outputs=[tokenized_output_1, token_count_label_1, utf8_output_1, tokenized_output_2, token_count_label_2, utf8_output_2])
|
| 112 |
+
|
| 113 |
+
tokenizer_search_2.change(tokenize_text,
|
| 114 |
+
inputs=[tokenizer_search_1, tokenizer_search_2, input_text, hf_token],
|
| 115 |
+
outputs=[tokenized_output_1, token_count_label_1, utf8_output_1, tokenized_output_2, token_count_label_2, utf8_output_2])
|
| 116 |
+
|
| 117 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
sentencepiece
|
| 3 |
+
huggingface_hub
|
| 4 |
+
transformers
|
| 5 |
+
gradio_huggingfacehub_search
|
| 6 |
+
markupsafe
|