Skip to content

Commit 75bcf3c

Browse files
author
Adi
committed
Upload nlp
1 parent 6b29e12 commit 75bcf3c

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

demos/nlp/nlp-example.ipynb

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# nuclio: ignore\n",
10+
"# if the nuclio-jupyter package is not installed run !pip install nuclio-jupyter\n",
11+
"import nuclio "
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"Requirement already satisfied: textblob in ./.pythonlibs/lib/python3.6/site-packages (0.15.3)\n",
24+
"Requirement already satisfied: nltk>=3.1 in ./.pythonlibs/lib/python3.6/site-packages (from textblob) (3.4)\n",
25+
"Requirement already satisfied: six in /conda/lib/python3.6/site-packages (from nltk>=3.1->textblob) (1.12.0)\n",
26+
"Requirement already satisfied: singledispatch in ./.pythonlibs/lib/python3.6/site-packages (from nltk>=3.1->textblob) (3.4.0.3)\n",
27+
"%nuclio: setting 'TO_LANG' environment variable\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"%nuclio cmd pip install textblob\n",
33+
"%nuclio env TO_LANG=fr"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 11,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"from textblob import TextBlob\n",
43+
"import os\n",
44+
"\n",
45+
"def handler(context, event):\n",
46+
" context.logger.info('This is an NLP example! ')\n",
47+
"\n",
48+
" # process and correct the text\n",
49+
" blob = TextBlob(str(event.body.decode('utf-8')))\n",
50+
" corrected = blob.correct()\n",
51+
"\n",
52+
" # debug print the text before and after correction\n",
53+
" context.logger.info_with(\"Corrected text\", corrected=str(corrected), orig=str(blob))\n",
54+
"\n",
55+
" # calculate sentiments\n",
56+
" context.logger.info_with(\"Sentiment\",\n",
57+
" polarity=str(corrected.sentiment.polarity),\n",
58+
" subjectivity=str(corrected.sentiment.subjectivity))\n",
59+
"\n",
60+
" # read target language from environment and return translated text\n",
61+
" lang = os.getenv('TO_LANG','fr')\n",
62+
" return str(corrected.translate(to=lang))"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 16,
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"Python> 2019-03-14 11:33:43,830 [info] This is an NLP example! \n",
75+
"Python> 2019-03-14 11:33:43,831 [info] Corrected text: {'corrected': 'good evening', 'orig': 'good evening'}\n",
76+
"Python> 2019-03-14 11:33:43,833 [info] Sentiment: {'polarity': '0.7', 'subjectivity': '0.6000000000000001'}\n"
77+
]
78+
},
79+
{
80+
"data": {
81+
"text/plain": [
82+
"'Bonsoir'"
83+
]
84+
},
85+
"execution_count": 16,
86+
"metadata": {},
87+
"output_type": "execute_result"
88+
}
89+
],
90+
"source": [
91+
"# nuclio: ignore\n",
92+
"event = nuclio.Event(body=b'good morninng')\n",
93+
"handler(context, event)"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 14,
99+
"metadata": {},
100+
"outputs": [
101+
{
102+
"name": "stdout",
103+
"output_type": "stream",
104+
"text": [
105+
"%nuclio: ['deploy', '-n', 'nlp', '-p', 'ai', '-c', '/User/nlp-example.ipynb']\n",
106+
"%nuclio: [nuclio.deploy] 2019-03-14 11:32:56,340 (info) Building processor image\n",
107+
"%nuclio: [nuclio.deploy] 2019-03-14 11:32:58,391 (info) Pushing image\n",
108+
"%nuclio: [nuclio.deploy] 2019-03-14 11:32:58,391 (info) Build complete\n",
109+
"%nuclio: [nuclio.deploy] 2019-03-14 11:33:02,434 (info) Function deploy complete\n",
110+
"%nuclio: [nuclio.deploy] 2019-03-14 11:33:02,442 done updating nlp, function address: 3.122.204.208:32182\n",
111+
"%nuclio: function deployed\n"
112+
]
113+
}
114+
],
115+
"source": [
116+
"%nuclio deploy -n nlp -p ai -c"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 15,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"bonne soir��e '"
129+
]
130+
}
131+
],
132+
"source": [
133+
"!curl -X POST -d \"good evening\" 3.122.204.208:32182"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": []
142+
}
143+
],
144+
"metadata": {
145+
"kernelspec": {
146+
"display_name": "Python 3",
147+
"language": "python",
148+
"name": "python3"
149+
},
150+
"language_info": {
151+
"codemirror_mode": {
152+
"name": "ipython",
153+
"version": 3
154+
},
155+
"file_extension": ".py",
156+
"mimetype": "text/x-python",
157+
"name": "python",
158+
"nbconvert_exporter": "python",
159+
"pygments_lexer": "ipython3",
160+
"version": "3.6.8"
161+
}
162+
},
163+
"nbformat": 4,
164+
"nbformat_minor": 2
165+
}

0 commit comments

Comments
 (0)