Views
No views yet
sep_token in between. We used a subset of the OpenSubtitles2018 dataset for training. We trained on the interleaved dataset for all directions between the following languages: English, German, Dutch, Spanish, Italian, and Greek.1
2model_name = 'voxreality/src_ctx_aware_nllb_1.3B'
3tokenizer = AutoTokenizer.from_pretrained(model_name)
4model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
5
6max_length = 100
7src_lang = 'eng_Latn'
8tgt_lang = 'deu_Latn'
9context_text = 'This is an optional context sentence.'
10sentence_text = 'Text to be translated.'
11
12# if the context is provided use the following:
13input_text = f'{context_text} {tokenizer.sep_token} {sentence_text}'
14# if no context is provided use the following:
15# input_text = sentence_text
16
17tokenizer.src_lang = src_lang
18inputs = tokenizer(input_text, return_tensors='pt').to(model.device)
19model_output = model.generate(**inputs,
20 forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang],
21 max_length=max_length)
22output_text = tokenizer.batch_decode(model_output, skip_special_tokens=True)[0]
23
24print(output_text)from transformers import pipeline
model_name = 'voxreality/src_ctx_aware_nllb_1.3B'
translation_pipeline = pipeline("translation", model=model_name)
src_lang = 'eng_Latn'
tgt_lang = 'deu_Latn'
context_text = 'This is an optional context sentence.'
sentence_text = 'Text to be translated.'
# if the context is provided use the following:
input_texts = [f'{context_text} {tokenizer.sep_token} {sentence_text}']
# if no context is provided use the following:
# input_texts = [sentence_text]
pipeline_output = translation_pipeline(input_texts, src_lang=src_lang, tgt_lang=tgt_lang)
print(pipeline_output[0]['translation_text'])