下载类库
下载语言模型
使用
- 脚本 offline_translation.py
# 离线翻译服务代码
import warnings
from flask import Flask, request
from gevent import pywsgi
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline, AutoModelWithLMHead
from transformers import pipeline
warnings.filterwarnings('ignore')
try:
print('正在加载【汉语-英语】翻译模型... ...')
# tokenizer = AutoTokenizer.from_pretrained('Helsinki-NLP/opus-mt-zh-en')
# model = AutoModelWithLMHead.from_pretrained('Helsinki-NLP/opus-mt-zh-en')
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
translation = pipeline('translation_zh_to_en', model=model, tokenizer=tokenizer)
# print('正在加载【英语-汉语】翻译模型... ...')
# model_en2zh = AutoModelWithLMHead.from_pretrained('Helsinki-NLP/opus-mt-en-zh')
# tokenizer_en2zh = AutoTokenizer.from_pretrained('Helsinki-NLP/opus-mt-en-zh')
# translation_en2zh = pipeline('translation_en_to_zh', model=model_en2zh, tokenizer=tokenizer_en2zh)
app = Flask(__name__)
@app.route('/wesky-translater', methods=['POST'])
def translate():
mod = request.form['mod']
text = request.form['text']
if mod == 'zh2en':
result = translation(text, max_length=10240)[0]['translation_text']
return result
# if mod == 'en2zh':
# result = translation_en2zh(text, max_length=10240)[0]['translation_text']
# return result
# if mod == 'de2zh':
# result = translation_de2zh(text, max_length=10240)[0]['translation_text']
# return result
# if mod == 'zh2de':
# result = translation_zh2de(text, max_length=10240)[0]['translation_text']
# return result
print('翻译服务已启动,请通过api形式访问该服务地址:http://127.0.0.1:16888/wesky-translater')
server = pywsgi.WSGIServer(('0.0.0.0', 16888), app)
server.serve_forever()
except Exception as ex:
print('翻译服务存在异常... ...')
print(ex)
- 启动服务
终端:python3 offline_translation.py
- 进行翻译
http://127.0.0.1:16888/wesky-translater
mod:zh2en
text:apple