1. 程式人生 > >AWS機器學習初探(2):文字翻譯Translate、文字轉語音Polly、語音轉文字Transcribe

AWS機器學習初探(2):文字翻譯Translate、文字轉語音Polly、語音轉文字Transcribe

這幾個服務的功能和使用都很直接和簡單,因此放在一篇文章中介紹。

1. 文字翻譯服務 Translate

1.1 功能介紹

AWS Translate 服務是一種AWS 機器學習應用服務,它利用高階機器學習技術來進行文字翻譯。它的使用非常簡單,只需要提供輸入文字,該服務就給出輸出文字。

  • 輸入文字(Source text):待翻譯的文字,必須是 UTF-8 格式。
  • 輸出文字(Output text):AWS Translate 服務輸出的翻譯好的文字,也是 UTF-8 格式。

AWS Translate 服務有兩個元件:

  • encoder:每次從輸入文字中讀取一個單詞,然後根據其含義構造語義表達。
  • decoder:利用encoder給出的語義表達,產生一個翻譯詞彙。

AWS Translate 利用 attention 機制來理解上下文,它幫助 decoder 聚焦在原文中最相關的部分,這有助於它翻譯模糊的單詞和短語。

Translate 目前只支援將多種語言翻譯為英文,以及將英文翻譯成多種目標語言。Translate 能自動檢測輸入文字是哪種語言,它是利用 Comprehend 來實現語言探測的。

來對比下AWS Translate 和 Google 翻譯的結果:

這是一段川普的推特文字:

I am hearing so many great things about the Republican Party’s California Gubernatorial Candidate, John Cox. He is
a very successful businessman who is tired of high Taxes & Crime. He will Make California Great Again & make you proud of your Great State again. Total Endorsement!

Google 翻譯結果:

關於共和黨加州州長候選人約翰考克斯,我聽到了很多很棒的事情。 他是一個非常成功的商人,厭倦了高稅收和犯罪。 他將使加利福尼亞再次偉大,讓你再次為你的偉大國家感到驕傲。 總代言!

AWS Translate 翻譯結果:

我聽到很多關於共和黨加州州長候選人約翰·考克斯的偉大事情。 他是一個非常成功的商人,厭倦了高稅與犯罪。 他將再次使加州成為偉大的國家,讓你再次為你的偉大國家感到驕傲。 完全贊同!

從結果看,AWS Translage的質量應該比Google 稍微好一些。

1.2 介面操作示例

以下示例將中文文字翻譯為英文:

1.3 CLI 操作示例

aws translate translate-text --region us-east-1 --source-language-code "auto" --target-language-code "zh" --text "I am hearing so many great things about the Republican Party California Gubernatorial Candidate, John Cox. He is a very successful businessman who is tired of high Taxes & Crime. He will Make California Great Again &  make you proud of your Great State again. Total Endorsement"
{
    "TranslatedText": "我聽到很多關於共和黨加州州長候選人約翰·考克斯的偉大事情。 他是一個非常成功的商人,厭倦了高稅與犯罪。 他將再次使加州成為偉大的國家,讓你再次為你的偉大國家感到驕傲。 完全贊同",
    "SourceLanguageCode": "en",
    "TargetLanguageCode": "zh"
}

1.4 API 

Translate 服務只有一個API,就是 TranslateText。

請求語法:

{
"SourceLanguageCode": "string", "TargetLanguageCode": "string", "Text": "string"
}

返回語法:

{
"SourceLanguageCode": "string", "TargetLanguageCode": "string", "TranslatedText": "string"
}

1.5 python 示例程式碼

程式碼:

import boto3

translate = boto3.client(service_name='translate', region_name='us-east-1', use_ssl=True)

result = translate.translate_text(Text="Hello World", SourceLanguageCode="auto", TargetLanguageCode="zh")

print('TranslatedText: ' + result.get('TranslatedText'))
print('SourceLanguageCode: ' + result.get('SourceLanguageCode'))
print('TargetLangaugeCode: ' + result.get('TargetLanguageCode'))

輸出:

TranslatedText: 您好世界
SourceLanguageCode: en
TargetLangaugeCode: zh

2. 文字轉語音Polly

2.1 功能介紹

所謂的文字轉語音服務,就是把文字朗讀出來。它的輸入輸出為:

  • 輸入文字:待被Polly轉化為語音的文字。可以是純文字(plain text),也可以是 SSML(Speech Syntessis Markup Language) 格式。SSML 格式可以進行更精細的控制,比如音量、語速、發音等。
  • 輸出的語言種類:Polly 支援多種語言,每種語音支援多種發聲模式,比如女生聲音和男性聲音。
  • 輸出格式:Polly 支援輸出多種格式的語音,比如 mp3格式,PCM 格式等。

幾個特色功能:

  • 支援發音字典(lexicon):通過發音字典可以自定義單詞的發音。使用者可以將發音字典上傳到AWS 上,然後將其應用到 SynthesizeSpeech API 中。
  • 支援非同步語音合成:可以以非同步方式為大文字合成語音。三步走:啟動一個合成任務,獲取任務的詳情,從S3中獲取合成結果。近實時API只支援3000個字元,而非同步API可以支援最多20萬個字元。
  • 支援 SSML:詳情可參考官方文件。

2.2 介面操作示例

  • Listen to speech:直接聽語音
  • Download MP3:可以將語音儲存為 MP3 格式,並直接下載
  • Syntesize to S3:將語音輸出儲存到 S3 中。

2.3 CLI 操作示例

SammydeMacBook-Air:~ Sammy$ aws polly synthesize-speech --output-format mp3 --voice-id Joanna --text 'Hello, my name is Joanna. I learned about the W3C on 10/3 of last year.' helloworld.mp3
{
    "ContentType": "audio/mpeg",
    "RequestCharacters": "71"
}
SammydeMacBook-Air:~ Sammy$ ls helloworld.mp3
helloworld.mp3

