Self-contained PyTorch example, escaped regex ranges
Browse files
README.md
CHANGED
|
@@ -60,8 +60,8 @@ sess = ort.InferenceSession(hf_hub_download(REPO, "ruq-30m.onnx"))
|
|
| 60 |
|
| 61 |
# The tokenizer was trained on normalised text: diacritics and tatweel
|
| 62 |
# removed. Skipping this step gives a different, worse tokenisation.
|
| 63 |
-
_DIACRITICS = re.compile(r"[
|
| 64 |
-
_ZERO_WIDTH = re.compile(r"[
|
| 65 |
|
| 66 |
def normalize(text):
|
| 67 |
text = unicodedata.normalize("NFC", text)
|
|
@@ -106,9 +106,22 @@ The same weights are published as safetensors alongside the architecture module,
|
|
| 106 |
continued training or fine-tuning:
|
| 107 |
|
| 108 |
```python
|
| 109 |
-
import json, torch
|
| 110 |
from huggingface_hub import hf_hub_download
|
| 111 |
from safetensors.torch import load_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# the architecture module has to be on the path before it can be imported
|
| 114 |
hf_hub_download(REPO, "modeling_ruqlm.py", local_dir=".")
|
|
@@ -122,8 +135,9 @@ state["lm_head.weight"] = state["tok_emb.weight"] # embeddings are tied
|
|
| 122 |
model.load_state_dict(state)
|
| 123 |
model.eval()
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 127 |
print(tok.decode(out[0].tolist()))
|
| 128 |
```
|
| 129 |
|
|
|
|
| 60 |
|
| 61 |
# The tokenizer was trained on normalised text: diacritics and tatweel
|
| 62 |
# removed. Skipping this step gives a different, worse tokenisation.
|
| 63 |
+
_DIACRITICS = re.compile(r"[\u064b-\u0652\u0670\u0653-\u0655]")
|
| 64 |
+
_ZERO_WIDTH = re.compile(r"[\u200b-\u200f\u202a-\u202e\ufeff]")
|
| 65 |
|
| 66 |
def normalize(text):
|
| 67 |
text = unicodedata.normalize("NFC", text)
|
|
|
|
| 106 |
continued training or fine-tuning:
|
| 107 |
|
| 108 |
```python
|
| 109 |
+
import json, re, unicodedata, torch
|
| 110 |
from huggingface_hub import hf_hub_download
|
| 111 |
from safetensors.torch import load_file
|
| 112 |
+
from tokenizers import Tokenizer
|
| 113 |
+
|
| 114 |
+
REPO, EOS = "Ruqiya/ruqlm", 2
|
| 115 |
+
|
| 116 |
+
tok = Tokenizer.from_file(hf_hub_download(REPO, "tokenizer.json"))
|
| 117 |
+
|
| 118 |
+
_DIACRITICS = re.compile(r"[\u064b-\u0652\u0670\u0653-\u0655]")
|
| 119 |
+
_ZERO_WIDTH = re.compile(r"[\u200b-\u200f\u202a-\u202e\ufeff]")
|
| 120 |
+
|
| 121 |
+
def normalize(text):
|
| 122 |
+
text = unicodedata.normalize("NFC", text)
|
| 123 |
+
text = _ZERO_WIDTH.sub("", text).replace("\u0640", "") # tatweel
|
| 124 |
+
return re.sub(r"\s+", " ", _DIACRITICS.sub("", text)).strip()
|
| 125 |
|
| 126 |
# the architecture module has to be on the path before it can be imported
|
| 127 |
hf_hub_download(REPO, "modeling_ruqlm.py", local_dir=".")
|
|
|
|
| 135 |
model.load_state_dict(state)
|
| 136 |
model.eval()
|
| 137 |
|
| 138 |
+
ids = tok.encode(normalize("كان يا ما كان")).ids[:-1] # drop the trailing </s>
|
| 139 |
+
out = model.generate(torch.tensor([ids]), max_new_tokens=120,
|
| 140 |
+
temperature=0.85, top_k=50, eos_id=EOS)
|
| 141 |
print(tok.decode(out[0].tolist()))
|
| 142 |
```
|
| 143 |
|