欢迎光临散文网 会员登陆 & 注册

【保姆级教程】无需编程基础 | 教你用python搭建本地翻译服务 | 基...

2023-02-02 15:01 作者:Stand114514  | 我要投稿

设置清华源:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple


三个依赖包:

pip install transformers

pip install pytouch

pip install sentencepiece


代码部分:

from transformers import pipeline, AutoModelWithLMHead, AutoTokenizer

import warnings

warnings.filterwarnings ('ignore')


modelName = "Helsinki-NLP/opus-mt-zh-en"#模型名

print('正在加载翻译模型......')

model = AutoModelWithLMHead.from_pretrained(modelName)

tokenizer = AutoTokenizer.from_pretrained(modelName)

translation = pipeline('translation_xx_to_yy',model=model,tokenizer=tokenizer)

print('加载完成')

while True:

  text = input('请输入需要翻译的语句:\n')

  res = translation(text)[0]['translation_text']

  print(res)


【保姆级教程】无需编程基础 | 教你用python搭建本地翻译服务 | 基...的评论 (共 条)

分享到微博请遵守国家法律