2.4 API

Polly 具有以下幾個API:

• SynthesizeSpeech:合成語音

• ListLexicons:列表發音詞典

• PutLexicon:建立發音詞典

• GetLexicon:檢索發音詞典

• DeleteLexicon:刪除發音詞典

• DescribeVoices:獲取聲音列表
• GetSpeechSynthesisTask:獲取語音生成任務

• ListSpeechSynthesisTasks:獲取語音生成任務列表

• StartSpeechSynthesisTask:開始語音生成任務

2.5 python 示例程式碼

from boto3 import Session
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir

session = Session(profile_name="default")
polly = session.client("polly")

try:
    text = "To the incredible people of the Great State of Wyoming: Go VOTE TODAY for Foster Friess - He will be a fantastic Governor! Strong on Crime, Borders & 2nd Amendment. Loves our Military & our Vets. He has my complete and total Endorsement!"
    response = polly.synthesize_speech(Text = text, OutputFormat="mp3", VoiceId="Joanna")
except Exception as error:
    print(error)
    sys.exit(-1)

if "AudioStream" in response:
    with closing(response["AudioStream"]) as stream:
        output = os.path.join(gettempdir(), "speech.mp3")
        try:
            with open(output, "wb") as file:
                file.write(stream.read())
        except IOError as error:
            print(error)
            sys.exit(-1)
else:
    print("Could not stream audio")
    sys.exit(-1)

if sys.platform == "win32":
    os.startfile(output)
else:
    opener = "open" if sys.platform == "darwin" else "xdg-open"
    subprocess.call([opener, output])

這段程式碼會將語音儲存到 speech.mp3中,然後呼叫系統預設播放器進行播放。

3. 語音轉文字服務Transcribe

3.1 功能介紹

AWS Transcribe 服務於利用機器學習來識別語音檔案中的聲音,然後將其轉化為文字。目前支援英語和西班牙文語音。必須將語音檔案儲存在S3中,輸出結果也會被儲存在S3中。

  • 輸入聲音檔案,支援 flac、mp3、mp4 和 wav 檔案格式。長度不能超過2小時。
  • 指定語言。

幾個特色功能:

  • 發音者識別(speaker identification):Transcribe 能區別一個語音檔案中的多個說話者。支援2到10個發音者。
  • 支援多聲道(channel identification): 如果聲音檔案中有多聲道,那麼
  • 支援字典(vocabulary):比如不能識別的單詞,特定領域不常用的單詞

3.2 介面操作示例

建立一個job:

job列表:

結果:

3.3 CLI 操作示例

(1)提交一個job

{
    "TranscriptionJobName": "testTranscribe",
    "LanguageCode": "en-US",
    "MediaFormat": "mp3",
    "Media": {
        "MediaFileUri": "https://s3.dualstack.us-east-1.amazonaws.com/*********/hellosammy.mp3"
    }
}

aws transcribe start-transcription-job --region us-east-1 --cli-input-json file://testTranscribeJob.json

(2)獲取job 列表

aws transcribe list-transcription-jobs --region us-east-1 --status IN_PROGRESS
{
    "Status": "IN_PROGRESS",
    "TranscriptionJobSummaries": [
        {
            "TranscriptionJobName": "testTranscribe",
            "CreationTime": 1535338023.662,
            "LanguageCode": "en-US",
            "TranscriptionJobStatus": "IN_PROGRESS",
            "OutputLocationType": "SERVICE_BUCKET"
        }
    ]
}

(3)一旦 job 完成後,從其包含的TranscriptFileUri地址可以下載輸出文字,部分內容如下:

{"jobName":"testTranscribe","accountId":"725348140609","results":{"transcripts":[{"transcript":"Hello, my name is sami. I learned about the w three c on october third last year."}],"items":[{"start_time":"0.0","end_time":"0.59","alternatives":[{"confidence":"0.9023","content":"Hello"}],"type":"pronunciation"},{"alternatives":[{"confidence":null,"content":","}],"type":"punctuation"},{"start_time":"0.7","end_time":"0.88","alternatives":
[{"confidence":"0.9867","content":"last"}],"type":"pronunciation"},{"start_time":"4.69","end_time":"5.07","alternatives":[{"confidence":"0.9867","content":"year"}],"type":"pronunciation"},{"alternatives":[{"confidence":null,"content":"."}],"type":"punctuation"}]},"status":"COMPLETED"}

3.4 API

  • StartTranscriptionJob:開始一個轉換任務
  • ListTranscriptionJobs:獲取任務列表
  • GetTranscriptionJob:獲取任務
  • CreateVocabulary:建立字典
  • DeleteVocabulary:刪除字典
  • GetVocabulary:獲取字典
  • ListVocabularies:獲取字典列表
  • UpdateVocabulary:上傳字典

3.5 python 示例程式碼

import time
import boto3

transcribe = boto3.client(('transcribe'))
job_name = "testTranscribeJob100"
job_uri = "https://s3.dualstack.us-east-1.amazonaws.com/*****/hellosammy.mp3"

transcribe.start_transcription_job(TranscriptionJobName=job_name, Media={'MediaFileUri': job_uri}, MediaFormat='mp3', LanguageCode='en-US')

while True:
    status = transcribe.get_transcription_job(TranscriptionJobName = job_name)
    if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', "FAILED"]:
        break

    print("Job not ready yet...")
    time.sleep(5)

print(status)

參考文件:

  • AWS Translate、Polly 和 Transcribe 開發者文件 

歡迎大家關注我的個人公眾號: