transformers中英文互译

```python

# @Time    : 2021/5/27 15:28
# @Author  :

from transformers import pipeline, AutoModelWithLMHead, AutoTokenizer

# 英文翻译成中文
# AutoModelForSeq2SeqLM.from_pretrained
model = AutoModelWithLMHead.from_pretrained("Helsinki-NLP/opus-mt-en-zh")
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-zh")
translation = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer)

text = "Student accommodation centres, resorts"
translated_text = translation(text, max_length=40)[0]['translation_text']

# 中文翻译成英文
model = AutoModelWithLMHead.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
translation = pipeline("translation_zh_to_en", model=model, tokenizer=tokenizer)

text = "学生住宿中心,度假屋"
translated_text = translation(text, max_length=40)[0]['translation_text']

```

参考Huggingface Transformer教程(一) - 李理的博客

参考Summary of the tasks — transformers 4.11.3 documentation

参考How to generate text: using different decoding methods for language generation with Transformers

猜你喜欢

转载自blog.csdn.net/Cocktail_py/article/details/117334425