

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用自訂語言模型
<a name="custom-language-models-using"></a>

建立自訂語言模型後，您可以將其包含在轉錄請求中；請參閱以下各節以取得範例。

您要包含在請求中的模型語言必須與您為媒體指定的語言代碼相符。如果語言不相符，則您的自訂語言模型不會套用於轉錄，且不會出現警告或錯誤。

## 在批次轉錄中使用自訂語言模型
<a name="custom-language-models-using-batch"></a>

若要搭配批次轉錄使用自訂語言模型，請參閱下列範例：

### AWS 管理主控台
<a name="model-console-batch"></a>

1. 登入 [AWS 管理主控台](https://console.aws.amazon.com/transcribe/)。

1. 在導覽窗格中，選擇**轉錄作業**，然後選擇**建立作業**(右上角)。這會開啟**指定作業詳細資訊**頁面。

1. 在**任務設定**面板的**模型類型**下，選擇**自訂語言模型**方塊。  
![Amazon Transcribe 主控台螢幕擷取畫面：「任務設定」頁面。](http://docs.aws.amazon.com/zh_tw/transcribe/latest/dg/images/clm-console.png)

   您還必須從下拉式選單中選擇輸入語言。  
![Amazon Transcribe 主控台螢幕擷取畫面：顯示下拉式選單的「工作設定」頁面。](http://docs.aws.amazon.com/zh_tw/transcribe/latest/dg/images/clm-console-language.png)

1. 在 **自訂模型選擇** 下，從下拉式選單選擇現有的自訂語言模型或 **建立新的語言模型**。

   在輸入**資料**面板中新增輸入檔案 Amazon S3 的位置。

1. 選擇**下一步**以取得其他組態選項。

   選擇**建立作業**以執行轉錄作業。

### AWS CLI
<a name="model-using-cli"></a>

此範例使用 [start-transcription-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/transcribe/start-transcription-job.html) 指令和 `ModelSettings` 參數與 `VocabularyName` 子參數。如需詳細資訊，請參閱[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html)及[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_ModelSettings.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_ModelSettings.html)。

```
aws transcribe start-transcription-job \
--region {{us-west-2}} \
--transcription-job-name {{my-first-transcription-job}} \
--media MediaFileUri=s3://{{amzn-s3-demo-bucket}}/{{my-input-files}}/{{my-media-file}}.{{flac}} \
--output-bucket-name {{amzn-s3-demo-bucket}} \
--output-key {{my-output-files}}/ \
--language-code {{en-US}} \
--model-settings LanguageModelName={{my-first-language-model}}
```

這是使用 [start-transcription-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/transcribe/start-transcription-job.html) 指令的另一個範例，以及包含該工作的自訂語言模型的請求主文。

```
aws transcribe start-transcription-job \
--region {{us-west-2}} \
--cli-input-json file://{{my-first-model-job}}.json
```

檔案 *my-first-model-job.json* 包含以下請求主文。

```
{
  "TranscriptionJobName": "{{my-first-transcription-job}}",
  "Media": {
        "MediaFileUri": "s3://{{amzn-s3-demo-bucket}}/{{my-input-files}}/{{my-media-file}}.{{flac}}"
  },
  "OutputBucketName": "{{amzn-s3-demo-bucket}}",
  "OutputKey": "{{my-output-files}}/", 
  "LanguageCode": "{{en-US}}",
  "ModelSettings": {
        "LanguageModelName": "{{my-first-language-model}}"
   }
}
```

### 適用於 Python (Boto3) 的 AWS SDK
<a name="model-using-python-batch"></a>

此範例使用 適用於 Python (Boto3) 的 AWS SDK 來包含使用 [start\_transcription\_job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/transcribe.html#TranscribeService.Client.start_transcription_job) 方法的 `ModelSettings` 引數的自訂語言模型。如需詳細資訊，請參閱[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html)及[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_ModelSettings.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_ModelSettings.html)。

如需使用 AWS SDKs 的其他範例，包括功能特定、案例和跨服務範例，請參閱 [Amazon Transcribe AWS SDKs的程式碼範例](service_code_examples.md)章節。

```
from __future__ import print_function
import time
import boto3
transcribe = boto3.client('transcribe', '{{us-west-2}}')
job_name = "{{my-first-transcription-job}}"
job_uri = "s3://{{amzn-s3-demo-bucket}}/{{my-input-files}}/{{my-media-file}}.{{flac}}"
transcribe.start_transcription_job(
    TranscriptionJobName = job_name,
    Media = {
        'MediaFileUri': job_uri
    },
    OutputBucketName = '{{amzn-s3-demo-bucket}}',
    OutputKey = '{{my-output-files}}/', 
    LanguageCode = '{{en-US}}', 
    ModelSettings = {
        'LanguageModelName': '{{my-first-language-model}}' 
   }
)

while True:
    status = transcribe.get_transcription_job(TranscriptionJobName = job_name)
    if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:
        break
    print("Not ready yet...")
    time.sleep(5)
print(status)
```

## 在串流轉錄中使用自訂語言模型
<a name="custom-language-models-using-stream"></a>

若要搭配串流轉錄使用自訂語言模型，請參閱下列範例：

### AWS 管理主控台
<a name="model-console-stream"></a>

1. 登入 [AWS 管理主控台](https://console.aws.amazon.com/transcribe/)。

1. 在導覽窗格中，選擇 **即時轉錄**。向下捲動至**自訂**，如果此欄位已最小化，請展開此欄位。  
![Amazon Transcribe 主控台螢幕擷取畫面：「即時轉錄」頁面。](http://docs.aws.amazon.com/zh_tw/transcribe/latest/dg/images/stream-main.png)

1. 開啟 **自訂語言模型**，然後從下拉式選單中選擇模型。  
![Amazon Transcribe 主控台螢幕擷取畫面：展開的「自訂」面板。](http://docs.aws.amazon.com/zh_tw/transcribe/latest/dg/images/model-stream2.png)

   加入您要套用至串流的其他設定。

1. 您現在已準備好轉錄串流。選擇**開始串流**並開始說話。若要結束聽寫，選擇**停止串流**。

### HTTP/2 串流
<a name="model-using-http2"></a>

此範例會建立包含您自訂語言模型的 HTTP/2 請求。如需搭配 使用 HTTP/2 串流的詳細資訊 Amazon Transcribe，請參閱 [設定 HTTP/2 串流](streaming-setting-up.md#streaming-http2)。如需特定參數和標頭的詳細資訊 Amazon Transcribe，請參閱 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_streaming_StartStreamTranscription.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_streaming_StartStreamTranscription.html)。

```
POST /stream-transcription HTTP/2
host: transcribestreaming.{{us-west-2}}.amazonaws.com
X-Amz-Target: com.amazonaws.transcribe.Transcribe.{{StartStreamTranscription}}
Content-Type: application/vnd.amazon.eventstream
X-Amz-Content-Sha256: {{string}}
X-Amz-Date: {{20220208}}T{{235959}}Z
Authorization: AWS4-HMAC-SHA256 Credential={{access-key}}/{{20220208}}/{{us-west-2}}/transcribe/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date;x-amz-target;x-amz-security-token, Signature={{string}}
x-amzn-transcribe-language-code: {{en-US}}
x-amzn-transcribe-media-encoding: {{flac}}
x-amzn-transcribe-sample-rate: {{16000}}      
x-amzn-transcribe-language-model-name: {{my-first-language-model}}
transfer-encoding: chunked
```

您可以在 [API 參考](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Reference.html)中找到參數定義；所有 AWS API 操作通用的參數都列在[通用參數](https://docs.aws.amazon.com/transcribe/latest/APIReference/CommonParameters.html)區段中。

### WebSocket 串流
<a name="model-using-websocket"></a>

此範例會建立預先簽署的 URL，將您的自訂語言模型套用至 WebSocket 串流。已加入分行符號以提高可讀性。如需搭配 使用 WebSocket 串流的詳細資訊 Amazon Transcribe，請參閱 [設定 WebSocket 串流](streaming-setting-up.md#streaming-websocket)。如需參數詳細資訊，請參閱 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_streaming_StartStreamTranscription.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_streaming_StartStreamTranscription.html)。

```
GET wss://transcribestreaming.{{us-west-2}}.amazonaws.com:8443/stream-transcription-websocket?
&X-Amz-Algorithm=AWS4-HMAC-SHA256
&X-Amz-Credential={{AKIAIOSFODNN7EXAMPLE}}%2F{{20220208}}%2F{{us-west-2}}%2F{{transcribe}}%2Faws4_request
&X-Amz-Date={{20220208}}T{{235959}}Z
&X-Amz-Expires={{300}}
&X-Amz-Security-Token={{security-token}}
&X-Amz-Signature={{string}}
&X-Amz-SignedHeaders=content-type%3Bhost%3Bx-amz-date
&language-code={{en-US}}
&media-encoding={{flac}}
&sample-rate={{16000}}    
&language-model-name={{my-first-language-model}}
```

您可以在 [API 參考](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Reference.html)中找到參數定義；所有 AWS API 操作通用的參數都列在[通用參數](https://docs.aws.amazon.com/transcribe/latest/APIReference/CommonParameters.html)區段中。