Spaces:
Sleeping
Sleeping
Commit
·
20b3af9
1
Parent(s):
edba557
Add application and requirements files
Browse files- app.py +33 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer
|
| 3 |
+
from huggingface_hub import repo_exists
|
| 4 |
+
|
| 5 |
+
def token_viz(model_name, text):
|
| 6 |
+
if not repo_exists(model_name):
|
| 7 |
+
gr.Error(f"{model_name} is not a valid HF repo. Please enter a valid repo.")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name,cache_dir=f"./.cache/hf/{model_name}")
|
| 9 |
+
print(model_name,text)
|
| 10 |
+
tokens = tokenizer.encode(text)
|
| 11 |
+
return [(tokenizer.decode(token).replace("\n",r"\n"), str(token)) for token in tokens]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
MARKDOWN = """
|
| 15 |
+
<h1 style='text-align: center; margin-bottom: 1rem'><div align="center">Token Visualizer ⚔️</div></h1>
|
| 16 |
+
|
| 17 |
+
Enter the Tokenizer you want to use to visualize the tokens.
|
| 18 |
+
|
| 19 |
+
Example: To use <https://huggingface.co/Qwen/Qwen2-72B-Instruct> model's tokenizer just enter **Qwen/Qwen2-72B-Instruct**
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown(MARKDOWN)
|
| 25 |
+
with gr.Row():
|
| 26 |
+
model_name = gr.Textbox(label="repo_name",interactive=True,placeholder="Enter the HF model here...")
|
| 27 |
+
text = gr.Textbox(label="text",interactive=True, placeholder="Enter the text to be tokenized")
|
| 28 |
+
output1 = gr.HighlightedText(show_inline_category=True)
|
| 29 |
+
|
| 30 |
+
btn = gr.Button("Run")
|
| 31 |
+
btn.click(token_viz, inputs=[model_name, text], outputs=[output1])
|
| 32 |
+
|
| 33 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.42.4
|
| 2 |
+
huggingface-hub===0.23.4
|