{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Amazon Lex Demo" ] }, { "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", "***\n", "\n", "### Prerequisites:\n", "\n", "#### Identity and Acces Management\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 should apply the following managed policy to your user or role:\n", "\n", " AmazonLexFullAccess\n", "\n", "Be aware that we recommend you follow AWS IAM best practices for production implementations, which is out of scope for this workshop.\n", "\n", "#### Coffee Bot\n", "\n", "This demo makes use of a fictional Coffee Bot. Instructions for the creation of the bot can be found at https://github.com/awslabs/amz-ai-building-better-bots/blob/master/README.md. There is no need to follow the steps for the integration with Mobile Hub. \n", "\n", "Alternatively, use the code examples provided in the [Amazon Lex Create Bot Demo Notebook](./Lex_CreateBot_Demo.ipynb) to create the chat bot skeleton using code.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "import IPython\n", "import base64\n", "from pprint import pprint\n", "\n", "bot_name = 'CoffeeBot'\n", "bot_alias = '$LATEST'\n", "\n", "lex = boto3.client('lex-runtime', region_name='eu-west-1')\n", "OUTPUT_BLACKLIST = (\"ResponseMetadata\")\n", "\n", "def print_response(response):\n", " for i in response:\n", " if i not in OUTPUT_BLACKLIST:\n", " if i == 'slots':\n", " print ('slots:')\n", " for s in response[i]:\n", " print (\"-\", s,\":\", response[i][s])\n", " else: \n", " print (i,\":\", response[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### User Chris is ordering a coffee" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file = \"mocha.wav\"\n", "IPython.display.Audio(file)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "message = open(file, \"rb\")\n", "\n", "response = lex.post_content(\n", " botName=bot_name,\n", " botAlias=bot_alias,\n", " userId='Chris',\n", " contentType='audio/x-l16',\n", " accept='audio/*',\n", " inputStream=message\n", ")\n", " \n", "print_response(response)\n", " \n", "outfile = \"lexresponse.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": "markdown", "metadata": {}, "source": [ "### User Bob has some special requirements" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file = \"special.wav\"\n", "IPython.display.Audio(file)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "message = open(file, \"rb\")\n", "\n", "response = lex.post_content(\n", " botName=bot_name,\n", " botAlias=bot_alias,\n", " userId='Bob',\n", " contentType='audio/x-l16',\n", " accept='audio/*',\n", " inputStream=message\n", ")\n", "\n", "print_response(response)\n", " \n", "outfile = \"lexresponse.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": [ "file = \"no.wav\"\n", "message = open(file, \"rb\")\n", "\n", "response = lex.post_content(\n", " botName=bot_name,\n", " botAlias=bot_alias,\n", " userId='Bob',\n", " contentType='audio/x-l16',\n", " accept='audio/*',\n", " inputStream=message\n", ")\n", "\n", "print_response(response) \n", " \n", "outfile = \"lexresponse.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": "markdown", "metadata": {}, "source": [ "### Chris has made up his mind on the size coffee he likes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file = \"large.wav\"\n", "IPython.display.Audio(file)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "message = open(file, \"rb\")\n", "\n", "response = lex.post_content(\n", " botName=bot_name,\n", " botAlias=bot_alias,\n", " userId='Chris',\n", " contentType='audio/x-l16',\n", " accept='audio/*',\n", " inputStream=message\n", ")\n", "\n", "for i in response:\n", " if i not in OUTPUT_BLACKLIST:\n", " if i == 'slots':\n", " print ('slots:')\n", " for s in response[i]:\n", " print (\"-\", s,\":\", response[i][s])\n", " else: \n", " print (i,\":\", response[i])\n", " \n", "outfile = \"lexresponse.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": [ "file = \"no.wav\"\n", "message = open(file, \"rb\")\n", "\n", "response = lex.post_content(\n", " botName=bot_name,\n", " botAlias=bot_alias,\n", " userId='Chris',\n", " contentType='audio/x-l16',\n", " accept='audio/*',\n", " inputStream=message\n", ")\n", "\n", "print_response(response) \n", " \n", "outfile = \"lexresponse.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": [] } ], "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 }