Skip to content

Commit 26be1ea

Browse files
authored
Merge pull request #1 from ankesh-kumar/flask_api
output as API in flask
2 parents 3622c75 + e4a3a09 commit 26be1ea

File tree

1 file changed

+199
-32
lines changed

1 file changed

+199
-32
lines changed

chatbotResponse.ipynb

Lines changed: 199 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,49 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
6-
"id": "economic-canadian",
5+
"execution_count": 1,
6+
"id": "worth-knock",
77
"metadata": {},
8-
"outputs": [],
8+
"outputs": [
9+
{
10+
"name": "stderr",
11+
"output_type": "stream",
12+
"text": [
13+
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
14+
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
15+
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
16+
"WARNING:root:Limited tf.summary API due to missing TensorBoard installation.\n",
17+
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
18+
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
19+
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n"
20+
]
21+
},
22+
{
23+
"name": "stdout",
24+
"output_type": "stream",
25+
"text": [
26+
"WARNING:tensorflow:From C:\\Users\\Ankesh Kumar\\anaconda3\\envs\\tfbase\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
27+
"Instructions for updating:\n",
28+
"non-resource variables are not supported in the long term\n"
29+
]
30+
},
31+
{
32+
"name": "stderr",
33+
"output_type": "stream",
34+
"text": [
35+
"WARNING:tensorflow:From C:\\Users\\Ankesh Kumar\\anaconda3\\envs\\tfbase\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
36+
"Instructions for updating:\n",
37+
"non-resource variables are not supported in the long term\n"
38+
]
39+
},
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"curses is not supported on this machine (please install/reinstall curses for an optimal experience)\n"
45+
]
46+
}
47+
],
948
"source": [
1049
"import nltk\n",
1150
"from nltk.stem.lancaster import LancasterStemmer\n",
@@ -21,7 +60,7 @@
2160
{
2261
"cell_type": "code",
2362
"execution_count": 2,
24-
"id": "broadband-country",
63+
"id": "greater-keeping",
2564
"metadata": {},
2665
"outputs": [],
2766
"source": [
@@ -41,10 +80,29 @@
4180
},
4281
{
4382
"cell_type": "code",
44-
"execution_count": null,
45-
"id": "ruled-seating",
83+
"execution_count": 3,
84+
"id": "liberal-valve",
4685
"metadata": {},
47-
"outputs": [],
86+
"outputs": [
87+
{
88+
"name": "stdout",
89+
"output_type": "stream",
90+
"text": [
91+
"WARNING:tensorflow:From C:\\Users\\Ankesh Kumar\\anaconda3\\envs\\tfbase\\lib\\site-packages\\tflearn\\initializations.py:164: calling TruncatedNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
92+
"Instructions for updating:\n",
93+
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
94+
]
95+
},
96+
{
97+
"name": "stderr",
98+
"output_type": "stream",
99+
"text": [
100+
"WARNING:tensorflow:From C:\\Users\\Ankesh Kumar\\anaconda3\\envs\\tfbase\\lib\\site-packages\\tflearn\\initializations.py:164: calling TruncatedNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
101+
"Instructions for updating:\n",
102+
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
103+
]
104+
}
105+
],
48106
"source": [
49107
"# Build neural network\n",
50108
"net = tflearn.input_data(shape=[None, len(train_x[0])])\n",
@@ -60,7 +118,7 @@
60118
{
61119
"cell_type": "code",
62120
"execution_count": 4,
63-
"id": "empirical-palace",
121+
"id": "fabulous-herald",
64122
"metadata": {},
65123
"outputs": [],
66124
"source": [
@@ -89,8 +147,8 @@
89147
},
90148
{
91149
"cell_type": "code",
92-
"execution_count": 7,
93-
"id": "naval-criticism",
150+
"execution_count": 5,
151+
"id": "typical-rental",
94152
"metadata": {},
95153
"outputs": [
96154
{
@@ -111,18 +169,33 @@
111169
},
112170
{
113171
"cell_type": "code",
114-
"execution_count": null,
115-
"id": "practical-internet",
172+
"execution_count": 6,
173+
"id": "headed-marks",
116174
"metadata": {},
117-
"outputs": [],
175+
"outputs": [
176+
{
177+
"name": "stdout",
178+
"output_type": "stream",
179+
"text": [
180+
"INFO:tensorflow:Restoring parameters from C:\\Users\\Ankesh Kumar\\UtilProject\\model.tflearn\n"
181+
]
182+
},
183+
{
184+
"name": "stderr",
185+
"output_type": "stream",
186+
"text": [
187+
"INFO:tensorflow:Restoring parameters from C:\\Users\\Ankesh Kumar\\UtilProject\\model.tflearn\n"
188+
]
189+
}
190+
],
118191
"source": [
119192
"model.load('./model.tflearn')"
120193
]
121194
},
122195
{
123196
"cell_type": "code",
124-
"execution_count": 9,
125-
"id": "close-mortality",
197+
"execution_count": 7,
198+
"id": "suitable-fitness",
126199
"metadata": {},
127200
"outputs": [],
128201
"source": [
@@ -168,17 +241,17 @@
168241
},
169242
{
170243
"cell_type": "code",
171-
"execution_count": 10,
172-
"id": "original-fifth",
244+
"execution_count": 8,
245+
"id": "innovative-auction",
173246
"metadata": {},
174247
"outputs": [
175248
{
176249
"data": {
177250
"text/plain": [
178-
"[('opentoday', 0.9936719)]"
251+
"[('opentoday', 0.7488097), ('today', 0.25112572)]"
179252
]
180253
},
181-
"execution_count": 10,
254+
"execution_count": 8,
182255
"metadata": {},
183256
"output_type": "execute_result"
184257
}
@@ -189,8 +262,8 @@
189262
},
190263
{
191264
"cell_type": "code",
192-
"execution_count": 11,
193-
"id": "binary-richards",
265+
"execution_count": 9,
266+
"id": "boring-cooper",
194267
"metadata": {},
195268
"outputs": [
196269
{
@@ -207,15 +280,15 @@
207280
},
208281
{
209282
"cell_type": "code",
210-
"execution_count": 12,
211-
"id": "focal-publisher",
283+
"execution_count": 10,
284+
"id": "pursuant-hamilton",
212285
"metadata": {},
213286
"outputs": [
214287
{
215288
"name": "stdout",
216289
"output_type": "stream",
217290
"text": [
218-
"See you later, thanks for visiting\n"
291+
"Bye! Come back again soon.\n"
219292
]
220293
}
221294
],
@@ -225,8 +298,8 @@
225298
},
226299
{
227300
"cell_type": "code",
228-
"execution_count": 13,
229-
"id": "serial-seeker",
301+
"execution_count": 11,
302+
"id": "exempt-proxy",
230303
"metadata": {},
231304
"outputs": [
232305
{
@@ -235,7 +308,7 @@
235308
"{}"
236309
]
237310
},
238-
"execution_count": 13,
311+
"execution_count": 11,
239312
"metadata": {},
240313
"output_type": "execute_result"
241314
}
@@ -246,8 +319,8 @@
246319
},
247320
{
248321
"cell_type": "code",
249-
"execution_count": 14,
250-
"id": "activated-ridge",
322+
"execution_count": 12,
323+
"id": "whole-politics",
251324
"metadata": {},
252325
"outputs": [
253326
{
@@ -264,21 +337,115 @@
264337
},
265338
{
266339
"cell_type": "code",
267-
"execution_count": 20,
268-
"id": "appropriate-atlantic",
340+
"execution_count": 13,
341+
"id": "collectible-occasions",
269342
"metadata": {},
270343
"outputs": [
271344
{
272345
"name": "stdout",
273346
"output_type": "stream",
274347
"text": [
275-
"My pleasure\n"
348+
"Any time!\n"
276349
]
277350
}
278351
],
279352
"source": [
280353
"response(\"thanks, your great\")"
281354
]
355+
},
356+
{
357+
"cell_type": "code",
358+
"execution_count": null,
359+
"id": "molecular-queen",
360+
"metadata": {},
361+
"outputs": [
362+
{
363+
"name": "stdout",
364+
"output_type": "stream",
365+
"text": [
366+
" * Serving Flask app \"__main__\" (lazy loading)\n",
367+
" * Environment: production\n",
368+
" WARNING: This is a development server. Do not use it in a production deployment.\n",
369+
" Use a production WSGI server instead.\n",
370+
" * Debug mode: off\n"
371+
]
372+
},
373+
{
374+
"name": "stderr",
375+
"output_type": "stream",
376+
"text": [
377+
"INFO:werkzeug: * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)\n"
378+
]
379+
}
380+
],
381+
"source": [
382+
"from flask import Flask, jsonify\n",
383+
"from flask import request \n",
384+
"from flask_cors import CORS, cross_origin\n",
385+
"\n",
386+
"app = Flask(__name__)\n",
387+
"\n",
388+
"CORS(app)\n",
389+
"\n",
390+
"@app.route(\"/api\", methods=['GET'])\n",
391+
"def classifydone():\n",
392+
" ERROR_THRESHOLD = 0.25\n",
393+
" \n",
394+
" #sentence = request.json['sentence']\n",
395+
" sentence = 'is your shop open today?'\n",
396+
" userID='123'\n",
397+
" show_details=False\n",
398+
" # generate probabilities from the model\n",
399+
" results = classify(sentence)\n",
400+
" # if we have a classification then find the matching intent tag\n",
401+
" if results:\n",
402+
" # loop as long as there are matches to process\n",
403+
" while results:\n",
404+
" for i in intents['intents']:\n",
405+
" # find a tag matching the first result\n",
406+
" if i['tag'] == results[0][0]:\n",
407+
" # set context for this intent if necessary\n",
408+
" if 'context_set' in i:\n",
409+
" if show_details: print ('context:', i['context_set'])\n",
410+
" context[userID] = i['context_set']\n",
411+
"\n",
412+
" # check if this intent is contextual and applies to this user's conversation\n",
413+
" if not 'context_filter' in i or \\\n",
414+
" (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]):\n",
415+
" if show_details: print ('tag:', i['tag'])\n",
416+
" # a random response from the intent\n",
417+
" return jsonify({'task': random.choice(i['responses'])})\n",
418+
"\n",
419+
" \n",
420+
"\n",
421+
"# def classify_local(sentence):\n",
422+
"# ERROR_THRESHOLD = 0.25\n",
423+
" \n",
424+
"# # generate probabilities from the model\n",
425+
"# results = model.predict([bow(sentence, words)])[0]\n",
426+
"# # filter out predictions below a threshold, and provide intent index\n",
427+
"# results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]\n",
428+
"# # sort by strength of probability\n",
429+
"# results.sort(key=lambda x: x[1], reverse=True)\n",
430+
"# return_list = []\n",
431+
"# for r in results:\n",
432+
"# return_list.append((classes[r[0]], str(r[1])))\n",
433+
"# # return tuple of intent and probability\n",
434+
" \n",
435+
"# return return_list\n",
436+
"\n",
437+
"# running REST interface\n",
438+
"if __name__ == \"__main__\":\n",
439+
" app.run(host='0.0.0.0')"
440+
]
441+
},
442+
{
443+
"cell_type": "code",
444+
"execution_count": null,
445+
"id": "french-scheme",
446+
"metadata": {},
447+
"outputs": [],
448+
"source": []
282449
}
283450
],
284451
"metadata": {

0 commit comments

Comments
 (0)