{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "DEFAULT_BASE_URL = \"http://pipeline:8080/%s\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import os.path\n", "import requests\n", "from urllib.parse import urlencode\n", "import json\n", "# things we need for NLP\n", "import nltk\n", "from nltk.stem.lancaster import LancasterStemmer\n", "stemmer = LancasterStemmer()\n", "import tensorflow.python.framework.dtypes\n", "# things we need for Tensorflow\n", "import numpy as np\n", "import tensorflow as tf\n", "import random\n", "\n", "from flask import Flask, jsonify, request\n", "from flask_cors import CORS, cross_origin\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# restore all of our data structures\n", "import pickle\n", "data = pickle.load( open( \"training_data.parquet\", \"rb\" ) )\n", "# data = pd.read_parquet(os.path.join(\"data\", \"training_data.parquet\"))\n", "words = data['words']\n", "classes = data['classes']\n", "train_x = data['train_x']\n", "train_y = data['train_y']\n", "\n", "# import our chat-bot intents file\n", "import json\n", "with open('intents.json') as json_data:\n", " intents = json.load(json_data)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "epochs = 1000\n", "batch_size = 8\n", "\n", "# Build neural network\n", "model = tf.keras.Sequential([\n", " tf.keras.layers.Dense(8, input_shape=(len(train_x[0]),)),\n", " tf.keras.layers.Dense(8),\n", " tf.keras.layers.Dense(len(train_y[0]), activation=\"softmax\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def clean_up_sentence(sentence):\n", " # tokenize the pattern\n", " sentence_words = nltk.word_tokenize(sentence)\n", " # stem each word\n", " sentence_words = [stemmer.stem(word.lower()) for word in sentence_words]\n", " return sentence_words\n", "\n", "# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence\n", "def bow(sentence, words, show_details=False):\n", " # tokenize the pattern\n", " sentence_words = clean_up_sentence(sentence)\n", " # bag of words\n", " bag = [0]*len(words) \n", " for s in sentence_words:\n", " for i,w in enumerate(words):\n", " if w == s: \n", " bag[i] = 1\n", " if show_details:\n", " print (\"found in bag: %s\" % w)\n", "\n", " return(np.array(bag))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0\n", " 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", " 0 0 0 0 0 0 0 0]\n", "['adverse_drug', 'blood_pressure', 'blood_pressure_search', 'goodbye', 'greeting', 'hospital_search', 'options', 'pharmacy_search', 'thanks']\n" ] } ], "source": [ "p = bow(\"looking for help\", words)\n", "print (p)\n", "print (classes)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# create a data structure to hold user context\n", "context = {}\n", "\n", "ERROR_THRESHOLD = 0.25\n", "userid =123\n", "\n", "\n", "def score_chat(text, url = None):\n", " d = np.array([bow(sentence, words)])\n", " payload = urlencode({\"json_args\" : json.dumps(d)})\n", " headers = {'content-type': 'application/x-www-form-urlencoded'}\n", " response = requests.request(\"POST\", url, data=payload, headers=headers)\n", " try:\n", " return json.loads(response.text)\n", " except BaseException as e:\n", " raise RuntimeError(\"Error: caught %r while processing %r (%r)\" % (e, response, response.text))\n", "\n", "\n", "# @app.route(\"/redsam/api/v1.0/classify\", methods=['POST'])\n", "def classify(sentence):\n", " # generate probabilities from the model\n", " \n", " results = score_chat(sentence)[0]\n", " # filter out predictions below a threshold\n", " results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]\n", " # sort by strength of probability\n", " results.sort(key=lambda x: x[1], reverse=True)\n", " return_list = []\n", " for r in results:\n", " return_list.append((classes[r[0]], r[1]))\n", " # return tuple of intent and probability\n", " return return_list\n", "\n", "def response(sentence, userID=userid, show_details=False):\n", " results = classify(sentence)\n", " # if we have a classification then find the matching intent tag\n", " if results:\n", " # loop as long as there are matches to process\n", " while results:\n", " for i in intents['intents']:\n", " # find a tag matching the first result\n", " if i['tag'] == results[0][0]:\n", " # set context for this intent if necessary\n", " if 'context_set' in i:\n", " if show_details: print ('context:', i['context_set'])\n", " context[userID] = i['context_set']\n", "\n", " # check if this intent is contextual and applies to this user's conversation\n", " if not 'context_filter' in i or \\\n", " (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]):\n", " if show_details: print ('tag:', i['tag'])\n", " # a random response from the intent\n", " print(random.choice(i['responses']))\n", " return\n", "\n", " results.pop(0)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello I don't know how to score the args (['what help you offer?']) you supplied\n" ] }, { "data": { "text/plain": [ "\"I don't know how to score the args (['what help you offer?']) you supplied\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "score_chat(\"what help you offer?\")" ] }, { "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }