cointegrated
commited on
Commit
•
89c5980
1
Parent(s):
485c05d
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- myv
|
4 |
+
- ru
|
5 |
+
- fi
|
6 |
+
- de
|
7 |
+
- es
|
8 |
+
- en
|
9 |
+
- hi
|
10 |
+
- zh
|
11 |
+
- tr
|
12 |
+
- uk
|
13 |
+
- fr
|
14 |
+
- ar
|
15 |
+
tags:
|
16 |
+
- erzya
|
17 |
+
- mordovian
|
18 |
+
- fill-mask
|
19 |
+
- pretraining
|
20 |
+
- embeddings
|
21 |
+
- masked-lm
|
22 |
+
- feature-extraction
|
23 |
+
- sentence-similarity
|
24 |
+
license: cc-by-sa-4.0
|
25 |
+
datasets:
|
26 |
+
- slone/myv_ru_2022
|
27 |
+
- yhavinga/ccmatrix
|
28 |
+
---
|
29 |
+
|
30 |
+
This a model to translate texts from the Erzya language (`myv`, cyrillic script) to 11 other languages: `ru,fi,de,es,en,hi,zh,tr,uk,fr,ar`.
|
31 |
+
|
32 |
+
It is described in the paper "The first neural machine translation system for the Erzya language".
|
33 |
+
|
34 |
+
This model is based on [facebook/mbart-large-50](https://huggingface.co/facebook/mbart-large-50) ([license here](https://tfhub.dev/google/LaBSE/2)), but with updated vocabulary and checkpoint:
|
35 |
+
- Added an extra language token `myv_XX` and 19K new BPE tokens for the Erzya language;
|
36 |
+
- Fine-tuned to translate to Erzya: first from Russian, then from all 11 languages.
|
37 |
+
|
38 |
+
The following code can be used to run translation using the model
|
39 |
+
|
40 |
+
```Python
|
41 |
+
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
|
42 |
+
|
43 |
+
|
44 |
+
def fix_tokenizer(tokenizer):
|
45 |
+
""" Add a new language token to the tokenizer vocabulary (this should be done each time after its initialization) """
|
46 |
+
old_len = len(tokenizer) - int('myv_XX' in tokenizer.added_tokens_encoder)
|
47 |
+
tokenizer.lang_code_to_id['myv_XX'] = old_len-1
|
48 |
+
tokenizer.id_to_lang_code[old_len-1] = 'myv_XX'
|
49 |
+
tokenizer.fairseq_tokens_to_ids["<mask>"] = len(tokenizer.sp_model) + len(tokenizer.lang_code_to_id) + tokenizer.fairseq_offset
|
50 |
+
|
51 |
+
tokenizer.fairseq_tokens_to_ids.update(tokenizer.lang_code_to_id)
|
52 |
+
tokenizer.fairseq_ids_to_tokens = {v: k for k, v in tokenizer.fairseq_tokens_to_ids.items()}
|
53 |
+
if 'myv_XX' not in tokenizer._additional_special_tokens:
|
54 |
+
tokenizer._additional_special_tokens.append('myv_XX')
|
55 |
+
tokenizer.added_tokens_encoder = {}
|
56 |
+
|
57 |
+
|
58 |
+
def translate(text, model, tokenizer, src='ru_RU', trg='myv_XX', max_length='auto', num_beams=3, repetition_penalty=5.0, train_mode=False, n_out=None, **kwargs):
|
59 |
+
tokenizer.src_lang = src
|
60 |
+
encoded = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
|
61 |
+
if max_length == 'auto':
|
62 |
+
max_length = int(32 + 1.5 * encoded.input_ids.shape[1])
|
63 |
+
if train_mode:
|
64 |
+
model.train()
|
65 |
+
else:
|
66 |
+
model.eval()
|
67 |
+
generated_tokens = model.generate(
|
68 |
+
**encoded.to(model.device),
|
69 |
+
forced_bos_token_id=tokenizer.lang_code_to_id[trg],
|
70 |
+
max_length=max_length,
|
71 |
+
num_beams=num_beams,
|
72 |
+
repetition_penalty=repetition_penalty,
|
73 |
+
num_return_sequences=n_out or 1,
|
74 |
+
**kwargs
|
75 |
+
)
|
76 |
+
out = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
77 |
+
if isinstance(text, str) and n_out is None:
|
78 |
+
return out[0]
|
79 |
+
return out
|
80 |
+
|
81 |
+
|
82 |
+
mname = 'slone/mbart-large-51-mul-myv-v1'
|
83 |
+
model = MBartForConditionalGeneration.from_pretrained(mname)
|
84 |
+
tokenizer = MBart50Tokenizer.from_pretrained(mname)
|
85 |
+
fix_tokenizer(tokenizer)
|
86 |
+
|
87 |
+
|
88 |
+
print(translate('Привет, собака!', model, tokenizer, src='ru_RU', trg='myv_XX'))
|
89 |
+
# Шумбрат, киска! # действительно, по-эрзянски собака именно так
|
90 |
+
print(translate('Hello, doggy!', model, tokenizer, src='en_XX', trg='myv_XX'))
|
91 |
+
# Шумбрат, киска!
|
92 |
+
```
|