如何使用 HuggingFace 将中文翻译成英文?

wtw*_*twt 5 translation nlp machine-translation huggingface-transformers huggingface-tokenizers

我想使用 HuggingFace 的转换器使用预训练"xlm-mlm-xnli15-1024"模型将中文翻译成英文。本教程展示了如何从英语到德语。

\n

我尝试按照教程进行操作,但它没有详细说明如何手动更改语言或解码结果。我不知道从哪里开始。抱歉,这个问题不能更具体了。

\n

这是我尝试过的:

\n
from transformers import AutoModelWithLMHead, AutoTokenizer\nbase_model = "xlm-mlm-xnli15-1024"\nmodel = AutoModelWithLMHead.from_pretrained(base_model)\ntokenizer = AutoTokenizer.from_pretrained(base_model)\n\ninputs = tokenizer.encode("translate English to Chinese: Hugging Face is a technology company based in New York and Paris", return_tensors="pt")\noutputs = model.generate(inputs, max_length=40, num_beams=4, early_stopping=True)\n\nprint(tokenizer.decode(outputs.tolist()[0]))\n
Run Code Online (Sandbox Code Playgroud)\n
\'<s>translate english to chinese : hugging face is a technology company based in new york and paris </s>china hug \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2 \xe2\x84\xa2\'\n
Run Code Online (Sandbox Code Playgroud)\n

Jus*_*ard 5

这可能会有所帮助。https://huggingface.co/Helsinki-NLP/opus-mt-zh-en

\n
import transformers\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\ntokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")\nmodel = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")\ntext =\'\xe5\xa4\xae\xe8\xa7\x86\xe6\x98\xa5\xe6\x99\x9a\xef\xbc\x8c\xe6\xb2\xa1\xe6\x9c\x89\xe6\x9c\x80\xe7\x83\x82\xef\xbc\x8c\xe5\x8f\xaa\xe6\x9c\x89\xe6\x9b\xb4\xe7\x83\x82\'\ntokenized_text = tokenizer.prepare_seq2seq_batch([text], return_tensors=\'pt\')\ntranslation = model.generate(**tokenized_text)\ntranslated_text = tokenizer.batch_decode(translation, skip_special_tokens=False)[0]\n
Run Code Online (Sandbox Code Playgroud)\n


Jin*_*ich 3

您提到的模型xlm-mlm-xnli15-1024可用于翻译,但不能按照您提供的链接中显示的方式进行翻译。

该链接特定于 T5 型号。使用XLM模型,您只需输入源句子,但需要添加语言嵌入。多语言模型教程中对此进行了解释。另请注意,此 XLM 模型主要旨在为下游任务提供跨语言表示,因此您不能期望非常好的翻译质量。