{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Amazon Polly Demo\n",
"\n",
"### Convert text to speech with Amazon Polly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***\n",
"Copyright [2017]-[2017] Amazon.com, Inc. or its affiliates. All Rights Reserved.\n",
"\n",
"Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at\n",
"\n",
"http://aws.amazon.com/apache2.0/\n",
"\n",
"or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n",
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Prerequisites:**\n",
"\n",
"The user or role that executes the commands must have permissions in AWS Identity and Access Management (IAM) to perform those actions. AWS provides a set of managed policies that help you get started quickly. For our example, you need to apply the following minimum managed policies to your user or role:\n",
"\n",
"* AmazonPollyFullAccess \n",
"\n",
"Be aware that we recommend you follow AWS IAM best practices for production implementations, which is out of scope fof this workshop."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import boto3\n",
"import IPython\n",
"from pprint import pprint\n",
"\n",
"polly = boto3.client('polly', region_name='eu-west-1')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = polly.synthesize_speech(\n",
" Text=\"It is great to see you today!\",\n",
" TextType=\"text\",\n",
" OutputFormat=\"mp3\", \n",
" VoiceId=\"Emma\")\n",
"\n",
"pprint (response)\n",
" \n",
"outfile = \"pollyresponse.mp3\"\n",
"data = response['AudioStream'].read()\n",
"\n",
"with open(outfile,'wb') as f:\n",
" f.write(data)\n",
"IPython.display.Audio(outfile) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = polly.synthesize_speech(\n",
" Text='I am fine, thank you. \\\n",
" What can I do for you?',\n",
" TextType=\"ssml\",\n",
" OutputFormat=\"mp3\", \n",
" VoiceId=\"Emma\")\n",
" \n",
"outfile = \"pollyresponse.mp3\"\n",
"data = response['AudioStream'].read()\n",
"\n",
"with open(outfile,'wb') as f:\n",
" f.write(data)\n",
"IPython.display.Audio(outfile) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# read (r\\id) vs read (r\\Ed)\n",
"response = polly.synthesize_speech(\n",
" Text=\"Iread this book\",\n",
" TextType=\"ssml\",\n",
" OutputFormat=\"mp3\", \n",
" VoiceId=\"Emma\")\n",
" \n",
"outfile = \"pollyresponse.mp3\"\n",
"data = response['AudioStream'].read()\n",
"\n",
"with open(outfile,'wb') as f:\n",
" f.write(data)\n",
"IPython.display.Audio(outfile) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = polly.synthesize_speech(\n",
" Text='My favorite chemical element is Al, \\\n",
" but Al prefers Mg.',\n",
" TextType=\"ssml\",\n",
" OutputFormat=\"mp3\", \n",
" VoiceId=\"Brian\")\n",
" \n",
"outfile = \"pollyresponse.mp3\"\n",
"data = response['AudioStream'].read()\n",
"\n",
"with open(outfile,'wb') as f:\n",
" f.write(data)\n",
"IPython.display.Audio(outfile) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = polly.synthesize_speech(\n",
" Text='My favorite chemical element is Mg',\n",
" TextType=\"text\",\n",
" OutputFormat=\"mp3\", \n",
" VoiceId=\"Brian\",\n",
" LexiconNames=[\"PollyPSE\"]\n",
" )\n",
" \n",
"outfile = \"pollyresponse.mp3\"\n",
"data = response['AudioStream'].read()\n",
"\n",
"with open(outfile,'wb') as f:\n",
" f.write(data)\n",
"IPython.display.Audio(outfile) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = polly.get_lexicon(\n",
" Name=\"PollyPSE\")\n",
"\n",
"xmlret = response['Lexicon']['Content']\n",
" \n",
"print (xmlret)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = polly.synthesize_speech(\n",
" Text=\"Beim sächsisch besiegen die weichen die harten.\",\n",
" TextType=\"ssml\",\n",
" OutputFormat=\"mp3\", \n",
" VoiceId=\"Hans\"\n",
" )\n",
" \n",
"outfile = \"pollyresponse.mp3\"\n",
"data = response['AudioStream'].read()\n",
"\n",
"with open(outfile,'wb') as f:\n",
" f.write(data)\n",
"IPython.display.Audio(outfile) "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}