Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
pipeline_tag: translation
|
3 |
+
---
|
4 |
+
|
5 |
+
The model and the tokenizer are based on [facebook/nllb-200-distilled-600M](https://huggingface.co/facebook/nllb-200-distilled-600M).
|
6 |
+
|
7 |
+
We trained the model to use one sentence of context. The context is prepended to the input sentence with the `sep_token` in between. We used a subset of the [OpenSubtitles2018]( https://huggingface.co/datasets/open_subtitles) dataset for training. We trained on the interleaved dataset for all directions between the following languages: English, German, Dutch, Spanish, Italian, and Greek.
|
8 |
+
The tokenizer of the base model was not changed. For the language codes, see the base model.
|
9 |
+
|
10 |
+
Use this code for translation:
|
11 |
+
``` from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
12 |
+
|
13 |
+
model_name = 'voxreality/src_ctx_aware_nllb_600M'
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
16 |
+
|
17 |
+
max_length = 100
|
18 |
+
src_lang = 'eng_Latn'
|
19 |
+
tgt_lang = 'deu_Latn'
|
20 |
+
context_text = 'This is an optional context sentence.' # use '' empty string if not context should be used
|
21 |
+
sentence_text = 'Text to be translated.'
|
22 |
+
input_text = f'{context_text} {tokenizer.sep_token} {sentence_text}'
|
23 |
+
|
24 |
+
tokenizer.src_lang = src_lang
|
25 |
+
inputs = tokenizer(input_text, return_tensors='pt').to(model.device)
|
26 |
+
model_output = model.generate(**inputs,
|
27 |
+
forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang],
|
28 |
+
max_length=max_length)
|
29 |
+
output_text = tokenizer.batch_decode(model_output, skip_special_tokens=True)[0]
|
30 |
+
|
31 |
+
print(output_text)
|
32 |
+
```
|
33 |
+
|
34 |
+
You can also use the pipeline
|
35 |
+
```
|
36 |
+
from transformers import pipeline
|
37 |
+
|
38 |
+
model_name = 'voxreality/src_ctx_aware_nllb_600M'
|
39 |
+
translation_pipeline = pipeline("translation", model=model_name)
|
40 |
+
src_lang = 'eng_Latn'
|
41 |
+
tgt_lang = 'deu_Latn'
|
42 |
+
context_text = 'This is an optional context sentence.' # use '' empty string if not context should be used
|
43 |
+
sentence_text = 'Text to be translated.'
|
44 |
+
input_texts = [f'{context_text} {translation_pipeline.tokenizer.sep_token} {sentence_text}']
|
45 |
+
|
46 |
+
pipeline_output = translation_pipeline(input_texts, src_lang=src_lang, tgt_lang=tgt_lang)
|
47 |
+
|
48 |
+
print(pipeline_output[0]['translation_text'])
|
49 |
+
|
50 |
+
```
|