Update README.md
Browse files
README.md
CHANGED
|
@@ -87,11 +87,33 @@ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
|
| 87 |
### Inference
|
| 88 |
|
| 89 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def fix_byte_encoding(text: str) -> str:
|
| 91 |
-
"""Fix byte-level BPE encoding for proper emoji/unicode display.
|
|
|
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
return text
|
| 96 |
|
| 97 |
messages = [
|
|
@@ -119,7 +141,7 @@ response = fix_byte_encoding(response) # Fix emoji/unicode display
|
|
| 119 |
print(response)
|
| 120 |
```
|
| 121 |
|
| 122 |
-
> **Note:** The `fix_byte_encoding` helper is needed because Mistral uses byte-level BPE tokenization.
|
| 123 |
|
| 124 |
### Quick Test
|
| 125 |
|
|
|
|
| 87 |
### Inference
|
| 88 |
|
| 89 |
```python
|
| 90 |
+
def _build_unicode_to_bytes_map() -> dict[str, int]:
|
| 91 |
+
"""Build inverse of GPT-2's bytes_to_unicode mapping."""
|
| 92 |
+
bs = (
|
| 93 |
+
list(range(ord("!"), ord("~") + 1))
|
| 94 |
+
+ list(range(ord("¡"), ord("¬") + 1))
|
| 95 |
+
+ list(range(ord("®"), ord("ÿ") + 1))
|
| 96 |
+
)
|
| 97 |
+
cs = bs[:]
|
| 98 |
+
n = 0
|
| 99 |
+
for b in range(256):
|
| 100 |
+
if b not in bs:
|
| 101 |
+
bs.append(b)
|
| 102 |
+
cs.append(256 + n)
|
| 103 |
+
n += 1
|
| 104 |
+
return {chr(c): b for b, c in zip(bs, cs)}
|
| 105 |
+
|
| 106 |
+
_UNICODE_TO_BYTE = _build_unicode_to_bytes_map()
|
| 107 |
+
|
| 108 |
def fix_byte_encoding(text: str) -> str:
|
| 109 |
+
"""Fix byte-level BPE encoding for proper emoji/unicode display.
|
| 110 |
+
|
| 111 |
+
Example: "ð٤Ĺ" -> "🤗"
|
| 112 |
+
"""
|
| 113 |
try:
|
| 114 |
+
byte_values = bytes([_UNICODE_TO_BYTE.get(c, ord(c)) for c in text])
|
| 115 |
+
return byte_values.decode("utf-8")
|
| 116 |
+
except (UnicodeDecodeError, KeyError, ValueError):
|
| 117 |
return text
|
| 118 |
|
| 119 |
messages = [
|
|
|
|
| 141 |
print(response)
|
| 142 |
```
|
| 143 |
|
| 144 |
+
> **Note:** The `fix_byte_encoding` helper is needed because Mistral uses byte-level BPE tokenization (GPT-2 style). UTF-8 bytes are encoded as individual Unicode characters (e.g., 🤗 becomes `ð٤Ĺ`). This function reverses that mapping for proper display.
|
| 145 |
|
| 146 |
### Quick Test
|
| 147 |
|