Python离线翻译

Python源码:


# 离线翻译服务代码
import os
from flask import Flask, request
from gevent import pywsgi
from transformers import pipeline, AutoModelWithLMHead, AutoTokenizer
import warnings, requests
warnings.filterwarnings('ignore')

try:    
    print('正在加载【汉语-英语】翻译模型... ...')
    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)
    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(__

你可能感兴趣的:(小方法,python,flask,开发语言)