Python离线翻译
生活随笔
收集整理的這篇文章主要介紹了
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(__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 resultif mod == 'en2zh':result = translation_en2zh(text, max_length=10240)[0]['translation_text']return resultif mod == 'de2zh':result = translation_de2zh(text, max_length=10240)[0]['translation_text']return resultif mod == 'zh2de':result = translation_zh2de(text, max_length=10240)[0]['translation_text']return resultprint('翻譯服務已啟動,請通過api形式訪問該服務地址:http://ip:16888/translater')server = pywsgi.WSGIServer(('0.0.0.0', 16888), app)server.serve_forever()except: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)?翻譯模型:huggingface-Helsinki-NLP
模型選擇根據上面地址,選擇自己需要的模型
查看調用方式
?
?
?
啟動服務,首次啟動需要安裝各種插件,根據提示pip安裝,還需要下載模型
PS C:\Desktop> python .\MyTranslate.py 2022-09-30 11:08:49.924425: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2022-09-30 11:08:49.924913: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.正在加載【漢語-英語】翻譯模型... ... Downloading: 100%|██████████████████████████████████████████████████████████████████| 312M/312M [00:38<00:00, 8.07MB/s] Downloading: 100%|██████████████████████████████████████████████████████████████████| 44.0/44.0 [00:00<00:00, 44.0kB/s] Downloading: 100%|███████████████████████████████████████████████████████████████████| 805k/805k [00:01<00:00, 703kB/s] Downloading: 100%|███████████████████████████████████████████████████████████████████| 807k/807k [00:01<00:00, 689kB/s] Downloading: 100%|████████████████████████████████████████████████████████████████| 1.62M/1.62M [00:01<00:00, 1.16MB/s] 正在加載【英語-漢語】翻譯模型... ... Downloading: 100%|████████████████████████████████████████████████████████████████| 1.40k/1.40k [00:00<00:00, 1.40MB/s] Downloading: 100%|██████████████████████████████████████████████████████████████████| 312M/312M [00:34<00:00, 9.17MB/s] Downloading: 100%|██████████████████████████████████████████████████████████████████| 44.0/44.0 [00:00<00:00, 44.0kB/s] Downloading: 100%|███████████████████████████████████████████████████████████████████| 806k/806k [00:01<00:00, 494kB/s] Downloading: 100%|███████████████████████████████████████████████████████████████████| 805k/805k [00:01<00:00, 685kB/s] Downloading: 100%|████████████████████████████████████████████████████████████████| 1.62M/1.62M [00:01<00:00, 1.01MB/s] 翻譯服務已啟動,請通過api形式訪問該服務地址:http://localhost:16888/translaterPostman測試
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Python离线翻译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CH438Q芯片详细解读
- 下一篇: websocket python爬虫_p