Last active
February 5, 2017 05:56
-
-
Save ranman/90d722c0c80ce56686cb333d264cd3da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# BabyLex Usage" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# [https://github.com/ranman/babylex](https://github.com/ranman/babylex)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{u'dialogState': u'Fulfilled',\n", | |
" u'intentName': u'WhereRandall',\n", | |
" u'message': u'Randall is in claremont, ca',\n", | |
" u'responseCard': None,\n", | |
" u'sessionAttributes': {},\n", | |
" u'slotToElicit': None,\n", | |
" u'slots': {}}" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lex_session = LexSession(\"WhereIsRandall\", \"$LATEST\", \"ranman\")\n", | |
"lex_session.text(\"Where is randall\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'x-amz-lex-message': 'Randall is in claremont, ca', 'x-amzn-requestid': 'b1547b1d-e3b8-11e6-916d-6fb777a9d4b2', 'x-amz-lex-dialog-state': 'Fulfilled', 'transfer-encoding': 'chunked', 'x-amz-lex-session-attributes': 'e30=', 'connection': 'keep-alive', 'x-amz-lex-slots': 'e30=', 'date': 'Thu, 26 Jan 2017 11:15:06 GMT', 'content-type': 'audio/pcm', 'x-amz-lex-intent-name': 'WhereRandall'}\n" | |
] | |
} | |
], | |
"source": [ | |
"import pyaudio\n", | |
"p = pyaudio.PyAudio()\n", | |
"lex_session = LexSession(\"WhereIsRandall\", \"$LATEST\", \"ranman\")\n", | |
"resp = lex_session.content(\"Where is Randall\", \"text/plain; charset=utf-8\", \"audio/pcm\")\n", | |
"print resp.headers\n", | |
"stream = p.open(format=p.get_format_from_width(width=2), channels=1, rate=16000, output=True)\n", | |
"stream.write(resp.content)\n", | |
"stream.stop_stream()\n", | |
"stream.close()\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# BabyLex" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"\n", | |
"import boto3\n", | |
"import base64\n", | |
"from botocore.auth import SigV4Auth\n", | |
"from botocore.awsrequest import AWSRequest\n", | |
"from botocore.vendored.requests import Session\n", | |
"\n", | |
"REGION = 'us-east-1'\n", | |
"SHA256_EMPTY_HASH = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'\n", | |
"\n", | |
"\n", | |
"class LexContentSigV4Auth(SigV4Auth):\n", | |
" def payload(self, request):\n", | |
" return SHA256_EMPTY_HASH\n", | |
"\n", | |
"\n", | |
"class LexSession:\n", | |
" LEX_URL = \"https://runtime.lex.{}.amazonaws.com/bot/{}/alias/{}/user/{}/\"\n", | |
"\n", | |
" def __init__(self, bot, alias, user, creds=None, region='us-east-1'):\n", | |
" if not creds:\n", | |
" self.creds = boto3.Session().get_credentials()\n", | |
" # region can be changed to refer to boto3.Session().region_name\n", | |
" self.region = region\n", | |
" self.url = LexSession.LEX_URL.format(region, bot, alias, user)\n", | |
" self.session = Session()\n", | |
"\n", | |
" def text(self, input_text, session_attributes=None):\n", | |
" \"\"\"Input text will be passed to your lex bot\"\"\"\n", | |
" url = self.url + 'text'\n", | |
" payload = json.dumps({\n", | |
" \"inputText\": input_text,\n", | |
" \"sessionAttributes\": session_attributes\n", | |
" })\n", | |
" request = AWSRequest(method=\"POST\", url=url, data=payload)\n", | |
" SigV4Auth(self.creds, 'lex', self.region).add_auth(request)\n", | |
"\n", | |
" return self.session.send(request.prepare()).json()\n", | |
"\n", | |
" def content(self, data, ctype, accept, session_attributes=None):\n", | |
" \"\"\"This will post any content to your lex bot\n", | |
"\n", | |
" Valid values for ctype and accept are found here:\n", | |
" http://docs.aws.amazon.com/lex/latest/dg/API_PostContent.html\"\"\"\n", | |
" url = self.url + 'content'\n", | |
" request = AWSRequest(method=\"POST\", url=url, data=data)\n", | |
" request.headers[\"accept\"] = accept\n", | |
" request.headers[\"content-type\"] = ctype\n", | |
" if session_attributes:\n", | |
" request.headers.add_header(\n", | |
" \"x-amz-lex-session-attributes\",\n", | |
" base64.b64encode(json.dumps(session_attributes))\n", | |
" )\n", | |
" LexContentSigV4Auth(self.creds, 'lex', self.region).add_auth(request)\n", | |
" prepared = request.prepare()\n", | |
" prepared.body = data\n", | |
"\n", | |
" return self.session.send(prepared)\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment