Pawel-M commited on
Commit
3aeadd4
1 Parent(s): 8441079

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +78 -0
README.md ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ - de
5
+ - it
6
+ - el
7
+ - es
8
+ - nl
9
+ pipeline_tag: translation
10
+ ---
11
+ The model and the tokenizer are based on [facebook/nllb-200-distilled-600M](https://huggingface.co/facebook/nllb-200-distilled-600M).
12
+
13
+ We trained the model to use one sentence of context and a single term of the terminology-constraint. The context is prepended to the input sentence with the `sep_token` in between. The term should be in the target language and be postpended to the input sentence with the `sep_token` in between. In case of no terminology constraint, the `sep_token` should also be added. 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.
14
+
15
+ The tokenizer of the base model was not changed. For the language codes, see the base model.
16
+
17
+ Use this code for translation:
18
+ ```
19
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
20
+
21
+ model_name = 'voxreality/src_ctx_and_term_nllb_600M'
22
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
23
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
24
+
25
+ max_length = 100
26
+ src_lang = 'eng_Latn'
27
+ tgt_lang = 'deu_Latn'
28
+ context_text = 'This is an optional context sentence.'
29
+ target_term = 'text' # term to be used in the target language
30
+ sentence_text = 'Text to be translated.'
31
+
32
+ # if a context and a term are provided use the following:
33
+ input_text = f'{context_text} {tokenizer.sep_token} {sentence_text} {tokenizer.sep_token} {target_term}'
34
+ # if no context but a term is provided use the following:
35
+ # input_text = f'{sentence_text} {tokenizer.sep_token} {target_term}'
36
+ # if a context is provided but no term use the following:
37
+ # input_text = f'{context_text} {tokenizer.sep_token} {sentence_text} {tokenizer.sep_token}'
38
+ # if not context nor term is provided use the following:
39
+ # input_text = f'{sentence_text} {tokenizer.sep_token}'
40
+
41
+ tokenizer.src_lang = src_lang
42
+ inputs = tokenizer(input_text, return_tensors='pt').to(model.device)
43
+ model_output = model.generate(**inputs,
44
+ forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang],
45
+ max_length=max_length)
46
+ output_text = tokenizer.batch_decode(model_output, skip_special_tokens=True)[0]
47
+
48
+ print(output_text)
49
+ ```
50
+
51
+ You can also use the pipeline
52
+ ```
53
+ from transformers import pipeline
54
+
55
+ model_name = 'voxreality/src_ctx_and_term_nllb_600M'
56
+ translation_pipeline = pipeline("translation", model=model_name)
57
+ sep_token = translation_pipeline.tokenizer.sep_token
58
+ src_lang = 'eng_Latn'
59
+ tgt_lang = 'deu_Latn'
60
+ context_text = 'This is an optional context sentence.'
61
+ target_term = 'text' # term to be used in the target language
62
+ sentence_text = 'Text to be translated.'
63
+
64
+
65
+ # if a context and a term are provided use the following:
66
+ input_texts = [f'{context_text} {sep_token} {sentence_text} {sep_token} {target_term}']
67
+ # if no context but a term is provided use the following:
68
+ # input_texts = [f'{sentence_text} {sep_token} {target_term}']
69
+ # if a context is provided but no term use the following:
70
+ # input_texts = [f'{context_text} {sep_token} {sentence_text} {sep_token}']
71
+ # if not context nor term is provided use the following:
72
+ # input_texts = [f'{sentence_text} {sep_token}']
73
+
74
+ pipeline_output = translation_pipeline(input_texts, src_lang=src_lang, tgt_lang=tgt_lang)
75
+
76
+ print(pipeline_output[0]['translation_text'])
77
+
78
+ ```