```python
# @Time : 2021/5/27 15:28
# @Author :
from transformers import pipeline, AutoModelWithLMHead, AutoTokenizer
# 英文翻译成中文
# AutoModelForSeq2SeqLM.from_pretrained
# local_files_only=True
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']
```
参考
Python实现英中互译:Helsinki-NLP模型实战

最低0.47元/天 解锁文章
2287





