Spaces:
Running
Running
Escape html symbols in visualization
Browse files- tests/test_utils.py +2 -1
- utils.py +3 -1
tests/test_utils.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import unittest
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
class TestTokenizerLoading(unittest.TestCase):
|
| 5 |
def test_load_hf_tokenizer(self):
|
|
|
|
| 1 |
import unittest
|
| 2 |
+
from unittest.mock import patch
|
| 3 |
+
from utils import load_hf_tokenizer, load_openai_tokenizer, load_tokenizers, tokenize, load_gsw_tokenizer, visualize_tokens
|
| 4 |
|
| 5 |
class TestTokenizerLoading(unittest.TestCase):
|
| 6 |
def test_load_hf_tokenizer(self):
|
utils.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from typing import Dict, List, Tuple
|
| 2 |
from pathlib import Path
|
| 3 |
|
|
@@ -229,7 +230,8 @@ def visualize_tokens(text: str, tokenizers: Dict[str, object]):
|
|
| 229 |
# Build the HTML with colored spans for each token
|
| 230 |
for token in tokens:
|
| 231 |
color = token_colors[token]
|
| 232 |
-
|
|
|
|
| 233 |
|
| 234 |
results[name] = html
|
| 235 |
|
|
|
|
| 1 |
+
import html
|
| 2 |
from typing import Dict, List, Tuple
|
| 3 |
from pathlib import Path
|
| 4 |
|
|
|
|
| 230 |
# Build the HTML with colored spans for each token
|
| 231 |
for token in tokens:
|
| 232 |
color = token_colors[token]
|
| 233 |
+
escaped_token = html.escape(token)
|
| 234 |
+
html += f'<span style="background-color: {color}; padding: 2px; margin: 1px; border-radius: 3px;">{escaped_token}</span>'
|
| 235 |
|
| 236 |
results[name] = html
|
| 237 |
|