Python輕鬆建立中英翻譯API

這篇我們使用Helsinki-NLP/opus-mt-zh-en翻譯模型,該模型是芬蘭赫爾辛基大學 (University of Helsinki)的語言科技研究小組所提供(Language Technology Research Group)的。

而API的架構採用flask,其架構我已經整理在 Chunshan-Theta/flaskHuggingfaceWorker,基本上可以直接封裝成服務提供使用。


成效如下,從隨意一則新聞中,抽取內容進行測試

原文:指揮中心再次呼籲,民眾應儘速完成疫苗接種,落實手部衛生、咳嗽禮節及佩戴口罩等個人防護措施,減少不必要移動、活動或集會,避免出入人多擁擠的場所,或高感染傳播風險場域,並主動積極配合各項防疫措施,下載及使用「臺灣社交距離APP」,共同嚴守社區防線。
翻譯:The command centre reiterated its call for people to complete their vaccinations as quickly as possible, to put in place personal protective measures such as hygiene in their hands, cough rituals and masks, to reduce unnecessary movement, activities or meetings, to avoid access to crowded sites, or to high-infection spreading risk areas, and to actively cooperate with vaccination measures, to download and use the Taiwan Social Distance APP, and to work together to secure community lines.

第一步 下載檔案

從Chunshan-Theta/flaskHuggingfaceWorker複製專案

第二步 調整設定

打開.env檔案,更新成下方

# SERVER
PORT=80
SERVER=0.0.0.0

# MODEL
MODEL_NAME=Helsinki-NLP/opus-mt-zh-en
MODEL_TOKENIZER=Helsinki-NLP/opus-mt-zh-en
MODEL_VERSION=main


# PIPELINE
TASK_TYPE=text2text-generation

第三步 啟動伺服器(二擇一)

command

python app.py

docker

docker build -t flask-nlu .          
docker run -it --rm -p 80:80 flask-nlu

第四步 進行測試

使用postman

使用swagger

http://127.0.0.1/api/docs/

Tags:, ,

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。