Skip to content

Commit

Permalink
Upload nlp
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi committed Mar 18, 2019
1 parent 6b29e12 commit 75bcf3c
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions demos/nlp/nlp-example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# nuclio: ignore\n",
"# if the nuclio-jupyter package is not installed run !pip install nuclio-jupyter\n",
"import nuclio "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: textblob in ./.pythonlibs/lib/python3.6/site-packages (0.15.3)\n",
"Requirement already satisfied: nltk>=3.1 in ./.pythonlibs/lib/python3.6/site-packages (from textblob) (3.4)\n",
"Requirement already satisfied: six in /conda/lib/python3.6/site-packages (from nltk>=3.1->textblob) (1.12.0)\n",
"Requirement already satisfied: singledispatch in ./.pythonlibs/lib/python3.6/site-packages (from nltk>=3.1->textblob) (3.4.0.3)\n",
"%nuclio: setting 'TO_LANG' environment variable\n"
]
}
],
"source": [
"%nuclio cmd pip install textblob\n",
"%nuclio env TO_LANG=fr"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from textblob import TextBlob\n",
"import os\n",
"\n",
"def handler(context, event):\n",
" context.logger.info('This is an NLP example! ')\n",
"\n",
" # process and correct the text\n",
" blob = TextBlob(str(event.body.decode('utf-8')))\n",
" corrected = blob.correct()\n",
"\n",
" # debug print the text before and after correction\n",
" context.logger.info_with(\"Corrected text\", corrected=str(corrected), orig=str(blob))\n",
"\n",
" # calculate sentiments\n",
" context.logger.info_with(\"Sentiment\",\n",
" polarity=str(corrected.sentiment.polarity),\n",
" subjectivity=str(corrected.sentiment.subjectivity))\n",
"\n",
" # read target language from environment and return translated text\n",
" lang = os.getenv('TO_LANG','fr')\n",
" return str(corrected.translate(to=lang))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python> 2019-03-14 11:33:43,830 [info] This is an NLP example! \n",
"Python> 2019-03-14 11:33:43,831 [info] Corrected text: {'corrected': 'good evening', 'orig': 'good evening'}\n",
"Python> 2019-03-14 11:33:43,833 [info] Sentiment: {'polarity': '0.7', 'subjectivity': '0.6000000000000001'}\n"
]
},
{
"data": {
"text/plain": [
"'Bonsoir'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# nuclio: ignore\n",
"event = nuclio.Event(body=b'good morninng')\n",
"handler(context, event)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"%nuclio: ['deploy', '-n', 'nlp', '-p', 'ai', '-c', '/User/nlp-example.ipynb']\n",
"%nuclio: [nuclio.deploy] 2019-03-14 11:32:56,340 (info) Building processor image\n",
"%nuclio: [nuclio.deploy] 2019-03-14 11:32:58,391 (info) Pushing image\n",
"%nuclio: [nuclio.deploy] 2019-03-14 11:32:58,391 (info) Build complete\n",
"%nuclio: [nuclio.deploy] 2019-03-14 11:33:02,434 (info) Function deploy complete\n",
"%nuclio: [nuclio.deploy] 2019-03-14 11:33:02,442 done updating nlp, function address: 3.122.204.208:32182\n",
"%nuclio: function deployed\n"
]
}
],
"source": [
"%nuclio deploy -n nlp -p ai -c"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bonne soir��e '"
]
}
],
"source": [
"!curl -X POST -d \"good evening\" 3.122.204.208:32182"
]
},
{
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 75bcf3c

Please sign in to comment.