Spaces:
Sleeping
Sleeping
CaesarCloudSync commited on
Commit ·
baa1558
0
Parent(s):
CaesarAINL Deployed Maybe
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +42 -0
- DockerFile +14 -0
- Procfile +1 -0
- README.md +8 -0
- __pycache__/caesarapis.cpython-39.pyc +0 -0
- __pycache__/caesarinfer.cpython-38.pyc +0 -0
- __pycache__/caesarinfer.cpython-39.pyc +0 -0
- app.py +29 -0
- bert tutorial/caesarbert.py +476 -0
- bert tutorial/intent_classification_with_bert.ipynb +1239 -0
- caesar_tensorflow_install.md +12 -0
- caesarapis.py +26 -0
- caesarberthubmodels/bert_to_handle.json +3 -0
- caesarberthubmodels/bert_to_preprocess.json +3 -0
- caesarcomplete/berttest.py +51 -0
- caesarcomplete/caesar_tensorflow_install.md +12 -0
- caesarcomplete/caesarapis.py +17 -0
- caesarcomplete/caesarnlexamples.py +69 -0
- caesarcomplete/data_aggregation.ipynb +493 -0
- caesarinfer.py +69 -0
- caesarintro.mp3 +0 -0
- caesarmodel/caesarnl/assets/vocab.txt +3 -0
- caesarmodel/caesarnl/keras_metadata.pb +3 -0
- caesarmodel/caesarnl/saved_model.pb +3 -0
- caesarmodel/caesarnl/variables/variables.data-00000-of-00001 +3 -0
- caesarmodel/caesarnl/variables/variables.index +3 -0
- caesarmodel/labelbinarizer.pkl +3 -0
- caesarnl.py +70 -0
- caesarnlexamples.py +69 -0
- caesarnlrasp.py +70 -0
- caesartrain.py +224 -0
- caesartrainperformance/.png +0 -0
- caesartrainperformance/history.png +0 -0
- data_aggregation.ipynb +277 -0
- glove/glove.6B.300d.txt +3 -0
- glove/glove.6B.300d_should_be_here.txt +0 -0
- intentdata/Intent.json +3 -0
- intentdata/Small_talk_Intent.csv +3 -0
- intentdata/aug/intent_aug.json +3 -0
- intentdata/aug/intent_aug_text.json +3 -0
- intentdata/intent_aug_text_test.json +3 -0
- intentdata/responses.json +3 -0
- intentdata/test.csv +3 -0
- intentdata/train.csv +3 -0
- intentdata/train_prev/train copy.csv +3 -0
- intentdata/train_prev/train copy_recent.csv +3 -0
- intentdata/train_prev/train_command.csv +3 -0
- intentdata/train_prev/train_no_half_response.csv +3 -0
- intentdata/valid.csv +3 -0
- main.py +55 -0
.gitattributes
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
glove.6B.300d_should_be_here.txt filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
glove.6B.300d.txt filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.csv filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
*.index filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
vocab.txt filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
caesarintro.mp3 filter=lfs diff=lfs merge=lfs -text
|
DockerFile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM tensorflow/tensorflow:latest-gpu-jupyter
|
| 2 |
+
|
| 3 |
+
#VOLUME CaesarAINL CaesarAINL
|
| 4 |
+
WORKDIR /CaesarAINL
|
| 5 |
+
|
| 6 |
+
ADD CaesarAINL /CaesarAINL
|
| 7 |
+
|
| 8 |
+
RUN pip install flask flask_cors tensorflow_hub tensorflow_text scikit-learn tqdm matplotlib gunicorn
|
| 9 |
+
|
| 10 |
+
EXPOSE 5000
|
| 11 |
+
#80
|
| 12 |
+
|
| 13 |
+
ENTRYPOINT [ "python" ]
|
| 14 |
+
CMD ["app.py"]
|
Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: gunicorn app:app
|
README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: CaesarAINL
|
| 3 |
+
emoji: 🏢
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
__pycache__/caesarapis.cpython-39.pyc
ADDED
|
Binary file (1.08 kB). View file
|
|
|
__pycache__/caesarinfer.cpython-38.pyc
ADDED
|
Binary file (1.58 kB). View file
|
|
|
__pycache__/caesarinfer.cpython-39.pyc
ADDED
|
Binary file (1.63 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask,request
|
| 2 |
+
from flask_cors import cross_origin
|
| 3 |
+
import os
|
| 4 |
+
from caesarinfer import CaesarNL
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
@app.route("/",methods=["GET"])
|
| 8 |
+
@cross_origin()
|
| 9 |
+
def caesarhome():
|
| 10 |
+
return "Caeser: How can I help you sir?"
|
| 11 |
+
|
| 12 |
+
@app.route("/caesarapi",methods=["POST","GET"])
|
| 13 |
+
@cross_origin()
|
| 14 |
+
def caesarapi():
|
| 15 |
+
if request.method == "GET":
|
| 16 |
+
return "Caeser: Hello sir, this is the CaesarAIAPI"
|
| 17 |
+
elif request.method == "POST":
|
| 18 |
+
user_input_json = request.get_json()
|
| 19 |
+
|
| 20 |
+
print("Caesar Processing...")
|
| 21 |
+
caesarResponse,intents = CaesarNL.run([user_input_json["caesarapi"]])
|
| 22 |
+
print("Caesar Processed.")
|
| 23 |
+
print(caesarResponse,"intent:",intents)
|
| 24 |
+
|
| 25 |
+
return {"caesarmessage":{"caesarResponse":caesarResponse,"intent":intents}}
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
port = int(os.environ.get('PORT', 5000)) # 80
|
| 29 |
+
app.run(debug=True,host="0.0.0.0",port=port) #
|
bert tutorial/caesarbert.py
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""intent_classification_with_bert.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1gTNCoFmqJslnlc3lSbOTqsBz_UjJarS5
|
| 8 |
+
|
| 9 |
+
# Intent Classification with BERT
|
| 10 |
+
|
| 11 |
+
This notebook demonstrates the fine-tuning of BERT to perform intent classification.
|
| 12 |
+
Intent classification tries to map given instructions (sentence in natural language) to a set of predefined intents.
|
| 13 |
+
|
| 14 |
+
## What you will learn
|
| 15 |
+
|
| 16 |
+
- Load data from csv and preprocess it for training and test
|
| 17 |
+
- Load a BERT model from TensorFlow Hub
|
| 18 |
+
- Build your own model by combining BERT with a classifier
|
| 19 |
+
- Train your own model, fine-tuning BERT as part of that
|
| 20 |
+
- Save your model and use it to recognize the intend of instructions
|
| 21 |
+
|
| 22 |
+
## About BERT
|
| 23 |
+
|
| 24 |
+
[BERT](https://arxiv.org/abs/1810.04805) and other Transformer encoder architectures have been shown to be successful on a variety of tasks in NLP (natural language processing). They compute vector-space representations of natural language that are suitable for use in deep learning models. The BERT family of models uses the Transformer encoder architecture to process each token of input text in the full context of all tokens before and after, hence the name: Bidirectional Encoder Representations from Transformers.
|
| 25 |
+
|
| 26 |
+
BERT models are usually pre-trained on a large corpus of text, then fine-tuned for specific tasks.
|
| 27 |
+
|
| 28 |
+
## Setup
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
import os
|
| 32 |
+
#import shutil
|
| 33 |
+
import pandas as pd
|
| 34 |
+
|
| 35 |
+
import tensorflow as tf
|
| 36 |
+
import tensorflow_hub as hub
|
| 37 |
+
import tensorflow_text as text
|
| 38 |
+
import seaborn as sns
|
| 39 |
+
from pylab import rcParams
|
| 40 |
+
|
| 41 |
+
import matplotlib.pyplot as plt
|
| 42 |
+
tf.get_logger().setLevel('ERROR')
|
| 43 |
+
|
| 44 |
+
sns.set(style='whitegrid', palette='muted', font_scale=1.2)
|
| 45 |
+
HAPPY_COLORS_PALETTE = ["#01BEFE", "#FFDD00", "#FF7D00", "#FF006D", "#ADFF02", "#8F00FF"]
|
| 46 |
+
sns.set_palette(sns.color_palette(HAPPY_COLORS_PALETTE))
|
| 47 |
+
rcParams['figure.figsize'] = 12, 8
|
| 48 |
+
import warnings
|
| 49 |
+
warnings.filterwarnings("ignore")
|
| 50 |
+
|
| 51 |
+
"""## Data Access
|
| 52 |
+
The data contains various user queries categorized into seven intents. It is hosted on [GitHub](https://github.com/snipsco/nlu-benchmark/tree/master/2017-06-custom-intent-engines) and is first presented in [this paper](https://arxiv.org/abs/1805.10190). In the list below the classes and an example for each class is given:
|
| 53 |
+
|
| 54 |
+
* `class`: SearchCreativeWork - `example`:*play hell house song*
|
| 55 |
+
* `class`: GetWeather - `example`: *is it windy in boston, mb right now*
|
| 56 |
+
* `class`: BookRestaurant - `example`: *book a restaurant for eight people in six years*
|
| 57 |
+
* `class`: PlayMusic - `example`: *play the song little robin redbreast*
|
| 58 |
+
* `class`: AddToPlaylist - `example`: *add step to me to the 50 clásicos playlist*
|
| 59 |
+
* `class`: RateBook - `example`: *give 6 stars to of mice and men*
|
| 60 |
+
* `class`: SearchScreeningEvent - `example` : *find fish story*
|
| 61 |
+
|
| 62 |
+
Data can be downloaded from a Google Drive by applying [gdown](https://pypi.org/project/gdown/). In the following code cells the download is invoked only if the corresponding file, does not yet exist at the corresponding location.
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
datafolder=""
|
| 66 |
+
|
| 67 |
+
trainfile=datafolder+"train.csv"
|
| 68 |
+
testfile=datafolder+"test.csv"
|
| 69 |
+
validfile=datafolder+"valid.csv"
|
| 70 |
+
|
| 71 |
+
"""Next, the downloaded .csv-files for training, validation and test are imported into pandas dataframes:"""
|
| 72 |
+
|
| 73 |
+
traindf = pd.read_csv(trainfile)
|
| 74 |
+
validdf = pd.read_csv(validfile)
|
| 75 |
+
testdf = pd.read_csv(testfile)
|
| 76 |
+
|
| 77 |
+
traindf.head()
|
| 78 |
+
|
| 79 |
+
"""Training data contains 13084 instructions:"""
|
| 80 |
+
|
| 81 |
+
traindf.shape
|
| 82 |
+
|
| 83 |
+
trainfeatures=traindf.copy()
|
| 84 |
+
trainlabels=trainfeatures.pop("intent")
|
| 85 |
+
|
| 86 |
+
trainfeatures=trainfeatures.values
|
| 87 |
+
|
| 88 |
+
"""Distribution of class-labels in training-data:"""
|
| 89 |
+
|
| 90 |
+
#chart = sns.countplot(trainlabels, palette=HAPPY_COLORS_PALETTE)
|
| 91 |
+
#plt.title("Number of texts per intent")
|
| 92 |
+
#chart
|
| 93 |
+
#`chart.set_xticklabels(chart.get_xticklabels(), rotation=30, horizontalalignment='right')
|
| 94 |
+
|
| 95 |
+
"""One-Hot-Encoding of class-labels:"""
|
| 96 |
+
|
| 97 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 98 |
+
|
| 99 |
+
binarizer=LabelBinarizer()
|
| 100 |
+
trainlabels=binarizer.fit_transform(trainlabels.values)
|
| 101 |
+
|
| 102 |
+
trainlabels.shape
|
| 103 |
+
|
| 104 |
+
"""Preprocess test- and validation data in the same way as it has been done for training-data:"""
|
| 105 |
+
|
| 106 |
+
testfeatures=testdf.copy()
|
| 107 |
+
testlabels=testfeatures.pop("intent")
|
| 108 |
+
validfeatures=validdf.copy()
|
| 109 |
+
validlabels=validfeatures.pop("intent")
|
| 110 |
+
|
| 111 |
+
testfeatures=testfeatures.values
|
| 112 |
+
validfeatures=validfeatures.values
|
| 113 |
+
|
| 114 |
+
testlabels=binarizer.transform(testlabels.values)
|
| 115 |
+
validlabels=binarizer.transform(validlabels.values)
|
| 116 |
+
|
| 117 |
+
"""## Loading models from TensorFlow Hub
|
| 118 |
+
|
| 119 |
+
Here you can choose which BERT model you will load from TensorFlow Hub and fine-tune. There are multiple BERT models available.
|
| 120 |
+
|
| 121 |
+
- [BERT-Base](https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3), [Uncased](https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3) and [seven more models](https://tfhub.dev/google/collections/bert/1) with trained weights released by the original BERT authors.
|
| 122 |
+
- [Small BERTs](https://tfhub.dev/google/collections/bert/1) have the same general architecture but fewer and/or smaller Transformer blocks, which lets you explore tradeoffs between speed, size and quality.
|
| 123 |
+
- [ALBERT](https://tfhub.dev/google/collections/albert/1): four different sizes of "A Lite BERT" that reduces model size (but not computation time) by sharing parameters between layers.
|
| 124 |
+
- [BERT Experts](https://tfhub.dev/google/collections/experts/bert/1): eight models that all have the BERT-base architecture but offer a choice between different pre-training domains, to align more closely with the target task.
|
| 125 |
+
- [Electra](https://tfhub.dev/google/collections/electra/1) has the same architecture as BERT (in three different sizes), but gets pre-trained as a discriminator in a set-up that resembles a Generative Adversarial Network (GAN).
|
| 126 |
+
- BERT with Talking-Heads Attention and Gated GELU [[base](https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_base/1), [large](https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_large/1)] has two improvements to the core of the Transformer architecture.
|
| 127 |
+
|
| 128 |
+
The model documentation on TensorFlow Hub has more details and references to the
|
| 129 |
+
research literature. Follow the links above, or click on the [`tfhub.dev`](http://tfhub.dev) URL
|
| 130 |
+
printed after the next cell execution.
|
| 131 |
+
|
| 132 |
+
The suggestion is to start with a Small BERT (with fewer parameters) since they are faster to fine-tune. If you like a small model but with higher accuracy, ALBERT might be your next option. If you want even better accuracy, choose
|
| 133 |
+
one of the classic BERT sizes or their recent refinements like Electra, Talking Heads, or a BERT Expert.
|
| 134 |
+
|
| 135 |
+
Aside from the models available below, there are [multiple versions](https://tfhub.dev/google/collections/transformer_encoders_text/1) of the models that are larger and can yield even better accuracy but they are too big to be fine-tuned on a single GPU. You will be able to do that on the [Solve GLUE tasks using BERT on a TPU colab](https://www.tensorflow.org/tutorials/text/solve_glue_tasks_using_bert_on_tpu).
|
| 136 |
+
|
| 137 |
+
You'll see in the code below that switching the tfhub.dev URL is enough to try any of these models, because all the differences between them are encapsulated in the SavedModels from TF Hub.
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
+
bert_model_name = 'small_bert/bert_en_uncased_L-8_H-512_A-8'
|
| 141 |
+
map_name_to_handle = {
|
| 142 |
+
'bert_en_uncased_L-12_H-768_A-12':
|
| 143 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3',
|
| 144 |
+
'bert_en_cased_L-12_H-768_A-12':
|
| 145 |
+
'https://tfhub.dev/tensorflow/bert_en_cased_L-12_H-768_A-12/3',
|
| 146 |
+
'bert_multi_cased_L-12_H-768_A-12':
|
| 147 |
+
'https://tfhub.dev/tensorflow/bert_multi_cased_L-12_H-768_A-12/3',
|
| 148 |
+
'small_bert/bert_en_uncased_L-2_H-128_A-2':
|
| 149 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-128_A-2/1',
|
| 150 |
+
'small_bert/bert_en_uncased_L-2_H-256_A-4':
|
| 151 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-256_A-4/1',
|
| 152 |
+
'small_bert/bert_en_uncased_L-2_H-512_A-8':
|
| 153 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-512_A-8/1',
|
| 154 |
+
'small_bert/bert_en_uncased_L-2_H-768_A-12':
|
| 155 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-768_A-12/1',
|
| 156 |
+
'small_bert/bert_en_uncased_L-4_H-128_A-2':
|
| 157 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-128_A-2/1',
|
| 158 |
+
'small_bert/bert_en_uncased_L-4_H-256_A-4':
|
| 159 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-256_A-4/1',
|
| 160 |
+
'small_bert/bert_en_uncased_L-4_H-512_A-8':
|
| 161 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1',
|
| 162 |
+
'small_bert/bert_en_uncased_L-4_H-768_A-12':
|
| 163 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-768_A-12/1',
|
| 164 |
+
'small_bert/bert_en_uncased_L-6_H-128_A-2':
|
| 165 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-128_A-2/1',
|
| 166 |
+
'small_bert/bert_en_uncased_L-6_H-256_A-4':
|
| 167 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-256_A-4/1',
|
| 168 |
+
'small_bert/bert_en_uncased_L-6_H-512_A-8':
|
| 169 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-512_A-8/1',
|
| 170 |
+
'small_bert/bert_en_uncased_L-6_H-768_A-12':
|
| 171 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-768_A-12/1',
|
| 172 |
+
'small_bert/bert_en_uncased_L-8_H-128_A-2':
|
| 173 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-128_A-2/1',
|
| 174 |
+
'small_bert/bert_en_uncased_L-8_H-256_A-4':
|
| 175 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-256_A-4/1',
|
| 176 |
+
'small_bert/bert_en_uncased_L-8_H-512_A-8':
|
| 177 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-512_A-8/1',
|
| 178 |
+
'small_bert/bert_en_uncased_L-8_H-768_A-12':
|
| 179 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-768_A-12/1',
|
| 180 |
+
'small_bert/bert_en_uncased_L-10_H-128_A-2':
|
| 181 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-128_A-2/1',
|
| 182 |
+
'small_bert/bert_en_uncased_L-10_H-256_A-4':
|
| 183 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-256_A-4/1',
|
| 184 |
+
'small_bert/bert_en_uncased_L-10_H-512_A-8':
|
| 185 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-512_A-8/1',
|
| 186 |
+
'small_bert/bert_en_uncased_L-10_H-768_A-12':
|
| 187 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-768_A-12/1',
|
| 188 |
+
'small_bert/bert_en_uncased_L-12_H-128_A-2':
|
| 189 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-128_A-2/1',
|
| 190 |
+
'small_bert/bert_en_uncased_L-12_H-256_A-4':
|
| 191 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-256_A-4/1',
|
| 192 |
+
'small_bert/bert_en_uncased_L-12_H-512_A-8':
|
| 193 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-512_A-8/1',
|
| 194 |
+
'small_bert/bert_en_uncased_L-12_H-768_A-12':
|
| 195 |
+
'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-768_A-12/1',
|
| 196 |
+
'albert_en_base':
|
| 197 |
+
'https://tfhub.dev/tensorflow/albert_en_base/2',
|
| 198 |
+
'electra_small':
|
| 199 |
+
'https://tfhub.dev/google/electra_small/2',
|
| 200 |
+
'electra_base':
|
| 201 |
+
'https://tfhub.dev/google/electra_base/2',
|
| 202 |
+
'experts_pubmed':
|
| 203 |
+
'https://tfhub.dev/google/experts/bert/pubmed/2',
|
| 204 |
+
'experts_wiki_books':
|
| 205 |
+
'https://tfhub.dev/google/experts/bert/wiki_books/2',
|
| 206 |
+
'talking-heads_base':
|
| 207 |
+
'https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_base/1',
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
map_model_to_preprocess = {
|
| 211 |
+
'bert_en_uncased_L-12_H-768_A-12':
|
| 212 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 213 |
+
'bert_en_cased_L-12_H-768_A-12':
|
| 214 |
+
'https://tfhub.dev/tensorflow/bert_en_cased_preprocess/2',
|
| 215 |
+
'small_bert/bert_en_uncased_L-2_H-128_A-2':
|
| 216 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 217 |
+
'small_bert/bert_en_uncased_L-2_H-256_A-4':
|
| 218 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 219 |
+
'small_bert/bert_en_uncased_L-2_H-512_A-8':
|
| 220 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 221 |
+
'small_bert/bert_en_uncased_L-2_H-768_A-12':
|
| 222 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 223 |
+
'small_bert/bert_en_uncased_L-4_H-128_A-2':
|
| 224 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 225 |
+
'small_bert/bert_en_uncased_L-4_H-256_A-4':
|
| 226 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 227 |
+
'small_bert/bert_en_uncased_L-4_H-512_A-8':
|
| 228 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 229 |
+
'small_bert/bert_en_uncased_L-4_H-768_A-12':
|
| 230 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 231 |
+
'small_bert/bert_en_uncased_L-6_H-128_A-2':
|
| 232 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 233 |
+
'small_bert/bert_en_uncased_L-6_H-256_A-4':
|
| 234 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 235 |
+
'small_bert/bert_en_uncased_L-6_H-512_A-8':
|
| 236 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 237 |
+
'small_bert/bert_en_uncased_L-6_H-768_A-12':
|
| 238 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 239 |
+
'small_bert/bert_en_uncased_L-8_H-128_A-2':
|
| 240 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 241 |
+
'small_bert/bert_en_uncased_L-8_H-256_A-4':
|
| 242 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 243 |
+
'small_bert/bert_en_uncased_L-8_H-512_A-8':
|
| 244 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 245 |
+
'small_bert/bert_en_uncased_L-8_H-768_A-12':
|
| 246 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 247 |
+
'small_bert/bert_en_uncased_L-10_H-128_A-2':
|
| 248 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 249 |
+
'small_bert/bert_en_uncased_L-10_H-256_A-4':
|
| 250 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 251 |
+
'small_bert/bert_en_uncased_L-10_H-512_A-8':
|
| 252 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 253 |
+
'small_bert/bert_en_uncased_L-10_H-768_A-12':
|
| 254 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 255 |
+
'small_bert/bert_en_uncased_L-12_H-128_A-2':
|
| 256 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 257 |
+
'small_bert/bert_en_uncased_L-12_H-256_A-4':
|
| 258 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 259 |
+
'small_bert/bert_en_uncased_L-12_H-512_A-8':
|
| 260 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 261 |
+
'small_bert/bert_en_uncased_L-12_H-768_A-12':
|
| 262 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 263 |
+
'bert_multi_cased_L-12_H-768_A-12':
|
| 264 |
+
'https://tfhub.dev/tensorflow/bert_multi_cased_preprocess/2',
|
| 265 |
+
'albert_en_base':
|
| 266 |
+
'https://tfhub.dev/tensorflow/albert_en_preprocess/2',
|
| 267 |
+
'electra_small':
|
| 268 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 269 |
+
'electra_base':
|
| 270 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 271 |
+
'experts_pubmed':
|
| 272 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 273 |
+
'experts_wiki_books':
|
| 274 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 275 |
+
'talking-heads_base':
|
| 276 |
+
'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
tfhub_handle_encoder = map_name_to_handle[bert_model_name]
|
| 280 |
+
tfhub_handle_preprocess = map_model_to_preprocess[bert_model_name]
|
| 281 |
+
|
| 282 |
+
print(f'BERT model selected : {tfhub_handle_encoder}')
|
| 283 |
+
print(f'Preprocess model auto-selected: {tfhub_handle_preprocess}')
|
| 284 |
+
|
| 285 |
+
"""## The preprocessing model
|
| 286 |
+
|
| 287 |
+
Text inputs need to be transformed to numeric token ids and arranged in several Tensors before being input to BERT. TensorFlow Hub provides a matching preprocessing model for each of the BERT models discussed above, which implements this transformation using TF ops from the TF.text library. It is not necessary to run pure Python code outside your TensorFlow model to preprocess text.
|
| 288 |
+
|
| 289 |
+
The preprocessing model must be the one referenced by the documentation of the BERT model, which you can read at the URL printed above. For BERT models from the drop-down above, the preprocessing model is selected automatically.
|
| 290 |
+
|
| 291 |
+
Note: You will load the preprocessing model into a [hub.KerasLayer](https://www.tensorflow.org/hub/api_docs/python/hub/KerasLayer) to compose your fine-tuned model. This is the preferred API to load a TF2-style SavedModel from TF Hub into a Keras model.
|
| 292 |
+
"""
|
| 293 |
+
|
| 294 |
+
bert_preprocess_model = hub.KerasLayer(tfhub_handle_preprocess)
|
| 295 |
+
|
| 296 |
+
"""Let's try the preprocessing model on some text and see the output:"""
|
| 297 |
+
|
| 298 |
+
trainfeatures[0]
|
| 299 |
+
|
| 300 |
+
text_test = trainfeatures[0]
|
| 301 |
+
text_preprocessed = bert_preprocess_model(text_test)
|
| 302 |
+
|
| 303 |
+
print(f'Keys : {list(text_preprocessed.keys())}')
|
| 304 |
+
print(f'Shape : {text_preprocessed["input_word_ids"].shape}')
|
| 305 |
+
print(f'Word Ids : {text_preprocessed["input_word_ids"][0, :12]}')
|
| 306 |
+
print(f'Input Mask : {text_preprocessed["input_mask"][0, :12]}')
|
| 307 |
+
print(f'Type Ids : {text_preprocessed["input_type_ids"][0, :12]}')
|
| 308 |
+
|
| 309 |
+
"""As can be seen, there are 3 outputs from the preprocessing that a BERT model would use (`input_words_id`, `input_mask` and `input_type_ids`).
|
| 310 |
+
|
| 311 |
+
Some other important points:
|
| 312 |
+
- The input is truncated to 128 tokens. The number of tokens can be customized and you can see more details on the [Solve GLUE tasks using BERT on a TPU colab](https://www.tensorflow.org/tutorials/text/solve_glue_tasks_using_bert_on_tpu).
|
| 313 |
+
- The `input_type_ids` only have one value (0) because this is a single sentence input. For a multiple sentence input, it would have one number for each input.
|
| 314 |
+
|
| 315 |
+
Since this text preprocessor is a TensorFlow model, It can be included in your model directly.
|
| 316 |
+
|
| 317 |
+
## Using the BERT model
|
| 318 |
+
|
| 319 |
+
Before putting BERT into an own model, let's take a look at its outputs. You will load it from TF Hub and see the returned values.
|
| 320 |
+
"""
|
| 321 |
+
|
| 322 |
+
bert_model = hub.KerasLayer(tfhub_handle_encoder)
|
| 323 |
+
|
| 324 |
+
bert_results = bert_model(text_preprocessed)
|
| 325 |
+
|
| 326 |
+
print(f'Loaded BERT: {tfhub_handle_encoder}')
|
| 327 |
+
print(f'Pooled Outputs Shape:{bert_results["pooled_output"].shape}')
|
| 328 |
+
print(f'Pooled Outputs Values:{bert_results["pooled_output"][0, :12]}')
|
| 329 |
+
print(f'Sequence Outputs Shape:{bert_results["sequence_output"].shape}')
|
| 330 |
+
print(f'Sequence Outputs Values:{bert_results["sequence_output"][0, :12]}')
|
| 331 |
+
|
| 332 |
+
"""The BERT models return a map with 3 important keys: `pooled_output`, `sequence_output`, `encoder_outputs`:
|
| 333 |
+
|
| 334 |
+
- `pooled_output` to represent each input sequence as a whole. The shape is `[batch_size, H]`. You can think of this as an embedding for the entire movie review.
|
| 335 |
+
- `sequence_output` represents each input token in the context. The shape is `[batch_size, seq_length, H]`. You can think of this as a contextual embedding for every token in the movie review.
|
| 336 |
+
- `encoder_outputs` are the intermediate activations of the `L` Transformer blocks. `outputs["encoder_outputs"][i]` is a Tensor of shape `[batch_size, seq_length, 1024]` with the outputs of the i-th Transformer block, for `0 <= i < L`. The last value of the list is equal to `sequence_output`.
|
| 337 |
+
|
| 338 |
+
For the fine-tuning you are going to use the `pooled_output` array.
|
| 339 |
+
|
| 340 |
+
## Define your model
|
| 341 |
+
|
| 342 |
+
You will create a very simple fine-tuned model, with the preprocessing model, the selected BERT model, one Dense and a Dropout layer.
|
| 343 |
+
|
| 344 |
+
Note: for more information about the base model's input and output you can use just follow the model's url for documentation. Here specifically you don't need to worry about it because the preprocessing model will take care of that for you.
|
| 345 |
+
"""
|
| 346 |
+
|
| 347 |
+
def build_classifier_model():
|
| 348 |
+
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
|
| 349 |
+
preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, name='preprocessing')
|
| 350 |
+
encoder_inputs = preprocessing_layer(text_input)
|
| 351 |
+
encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')
|
| 352 |
+
outputs = encoder(encoder_inputs)
|
| 353 |
+
net = outputs['pooled_output']
|
| 354 |
+
net = tf.keras.layers.Dropout(0.1)(net)
|
| 355 |
+
net = tf.keras.layers.Dense(7, activation=None, name='classifier')(net)
|
| 356 |
+
return tf.keras.Model(text_input, net)
|
| 357 |
+
|
| 358 |
+
"""Let's check that the model runs with the output of the preprocessing model."""
|
| 359 |
+
|
| 360 |
+
classifier_model = build_classifier_model()
|
| 361 |
+
bert_raw_result = classifier_model(tf.constant(trainfeatures[0]))
|
| 362 |
+
print(tf.keras.activations.softmax(bert_raw_result))
|
| 363 |
+
|
| 364 |
+
"""The output is meaningless, of course, because the model has not been trained yet.
|
| 365 |
+
|
| 366 |
+
Let's take a look at the model's structure.
|
| 367 |
+
"""
|
| 368 |
+
|
| 369 |
+
classifier_model.summary()
|
| 370 |
+
|
| 371 |
+
"""## Model training
|
| 372 |
+
|
| 373 |
+
You now have all the pieces to train a model, including the preprocessing module, BERT encoder, data, and classifier.
|
| 374 |
+
|
| 375 |
+
Since this is a non-binary classification problem and the model outputs probabilities, you'll use `losses.CategoricalCrossentropy` loss function.
|
| 376 |
+
"""
|
| 377 |
+
|
| 378 |
+
loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
|
| 379 |
+
metrics = tf.metrics.CategoricalAccuracy()
|
| 380 |
+
|
| 381 |
+
"""### Loading the BERT model and training
|
| 382 |
+
|
| 383 |
+
Using the `classifier_model` you created earlier, you can compile the model with the loss, metric and optimizer.
|
| 384 |
+
"""
|
| 385 |
+
|
| 386 |
+
epochs=5
|
| 387 |
+
optimizer=tf.keras.optimizers.Adam(1e-5)
|
| 388 |
+
classifier_model.compile(optimizer=optimizer,
|
| 389 |
+
loss=loss,
|
| 390 |
+
metrics=metrics)
|
| 391 |
+
|
| 392 |
+
"""Note: training time will vary depending on the complexity of the BERT model you have selected."""
|
| 393 |
+
|
| 394 |
+
print(f'Training model with {tfhub_handle_encoder}')
|
| 395 |
+
history = classifier_model.fit(x=trainfeatures,y=trainlabels,
|
| 396 |
+
validation_data=(validfeatures,validlabels),
|
| 397 |
+
batch_size=32,
|
| 398 |
+
epochs=epochs)
|
| 399 |
+
classifier_model.save("CaesarAI.h5")
|
| 400 |
+
|
| 401 |
+
"""### Evaluate the model
|
| 402 |
+
|
| 403 |
+
Let's see how the model performs. Two values will be returned. Loss (a number which represents the error, lower values are better), and accuracy.
|
| 404 |
+
"""
|
| 405 |
+
|
| 406 |
+
loss, accuracy = classifier_model.evaluate(testfeatures,testlabels)
|
| 407 |
+
|
| 408 |
+
print(f'Loss: {loss}')
|
| 409 |
+
print(f'Accuracy: {accuracy}')
|
| 410 |
+
|
| 411 |
+
"""### Plot the accuracy and loss over time
|
| 412 |
+
|
| 413 |
+
Based on the `History` object returned by `model.fit()`. You can plot the training and validation loss for comparison, as well as the training and validation accuracy:
|
| 414 |
+
"""
|
| 415 |
+
|
| 416 |
+
history_dict = history.history
|
| 417 |
+
print(history_dict.keys())
|
| 418 |
+
|
| 419 |
+
acc = history_dict['categorical_accuracy']
|
| 420 |
+
val_acc = history_dict['val_categorical_accuracy']
|
| 421 |
+
loss = history_dict['loss']
|
| 422 |
+
val_loss = history_dict['val_loss']
|
| 423 |
+
|
| 424 |
+
epochs = range(1, len(acc) + 1)
|
| 425 |
+
fig = plt.figure(figsize=(10, 8))
|
| 426 |
+
fig.tight_layout()
|
| 427 |
+
|
| 428 |
+
plt.subplot(2, 1, 1)
|
| 429 |
+
# "bo" is for "blue dot"
|
| 430 |
+
plt.plot(epochs, loss, 'r', label='Training loss')
|
| 431 |
+
# b is for "solid blue line"
|
| 432 |
+
plt.plot(epochs, val_loss, 'b', label='Validation loss')
|
| 433 |
+
plt.title('Training and validation loss')
|
| 434 |
+
plt.grid(True)
|
| 435 |
+
# plt.xlabel('Epochs')
|
| 436 |
+
plt.ylabel('Loss')
|
| 437 |
+
plt.legend()
|
| 438 |
+
|
| 439 |
+
plt.subplot(2, 1, 2)
|
| 440 |
+
plt.plot(epochs, acc, 'r', label='Training acc')
|
| 441 |
+
plt.plot(epochs, val_acc, 'b', label='Validation acc')
|
| 442 |
+
plt.title('Training and validation accuracy')
|
| 443 |
+
plt.grid(True)
|
| 444 |
+
plt.xlabel('Epochs')
|
| 445 |
+
plt.ylabel('Accuracy')
|
| 446 |
+
plt.legend(loc='lower right')
|
| 447 |
+
|
| 448 |
+
"""In this plot, the red lines represents the training loss and accuracy, and the blue lines are the validation loss and accuracy.
|
| 449 |
+
|
| 450 |
+
Classifying arbitrary instructions:
|
| 451 |
+
"""
|
| 452 |
+
|
| 453 |
+
def print_my_examples(inputs, results):
|
| 454 |
+
result_for_printing = \
|
| 455 |
+
[f'input: {inputs[i]:<30} : estimated intent: {results[i]}'
|
| 456 |
+
for i in range(len(inputs))]
|
| 457 |
+
print(*result_for_printing, sep='\n')
|
| 458 |
+
print()
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
examples = [
|
| 462 |
+
'play a song from U2', # this is the same sentence tried earlier
|
| 463 |
+
'Will it rain tomorrow',
|
| 464 |
+
'I like to hear greatist hits from beastie boys',
|
| 465 |
+
'I like to book a table for 3 persons',
|
| 466 |
+
'5 stars for machines like me'
|
| 467 |
+
]
|
| 468 |
+
|
| 469 |
+
results = tf.nn.softmax(classifier_model(tf.constant(examples)))
|
| 470 |
+
|
| 471 |
+
binarizer.classes_
|
| 472 |
+
|
| 473 |
+
intents=binarizer.inverse_transform(results.numpy())
|
| 474 |
+
|
| 475 |
+
print_my_examples(examples, intents)
|
| 476 |
+
|
bert tutorial/intent_classification_with_bert.ipynb
ADDED
|
@@ -0,0 +1,1239 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {
|
| 6 |
+
"id": "IZ6SNYq_tVVC"
|
| 7 |
+
},
|
| 8 |
+
"source": [
|
| 9 |
+
"# Intent Classification with BERT\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"This notebook demonstrates the fine-tuning of BERT to perform intent classification.\n",
|
| 12 |
+
"Intent classification tries to map given instructions (sentence in natural language) to a set of predefined intents. \n",
|
| 13 |
+
"\n",
|
| 14 |
+
"## What you will learn\n",
|
| 15 |
+
"\n",
|
| 16 |
+
"- Load data from csv and preprocess it for training and test\n",
|
| 17 |
+
"- Load a BERT model from TensorFlow Hub\n",
|
| 18 |
+
"- Build your own model by combining BERT with a classifier\n",
|
| 19 |
+
"- Train your own model, fine-tuning BERT as part of that\n",
|
| 20 |
+
"- Save your model and use it to recognize the intend of instructions\n"
|
| 21 |
+
]
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"cell_type": "markdown",
|
| 25 |
+
"metadata": {
|
| 26 |
+
"id": "2PHBpLPuQdmK"
|
| 27 |
+
},
|
| 28 |
+
"source": [
|
| 29 |
+
"## About BERT\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"[BERT](https://arxiv.org/abs/1810.04805) and other Transformer encoder architectures have been shown to be successful on a variety of tasks in NLP (natural language processing). They compute vector-space representations of natural language that are suitable for use in deep learning models. The BERT family of models uses the Transformer encoder architecture to process each token of input text in the full context of all tokens before and after, hence the name: Bidirectional Encoder Representations from Transformers. \n",
|
| 32 |
+
"\n",
|
| 33 |
+
"BERT models are usually pre-trained on a large corpus of text, then fine-tuned for specific tasks.\n"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"cell_type": "markdown",
|
| 38 |
+
"metadata": {
|
| 39 |
+
"id": "SCjmX4zTCkRK"
|
| 40 |
+
},
|
| 41 |
+
"source": [
|
| 42 |
+
"## Setup\n"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 1,
|
| 48 |
+
"metadata": {
|
| 49 |
+
"execution": {
|
| 50 |
+
"iopub.execute_input": "2021-01-13T03:07:33.561260Z",
|
| 51 |
+
"iopub.status.busy": "2021-01-13T03:07:33.560567Z",
|
| 52 |
+
"iopub.status.idle": "2021-01-13T03:07:40.852309Z",
|
| 53 |
+
"shell.execute_reply": "2021-01-13T03:07:40.851601Z"
|
| 54 |
+
},
|
| 55 |
+
"id": "_XgTpm9ZxoN9"
|
| 56 |
+
},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"import os\n",
|
| 60 |
+
"#import shutil\n",
|
| 61 |
+
"import pandas as pd\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"import tensorflow as tf\n",
|
| 64 |
+
"import tensorflow_hub as hub\n",
|
| 65 |
+
"import tensorflow_text as text\n",
|
| 66 |
+
"import seaborn as sns\n",
|
| 67 |
+
"from pylab import rcParams\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"import matplotlib.pyplot as plt\n",
|
| 70 |
+
"tf.get_logger().setLevel('ERROR')\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"sns.set(style='whitegrid', palette='muted', font_scale=1.2)\n",
|
| 73 |
+
"HAPPY_COLORS_PALETTE = [\"#01BEFE\", \"#FFDD00\", \"#FF7D00\", \"#FF006D\", \"#ADFF02\", \"#8F00FF\"]\n",
|
| 74 |
+
"sns.set_palette(sns.color_palette(HAPPY_COLORS_PALETTE))\n",
|
| 75 |
+
"rcParams['figure.figsize'] = 12, 8\n",
|
| 76 |
+
"import warnings\n",
|
| 77 |
+
"warnings.filterwarnings(\"ignore\")"
|
| 78 |
+
]
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"cell_type": "markdown",
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"source": [
|
| 84 |
+
"## Data Access\n",
|
| 85 |
+
"The data contains various user queries categorized into seven intents. It is hosted on [GitHub](https://github.com/snipsco/nlu-benchmark/tree/master/2017-06-custom-intent-engines) and is first presented in [this paper](https://arxiv.org/abs/1805.10190). In the list below the classes and an example for each class is given:\n",
|
| 86 |
+
"\n",
|
| 87 |
+
"* `class`: SearchCreativeWork - `example`:*play hell house song*\n",
|
| 88 |
+
"* `class`: GetWeather - `example`: *is it windy in boston, mb right now*\n",
|
| 89 |
+
"* `class`: BookRestaurant - `example`: *book a restaurant for eight people in six years*\n",
|
| 90 |
+
"* `class`: PlayMusic - `example`: *play the song little robin redbreast*\n",
|
| 91 |
+
"* `class`: AddToPlaylist - `example`: *add step to me to the 50 clásicos playlist*\n",
|
| 92 |
+
"* `class`: RateBook - `example`: *give 6 stars to of mice and men*\n",
|
| 93 |
+
"* `class`: SearchScreeningEvent - `example` : *find fish story*"
|
| 94 |
+
]
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"cell_type": "markdown",
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"source": [
|
| 100 |
+
"Data can be downloaded from a Google Drive by applying [gdown](https://pypi.org/project/gdown/). In the following code cells the download is invoked only if the corresponding file, does not yet exist at the corresponding location."
|
| 101 |
+
]
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"cell_type": "code",
|
| 105 |
+
"execution_count": 2,
|
| 106 |
+
"metadata": {},
|
| 107 |
+
"outputs": [],
|
| 108 |
+
"source": [
|
| 109 |
+
"datafolder=\"\""
|
| 110 |
+
]
|
| 111 |
+
},
|
| 112 |
+
{
|
| 113 |
+
"cell_type": "code",
|
| 114 |
+
"execution_count": 3,
|
| 115 |
+
"metadata": {},
|
| 116 |
+
"outputs": [],
|
| 117 |
+
"source": [
|
| 118 |
+
"trainfile=datafolder+\"train.csv\"\n",
|
| 119 |
+
"testfile=datafolder+\"test.csv\"\n",
|
| 120 |
+
"validfile=datafolder+\"valid.csv\""
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"cell_type": "markdown",
|
| 125 |
+
"metadata": {},
|
| 126 |
+
"source": [
|
| 127 |
+
"Next, the downloaded .csv-files for training, validation and test are imported into pandas dataframes:"
|
| 128 |
+
]
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"cell_type": "code",
|
| 132 |
+
"execution_count": 4,
|
| 133 |
+
"metadata": {},
|
| 134 |
+
"outputs": [],
|
| 135 |
+
"source": [
|
| 136 |
+
"traindf = pd.read_csv(trainfile)\n",
|
| 137 |
+
"validdf = pd.read_csv(validfile)\n",
|
| 138 |
+
"testdf = pd.read_csv(testfile)"
|
| 139 |
+
]
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"cell_type": "code",
|
| 143 |
+
"execution_count": 5,
|
| 144 |
+
"metadata": {},
|
| 145 |
+
"outputs": [
|
| 146 |
+
{
|
| 147 |
+
"data": {
|
| 148 |
+
"text/html": [
|
| 149 |
+
"<div>\n",
|
| 150 |
+
"<style scoped>\n",
|
| 151 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 152 |
+
" vertical-align: middle;\n",
|
| 153 |
+
" }\n",
|
| 154 |
+
"\n",
|
| 155 |
+
" .dataframe tbody tr th {\n",
|
| 156 |
+
" vertical-align: top;\n",
|
| 157 |
+
" }\n",
|
| 158 |
+
"\n",
|
| 159 |
+
" .dataframe thead th {\n",
|
| 160 |
+
" text-align: right;\n",
|
| 161 |
+
" }\n",
|
| 162 |
+
"</style>\n",
|
| 163 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 164 |
+
" <thead>\n",
|
| 165 |
+
" <tr style=\"text-align: right;\">\n",
|
| 166 |
+
" <th></th>\n",
|
| 167 |
+
" <th>text</th>\n",
|
| 168 |
+
" <th>intent</th>\n",
|
| 169 |
+
" </tr>\n",
|
| 170 |
+
" </thead>\n",
|
| 171 |
+
" <tbody>\n",
|
| 172 |
+
" <tr>\n",
|
| 173 |
+
" <th>0</th>\n",
|
| 174 |
+
" <td>listen to westbam alumb allergic on google music</td>\n",
|
| 175 |
+
" <td>PlayMusic</td>\n",
|
| 176 |
+
" </tr>\n",
|
| 177 |
+
" <tr>\n",
|
| 178 |
+
" <th>1</th>\n",
|
| 179 |
+
" <td>add step to me to the 50 clásicos playlist</td>\n",
|
| 180 |
+
" <td>AddToPlaylist</td>\n",
|
| 181 |
+
" </tr>\n",
|
| 182 |
+
" <tr>\n",
|
| 183 |
+
" <th>2</th>\n",
|
| 184 |
+
" <td>i give this current textbook a rating value of...</td>\n",
|
| 185 |
+
" <td>RateBook</td>\n",
|
| 186 |
+
" </tr>\n",
|
| 187 |
+
" <tr>\n",
|
| 188 |
+
" <th>3</th>\n",
|
| 189 |
+
" <td>play the song little robin redbreast</td>\n",
|
| 190 |
+
" <td>PlayMusic</td>\n",
|
| 191 |
+
" </tr>\n",
|
| 192 |
+
" <tr>\n",
|
| 193 |
+
" <th>4</th>\n",
|
| 194 |
+
" <td>please add iris dement to my playlist this is ...</td>\n",
|
| 195 |
+
" <td>AddToPlaylist</td>\n",
|
| 196 |
+
" </tr>\n",
|
| 197 |
+
" </tbody>\n",
|
| 198 |
+
"</table>\n",
|
| 199 |
+
"</div>"
|
| 200 |
+
],
|
| 201 |
+
"text/plain": [
|
| 202 |
+
" text intent\n",
|
| 203 |
+
"0 listen to westbam alumb allergic on google music PlayMusic\n",
|
| 204 |
+
"1 add step to me to the 50 clásicos playlist AddToPlaylist\n",
|
| 205 |
+
"2 i give this current textbook a rating value of... RateBook\n",
|
| 206 |
+
"3 play the song little robin redbreast PlayMusic\n",
|
| 207 |
+
"4 please add iris dement to my playlist this is ... AddToPlaylist"
|
| 208 |
+
]
|
| 209 |
+
},
|
| 210 |
+
"execution_count": 5,
|
| 211 |
+
"metadata": {},
|
| 212 |
+
"output_type": "execute_result"
|
| 213 |
+
}
|
| 214 |
+
],
|
| 215 |
+
"source": [
|
| 216 |
+
"traindf.head()"
|
| 217 |
+
]
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"cell_type": "markdown",
|
| 221 |
+
"metadata": {},
|
| 222 |
+
"source": [
|
| 223 |
+
"Training data contains 13084 instructions:"
|
| 224 |
+
]
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"cell_type": "code",
|
| 228 |
+
"execution_count": 6,
|
| 229 |
+
"metadata": {
|
| 230 |
+
"scrolled": true
|
| 231 |
+
},
|
| 232 |
+
"outputs": [
|
| 233 |
+
{
|
| 234 |
+
"data": {
|
| 235 |
+
"text/plain": [
|
| 236 |
+
"(13084, 2)"
|
| 237 |
+
]
|
| 238 |
+
},
|
| 239 |
+
"execution_count": 6,
|
| 240 |
+
"metadata": {},
|
| 241 |
+
"output_type": "execute_result"
|
| 242 |
+
}
|
| 243 |
+
],
|
| 244 |
+
"source": [
|
| 245 |
+
"traindf.shape"
|
| 246 |
+
]
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"cell_type": "code",
|
| 250 |
+
"execution_count": 7,
|
| 251 |
+
"metadata": {},
|
| 252 |
+
"outputs": [],
|
| 253 |
+
"source": [
|
| 254 |
+
"trainfeatures=traindf.copy()\n",
|
| 255 |
+
"trainlabels=trainfeatures.pop(\"intent\")"
|
| 256 |
+
]
|
| 257 |
+
},
|
| 258 |
+
{
|
| 259 |
+
"cell_type": "code",
|
| 260 |
+
"execution_count": 8,
|
| 261 |
+
"metadata": {},
|
| 262 |
+
"outputs": [],
|
| 263 |
+
"source": [
|
| 264 |
+
"trainfeatures=trainfeatures.values"
|
| 265 |
+
]
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"cell_type": "markdown",
|
| 269 |
+
"metadata": {},
|
| 270 |
+
"source": [
|
| 271 |
+
"Distribution of class-labels in training-data:"
|
| 272 |
+
]
|
| 273 |
+
},
|
| 274 |
+
{
|
| 275 |
+
"cell_type": "code",
|
| 276 |
+
"execution_count": 9,
|
| 277 |
+
"metadata": {},
|
| 278 |
+
"outputs": [],
|
| 279 |
+
"source": [
|
| 280 |
+
"#chart = sns.countplot(trainlabels, palette=HAPPY_COLORS_PALETTE)\n",
|
| 281 |
+
"#plt.title(\"Number of texts per intent\")\n",
|
| 282 |
+
"#chart\n",
|
| 283 |
+
"#`chart.set_xticklabels(chart.get_xticklabels(), rotation=30, horizontalalignment='right')"
|
| 284 |
+
]
|
| 285 |
+
},
|
| 286 |
+
{
|
| 287 |
+
"cell_type": "markdown",
|
| 288 |
+
"metadata": {},
|
| 289 |
+
"source": [
|
| 290 |
+
"One-Hot-Encoding of class-labels:"
|
| 291 |
+
]
|
| 292 |
+
},
|
| 293 |
+
{
|
| 294 |
+
"cell_type": "code",
|
| 295 |
+
"execution_count": 10,
|
| 296 |
+
"metadata": {},
|
| 297 |
+
"outputs": [],
|
| 298 |
+
"source": [
|
| 299 |
+
"from sklearn.preprocessing import LabelBinarizer"
|
| 300 |
+
]
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
"cell_type": "code",
|
| 304 |
+
"execution_count": 11,
|
| 305 |
+
"metadata": {},
|
| 306 |
+
"outputs": [],
|
| 307 |
+
"source": [
|
| 308 |
+
"binarizer=LabelBinarizer()\n",
|
| 309 |
+
"trainlabels=binarizer.fit_transform(trainlabels.values)"
|
| 310 |
+
]
|
| 311 |
+
},
|
| 312 |
+
{
|
| 313 |
+
"cell_type": "code",
|
| 314 |
+
"execution_count": 12,
|
| 315 |
+
"metadata": {},
|
| 316 |
+
"outputs": [
|
| 317 |
+
{
|
| 318 |
+
"data": {
|
| 319 |
+
"text/plain": [
|
| 320 |
+
"(13084, 7)"
|
| 321 |
+
]
|
| 322 |
+
},
|
| 323 |
+
"execution_count": 12,
|
| 324 |
+
"metadata": {},
|
| 325 |
+
"output_type": "execute_result"
|
| 326 |
+
}
|
| 327 |
+
],
|
| 328 |
+
"source": [
|
| 329 |
+
"trainlabels.shape"
|
| 330 |
+
]
|
| 331 |
+
},
|
| 332 |
+
{
|
| 333 |
+
"cell_type": "markdown",
|
| 334 |
+
"metadata": {},
|
| 335 |
+
"source": [
|
| 336 |
+
"Preprocess test- and validation data in the same way as it has been done for training-data:"
|
| 337 |
+
]
|
| 338 |
+
},
|
| 339 |
+
{
|
| 340 |
+
"cell_type": "code",
|
| 341 |
+
"execution_count": 13,
|
| 342 |
+
"metadata": {},
|
| 343 |
+
"outputs": [],
|
| 344 |
+
"source": [
|
| 345 |
+
"testfeatures=testdf.copy()\n",
|
| 346 |
+
"testlabels=testfeatures.pop(\"intent\")\n",
|
| 347 |
+
"validfeatures=validdf.copy()\n",
|
| 348 |
+
"validlabels=validfeatures.pop(\"intent\")\n",
|
| 349 |
+
"\n",
|
| 350 |
+
"testfeatures=testfeatures.values\n",
|
| 351 |
+
"validfeatures=validfeatures.values\n",
|
| 352 |
+
"\n",
|
| 353 |
+
"testlabels=binarizer.transform(testlabels.values)\n",
|
| 354 |
+
"validlabels=binarizer.transform(validlabels.values)"
|
| 355 |
+
]
|
| 356 |
+
},
|
| 357 |
+
{
|
| 358 |
+
"cell_type": "markdown",
|
| 359 |
+
"metadata": {
|
| 360 |
+
"id": "dX8FtlpGJRE6"
|
| 361 |
+
},
|
| 362 |
+
"source": [
|
| 363 |
+
"## Loading models from TensorFlow Hub\n",
|
| 364 |
+
"\n",
|
| 365 |
+
"Here you can choose which BERT model you will load from TensorFlow Hub and fine-tune. There are multiple BERT models available.\n",
|
| 366 |
+
"\n",
|
| 367 |
+
" - [BERT-Base](https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3), [Uncased](https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3) and [seven more models](https://tfhub.dev/google/collections/bert/1) with trained weights released by the original BERT authors.\n",
|
| 368 |
+
" - [Small BERTs](https://tfhub.dev/google/collections/bert/1) have the same general architecture but fewer and/or smaller Transformer blocks, which lets you explore tradeoffs between speed, size and quality.\n",
|
| 369 |
+
" - [ALBERT](https://tfhub.dev/google/collections/albert/1): four different sizes of \"A Lite BERT\" that reduces model size (but not computation time) by sharing parameters between layers.\n",
|
| 370 |
+
" - [BERT Experts](https://tfhub.dev/google/collections/experts/bert/1): eight models that all have the BERT-base architecture but offer a choice between different pre-training domains, to align more closely with the target task.\n",
|
| 371 |
+
" - [Electra](https://tfhub.dev/google/collections/electra/1) has the same architecture as BERT (in three different sizes), but gets pre-trained as a discriminator in a set-up that resembles a Generative Adversarial Network (GAN).\n",
|
| 372 |
+
" - BERT with Talking-Heads Attention and Gated GELU [[base](https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_base/1), [large](https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_large/1)] has two improvements to the core of the Transformer architecture.\n",
|
| 373 |
+
"\n",
|
| 374 |
+
"The model documentation on TensorFlow Hub has more details and references to the\n",
|
| 375 |
+
"research literature. Follow the links above, or click on the [`tfhub.dev`](http://tfhub.dev) URL\n",
|
| 376 |
+
"printed after the next cell execution.\n",
|
| 377 |
+
"\n",
|
| 378 |
+
"The suggestion is to start with a Small BERT (with fewer parameters) since they are faster to fine-tune. If you like a small model but with higher accuracy, ALBERT might be your next option. If you want even better accuracy, choose\n",
|
| 379 |
+
"one of the classic BERT sizes or their recent refinements like Electra, Talking Heads, or a BERT Expert.\n",
|
| 380 |
+
"\n",
|
| 381 |
+
"Aside from the models available below, there are [multiple versions](https://tfhub.dev/google/collections/transformer_encoders_text/1) of the models that are larger and can yield even better accuracy but they are too big to be fine-tuned on a single GPU. You will be able to do that on the [Solve GLUE tasks using BERT on a TPU colab](https://www.tensorflow.org/tutorials/text/solve_glue_tasks_using_bert_on_tpu).\n",
|
| 382 |
+
"\n",
|
| 383 |
+
"You'll see in the code below that switching the tfhub.dev URL is enough to try any of these models, because all the differences between them are encapsulated in the SavedModels from TF Hub."
|
| 384 |
+
]
|
| 385 |
+
},
|
| 386 |
+
{
|
| 387 |
+
"cell_type": "code",
|
| 388 |
+
"execution_count": 14,
|
| 389 |
+
"metadata": {
|
| 390 |
+
"execution": {
|
| 391 |
+
"iopub.execute_input": "2021-01-13T03:08:07.421163Z",
|
| 392 |
+
"iopub.status.busy": "2021-01-13T03:08:07.420022Z",
|
| 393 |
+
"iopub.status.idle": "2021-01-13T03:08:07.423061Z",
|
| 394 |
+
"shell.execute_reply": "2021-01-13T03:08:07.423513Z"
|
| 395 |
+
},
|
| 396 |
+
"id": "y8_ctG55-uTX"
|
| 397 |
+
},
|
| 398 |
+
"outputs": [
|
| 399 |
+
{
|
| 400 |
+
"name": "stdout",
|
| 401 |
+
"output_type": "stream",
|
| 402 |
+
"text": [
|
| 403 |
+
"BERT model selected : https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-512_A-8/1\n",
|
| 404 |
+
"Preprocess model auto-selected: https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2\n"
|
| 405 |
+
]
|
| 406 |
+
}
|
| 407 |
+
],
|
| 408 |
+
"source": [
|
| 409 |
+
"bert_model_name = 'small_bert/bert_en_uncased_L-8_H-512_A-8' \n",
|
| 410 |
+
"map_name_to_handle = {\n",
|
| 411 |
+
" 'bert_en_uncased_L-12_H-768_A-12':\n",
|
| 412 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3',\n",
|
| 413 |
+
" 'bert_en_cased_L-12_H-768_A-12':\n",
|
| 414 |
+
" 'https://tfhub.dev/tensorflow/bert_en_cased_L-12_H-768_A-12/3',\n",
|
| 415 |
+
" 'bert_multi_cased_L-12_H-768_A-12':\n",
|
| 416 |
+
" 'https://tfhub.dev/tensorflow/bert_multi_cased_L-12_H-768_A-12/3',\n",
|
| 417 |
+
" 'small_bert/bert_en_uncased_L-2_H-128_A-2':\n",
|
| 418 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-128_A-2/1',\n",
|
| 419 |
+
" 'small_bert/bert_en_uncased_L-2_H-256_A-4':\n",
|
| 420 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-256_A-4/1',\n",
|
| 421 |
+
" 'small_bert/bert_en_uncased_L-2_H-512_A-8':\n",
|
| 422 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-512_A-8/1',\n",
|
| 423 |
+
" 'small_bert/bert_en_uncased_L-2_H-768_A-12':\n",
|
| 424 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-768_A-12/1',\n",
|
| 425 |
+
" 'small_bert/bert_en_uncased_L-4_H-128_A-2':\n",
|
| 426 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-128_A-2/1',\n",
|
| 427 |
+
" 'small_bert/bert_en_uncased_L-4_H-256_A-4':\n",
|
| 428 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-256_A-4/1',\n",
|
| 429 |
+
" 'small_bert/bert_en_uncased_L-4_H-512_A-8':\n",
|
| 430 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1',\n",
|
| 431 |
+
" 'small_bert/bert_en_uncased_L-4_H-768_A-12':\n",
|
| 432 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-768_A-12/1',\n",
|
| 433 |
+
" 'small_bert/bert_en_uncased_L-6_H-128_A-2':\n",
|
| 434 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-128_A-2/1',\n",
|
| 435 |
+
" 'small_bert/bert_en_uncased_L-6_H-256_A-4':\n",
|
| 436 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-256_A-4/1',\n",
|
| 437 |
+
" 'small_bert/bert_en_uncased_L-6_H-512_A-8':\n",
|
| 438 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-512_A-8/1',\n",
|
| 439 |
+
" 'small_bert/bert_en_uncased_L-6_H-768_A-12':\n",
|
| 440 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-6_H-768_A-12/1',\n",
|
| 441 |
+
" 'small_bert/bert_en_uncased_L-8_H-128_A-2':\n",
|
| 442 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-128_A-2/1',\n",
|
| 443 |
+
" 'small_bert/bert_en_uncased_L-8_H-256_A-4':\n",
|
| 444 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-256_A-4/1',\n",
|
| 445 |
+
" 'small_bert/bert_en_uncased_L-8_H-512_A-8':\n",
|
| 446 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-512_A-8/1',\n",
|
| 447 |
+
" 'small_bert/bert_en_uncased_L-8_H-768_A-12':\n",
|
| 448 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-768_A-12/1',\n",
|
| 449 |
+
" 'small_bert/bert_en_uncased_L-10_H-128_A-2':\n",
|
| 450 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-128_A-2/1',\n",
|
| 451 |
+
" 'small_bert/bert_en_uncased_L-10_H-256_A-4':\n",
|
| 452 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-256_A-4/1',\n",
|
| 453 |
+
" 'small_bert/bert_en_uncased_L-10_H-512_A-8':\n",
|
| 454 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-512_A-8/1',\n",
|
| 455 |
+
" 'small_bert/bert_en_uncased_L-10_H-768_A-12':\n",
|
| 456 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-10_H-768_A-12/1',\n",
|
| 457 |
+
" 'small_bert/bert_en_uncased_L-12_H-128_A-2':\n",
|
| 458 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-128_A-2/1',\n",
|
| 459 |
+
" 'small_bert/bert_en_uncased_L-12_H-256_A-4':\n",
|
| 460 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-256_A-4/1',\n",
|
| 461 |
+
" 'small_bert/bert_en_uncased_L-12_H-512_A-8':\n",
|
| 462 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-512_A-8/1',\n",
|
| 463 |
+
" 'small_bert/bert_en_uncased_L-12_H-768_A-12':\n",
|
| 464 |
+
" 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-12_H-768_A-12/1',\n",
|
| 465 |
+
" 'albert_en_base':\n",
|
| 466 |
+
" 'https://tfhub.dev/tensorflow/albert_en_base/2',\n",
|
| 467 |
+
" 'electra_small':\n",
|
| 468 |
+
" 'https://tfhub.dev/google/electra_small/2',\n",
|
| 469 |
+
" 'electra_base':\n",
|
| 470 |
+
" 'https://tfhub.dev/google/electra_base/2',\n",
|
| 471 |
+
" 'experts_pubmed':\n",
|
| 472 |
+
" 'https://tfhub.dev/google/experts/bert/pubmed/2',\n",
|
| 473 |
+
" 'experts_wiki_books':\n",
|
| 474 |
+
" 'https://tfhub.dev/google/experts/bert/wiki_books/2',\n",
|
| 475 |
+
" 'talking-heads_base':\n",
|
| 476 |
+
" 'https://tfhub.dev/tensorflow/talkheads_ggelu_bert_en_base/1',\n",
|
| 477 |
+
"}\n",
|
| 478 |
+
"\n",
|
| 479 |
+
"map_model_to_preprocess = {\n",
|
| 480 |
+
" 'bert_en_uncased_L-12_H-768_A-12':\n",
|
| 481 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 482 |
+
" 'bert_en_cased_L-12_H-768_A-12':\n",
|
| 483 |
+
" 'https://tfhub.dev/tensorflow/bert_en_cased_preprocess/2',\n",
|
| 484 |
+
" 'small_bert/bert_en_uncased_L-2_H-128_A-2':\n",
|
| 485 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 486 |
+
" 'small_bert/bert_en_uncased_L-2_H-256_A-4':\n",
|
| 487 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 488 |
+
" 'small_bert/bert_en_uncased_L-2_H-512_A-8':\n",
|
| 489 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 490 |
+
" 'small_bert/bert_en_uncased_L-2_H-768_A-12':\n",
|
| 491 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 492 |
+
" 'small_bert/bert_en_uncased_L-4_H-128_A-2':\n",
|
| 493 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 494 |
+
" 'small_bert/bert_en_uncased_L-4_H-256_A-4':\n",
|
| 495 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 496 |
+
" 'small_bert/bert_en_uncased_L-4_H-512_A-8':\n",
|
| 497 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 498 |
+
" 'small_bert/bert_en_uncased_L-4_H-768_A-12':\n",
|
| 499 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 500 |
+
" 'small_bert/bert_en_uncased_L-6_H-128_A-2':\n",
|
| 501 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 502 |
+
" 'small_bert/bert_en_uncased_L-6_H-256_A-4':\n",
|
| 503 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 504 |
+
" 'small_bert/bert_en_uncased_L-6_H-512_A-8':\n",
|
| 505 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 506 |
+
" 'small_bert/bert_en_uncased_L-6_H-768_A-12':\n",
|
| 507 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 508 |
+
" 'small_bert/bert_en_uncased_L-8_H-128_A-2':\n",
|
| 509 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 510 |
+
" 'small_bert/bert_en_uncased_L-8_H-256_A-4':\n",
|
| 511 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 512 |
+
" 'small_bert/bert_en_uncased_L-8_H-512_A-8':\n",
|
| 513 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 514 |
+
" 'small_bert/bert_en_uncased_L-8_H-768_A-12':\n",
|
| 515 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 516 |
+
" 'small_bert/bert_en_uncased_L-10_H-128_A-2':\n",
|
| 517 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 518 |
+
" 'small_bert/bert_en_uncased_L-10_H-256_A-4':\n",
|
| 519 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 520 |
+
" 'small_bert/bert_en_uncased_L-10_H-512_A-8':\n",
|
| 521 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 522 |
+
" 'small_bert/bert_en_uncased_L-10_H-768_A-12':\n",
|
| 523 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 524 |
+
" 'small_bert/bert_en_uncased_L-12_H-128_A-2':\n",
|
| 525 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 526 |
+
" 'small_bert/bert_en_uncased_L-12_H-256_A-4':\n",
|
| 527 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 528 |
+
" 'small_bert/bert_en_uncased_L-12_H-512_A-8':\n",
|
| 529 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 530 |
+
" 'small_bert/bert_en_uncased_L-12_H-768_A-12':\n",
|
| 531 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 532 |
+
" 'bert_multi_cased_L-12_H-768_A-12':\n",
|
| 533 |
+
" 'https://tfhub.dev/tensorflow/bert_multi_cased_preprocess/2',\n",
|
| 534 |
+
" 'albert_en_base':\n",
|
| 535 |
+
" 'https://tfhub.dev/tensorflow/albert_en_preprocess/2',\n",
|
| 536 |
+
" 'electra_small':\n",
|
| 537 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 538 |
+
" 'electra_base':\n",
|
| 539 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 540 |
+
" 'experts_pubmed':\n",
|
| 541 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 542 |
+
" 'experts_wiki_books':\n",
|
| 543 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 544 |
+
" 'talking-heads_base':\n",
|
| 545 |
+
" 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/2',\n",
|
| 546 |
+
"}\n",
|
| 547 |
+
"\n",
|
| 548 |
+
"tfhub_handle_encoder = map_name_to_handle[bert_model_name]\n",
|
| 549 |
+
"tfhub_handle_preprocess = map_model_to_preprocess[bert_model_name]\n",
|
| 550 |
+
"\n",
|
| 551 |
+
"print(f'BERT model selected : {tfhub_handle_encoder}')\n",
|
| 552 |
+
"print(f'Preprocess model auto-selected: {tfhub_handle_preprocess}')"
|
| 553 |
+
]
|
| 554 |
+
},
|
| 555 |
+
{
|
| 556 |
+
"cell_type": "markdown",
|
| 557 |
+
"metadata": {
|
| 558 |
+
"id": "7WrcxxTRDdHi"
|
| 559 |
+
},
|
| 560 |
+
"source": [
|
| 561 |
+
"## The preprocessing model\n",
|
| 562 |
+
"\n",
|
| 563 |
+
"Text inputs need to be transformed to numeric token ids and arranged in several Tensors before being input to BERT. TensorFlow Hub provides a matching preprocessing model for each of the BERT models discussed above, which implements this transformation using TF ops from the TF.text library. It is not necessary to run pure Python code outside your TensorFlow model to preprocess text.\n",
|
| 564 |
+
"\n",
|
| 565 |
+
"The preprocessing model must be the one referenced by the documentation of the BERT model, which you can read at the URL printed above. For BERT models from the drop-down above, the preprocessing model is selected automatically.\n",
|
| 566 |
+
"\n",
|
| 567 |
+
"Note: You will load the preprocessing model into a [hub.KerasLayer](https://www.tensorflow.org/hub/api_docs/python/hub/KerasLayer) to compose your fine-tuned model. This is the preferred API to load a TF2-style SavedModel from TF Hub into a Keras model."
|
| 568 |
+
]
|
| 569 |
+
},
|
| 570 |
+
{
|
| 571 |
+
"cell_type": "code",
|
| 572 |
+
"execution_count": 15,
|
| 573 |
+
"metadata": {
|
| 574 |
+
"execution": {
|
| 575 |
+
"iopub.execute_input": "2021-01-13T03:08:07.428866Z",
|
| 576 |
+
"iopub.status.busy": "2021-01-13T03:08:07.427625Z",
|
| 577 |
+
"iopub.status.idle": "2021-01-13T03:08:10.467605Z",
|
| 578 |
+
"shell.execute_reply": "2021-01-13T03:08:10.468143Z"
|
| 579 |
+
},
|
| 580 |
+
"id": "0SQi-jWd_jzq"
|
| 581 |
+
},
|
| 582 |
+
"outputs": [],
|
| 583 |
+
"source": [
|
| 584 |
+
"bert_preprocess_model = hub.KerasLayer(tfhub_handle_preprocess)"
|
| 585 |
+
]
|
| 586 |
+
},
|
| 587 |
+
{
|
| 588 |
+
"cell_type": "markdown",
|
| 589 |
+
"metadata": {
|
| 590 |
+
"id": "x4naBiEE_cZX"
|
| 591 |
+
},
|
| 592 |
+
"source": [
|
| 593 |
+
"Let's try the preprocessing model on some text and see the output:"
|
| 594 |
+
]
|
| 595 |
+
},
|
| 596 |
+
{
|
| 597 |
+
"cell_type": "code",
|
| 598 |
+
"execution_count": 16,
|
| 599 |
+
"metadata": {},
|
| 600 |
+
"outputs": [
|
| 601 |
+
{
|
| 602 |
+
"data": {
|
| 603 |
+
"text/plain": [
|
| 604 |
+
"array(['listen to westbam alumb allergic on google music'], dtype=object)"
|
| 605 |
+
]
|
| 606 |
+
},
|
| 607 |
+
"execution_count": 16,
|
| 608 |
+
"metadata": {},
|
| 609 |
+
"output_type": "execute_result"
|
| 610 |
+
}
|
| 611 |
+
],
|
| 612 |
+
"source": [
|
| 613 |
+
"trainfeatures[0]"
|
| 614 |
+
]
|
| 615 |
+
},
|
| 616 |
+
{
|
| 617 |
+
"cell_type": "code",
|
| 618 |
+
"execution_count": 17,
|
| 619 |
+
"metadata": {
|
| 620 |
+
"execution": {
|
| 621 |
+
"iopub.execute_input": "2021-01-13T03:08:10.482448Z",
|
| 622 |
+
"iopub.status.busy": "2021-01-13T03:08:10.478061Z",
|
| 623 |
+
"iopub.status.idle": "2021-01-13T03:08:10.652900Z",
|
| 624 |
+
"shell.execute_reply": "2021-01-13T03:08:10.652248Z"
|
| 625 |
+
},
|
| 626 |
+
"id": "r9-zCzJpnuwS"
|
| 627 |
+
},
|
| 628 |
+
"outputs": [
|
| 629 |
+
{
|
| 630 |
+
"name": "stdout",
|
| 631 |
+
"output_type": "stream",
|
| 632 |
+
"text": [
|
| 633 |
+
"Keys : ['input_type_ids', 'input_mask', 'input_word_ids']\n",
|
| 634 |
+
"Shape : (1, 128)\n",
|
| 635 |
+
"Word Ids : [ 101 4952 2000 2225 3676 2213 2632 25438 27395 2006 8224 2189]\n",
|
| 636 |
+
"Input Mask : [1 1 1 1 1 1 1 1 1 1 1 1]\n",
|
| 637 |
+
"Type Ids : [0 0 0 0 0 0 0 0 0 0 0 0]\n"
|
| 638 |
+
]
|
| 639 |
+
}
|
| 640 |
+
],
|
| 641 |
+
"source": [
|
| 642 |
+
"text_test = trainfeatures[0]\n",
|
| 643 |
+
"text_preprocessed = bert_preprocess_model(text_test)\n",
|
| 644 |
+
"\n",
|
| 645 |
+
"print(f'Keys : {list(text_preprocessed.keys())}')\n",
|
| 646 |
+
"print(f'Shape : {text_preprocessed[\"input_word_ids\"].shape}')\n",
|
| 647 |
+
"print(f'Word Ids : {text_preprocessed[\"input_word_ids\"][0, :12]}')\n",
|
| 648 |
+
"print(f'Input Mask : {text_preprocessed[\"input_mask\"][0, :12]}')\n",
|
| 649 |
+
"print(f'Type Ids : {text_preprocessed[\"input_type_ids\"][0, :12]}')"
|
| 650 |
+
]
|
| 651 |
+
},
|
| 652 |
+
{
|
| 653 |
+
"cell_type": "markdown",
|
| 654 |
+
"metadata": {
|
| 655 |
+
"id": "EqL7ihkN_862"
|
| 656 |
+
},
|
| 657 |
+
"source": [
|
| 658 |
+
"As can be seen, there are 3 outputs from the preprocessing that a BERT model would use (`input_words_id`, `input_mask` and `input_type_ids`).\n",
|
| 659 |
+
"\n",
|
| 660 |
+
"Some other important points:\n",
|
| 661 |
+
"- The input is truncated to 128 tokens. The number of tokens can be customized and you can see more details on the [Solve GLUE tasks using BERT on a TPU colab](https://www.tensorflow.org/tutorials/text/solve_glue_tasks_using_bert_on_tpu).\n",
|
| 662 |
+
"- The `input_type_ids` only have one value (0) because this is a single sentence input. For a multiple sentence input, it would have one number for each input.\n",
|
| 663 |
+
"\n",
|
| 664 |
+
"Since this text preprocessor is a TensorFlow model, It can be included in your model directly."
|
| 665 |
+
]
|
| 666 |
+
},
|
| 667 |
+
{
|
| 668 |
+
"cell_type": "markdown",
|
| 669 |
+
"metadata": {
|
| 670 |
+
"id": "DKnLPSEmtp9i"
|
| 671 |
+
},
|
| 672 |
+
"source": [
|
| 673 |
+
"## Using the BERT model\n",
|
| 674 |
+
"\n",
|
| 675 |
+
"Before putting BERT into an own model, let's take a look at its outputs. You will load it from TF Hub and see the returned values."
|
| 676 |
+
]
|
| 677 |
+
},
|
| 678 |
+
{
|
| 679 |
+
"cell_type": "code",
|
| 680 |
+
"execution_count": 18,
|
| 681 |
+
"metadata": {
|
| 682 |
+
"execution": {
|
| 683 |
+
"iopub.execute_input": "2021-01-13T03:08:10.658519Z",
|
| 684 |
+
"iopub.status.busy": "2021-01-13T03:08:10.657556Z",
|
| 685 |
+
"iopub.status.idle": "2021-01-13T03:08:19.674983Z",
|
| 686 |
+
"shell.execute_reply": "2021-01-13T03:08:19.675465Z"
|
| 687 |
+
},
|
| 688 |
+
"id": "tXxYpK8ixL34"
|
| 689 |
+
},
|
| 690 |
+
"outputs": [],
|
| 691 |
+
"source": [
|
| 692 |
+
"bert_model = hub.KerasLayer(tfhub_handle_encoder)"
|
| 693 |
+
]
|
| 694 |
+
},
|
| 695 |
+
{
|
| 696 |
+
"cell_type": "code",
|
| 697 |
+
"execution_count": 19,
|
| 698 |
+
"metadata": {
|
| 699 |
+
"execution": {
|
| 700 |
+
"iopub.execute_input": "2021-01-13T03:08:19.682552Z",
|
| 701 |
+
"iopub.status.busy": "2021-01-13T03:08:19.681441Z",
|
| 702 |
+
"iopub.status.idle": "2021-01-13T03:08:20.297383Z",
|
| 703 |
+
"shell.execute_reply": "2021-01-13T03:08:20.297932Z"
|
| 704 |
+
},
|
| 705 |
+
"id": "_OoF9mebuSZc"
|
| 706 |
+
},
|
| 707 |
+
"outputs": [
|
| 708 |
+
{
|
| 709 |
+
"name": "stdout",
|
| 710 |
+
"output_type": "stream",
|
| 711 |
+
"text": [
|
| 712 |
+
"Loaded BERT: https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-512_A-8/1\n",
|
| 713 |
+
"Pooled Outputs Shape:(1, 512)\n",
|
| 714 |
+
"Pooled Outputs Values:[-0.04969434 -0.16525201 -0.99807066 -0.93279284 -0.6145217 -0.22613084\n",
|
| 715 |
+
" -0.95588505 -0.50678337 0.29122898 0.2631647 0.7982282 0.49405995]\n",
|
| 716 |
+
"Sequence Outputs Shape:(1, 128, 512)\n",
|
| 717 |
+
"Sequence Outputs Values:[[-0.1024768 0.22204846 0.59883934 ... -0.25584042 0.61985433\n",
|
| 718 |
+
" -0.01822574]\n",
|
| 719 |
+
" [ 0.4550366 -0.57238305 0.5542101 ... -0.28608793 1.3628979\n",
|
| 720 |
+
" 0.9131196 ]\n",
|
| 721 |
+
" [ 0.42473704 0.29045174 0.82693 ... 0.28371704 1.7948036\n",
|
| 722 |
+
" -0.36674204]\n",
|
| 723 |
+
" ...\n",
|
| 724 |
+
" [-0.46153253 0.02829356 0.51673454 ... -0.15035403 1.4651561\n",
|
| 725 |
+
" 0.6449582 ]\n",
|
| 726 |
+
" [ 0.7110826 1.0848484 0.66065294 ... 0.4794111 0.723307\n",
|
| 727 |
+
" -0.08312207]\n",
|
| 728 |
+
" [ 0.35558882 -0.3890488 0.5101847 ... 0.19970936 0.86474574\n",
|
| 729 |
+
" 0.12227032]]\n"
|
| 730 |
+
]
|
| 731 |
+
}
|
| 732 |
+
],
|
| 733 |
+
"source": [
|
| 734 |
+
"bert_results = bert_model(text_preprocessed)\n",
|
| 735 |
+
"\n",
|
| 736 |
+
"print(f'Loaded BERT: {tfhub_handle_encoder}')\n",
|
| 737 |
+
"print(f'Pooled Outputs Shape:{bert_results[\"pooled_output\"].shape}')\n",
|
| 738 |
+
"print(f'Pooled Outputs Values:{bert_results[\"pooled_output\"][0, :12]}')\n",
|
| 739 |
+
"print(f'Sequence Outputs Shape:{bert_results[\"sequence_output\"].shape}')\n",
|
| 740 |
+
"print(f'Sequence Outputs Values:{bert_results[\"sequence_output\"][0, :12]}')"
|
| 741 |
+
]
|
| 742 |
+
},
|
| 743 |
+
{
|
| 744 |
+
"cell_type": "markdown",
|
| 745 |
+
"metadata": {
|
| 746 |
+
"id": "sm61jDrezAll"
|
| 747 |
+
},
|
| 748 |
+
"source": [
|
| 749 |
+
"The BERT models return a map with 3 important keys: `pooled_output`, `sequence_output`, `encoder_outputs`:\n",
|
| 750 |
+
"\n",
|
| 751 |
+
"- `pooled_output` to represent each input sequence as a whole. The shape is `[batch_size, H]`. You can think of this as an embedding for the entire movie review.\n",
|
| 752 |
+
"- `sequence_output` represents each input token in the context. The shape is `[batch_size, seq_length, H]`. You can think of this as a contextual embedding for every token in the movie review.\n",
|
| 753 |
+
"- `encoder_outputs` are the intermediate activations of the `L` Transformer blocks. `outputs[\"encoder_outputs\"][i]` is a Tensor of shape `[batch_size, seq_length, 1024]` with the outputs of the i-th Transformer block, for `0 <= i < L`. The last value of the list is equal to `sequence_output`.\n",
|
| 754 |
+
"\n",
|
| 755 |
+
"For the fine-tuning you are going to use the `pooled_output` array."
|
| 756 |
+
]
|
| 757 |
+
},
|
| 758 |
+
{
|
| 759 |
+
"cell_type": "markdown",
|
| 760 |
+
"metadata": {
|
| 761 |
+
"id": "pDNKfAXbDnJH"
|
| 762 |
+
},
|
| 763 |
+
"source": [
|
| 764 |
+
"## Define your model\n",
|
| 765 |
+
"\n",
|
| 766 |
+
"You will create a very simple fine-tuned model, with the preprocessing model, the selected BERT model, one Dense and a Dropout layer.\n",
|
| 767 |
+
"\n",
|
| 768 |
+
"Note: for more information about the base model's input and output you can use just follow the model's url for documentation. Here specifically you don't need to worry about it because the preprocessing model will take care of that for you.\n"
|
| 769 |
+
]
|
| 770 |
+
},
|
| 771 |
+
{
|
| 772 |
+
"cell_type": "code",
|
| 773 |
+
"execution_count": 20,
|
| 774 |
+
"metadata": {
|
| 775 |
+
"execution": {
|
| 776 |
+
"iopub.execute_input": "2021-01-13T03:08:20.306302Z",
|
| 777 |
+
"iopub.status.busy": "2021-01-13T03:08:20.305016Z",
|
| 778 |
+
"iopub.status.idle": "2021-01-13T03:08:20.307988Z",
|
| 779 |
+
"shell.execute_reply": "2021-01-13T03:08:20.307291Z"
|
| 780 |
+
},
|
| 781 |
+
"id": "aksj743St9ga"
|
| 782 |
+
},
|
| 783 |
+
"outputs": [],
|
| 784 |
+
"source": [
|
| 785 |
+
"def build_classifier_model():\n",
|
| 786 |
+
" text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')\n",
|
| 787 |
+
" preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, name='preprocessing')\n",
|
| 788 |
+
" encoder_inputs = preprocessing_layer(text_input)\n",
|
| 789 |
+
" encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')\n",
|
| 790 |
+
" outputs = encoder(encoder_inputs)\n",
|
| 791 |
+
" net = outputs['pooled_output']\n",
|
| 792 |
+
" net = tf.keras.layers.Dropout(0.1)(net)\n",
|
| 793 |
+
" net = tf.keras.layers.Dense(7, activation=None, name='classifier')(net)\n",
|
| 794 |
+
" return tf.keras.Model(text_input, net)"
|
| 795 |
+
]
|
| 796 |
+
},
|
| 797 |
+
{
|
| 798 |
+
"cell_type": "markdown",
|
| 799 |
+
"metadata": {
|
| 800 |
+
"id": "Zs4yhFraBuGQ"
|
| 801 |
+
},
|
| 802 |
+
"source": [
|
| 803 |
+
"Let's check that the model runs with the output of the preprocessing model."
|
| 804 |
+
]
|
| 805 |
+
},
|
| 806 |
+
{
|
| 807 |
+
"cell_type": "code",
|
| 808 |
+
"execution_count": 21,
|
| 809 |
+
"metadata": {
|
| 810 |
+
"execution": {
|
| 811 |
+
"iopub.execute_input": "2021-01-13T03:08:20.317706Z",
|
| 812 |
+
"iopub.status.busy": "2021-01-13T03:08:20.316292Z",
|
| 813 |
+
"iopub.status.idle": "2021-01-13T03:08:27.279676Z",
|
| 814 |
+
"shell.execute_reply": "2021-01-13T03:08:27.279042Z"
|
| 815 |
+
},
|
| 816 |
+
"id": "mGMF8AZcB2Zy"
|
| 817 |
+
},
|
| 818 |
+
"outputs": [
|
| 819 |
+
{
|
| 820 |
+
"name": "stdout",
|
| 821 |
+
"output_type": "stream",
|
| 822 |
+
"text": [
|
| 823 |
+
"tf.Tensor(\n",
|
| 824 |
+
"[[0.15770487 0.21116826 0.03859028 0.04381749 0.03109054 0.0646299\n",
|
| 825 |
+
" 0.4529987 ]], shape=(1, 7), dtype=float32)\n"
|
| 826 |
+
]
|
| 827 |
+
}
|
| 828 |
+
],
|
| 829 |
+
"source": [
|
| 830 |
+
"classifier_model = build_classifier_model()\n",
|
| 831 |
+
"bert_raw_result = classifier_model(tf.constant(trainfeatures[0]))\n",
|
| 832 |
+
"print(tf.keras.activations.softmax(bert_raw_result))"
|
| 833 |
+
]
|
| 834 |
+
},
|
| 835 |
+
{
|
| 836 |
+
"cell_type": "markdown",
|
| 837 |
+
"metadata": {
|
| 838 |
+
"id": "ZTUzNV2JE2G3"
|
| 839 |
+
},
|
| 840 |
+
"source": [
|
| 841 |
+
"The output is meaningless, of course, because the model has not been trained yet.\n",
|
| 842 |
+
"\n",
|
| 843 |
+
"Let's take a look at the model's structure."
|
| 844 |
+
]
|
| 845 |
+
},
|
| 846 |
+
{
|
| 847 |
+
"cell_type": "code",
|
| 848 |
+
"execution_count": 22,
|
| 849 |
+
"metadata": {},
|
| 850 |
+
"outputs": [
|
| 851 |
+
{
|
| 852 |
+
"name": "stdout",
|
| 853 |
+
"output_type": "stream",
|
| 854 |
+
"text": [
|
| 855 |
+
"Model: \"model\"\n",
|
| 856 |
+
"__________________________________________________________________________________________________\n",
|
| 857 |
+
" Layer (type) Output Shape Param # Connected to \n",
|
| 858 |
+
"==================================================================================================\n",
|
| 859 |
+
" text (InputLayer) [(None,)] 0 [] \n",
|
| 860 |
+
" \n",
|
| 861 |
+
" preprocessing (KerasLayer) {'input_type_ids': 0 ['text[0][0]'] \n",
|
| 862 |
+
" (None, 128), \n",
|
| 863 |
+
" 'input_mask': (Non \n",
|
| 864 |
+
" e, 128), \n",
|
| 865 |
+
" 'input_word_ids': \n",
|
| 866 |
+
" (None, 128)} \n",
|
| 867 |
+
" \n",
|
| 868 |
+
" BERT_encoder (KerasLayer) {'encoder_outputs': 41373185 ['preprocessing[0][0]', \n",
|
| 869 |
+
" [(None, 128, 512), 'preprocessing[0][1]', \n",
|
| 870 |
+
" (None, 128, 512), 'preprocessing[0][2]'] \n",
|
| 871 |
+
" (None, 128, 512), \n",
|
| 872 |
+
" (None, 128, 512), \n",
|
| 873 |
+
" (None, 128, 512), \n",
|
| 874 |
+
" (None, 128, 512), \n",
|
| 875 |
+
" (None, 128, 512), \n",
|
| 876 |
+
" (None, 128, 512)], \n",
|
| 877 |
+
" 'default': (None, \n",
|
| 878 |
+
" 512), \n",
|
| 879 |
+
" 'sequence_output': \n",
|
| 880 |
+
" (None, 128, 512), \n",
|
| 881 |
+
" 'pooled_output': ( \n",
|
| 882 |
+
" None, 512)} \n",
|
| 883 |
+
" \n",
|
| 884 |
+
" dropout (Dropout) (None, 512) 0 ['BERT_encoder[0][9]'] \n",
|
| 885 |
+
" \n",
|
| 886 |
+
" classifier (Dense) (None, 7) 3591 ['dropout[0][0]'] \n",
|
| 887 |
+
" \n",
|
| 888 |
+
"==================================================================================================\n",
|
| 889 |
+
"Total params: 41,376,776\n",
|
| 890 |
+
"Trainable params: 41,376,775\n",
|
| 891 |
+
"Non-trainable params: 1\n",
|
| 892 |
+
"__________________________________________________________________________________________________\n"
|
| 893 |
+
]
|
| 894 |
+
}
|
| 895 |
+
],
|
| 896 |
+
"source": [
|
| 897 |
+
"classifier_model.summary()"
|
| 898 |
+
]
|
| 899 |
+
},
|
| 900 |
+
{
|
| 901 |
+
"cell_type": "markdown",
|
| 902 |
+
"metadata": {
|
| 903 |
+
"id": "WbUWoZMwc302"
|
| 904 |
+
},
|
| 905 |
+
"source": [
|
| 906 |
+
"## Model training\n",
|
| 907 |
+
"\n",
|
| 908 |
+
"You now have all the pieces to train a model, including the preprocessing module, BERT encoder, data, and classifier."
|
| 909 |
+
]
|
| 910 |
+
},
|
| 911 |
+
{
|
| 912 |
+
"cell_type": "markdown",
|
| 913 |
+
"metadata": {
|
| 914 |
+
"id": "WpJ3xcwDT56v"
|
| 915 |
+
},
|
| 916 |
+
"source": [
|
| 917 |
+
"Since this is a non-binary classification problem and the model outputs probabilities, you'll use `losses.CategoricalCrossentropy` loss function.\n"
|
| 918 |
+
]
|
| 919 |
+
},
|
| 920 |
+
{
|
| 921 |
+
"cell_type": "code",
|
| 922 |
+
"execution_count": 23,
|
| 923 |
+
"metadata": {
|
| 924 |
+
"execution": {
|
| 925 |
+
"iopub.execute_input": "2021-01-13T03:08:27.596402Z",
|
| 926 |
+
"iopub.status.busy": "2021-01-13T03:08:27.595622Z",
|
| 927 |
+
"iopub.status.idle": "2021-01-13T03:08:27.600436Z",
|
| 928 |
+
"shell.execute_reply": "2021-01-13T03:08:27.600889Z"
|
| 929 |
+
},
|
| 930 |
+
"id": "OWPOZE-L3AgE"
|
| 931 |
+
},
|
| 932 |
+
"outputs": [],
|
| 933 |
+
"source": [
|
| 934 |
+
"loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)\n",
|
| 935 |
+
"metrics = tf.metrics.CategoricalAccuracy()"
|
| 936 |
+
]
|
| 937 |
+
},
|
| 938 |
+
{
|
| 939 |
+
"cell_type": "markdown",
|
| 940 |
+
"metadata": {
|
| 941 |
+
"id": "SqlarlpC_v0g"
|
| 942 |
+
},
|
| 943 |
+
"source": [
|
| 944 |
+
"### Loading the BERT model and training\n",
|
| 945 |
+
"\n",
|
| 946 |
+
"Using the `classifier_model` you created earlier, you can compile the model with the loss, metric and optimizer."
|
| 947 |
+
]
|
| 948 |
+
},
|
| 949 |
+
{
|
| 950 |
+
"cell_type": "code",
|
| 951 |
+
"execution_count": 24,
|
| 952 |
+
"metadata": {
|
| 953 |
+
"execution": {
|
| 954 |
+
"iopub.execute_input": "2021-01-13T03:08:27.621858Z",
|
| 955 |
+
"iopub.status.busy": "2021-01-13T03:08:27.621180Z",
|
| 956 |
+
"iopub.status.idle": "2021-01-13T03:08:27.631397Z",
|
| 957 |
+
"shell.execute_reply": "2021-01-13T03:08:27.631841Z"
|
| 958 |
+
},
|
| 959 |
+
"id": "-7GPDhR98jsD"
|
| 960 |
+
},
|
| 961 |
+
"outputs": [],
|
| 962 |
+
"source": [
|
| 963 |
+
"epochs=5\n",
|
| 964 |
+
"optimizer=tf.keras.optimizers.Adam(1e-5)\n",
|
| 965 |
+
"classifier_model.compile(optimizer=optimizer,\n",
|
| 966 |
+
" loss=loss,\n",
|
| 967 |
+
" metrics=metrics)"
|
| 968 |
+
]
|
| 969 |
+
},
|
| 970 |
+
{
|
| 971 |
+
"cell_type": "markdown",
|
| 972 |
+
"metadata": {
|
| 973 |
+
"id": "CpBuV5j2cS_b"
|
| 974 |
+
},
|
| 975 |
+
"source": [
|
| 976 |
+
"Note: training time will vary depending on the complexity of the BERT model you have selected."
|
| 977 |
+
]
|
| 978 |
+
},
|
| 979 |
+
{
|
| 980 |
+
"cell_type": "code",
|
| 981 |
+
"execution_count": 25,
|
| 982 |
+
"metadata": {
|
| 983 |
+
"execution": {
|
| 984 |
+
"iopub.execute_input": "2021-01-13T03:08:27.636326Z",
|
| 985 |
+
"iopub.status.busy": "2021-01-13T03:08:27.635519Z",
|
| 986 |
+
"iopub.status.idle": "2021-01-13T03:15:50.893395Z",
|
| 987 |
+
"shell.execute_reply": "2021-01-13T03:15:50.893900Z"
|
| 988 |
+
},
|
| 989 |
+
"id": "HtfDFAnN_Neu",
|
| 990 |
+
"scrolled": true
|
| 991 |
+
},
|
| 992 |
+
"outputs": [
|
| 993 |
+
{
|
| 994 |
+
"name": "stdout",
|
| 995 |
+
"output_type": "stream",
|
| 996 |
+
"text": [
|
| 997 |
+
"Training model with https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-8_H-512_A-8/1\n",
|
| 998 |
+
"Epoch 1/5\n",
|
| 999 |
+
" 11/409 [..............................] - ETA: 1:40:36 - loss: 1.9467 - categorical_accuracy: 0.2159"
|
| 1000 |
+
]
|
| 1001 |
+
}
|
| 1002 |
+
],
|
| 1003 |
+
"source": [
|
| 1004 |
+
"print(f'Training model with {tfhub_handle_encoder}')\n",
|
| 1005 |
+
"history = classifier_model.fit(x=trainfeatures,y=trainlabels,\n",
|
| 1006 |
+
" validation_data=(validfeatures,validlabels),\n",
|
| 1007 |
+
" batch_size=32,\n",
|
| 1008 |
+
" epochs=epochs)"
|
| 1009 |
+
]
|
| 1010 |
+
},
|
| 1011 |
+
{
|
| 1012 |
+
"cell_type": "markdown",
|
| 1013 |
+
"metadata": {
|
| 1014 |
+
"id": "uBthMlTSV8kn"
|
| 1015 |
+
},
|
| 1016 |
+
"source": [
|
| 1017 |
+
"### Evaluate the model\n",
|
| 1018 |
+
"\n",
|
| 1019 |
+
"Let's see how the model performs. Two values will be returned. Loss (a number which represents the error, lower values are better), and accuracy."
|
| 1020 |
+
]
|
| 1021 |
+
},
|
| 1022 |
+
{
|
| 1023 |
+
"cell_type": "code",
|
| 1024 |
+
"execution_count": null,
|
| 1025 |
+
"metadata": {
|
| 1026 |
+
"execution": {
|
| 1027 |
+
"iopub.execute_input": "2021-01-13T03:15:50.898967Z",
|
| 1028 |
+
"iopub.status.busy": "2021-01-13T03:15:50.898282Z",
|
| 1029 |
+
"iopub.status.idle": "2021-01-13T03:16:53.613847Z",
|
| 1030 |
+
"shell.execute_reply": "2021-01-13T03:16:53.614285Z"
|
| 1031 |
+
},
|
| 1032 |
+
"id": "slqB-urBV9sP"
|
| 1033 |
+
},
|
| 1034 |
+
"outputs": [],
|
| 1035 |
+
"source": [
|
| 1036 |
+
"loss, accuracy = classifier_model.evaluate(testfeatures,testlabels)\n",
|
| 1037 |
+
"\n",
|
| 1038 |
+
"print(f'Loss: {loss}')\n",
|
| 1039 |
+
"print(f'Accuracy: {accuracy}')"
|
| 1040 |
+
]
|
| 1041 |
+
},
|
| 1042 |
+
{
|
| 1043 |
+
"cell_type": "markdown",
|
| 1044 |
+
"metadata": {
|
| 1045 |
+
"id": "uttWpgmSfzq9"
|
| 1046 |
+
},
|
| 1047 |
+
"source": [
|
| 1048 |
+
"### Plot the accuracy and loss over time\n",
|
| 1049 |
+
"\n",
|
| 1050 |
+
"Based on the `History` object returned by `model.fit()`. You can plot the training and validation loss for comparison, as well as the training and validation accuracy:"
|
| 1051 |
+
]
|
| 1052 |
+
},
|
| 1053 |
+
{
|
| 1054 |
+
"cell_type": "code",
|
| 1055 |
+
"execution_count": null,
|
| 1056 |
+
"metadata": {
|
| 1057 |
+
"execution": {
|
| 1058 |
+
"iopub.execute_input": "2021-01-13T03:16:53.641616Z",
|
| 1059 |
+
"iopub.status.busy": "2021-01-13T03:16:53.638634Z",
|
| 1060 |
+
"iopub.status.idle": "2021-01-13T03:16:53.950276Z",
|
| 1061 |
+
"shell.execute_reply": "2021-01-13T03:16:53.950768Z"
|
| 1062 |
+
},
|
| 1063 |
+
"id": "fiythcODf0xo"
|
| 1064 |
+
},
|
| 1065 |
+
"outputs": [],
|
| 1066 |
+
"source": [
|
| 1067 |
+
"history_dict = history.history\n",
|
| 1068 |
+
"print(history_dict.keys())\n",
|
| 1069 |
+
"\n",
|
| 1070 |
+
"acc = history_dict['categorical_accuracy']\n",
|
| 1071 |
+
"val_acc = history_dict['val_categorical_accuracy']\n",
|
| 1072 |
+
"loss = history_dict['loss']\n",
|
| 1073 |
+
"val_loss = history_dict['val_loss']\n",
|
| 1074 |
+
"\n",
|
| 1075 |
+
"epochs = range(1, len(acc) + 1)\n",
|
| 1076 |
+
"fig = plt.figure(figsize=(10, 8))\n",
|
| 1077 |
+
"fig.tight_layout()\n",
|
| 1078 |
+
"\n",
|
| 1079 |
+
"plt.subplot(2, 1, 1)\n",
|
| 1080 |
+
"# \"bo\" is for \"blue dot\"\n",
|
| 1081 |
+
"plt.plot(epochs, loss, 'r', label='Training loss')\n",
|
| 1082 |
+
"# b is for \"solid blue line\"\n",
|
| 1083 |
+
"plt.plot(epochs, val_loss, 'b', label='Validation loss')\n",
|
| 1084 |
+
"plt.title('Training and validation loss')\n",
|
| 1085 |
+
"plt.grid(True)\n",
|
| 1086 |
+
"# plt.xlabel('Epochs')\n",
|
| 1087 |
+
"plt.ylabel('Loss')\n",
|
| 1088 |
+
"plt.legend()\n",
|
| 1089 |
+
"\n",
|
| 1090 |
+
"plt.subplot(2, 1, 2)\n",
|
| 1091 |
+
"plt.plot(epochs, acc, 'r', label='Training acc')\n",
|
| 1092 |
+
"plt.plot(epochs, val_acc, 'b', label='Validation acc')\n",
|
| 1093 |
+
"plt.title('Training and validation accuracy')\n",
|
| 1094 |
+
"plt.grid(True)\n",
|
| 1095 |
+
"plt.xlabel('Epochs')\n",
|
| 1096 |
+
"plt.ylabel('Accuracy')\n",
|
| 1097 |
+
"plt.legend(loc='lower right')"
|
| 1098 |
+
]
|
| 1099 |
+
},
|
| 1100 |
+
{
|
| 1101 |
+
"cell_type": "markdown",
|
| 1102 |
+
"metadata": {
|
| 1103 |
+
"id": "WzJZCo-cf-Jf"
|
| 1104 |
+
},
|
| 1105 |
+
"source": [
|
| 1106 |
+
"In this plot, the red lines represents the training loss and accuracy, and the blue lines are the validation loss and accuracy."
|
| 1107 |
+
]
|
| 1108 |
+
},
|
| 1109 |
+
{
|
| 1110 |
+
"cell_type": "markdown",
|
| 1111 |
+
"metadata": {
|
| 1112 |
+
"id": "oyTappHTvNCz"
|
| 1113 |
+
},
|
| 1114 |
+
"source": [
|
| 1115 |
+
"Classifying arbitrary instructions:"
|
| 1116 |
+
]
|
| 1117 |
+
},
|
| 1118 |
+
{
|
| 1119 |
+
"cell_type": "code",
|
| 1120 |
+
"execution_count": null,
|
| 1121 |
+
"metadata": {
|
| 1122 |
+
"execution": {
|
| 1123 |
+
"iopub.execute_input": "2021-01-13T03:17:08.070832Z",
|
| 1124 |
+
"iopub.status.busy": "2021-01-13T03:17:08.068184Z",
|
| 1125 |
+
"iopub.status.idle": "2021-01-13T03:17:08.879352Z",
|
| 1126 |
+
"shell.execute_reply": "2021-01-13T03:17:08.878815Z"
|
| 1127 |
+
},
|
| 1128 |
+
"id": "VBWzH6exlCPS"
|
| 1129 |
+
},
|
| 1130 |
+
"outputs": [],
|
| 1131 |
+
"source": [
|
| 1132 |
+
"def print_my_examples(inputs, results):\n",
|
| 1133 |
+
" result_for_printing = \\\n",
|
| 1134 |
+
" [f'input: {inputs[i]:<30} : estimated intent: {results[i]}'\n",
|
| 1135 |
+
" for i in range(len(inputs))]\n",
|
| 1136 |
+
" print(*result_for_printing, sep='\\n')\n",
|
| 1137 |
+
" print()\n",
|
| 1138 |
+
"\n",
|
| 1139 |
+
"\n",
|
| 1140 |
+
"examples = [\n",
|
| 1141 |
+
" 'play a song from U2', # this is the same sentence tried earlier\n",
|
| 1142 |
+
" 'Will it rain tomorrow',\n",
|
| 1143 |
+
" 'I like to hear greatist hits from beastie boys',\n",
|
| 1144 |
+
" 'I like to book a table for 3 persons',\n",
|
| 1145 |
+
" '5 stars for machines like me'\n",
|
| 1146 |
+
"]\n",
|
| 1147 |
+
"\n",
|
| 1148 |
+
"results = tf.nn.softmax(classifier_model(tf.constant(examples)))"
|
| 1149 |
+
]
|
| 1150 |
+
},
|
| 1151 |
+
{
|
| 1152 |
+
"cell_type": "code",
|
| 1153 |
+
"execution_count": null,
|
| 1154 |
+
"metadata": {},
|
| 1155 |
+
"outputs": [],
|
| 1156 |
+
"source": [
|
| 1157 |
+
"binarizer.classes_"
|
| 1158 |
+
]
|
| 1159 |
+
},
|
| 1160 |
+
{
|
| 1161 |
+
"cell_type": "code",
|
| 1162 |
+
"execution_count": null,
|
| 1163 |
+
"metadata": {},
|
| 1164 |
+
"outputs": [],
|
| 1165 |
+
"source": [
|
| 1166 |
+
"intents=binarizer.inverse_transform(results.numpy())"
|
| 1167 |
+
]
|
| 1168 |
+
},
|
| 1169 |
+
{
|
| 1170 |
+
"cell_type": "code",
|
| 1171 |
+
"execution_count": null,
|
| 1172 |
+
"metadata": {
|
| 1173 |
+
"execution": {
|
| 1174 |
+
"iopub.execute_input": "2021-01-13T03:17:08.070832Z",
|
| 1175 |
+
"iopub.status.busy": "2021-01-13T03:17:08.068184Z",
|
| 1176 |
+
"iopub.status.idle": "2021-01-13T03:17:08.879352Z",
|
| 1177 |
+
"shell.execute_reply": "2021-01-13T03:17:08.878815Z"
|
| 1178 |
+
},
|
| 1179 |
+
"id": "VBWzH6exlCPS"
|
| 1180 |
+
},
|
| 1181 |
+
"outputs": [],
|
| 1182 |
+
"source": [
|
| 1183 |
+
"print_my_examples(examples, intents)"
|
| 1184 |
+
]
|
| 1185 |
+
},
|
| 1186 |
+
{
|
| 1187 |
+
"cell_type": "code",
|
| 1188 |
+
"execution_count": null,
|
| 1189 |
+
"metadata": {},
|
| 1190 |
+
"outputs": [],
|
| 1191 |
+
"source": []
|
| 1192 |
+
}
|
| 1193 |
+
],
|
| 1194 |
+
"metadata": {
|
| 1195 |
+
"accelerator": "GPU",
|
| 1196 |
+
"colab": {
|
| 1197 |
+
"collapsed_sections": [],
|
| 1198 |
+
"name": "classify_text_with_bert.ipynb",
|
| 1199 |
+
"toc_visible": true
|
| 1200 |
+
},
|
| 1201 |
+
"kernelspec": {
|
| 1202 |
+
"display_name": "Python 3.8.10 ('caesarainl': venv)",
|
| 1203 |
+
"language": "python",
|
| 1204 |
+
"name": "python3"
|
| 1205 |
+
},
|
| 1206 |
+
"language_info": {
|
| 1207 |
+
"codemirror_mode": {
|
| 1208 |
+
"name": "ipython",
|
| 1209 |
+
"version": 3
|
| 1210 |
+
},
|
| 1211 |
+
"file_extension": ".py",
|
| 1212 |
+
"mimetype": "text/x-python",
|
| 1213 |
+
"name": "python",
|
| 1214 |
+
"nbconvert_exporter": "python",
|
| 1215 |
+
"pygments_lexer": "ipython3",
|
| 1216 |
+
"version": "3.8.10"
|
| 1217 |
+
},
|
| 1218 |
+
"toc": {
|
| 1219 |
+
"base_numbering": 1,
|
| 1220 |
+
"nav_menu": {},
|
| 1221 |
+
"number_sections": true,
|
| 1222 |
+
"sideBar": true,
|
| 1223 |
+
"skip_h1_title": false,
|
| 1224 |
+
"title_cell": "Table of Contents",
|
| 1225 |
+
"title_sidebar": "Contents",
|
| 1226 |
+
"toc_cell": false,
|
| 1227 |
+
"toc_position": {},
|
| 1228 |
+
"toc_section_display": true,
|
| 1229 |
+
"toc_window_display": false
|
| 1230 |
+
},
|
| 1231 |
+
"vscode": {
|
| 1232 |
+
"interpreter": {
|
| 1233 |
+
"hash": "405f94f3dc67f15d7addffc962b8172d0d4960d3524e541166097ab2d7a328e0"
|
| 1234 |
+
}
|
| 1235 |
+
}
|
| 1236 |
+
},
|
| 1237 |
+
"nbformat": 4,
|
| 1238 |
+
"nbformat_minor": 1
|
| 1239 |
+
}
|
caesar_tensorflow_install.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Caesar Tensorflow Install
|
| 2 |
+
# Tensorflow-gpu source versions
|
| 3 |
+
# https://www.tensorflow.org/install/source#gpu
|
| 4 |
+
|
| 5 |
+
# Use Youtube video
|
| 6 |
+
# MiniConda
|
| 7 |
+
# https://docs.conda.io/en/main/miniconda.html
|
| 8 |
+
# Cuda Toolkit
|
| 9 |
+
# https://developer.nvidia.com/cuda-11.2.2-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exenetwork
|
| 10 |
+
|
| 11 |
+
# Cuda Deep Neural Network cudnn
|
| 12 |
+
# https://developer.nvidia.com/rdp/cudnn-archive
|
caesarapis.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pyttsx3
|
| 3 |
+
engine=pyttsx3.init('sapi5')
|
| 4 |
+
voices=engine.getProperty('voices')
|
| 5 |
+
engine.setProperty('voice',voices[1].id)
|
| 6 |
+
|
| 7 |
+
def speak(text,whisper_mode=None):
|
| 8 |
+
if whisper_mode == 0:
|
| 9 |
+
engine.say(text)
|
| 10 |
+
engine.runAndWait()
|
| 11 |
+
|
| 12 |
+
class CaesarAPIs:
|
| 13 |
+
def __init__(self) -> None:
|
| 14 |
+
self.whisper_mode = 0
|
| 15 |
+
def runapis(self,caesarResponse=None,intent=None,userinput=None):
|
| 16 |
+
if intent == "whisper_mode" and "on" in userinput:
|
| 17 |
+
self.whisper_mode = 1
|
| 18 |
+
speak("I will be quiet now sir",0)
|
| 19 |
+
elif intent == "whisper_mode" and "off" in userinput:
|
| 20 |
+
self.whisper_mode = 0
|
| 21 |
+
speak("Can you hear me now sir",0)
|
| 22 |
+
#elif
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
caesarberthubmodels/bert_to_handle.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:909489abdc059a3f1144c6f69ce064fd0a0932d5d0c4e373f83eb5995831d990
|
| 3 |
+
size 4066
|
caesarberthubmodels/bert_to_preprocess.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a5c24a7df82f9d8d5b5ec8f94c2b3985e7150bfb30428a3179fd2cc0850c5539
|
| 3 |
+
size 3758
|
caesarcomplete/berttest.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow_hub as hub
|
| 2 |
+
|
| 3 |
+
BERT_URL = 'https://tfhub.dev/google/bert_cased_L-12_H-768_A-12/1'
|
| 4 |
+
module = hub.Module(BERT_URL)
|
| 5 |
+
|
| 6 |
+
# Look at the descriptor. This would tell you the model name
|
| 7 |
+
# cat $TFHUB_CACHE_DIR/ecd2596ce849110246602e3d4d81e2d9719cb027.descriptor.txt
|
| 8 |
+
|
| 9 |
+
# Further look at the assets folder, this has the file `vocab.txt`
|
| 10 |
+
# ls $TFHUB_CACHE_DIR/ecd2596ce849110246602e3d4d81e2d9719cb027/assets
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import tokenization
|
| 14 |
+
|
| 15 |
+
def create_tokenizer(vocab_file, do_lower_case=False):
|
| 16 |
+
return tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)
|
| 17 |
+
|
| 18 |
+
tokenizer = create_tokenizer('vocab.txt', do_lower_case=False)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def convert_sentence_to_features(sentence, tokenizer, max_seq_len):
|
| 22 |
+
tokens = ['[CLS]']
|
| 23 |
+
tokens.extend(tokenizer.tokenize(sentence))
|
| 24 |
+
if len(tokens) > max_seq_len-1:
|
| 25 |
+
tokens = tokens[:max_seq_len-1]
|
| 26 |
+
tokens.append('[SEP]')
|
| 27 |
+
|
| 28 |
+
segment_ids = [0] * len(tokens)
|
| 29 |
+
input_ids = tokenizer.convert_tokens_to_ids(tokens)
|
| 30 |
+
input_mask = [1] * len(input_ids)
|
| 31 |
+
|
| 32 |
+
#Zero Mask till seq_length
|
| 33 |
+
zero_mask = [0] * (max_seq_len-len(tokens))
|
| 34 |
+
input_ids.extend(zero_mask)
|
| 35 |
+
input_mask.extend(zero_mask)
|
| 36 |
+
segment_ids.extend(zero_mask)
|
| 37 |
+
|
| 38 |
+
return input_ids, input_mask, segment_ids
|
| 39 |
+
|
| 40 |
+
def convert_sentences_to_features(sentences, tokenizer, max_seq_len=20):
|
| 41 |
+
all_input_ids = []
|
| 42 |
+
all_input_mask = []
|
| 43 |
+
all_segment_ids = []
|
| 44 |
+
|
| 45 |
+
for sentence in sentences:
|
| 46 |
+
input_ids, input_mask, segment_ids = convert_sentence_to_features(sentence, tokenizer, max_seq_len)
|
| 47 |
+
all_input_ids.append(input_ids)
|
| 48 |
+
all_input_mask.append(input_mask)
|
| 49 |
+
all_segment_ids.append(segment_ids)
|
| 50 |
+
|
| 51 |
+
return all_input_ids, all_input_mask, all_segment_ids
|
caesarcomplete/caesar_tensorflow_install.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Caesar Tensorflow Install
|
| 2 |
+
# Tensorflow-gpu source versions
|
| 3 |
+
# https://www.tensorflow.org/install/source#gpu
|
| 4 |
+
|
| 5 |
+
# Use Youtube video
|
| 6 |
+
# MiniConda
|
| 7 |
+
# https://docs.conda.io/en/main/miniconda.html
|
| 8 |
+
# Cuda Toolkit
|
| 9 |
+
# https://developer.nvidia.com/cuda-11.2.2-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exenetwork
|
| 10 |
+
|
| 11 |
+
# Cuda Deep Neural Network cudnn
|
| 12 |
+
# https://developer.nvidia.com/rdp/cudnn-archive
|
caesarcomplete/caesarapis.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
class CaesarAPIs:
|
| 3 |
+
def __init__(self) -> None:
|
| 4 |
+
self.whisper_mode = 0
|
| 5 |
+
def runapis(self,caesarResponse=None,intent=None,userinput=None,voiceengine=None):
|
| 6 |
+
if intent == "whisper_mode" and "on" in userinput:
|
| 7 |
+
self.whisper_mode = 1
|
| 8 |
+
voiceengine.say("I will be quiet now sir")
|
| 9 |
+
voiceengine.runAndWait()
|
| 10 |
+
elif intent == "whisper_mode" and "off" in userinput:
|
| 11 |
+
self.whisper_mode = 0
|
| 12 |
+
voiceengine.say("Can you hear me now sir")
|
| 13 |
+
voiceengine.runAndWait()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
caesarcomplete/caesarnlexamples.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import json
|
| 3 |
+
import spacy
|
| 4 |
+
import pickle
|
| 5 |
+
import random
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
import tensorflow_hub as hub
|
| 8 |
+
import tensorflow_text as text
|
| 9 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 10 |
+
|
| 11 |
+
def print_my_examples(inputs, results):
|
| 12 |
+
result_for_printing = \
|
| 13 |
+
[f'input: {inputs[i]:<30} : estimated intent: {results[i]}'
|
| 14 |
+
for i in range(len(inputs))]
|
| 15 |
+
print(*result_for_printing, sep='\n')
|
| 16 |
+
print()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
examples = [
|
| 20 |
+
'play a song from U2', # this is the same sentence tried earlier
|
| 21 |
+
'Will it rain tomorrow',
|
| 22 |
+
'I like to hear greatist hits from beastie boys',
|
| 23 |
+
'I like to book a table for 3 persons',
|
| 24 |
+
'5 stars for machines like me',
|
| 25 |
+
'play a boogie wit da hoodie',
|
| 26 |
+
"play Bob's favorite song",
|
| 27 |
+
"give me a hug",
|
| 28 |
+
"hello"
|
| 29 |
+
]
|
| 30 |
+
greetings = ["Greeting","smalltalk_greetings_hello"]
|
| 31 |
+
courtesy_greeting = ["CourtesyGreeting"]
|
| 32 |
+
stored_name = "Amari"
|
| 33 |
+
examples = ["hello"]
|
| 34 |
+
#nlp = spacy.load("en_core_web_sm")
|
| 35 |
+
classifier_model = tf.keras.models.load_model('caesarmodel/caesarnl.h5',custom_objects={'KerasLayer':hub.KerasLayer})
|
| 36 |
+
|
| 37 |
+
# Show the model architecture
|
| 38 |
+
results = tf.nn.softmax(classifier_model(tf.constant(examples)))
|
| 39 |
+
with open("caesarmodel/labelbinarizer.pkl","rb") as f:
|
| 40 |
+
binarizer = pickle.load(f)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
intents=binarizer.inverse_transform(results.numpy())
|
| 44 |
+
with open("intentdata/responses.json","r") as f:
|
| 45 |
+
responses = json.load(f)["responses"]
|
| 46 |
+
|
| 47 |
+
if intents[0] in greetings:
|
| 48 |
+
greetresponse = random.choice(responses["Greeting"]).replace("<HUMAN>",stored_name)
|
| 49 |
+
print(greetresponse)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
#sentence_intents = dict(zip(examples,intents))
|
| 54 |
+
#print(sentence_intents)
|
| 55 |
+
#print_my_examples(examples, intents)
|
| 56 |
+
|
| 57 |
+
# TODO AIM - Implement Chatbot Gossip to Caesar
|
| 58 |
+
# 1. Add data to datasets train | valid | test
|
| 59 |
+
# a. then clean labels
|
| 60 |
+
# 2. Augment data to provide more potential possibilites
|
| 61 |
+
# 3. Use BERT to match input with the response
|
| 62 |
+
# Command Labels - AddToPlaylist | GetWeather -> API -> user
|
| 63 |
+
# Conversation Labes - Greeting | Goodbye -> BERTNN: input:"hello" => response:"hi there, I am caesar" -> user
|
| 64 |
+
|
| 65 |
+
# TODO AIM - Single names of songs artists like "play a boogie" and it will play a boogie's music.
|
| 66 |
+
# 1. Idea one - NER detect the named entities
|
| 67 |
+
# 2. Create new Neural Network that detects that. * Have to determine the relationship between the entites
|
| 68 |
+
|
| 69 |
+
|
caesarcomplete/data_aggregation.ipynb
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import pandas as pd\n",
|
| 10 |
+
"import json\n",
|
| 11 |
+
"smalltalkintent = pd.read_csv(\"intentdata/Small_talk_Intent.csv\").rename(columns={\"Utterances\":\"text\",\"Intent\":\"intent\"})\n",
|
| 12 |
+
"training = pd.read_csv(\"intentdata/train_command.csv\")\n",
|
| 13 |
+
"df3 = pd.concat([training,smalltalkintent], ignore_index=True)\n",
|
| 14 |
+
"df3.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)\n",
|
| 15 |
+
"# TODO Do NLPAugmentation\n",
|
| 16 |
+
"#df3 = pd.concat([training,smalltalkintent], ignore_index=True)\n",
|
| 17 |
+
"#with open(\"intentdata/Intent.json\",\"r\") as f:\n",
|
| 18 |
+
"# chatbotXresponseintent = json.load(f)\n",
|
| 19 |
+
"#chatbotXresponseintent[\"intents\"][0] \n"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "code",
|
| 24 |
+
"execution_count": null,
|
| 25 |
+
"metadata": {},
|
| 26 |
+
"outputs": [],
|
| 27 |
+
"source": [
|
| 28 |
+
"# Produce responses\n",
|
| 29 |
+
"import json \n",
|
| 30 |
+
"with open(\"intentdata/Intent.json\",\"r\") as f:\n",
|
| 31 |
+
" data = json.load(f)\n",
|
| 32 |
+
"intents = []\n",
|
| 33 |
+
"for intent in data[\"intents\"]:\n",
|
| 34 |
+
" intents.append({\"intent\":intent[\"intent\"],\"responses\":intent[\"responses\"]})\n",
|
| 35 |
+
"with open(\"intentdata/responses.json\",\"w+\") as f:\n",
|
| 36 |
+
" json.dump({\"response\":intents},f)\n",
|
| 37 |
+
" "
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"source": [
|
| 46 |
+
"with open(\"intentdata/responses.json\",\"r\") as f:\n",
|
| 47 |
+
" responses = json.load(f)[\"responses\"]\n",
|
| 48 |
+
"#print(intents[0] in greetings)\n",
|
| 49 |
+
"#if intents[0] in greetings:\n",
|
| 50 |
+
"print(responses[\"Greeting\"])"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"execution_count": null,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"import json\n",
|
| 60 |
+
"import pandas as pd \n",
|
| 61 |
+
"from IPython.display import display\n",
|
| 62 |
+
"with open(\"intentdata/aug/intent_aug_text.json\",\"r\") as f:\n",
|
| 63 |
+
" data = json.load(f)[\"intents\"]\n",
|
| 64 |
+
"columns_to_concat = []\n",
|
| 65 |
+
"for i in range(len(data)):\n",
|
| 66 |
+
" column1 = pd.DataFrame.from_dict({\"text\":data[i][\"text\"]})\n",
|
| 67 |
+
" #print(column1)\n",
|
| 68 |
+
" column2 = pd.DataFrame.from_dict({\"intent\":[data[i][\"intent\"] for j in range(len(data[i][\"text\"]))]})\n",
|
| 69 |
+
" #print(column2)\n",
|
| 70 |
+
" tent_concat= pd.concat([column1,column2],axis=1)\n",
|
| 71 |
+
" #print(tent_concat)\n",
|
| 72 |
+
" columns_to_concat.append(tent_concat)\n",
|
| 73 |
+
"intentdf = pd.concat(columns_to_concat,axis=0)\n",
|
| 74 |
+
"intentdf\n",
|
| 75 |
+
"df = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 76 |
+
"newdf = pd.concat([df,intentdf],axis=0)\n",
|
| 77 |
+
"newdf.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"\n"
|
| 80 |
+
]
|
| 81 |
+
},
|
| 82 |
+
{
|
| 83 |
+
"cell_type": "code",
|
| 84 |
+
"execution_count": null,
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"outputs": [],
|
| 87 |
+
"source": [
|
| 88 |
+
"import pandas as pd\n",
|
| 89 |
+
"data = pd.read_csv(\"intentdata/train.csv\")\n"
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"cell_type": "code",
|
| 94 |
+
"execution_count": null,
|
| 95 |
+
"metadata": {},
|
| 96 |
+
"outputs": [],
|
| 97 |
+
"source": [
|
| 98 |
+
"import pandas as pd\n",
|
| 99 |
+
"data = pd.read_csv(\"intentdata/train_no_half_response.csv\") # Original data is a (2000, 7) DataFrame\n",
|
| 100 |
+
"data = data.replace(\"smalltalk_agent_acquaintance\",\"CourtesyGreeting\")\n",
|
| 101 |
+
"data = data.replace(\"smalltalk_agent_age\",\"CurrentHumanQuery\")\n",
|
| 102 |
+
"data = data.replace(\"smalltalk_agent_annoying\",\"NotTalking2U\")\n",
|
| 103 |
+
"data = data.replace(\"smalltalk_agent_bad\",\"Swearing\")\n",
|
| 104 |
+
"data = data.replace(\"smalltalk_agent_boss\",\"CurrentHumanQuery\")\n",
|
| 105 |
+
"data = data.replace(\"smalltalk_agent_clever\",\"Clever\")\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"data = data.replace(\"smalltalk_agent_beautiful\",\"Clever\")\n",
|
| 108 |
+
"data = data.replace(\"smalltalk_agent_fired\",\"Shutup\")\n",
|
| 109 |
+
"data = data.replace(\"smalltalk_agent_good\",\"Thanks\")\n",
|
| 110 |
+
"data = data.replace(\"smalltalk_agent_chatbot\",\"SelfAware\")\n",
|
| 111 |
+
"data = data.replace(\"smalltalk_agent_real\",\"SelfAware\")\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"data.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": null,
|
| 120 |
+
"metadata": {},
|
| 121 |
+
"outputs": [],
|
| 122 |
+
"source": [
|
| 123 |
+
"# TODO Checks label data balance\n",
|
| 124 |
+
"import pandas as pd\n",
|
| 125 |
+
"data = pd.read_csv(\"intentdata/train.csv\") # Original data is a (2000, 7) DataFrame\n",
|
| 126 |
+
"# data contains 6 feature columns and 1 target column.\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"# Separate the design matrix from the target labels.\n",
|
| 129 |
+
"X = data.iloc[:, :-1]\n",
|
| 130 |
+
"y = data['intent']\n",
|
| 131 |
+
"\n",
|
| 132 |
+
"y.value_counts().sort_index().plot.bar(x='Target Value', y='Number of Occurrences',figsize=(20,20))"
|
| 133 |
+
]
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"cell_type": "code",
|
| 137 |
+
"execution_count": null,
|
| 138 |
+
"metadata": {},
|
| 139 |
+
"outputs": [],
|
| 140 |
+
"source": []
|
| 141 |
+
},
|
| 142 |
+
{
|
| 143 |
+
"cell_type": "code",
|
| 144 |
+
"execution_count": null,
|
| 145 |
+
"metadata": {},
|
| 146 |
+
"outputs": [],
|
| 147 |
+
"source": [
|
| 148 |
+
"import torch\n",
|
| 149 |
+
"torch.cuda.is_available()"
|
| 150 |
+
]
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"cell_type": "code",
|
| 154 |
+
"execution_count": null,
|
| 155 |
+
"metadata": {},
|
| 156 |
+
"outputs": [],
|
| 157 |
+
"source": [
|
| 158 |
+
"import json \n",
|
| 159 |
+
"import nlpaug.augmenter.word as naw\n",
|
| 160 |
+
"import nlpaug.flow as naf\n",
|
| 161 |
+
"print(\"Loading Models...\")\n",
|
| 162 |
+
"import nltk \n",
|
| 163 |
+
"nltk.download('wordnet')\n",
|
| 164 |
+
"nltk.download('omw-1.4')\n",
|
| 165 |
+
"TOPK=20 #default=100\n",
|
| 166 |
+
"ACT = 'insert' #\"substitute\"\n",
|
| 167 |
+
"def tst():\n",
|
| 168 |
+
" aug_w2v= naw.WordEmbsAug(\n",
|
| 169 |
+
" model_type='glove', model_path='glove/glove.6B.300d.txt',\n",
|
| 170 |
+
" action=\"substitute\")\n",
|
| 171 |
+
" aug_bert = naw.ContextualWordEmbsAug(\n",
|
| 172 |
+
" model_path='distilbert-base-uncased', \n",
|
| 173 |
+
" \n",
|
| 174 |
+
" action=ACT, top_k=TOPK)\n",
|
| 175 |
+
" aug = naf.Sequential([\n",
|
| 176 |
+
" aug_bert,aug_w2v\n",
|
| 177 |
+
" ])\n",
|
| 178 |
+
"print(\"Models Loaded.\")\n",
|
| 179 |
+
"with open(\"intentdata/intent.json\") as f:\n",
|
| 180 |
+
" intentwhole = json.load(f)[\"intents\"]\n",
|
| 181 |
+
"#text = intent[0][\"text\"][3]\n",
|
| 182 |
+
"for intent in intentwhole:\n",
|
| 183 |
+
" for text in intent[\"text\"]:\n",
|
| 184 |
+
" augmented_texts = set()\n",
|
| 185 |
+
" for i in range(20):\n",
|
| 186 |
+
" aug = naw.SynonymAug(aug_src='wordnet',aug_min=1, aug_max=10, aug_p=i/10)\n",
|
| 187 |
+
" augmented_text = str(aug.augment(text))\n",
|
| 188 |
+
" print(augmented_text)\n",
|
| 189 |
+
" #print(augmented_text)\n",
|
| 190 |
+
" augmented_texts.add(augmented_text)\n",
|
| 191 |
+
" augmented_texts = augmented_texts.union(set(intent[\"text\"]))\n",
|
| 192 |
+
" intent[\"text\"] = list(augmented_texts)\n",
|
| 193 |
+
" def test():\n",
|
| 194 |
+
" if intent[\"intent\"] != \"Jokes\": \n",
|
| 195 |
+
" for response in intent[\"responses\"]:\n",
|
| 196 |
+
" augmented_responses = set()\n",
|
| 197 |
+
" for i in range(20):\n",
|
| 198 |
+
" #aug = naw.SynonymAug(aug_src='wordnet',aug_min=1, aug_max=10, aug_p=i/50,stopwords=[\"<HUMAN>\",\"<HUMAN>,\",\"<HUMAN>!\"])\n",
|
| 199 |
+
" augmented_response = str(aug.augment(response)[0])\n",
|
| 200 |
+
" print(augmented_response)\n",
|
| 201 |
+
" try:\n",
|
| 202 |
+
" augmented_response = augmented_response[:augmented_response.index(\"<\")] + \"<HUMAN\" + augmented_response[augmented_response.index(\">\"):]\n",
|
| 203 |
+
" except ValueError as vex:\n",
|
| 204 |
+
" pass\n",
|
| 205 |
+
" #print(augmented_text)\n",
|
| 206 |
+
" augmented_responses.add(augmented_response)\n",
|
| 207 |
+
" augmented_responses = augmented_responses.union(set(intent[\"responses\"]))\n",
|
| 208 |
+
" intent[\"responses\"] = list(augmented_responses) \n",
|
| 209 |
+
"with open(\"intentdata/intent_aug_text_test.json\",\"w+\") as f:\n",
|
| 210 |
+
" json.dump({\"intents\":intentwhole},f)\n",
|
| 211 |
+
"\n",
|
| 212 |
+
"\n",
|
| 213 |
+
"print(intentwhole[1][\"responses\"])"
|
| 214 |
+
]
|
| 215 |
+
},
|
| 216 |
+
{
|
| 217 |
+
"cell_type": "code",
|
| 218 |
+
"execution_count": null,
|
| 219 |
+
"metadata": {},
|
| 220 |
+
"outputs": [],
|
| 221 |
+
"source": [
|
| 222 |
+
"df = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 223 |
+
"len(list(pd.unique(df[\"intent\"])))"
|
| 224 |
+
]
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"cell_type": "code",
|
| 228 |
+
"execution_count": null,
|
| 229 |
+
"metadata": {},
|
| 230 |
+
"outputs": [],
|
| 231 |
+
"source": [
|
| 232 |
+
"import os\n",
|
| 233 |
+
"import json \n",
|
| 234 |
+
"import pandas as pd\n",
|
| 235 |
+
"os.listdir(\"new_intent_data\")"
|
| 236 |
+
]
|
| 237 |
+
},
|
| 238 |
+
{
|
| 239 |
+
"cell_type": "code",
|
| 240 |
+
"execution_count": null,
|
| 241 |
+
"metadata": {},
|
| 242 |
+
"outputs": [],
|
| 243 |
+
"source": []
|
| 244 |
+
},
|
| 245 |
+
{
|
| 246 |
+
"cell_type": "code",
|
| 247 |
+
"execution_count": null,
|
| 248 |
+
"metadata": {},
|
| 249 |
+
"outputs": [],
|
| 250 |
+
"source": [
|
| 251 |
+
"import spacy\n",
|
| 252 |
+
"\n",
|
| 253 |
+
"nlp = spacy.load(\"en_core_web_sm\")\n",
|
| 254 |
+
"nlp(\"start the fan\").similarity(nlp(\"turn the fan on\"))"
|
| 255 |
+
]
|
| 256 |
+
},
|
| 257 |
+
{
|
| 258 |
+
"cell_type": "code",
|
| 259 |
+
"execution_count": null,
|
| 260 |
+
"metadata": {},
|
| 261 |
+
"outputs": [],
|
| 262 |
+
"source": [
|
| 263 |
+
"import pandas as pd\n",
|
| 264 |
+
"traindf = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 265 |
+
"traindf"
|
| 266 |
+
]
|
| 267 |
+
},
|
| 268 |
+
{
|
| 269 |
+
"cell_type": "code",
|
| 270 |
+
"execution_count": null,
|
| 271 |
+
"metadata": {},
|
| 272 |
+
"outputs": [],
|
| 273 |
+
"source": [
|
| 274 |
+
"import pandas as pd\n",
|
| 275 |
+
"intentdf = pd.read_csv(\"new_intent_data/intent_classification.csv\")\n",
|
| 276 |
+
"traindf = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 277 |
+
"smart_home_intent = intentdf[[\"text-en\",\"intent-en\"]].rename({\"text-en\":\"text\",\"intent-en\":\"intent\"},axis=1)\n",
|
| 278 |
+
"newdf = pd.concat([traindf,smart_home_intent],axis=0)\n",
|
| 279 |
+
"\n",
|
| 280 |
+
"newdf.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)"
|
| 281 |
+
]
|
| 282 |
+
},
|
| 283 |
+
{
|
| 284 |
+
"cell_type": "code",
|
| 285 |
+
"execution_count": null,
|
| 286 |
+
"metadata": {},
|
| 287 |
+
"outputs": [],
|
| 288 |
+
"source": [
|
| 289 |
+
"with open(\"new_intent_data/Dataset-train_old.txt\") as f:\n",
|
| 290 |
+
" textdata = f.readlines()\n",
|
| 291 |
+
"sent = \"\"\n",
|
| 292 |
+
"for text in textdata:\n",
|
| 293 |
+
" if text == \"\":\n",
|
| 294 |
+
" pass\n",
|
| 295 |
+
" elif text != \"\":\n",
|
| 296 |
+
" if text.count(\",\") == 2:\n",
|
| 297 |
+
" #print(text.replace(\",\",\";\",1))\n",
|
| 298 |
+
" sent += text.replace(\",\",\";\",1)\n",
|
| 299 |
+
" else:\n",
|
| 300 |
+
" \n",
|
| 301 |
+
" sent += text\n",
|
| 302 |
+
"print(sent)\n",
|
| 303 |
+
"with open(\"new_intent_data/Dataset-train.txt\",\"w+\") as f:\n",
|
| 304 |
+
" f.write(sent)\n",
|
| 305 |
+
" "
|
| 306 |
+
]
|
| 307 |
+
},
|
| 308 |
+
{
|
| 309 |
+
"cell_type": "code",
|
| 310 |
+
"execution_count": null,
|
| 311 |
+
"metadata": {},
|
| 312 |
+
"outputs": [],
|
| 313 |
+
"source": [
|
| 314 |
+
"import pandas as pd\n",
|
| 315 |
+
"data = pd.read_csv(\"new_intent_data/Dataset-train.csv\",delimiter=\",\") \n",
|
| 316 |
+
"data = data.drop(\"Unnamed: 0\",axis=1)\n",
|
| 317 |
+
"data\n",
|
| 318 |
+
"data.to_csv(\"new_intent_data/Dataset-train.csv\",index=False)"
|
| 319 |
+
]
|
| 320 |
+
},
|
| 321 |
+
{
|
| 322 |
+
"cell_type": "code",
|
| 323 |
+
"execution_count": null,
|
| 324 |
+
"metadata": {},
|
| 325 |
+
"outputs": [],
|
| 326 |
+
"source": [
|
| 327 |
+
"import pandas as pd\n",
|
| 328 |
+
"data = pd.read_csv(\"new_intent_data/Dataset-train.csv\",delimiter=\",\") # Original data is a (2000, 7) DataFrame\n",
|
| 329 |
+
"data = data.apply(lambda x: x.replace('\"',''),axis=1)\n",
|
| 330 |
+
"data.to_csv(\"new_intent_data/Dataset-train.csv\")"
|
| 331 |
+
]
|
| 332 |
+
},
|
| 333 |
+
{
|
| 334 |
+
"cell_type": "code",
|
| 335 |
+
"execution_count": 1,
|
| 336 |
+
"metadata": {},
|
| 337 |
+
"outputs": [],
|
| 338 |
+
"source": [
|
| 339 |
+
"import pandas as pd\n",
|
| 340 |
+
"data = pd.read_csv(\"new_intent_data/Dataset-train.csv\",delimiter=\"|\") \n",
|
| 341 |
+
"traindf = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 342 |
+
"new_data = pd.concat([traindf,data],axis=0)\n",
|
| 343 |
+
"new_data.to_csv(\"intentdata/train.csv\",index=False)"
|
| 344 |
+
]
|
| 345 |
+
},
|
| 346 |
+
{
|
| 347 |
+
"cell_type": "code",
|
| 348 |
+
"execution_count": 2,
|
| 349 |
+
"metadata": {},
|
| 350 |
+
"outputs": [
|
| 351 |
+
{
|
| 352 |
+
"data": {
|
| 353 |
+
"text/html": [
|
| 354 |
+
"<div>\n",
|
| 355 |
+
"<style scoped>\n",
|
| 356 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 357 |
+
" vertical-align: middle;\n",
|
| 358 |
+
" }\n",
|
| 359 |
+
"\n",
|
| 360 |
+
" .dataframe tbody tr th {\n",
|
| 361 |
+
" vertical-align: top;\n",
|
| 362 |
+
" }\n",
|
| 363 |
+
"\n",
|
| 364 |
+
" .dataframe thead th {\n",
|
| 365 |
+
" text-align: right;\n",
|
| 366 |
+
" }\n",
|
| 367 |
+
"</style>\n",
|
| 368 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 369 |
+
" <thead>\n",
|
| 370 |
+
" <tr style=\"text-align: right;\">\n",
|
| 371 |
+
" <th></th>\n",
|
| 372 |
+
" <th>text</th>\n",
|
| 373 |
+
" <th>intent</th>\n",
|
| 374 |
+
" </tr>\n",
|
| 375 |
+
" </thead>\n",
|
| 376 |
+
" <tbody>\n",
|
| 377 |
+
" <tr>\n",
|
| 378 |
+
" <th>0</th>\n",
|
| 379 |
+
" <td>listen to westbam alumb allergic on google music</td>\n",
|
| 380 |
+
" <td>PlayMusic</td>\n",
|
| 381 |
+
" </tr>\n",
|
| 382 |
+
" <tr>\n",
|
| 383 |
+
" <th>1</th>\n",
|
| 384 |
+
" <td>add step to me to the 50 clásicos playlist</td>\n",
|
| 385 |
+
" <td>AddToPlaylist</td>\n",
|
| 386 |
+
" </tr>\n",
|
| 387 |
+
" <tr>\n",
|
| 388 |
+
" <th>2</th>\n",
|
| 389 |
+
" <td>i give this current textbook a rating value of...</td>\n",
|
| 390 |
+
" <td>RateBook</td>\n",
|
| 391 |
+
" </tr>\n",
|
| 392 |
+
" <tr>\n",
|
| 393 |
+
" <th>3</th>\n",
|
| 394 |
+
" <td>play the song little robin redbreast</td>\n",
|
| 395 |
+
" <td>PlayMusic</td>\n",
|
| 396 |
+
" </tr>\n",
|
| 397 |
+
" <tr>\n",
|
| 398 |
+
" <th>4</th>\n",
|
| 399 |
+
" <td>please add iris dement to my playlist this is ...</td>\n",
|
| 400 |
+
" <td>AddToPlaylist</td>\n",
|
| 401 |
+
" </tr>\n",
|
| 402 |
+
" <tr>\n",
|
| 403 |
+
" <th>...</th>\n",
|
| 404 |
+
" <td>...</td>\n",
|
| 405 |
+
" <td>...</td>\n",
|
| 406 |
+
" </tr>\n",
|
| 407 |
+
" <tr>\n",
|
| 408 |
+
" <th>37306</th>\n",
|
| 409 |
+
" <td>what is your date of birth</td>\n",
|
| 410 |
+
" <td>how_old_are_you</td>\n",
|
| 411 |
+
" </tr>\n",
|
| 412 |
+
" <tr>\n",
|
| 413 |
+
" <th>37307</th>\n",
|
| 414 |
+
" <td>what year were you born in</td>\n",
|
| 415 |
+
" <td>how_old_are_you</td>\n",
|
| 416 |
+
" </tr>\n",
|
| 417 |
+
" <tr>\n",
|
| 418 |
+
" <th>37308</th>\n",
|
| 419 |
+
" <td>what is the year that were you born</td>\n",
|
| 420 |
+
" <td>how_old_are_you</td>\n",
|
| 421 |
+
" </tr>\n",
|
| 422 |
+
" <tr>\n",
|
| 423 |
+
" <th>37309</th>\n",
|
| 424 |
+
" <td>how old are you ai</td>\n",
|
| 425 |
+
" <td>how_old_are_you</td>\n",
|
| 426 |
+
" </tr>\n",
|
| 427 |
+
" <tr>\n",
|
| 428 |
+
" <th>37310</th>\n",
|
| 429 |
+
" <td>are you 16 years old</td>\n",
|
| 430 |
+
" <td>how_old_are_you</td>\n",
|
| 431 |
+
" </tr>\n",
|
| 432 |
+
" </tbody>\n",
|
| 433 |
+
"</table>\n",
|
| 434 |
+
"<p>37311 rows × 2 columns</p>\n",
|
| 435 |
+
"</div>"
|
| 436 |
+
],
|
| 437 |
+
"text/plain": [
|
| 438 |
+
" text intent\n",
|
| 439 |
+
"0 listen to westbam alumb allergic on google music PlayMusic\n",
|
| 440 |
+
"1 add step to me to the 50 clásicos playlist AddToPlaylist\n",
|
| 441 |
+
"2 i give this current textbook a rating value of... RateBook\n",
|
| 442 |
+
"3 play the song little robin redbreast PlayMusic\n",
|
| 443 |
+
"4 please add iris dement to my playlist this is ... AddToPlaylist\n",
|
| 444 |
+
"... ... ...\n",
|
| 445 |
+
"37306 what is your date of birth how_old_are_you\n",
|
| 446 |
+
"37307 what year were you born in how_old_are_you\n",
|
| 447 |
+
"37308 what is the year that were you born how_old_are_you\n",
|
| 448 |
+
"37309 how old are you ai how_old_are_you\n",
|
| 449 |
+
"37310 are you 16 years old how_old_are_you\n",
|
| 450 |
+
"\n",
|
| 451 |
+
"[37311 rows x 2 columns]"
|
| 452 |
+
]
|
| 453 |
+
},
|
| 454 |
+
"execution_count": 2,
|
| 455 |
+
"metadata": {},
|
| 456 |
+
"output_type": "execute_result"
|
| 457 |
+
}
|
| 458 |
+
],
|
| 459 |
+
"source": [
|
| 460 |
+
"import pandas as pd\n",
|
| 461 |
+
"traindf = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 462 |
+
"traindf"
|
| 463 |
+
]
|
| 464 |
+
}
|
| 465 |
+
],
|
| 466 |
+
"metadata": {
|
| 467 |
+
"kernelspec": {
|
| 468 |
+
"display_name": "Python 3.6.13 ('caesarnlradeon')",
|
| 469 |
+
"language": "python",
|
| 470 |
+
"name": "python3"
|
| 471 |
+
},
|
| 472 |
+
"language_info": {
|
| 473 |
+
"codemirror_mode": {
|
| 474 |
+
"name": "ipython",
|
| 475 |
+
"version": 3
|
| 476 |
+
},
|
| 477 |
+
"file_extension": ".py",
|
| 478 |
+
"mimetype": "text/x-python",
|
| 479 |
+
"name": "python",
|
| 480 |
+
"nbconvert_exporter": "python",
|
| 481 |
+
"pygments_lexer": "ipython3",
|
| 482 |
+
"version": "3.6.13"
|
| 483 |
+
},
|
| 484 |
+
"orig_nbformat": 4,
|
| 485 |
+
"vscode": {
|
| 486 |
+
"interpreter": {
|
| 487 |
+
"hash": "bfcbd6a2138b43c88138636b277dc6540d170334c8840148725250eab505a128"
|
| 488 |
+
}
|
| 489 |
+
}
|
| 490 |
+
},
|
| 491 |
+
"nbformat": 4,
|
| 492 |
+
"nbformat_minor": 2
|
| 493 |
+
}
|
caesarinfer.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import json
|
| 3 |
+
#import spacy
|
| 4 |
+
import pickle
|
| 5 |
+
import random
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
import tensorflow_hub as hub
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import tensorflow_text as text
|
| 10 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# TODO AIM - Implement Chatbot Gossip to Caesar
|
| 14 |
+
# 1. Add data to datasets train | valid | test
|
| 15 |
+
# a. then clean labels
|
| 16 |
+
# 2. Augment data to provide more potential possibilites
|
| 17 |
+
# 3. Use BERT to match input with the response
|
| 18 |
+
# Command Labels - AddToPlaylist | GetWeather -> API -> user
|
| 19 |
+
# Conversation Labes - Greeting | Goodbye -> BERTNN: input:"hello" => response:"hi there, I am caesar" -> user
|
| 20 |
+
|
| 21 |
+
# TODO AIM - Single names of songs artists like "play a boogie" and it will play a boogie's music.
|
| 22 |
+
# 1. Idea one - NER detect the named entities
|
| 23 |
+
# 2. Create new Neural Network that detects that. * Have to determine the relationship between the entites
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
greetings = ["Greeting","smalltalk_greetings_hello","greeting"]
|
| 27 |
+
courtesy_greeting = ["CourtesyGreeting"]
|
| 28 |
+
|
| 29 |
+
class CaesarNL:
|
| 30 |
+
@staticmethod
|
| 31 |
+
def run(userinput):
|
| 32 |
+
#if len(sys.argv) == 2:
|
| 33 |
+
#userinput = [sys.argv[1].lower()]
|
| 34 |
+
stored_name = "Amari"
|
| 35 |
+
classifier_model = tf.keras.models.load_model('caesarmodel/caesarnl',custom_objects={'KerasLayer':hub.KerasLayer})
|
| 36 |
+
|
| 37 |
+
# Show the model architecture
|
| 38 |
+
results = tf.nn.softmax(classifier_model(tf.constant(userinput)))
|
| 39 |
+
print(results.shape)
|
| 40 |
+
with open("caesarmodel/labelbinarizer.pkl","rb") as f:
|
| 41 |
+
binarizer = pickle.load(f)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
intents=binarizer.inverse_transform(results.numpy())
|
| 45 |
+
with open("intentdata/responses.json","r") as f:
|
| 46 |
+
responses = json.load(f)["responses"]
|
| 47 |
+
|
| 48 |
+
if intents[0] in greetings:
|
| 49 |
+
greetresponse = random.choice(responses["Greeting"]).replace("<HUMAN>",stored_name)
|
| 50 |
+
#print(greetresponse)
|
| 51 |
+
return greetresponse, intents[0]
|
| 52 |
+
else:
|
| 53 |
+
response = f"response to be implemented for text:{userinput}, predicted intent:{intents[0]}"
|
| 54 |
+
#print(response)
|
| 55 |
+
return response,intents[0]
|
| 56 |
+
#elif len(sys.argv) < 2:
|
| 57 |
+
#response = "What is it, sir?"
|
| 58 |
+
#print(response)
|
| 59 |
+
#return response
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
# Takes 17 seconds
|
| 64 |
+
userinput = ["Hello"]
|
| 65 |
+
greetresponse,intents = CaesarNL.run(userinput)
|
| 66 |
+
print(greetresponse,f"intent:{intents}")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
caesarintro.mp3
ADDED
|
File without changes
|
caesarmodel/caesarnl/assets/vocab.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3
|
| 3 |
+
size 231508
|
caesarmodel/caesarnl/keras_metadata.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4be3f21ee8a1f0852d551bcf7ba86f54d14612589079c7d9079f443e945cbaf
|
| 3 |
+
size 9071
|
caesarmodel/caesarnl/saved_model.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f0690b8d1f7a6f206358a09b1e983f940132344332858962a723baa31a7e7083
|
| 3 |
+
size 12480367
|
caesarmodel/caesarnl/variables/variables.data-00000-of-00001
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:859f00425f0b1dd4c873ea0c4311ef243f962d474fa9b5eb8e8eb7fdb1a44484
|
| 3 |
+
size 499589851
|
caesarmodel/caesarnl/variables/variables.index
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2afa200016405bedd5de9eb7c7dcabb90c22f2dd695bd06b1b2373448a5ed819
|
| 3 |
+
size 32063
|
caesarmodel/labelbinarizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6f1b28f7b4ef1ecd72e399caa96f868c2bd24f434a3d1cfe2cbf2fc79582e768
|
| 3 |
+
size 44048
|
caesarnl.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import library
|
| 2 |
+
import warnings
|
| 3 |
+
#from gtts import gTTS
|
| 4 |
+
import os
|
| 5 |
+
#from playsound import playsound
|
| 6 |
+
from caesarapis import CaesarAPIs
|
| 7 |
+
import time
|
| 8 |
+
import pyttsx3
|
| 9 |
+
warnings.filterwarnings("ignore")
|
| 10 |
+
import os
|
| 11 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 12 |
+
import speech_recognition as sr
|
| 13 |
+
from caesarinfer import CaesarNL
|
| 14 |
+
# Initialize recognizer class (for recognizing the speech)
|
| 15 |
+
engine=pyttsx3.init('sapi5')
|
| 16 |
+
voices=engine.getProperty('voices')
|
| 17 |
+
engine.setProperty('voice',voices[1].id)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def speak(text,whisper_mode=None):
|
| 21 |
+
if whisper_mode == 0:
|
| 22 |
+
engine.say(text)
|
| 23 |
+
engine.runAndWait()
|
| 24 |
+
recognizer = sr.Recognizer()
|
| 25 |
+
|
| 26 |
+
#def caesartalk(caesarspeech,whisper_mode,filename="example.mp3"):
|
| 27 |
+
# if whisper_mode == 0:
|
| 28 |
+
# audio = gTTS(caesarspeech, lang="en", slow=False)
|
| 29 |
+
# audio.save(filename)
|
| 30 |
+
# playsound(filename)
|
| 31 |
+
# time.sleep(1)
|
| 32 |
+
# if filename in os.listdir():
|
| 33 |
+
# os.remove(filename)
|
| 34 |
+
# Reading Microphone as source
|
| 35 |
+
# listening the speech and store in audio_text variable
|
| 36 |
+
whisper_state = 1
|
| 37 |
+
caesarapis = CaesarAPIs()
|
| 38 |
+
while True:
|
| 39 |
+
with sr.Microphone() as source:
|
| 40 |
+
|
| 41 |
+
caesarintro ="How can I help you sir?"
|
| 42 |
+
print(caesarintro)
|
| 43 |
+
#caesartalk(caesarintro,caesarapis.whisper_mode,filename="caesarintro.mp3")
|
| 44 |
+
speak(caesarintro,caesarapis.whisper_mode)
|
| 45 |
+
#recognizer
|
| 46 |
+
recognizer.adjust_for_ambient_noise(source,duration=1)
|
| 47 |
+
audio_text = recognizer.listen(source)
|
| 48 |
+
understood = "Understood sir, processing..."
|
| 49 |
+
print(understood)
|
| 50 |
+
#caesartalk(understood,caesarapis.whisper_mode,filename="caesar_understood.mp3")
|
| 51 |
+
speak(understood,caesarapis.whisper_mode)
|
| 52 |
+
try:
|
| 53 |
+
# using google speech recognition
|
| 54 |
+
text = recognizer.recognize_google(audio_text)
|
| 55 |
+
print(text)
|
| 56 |
+
print("Caesar processing...")
|
| 57 |
+
caesarResponse,intent = CaesarNL.run([text])
|
| 58 |
+
caesarapis.runapis(caesarResponse,intent,text)
|
| 59 |
+
|
| 60 |
+
print(f"User Input: {text}")
|
| 61 |
+
print(f"Caesar: {caesarResponse}")
|
| 62 |
+
#caesartalk(caesarResponse,caesarapis.whisper_mode,filename="caesarResponse.mp3")
|
| 63 |
+
speak(caesarResponse,caesarapis.whisper_mode)
|
| 64 |
+
|
| 65 |
+
except Exception as ex:
|
| 66 |
+
print(type(ex),ex)
|
| 67 |
+
print("Sorry, I did not get that")
|
| 68 |
+
|
| 69 |
+
# whisper_mode
|
| 70 |
+
|
caesarnlexamples.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import json
|
| 3 |
+
import spacy
|
| 4 |
+
import pickle
|
| 5 |
+
import random
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
import tensorflow_hub as hub
|
| 8 |
+
import tensorflow_text as text
|
| 9 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 10 |
+
|
| 11 |
+
def print_my_examples(inputs, results):
|
| 12 |
+
result_for_printing = \
|
| 13 |
+
[f'input: {inputs[i]:<30} : estimated intent: {results[i]}'
|
| 14 |
+
for i in range(len(inputs))]
|
| 15 |
+
print(*result_for_printing, sep='\n')
|
| 16 |
+
print()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
examples = [
|
| 20 |
+
'play a song from U2', # this is the same sentence tried earlier
|
| 21 |
+
'Will it rain tomorrow',
|
| 22 |
+
'I like to hear greatist hits from beastie boys',
|
| 23 |
+
'I like to book a table for 3 persons',
|
| 24 |
+
'5 stars for machines like me',
|
| 25 |
+
'play a boogie wit da hoodie',
|
| 26 |
+
"play Bob's favorite song",
|
| 27 |
+
"give me a hug",
|
| 28 |
+
"hello"
|
| 29 |
+
]
|
| 30 |
+
greetings = ["Greeting","smalltalk_greetings_hello"]
|
| 31 |
+
courtesy_greeting = ["CourtesyGreeting"]
|
| 32 |
+
stored_name = "Amari"
|
| 33 |
+
examples = ["hello"]
|
| 34 |
+
#nlp = spacy.load("en_core_web_sm")
|
| 35 |
+
classifier_model = tf.keras.models.load_model('caesarmodel/caesarnl.h5',custom_objects={'KerasLayer':hub.KerasLayer})
|
| 36 |
+
|
| 37 |
+
# Show the model architecture
|
| 38 |
+
results = tf.nn.softmax(classifier_model(tf.constant(examples)))
|
| 39 |
+
with open("caesarmodel/labelbinarizer.pkl","rb") as f:
|
| 40 |
+
binarizer = pickle.load(f)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
intents=binarizer.inverse_transform(results.numpy())
|
| 44 |
+
with open("intentdata/responses.json","r") as f:
|
| 45 |
+
responses = json.load(f)["responses"]
|
| 46 |
+
|
| 47 |
+
if intents[0] in greetings:
|
| 48 |
+
greetresponse = random.choice(responses["Greeting"]).replace("<HUMAN>",stored_name)
|
| 49 |
+
print(greetresponse)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
#sentence_intents = dict(zip(examples,intents))
|
| 54 |
+
#print(sentence_intents)
|
| 55 |
+
#print_my_examples(examples, intents)
|
| 56 |
+
|
| 57 |
+
# TODO AIM - Implement Chatbot Gossip to Caesar
|
| 58 |
+
# 1. Add data to datasets train | valid | test
|
| 59 |
+
# a. then clean labels
|
| 60 |
+
# 2. Augment data to provide more potential possibilites
|
| 61 |
+
# 3. Use BERT to match input with the response
|
| 62 |
+
# Command Labels - AddToPlaylist | GetWeather -> API -> user
|
| 63 |
+
# Conversation Labes - Greeting | Goodbye -> BERTNN: input:"hello" => response:"hi there, I am caesar" -> user
|
| 64 |
+
|
| 65 |
+
# TODO AIM - Single names of songs artists like "play a boogie" and it will play a boogie's music.
|
| 66 |
+
# 1. Idea one - NER detect the named entities
|
| 67 |
+
# 2. Create new Neural Network that detects that. * Have to determine the relationship between the entites
|
| 68 |
+
|
| 69 |
+
|
caesarnlrasp.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import library
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import warnings
|
| 5 |
+
from caesarapis import CaesarAPIs
|
| 6 |
+
import speech_recognition as sr
|
| 7 |
+
warnings.filterwarnings("ignore")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 12 |
+
# Initialize recognizer class (for recognizing the speech)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def speak(text,whisper_mode=0):
|
| 16 |
+
if whisper_mode == 0:
|
| 17 |
+
#espeak.synth(text)
|
| 18 |
+
if "output.mp3" in os.listdir():
|
| 19 |
+
os.remove("output.mp3")
|
| 20 |
+
os.system(f'espeak "{text}" --stdout | ffmpeg -i pipe:0 output.mp3')
|
| 21 |
+
os.system(f'mplayer -volume 300 output.mp3')
|
| 22 |
+
os.system("pkill mplayer")
|
| 23 |
+
#raise KeyboardInterrupt
|
| 24 |
+
#os.system(f'mplayer -af volume=30:1 output.mp3')
|
| 25 |
+
|
| 26 |
+
recognizer = sr.Recognizer()
|
| 27 |
+
# Reading Microphone as source
|
| 28 |
+
# listening the speech and store in audio_text variable
|
| 29 |
+
whisper_state = 1
|
| 30 |
+
caesarapis = CaesarAPIs()
|
| 31 |
+
while True:
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
caesarintro ="How can I help you sir?"
|
| 35 |
+
print(caesarintro)
|
| 36 |
+
#caesartalk(caesarintro,caesarapis.whisper_mode,filename="caesarintro.mp3")
|
| 37 |
+
speak(caesarintro,caesarapis.whisper_mode)
|
| 38 |
+
print("Listening...")
|
| 39 |
+
|
| 40 |
+
with sr.Microphone() as source:
|
| 41 |
+
recognizer.adjust_for_ambient_noise(source,duration=1)
|
| 42 |
+
audio_text = recognizer.listen(source)
|
| 43 |
+
|
| 44 |
+
#print("Billyy")
|
| 45 |
+
#recognizer
|
| 46 |
+
understood = "Understood sir, processing..."
|
| 47 |
+
print(understood)
|
| 48 |
+
#caesartalk(understood,caesarapis.whisper_mode,filename="caesar_understood.mp3")
|
| 49 |
+
speak(understood,caesarapis.whisper_mode)
|
| 50 |
+
# using google speech recognition
|
| 51 |
+
text = recognizer.recognize_google(audio_text)
|
| 52 |
+
print("output:",text)
|
| 53 |
+
print("Caesar processing...")
|
| 54 |
+
# TODO Send to Azure API
|
| 55 |
+
if "hello" in text:
|
| 56 |
+
print("Hola Amari")
|
| 57 |
+
break
|
| 58 |
+
#caesarResponse,intent = ("Hola Amari","greeting")
|
| 59 |
+
#caesarapis.runapis(caesarResponse,intent,text,speak)
|
| 60 |
+
|
| 61 |
+
#print(f"User Input: {text}")
|
| 62 |
+
#print(f"Caesar: {caesarResponse}")
|
| 63 |
+
#caesartalk(caesarResponse,caesarapis.whisper_mode,filename="caesarResponse.mp3")
|
| 64 |
+
#speak(caesarResponse,caesarapis.whisper_mode)
|
| 65 |
+
except Exception as uex:
|
| 66 |
+
continue
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# whisper_mode
|
| 70 |
+
|
caesartrain.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
#import shutil
|
| 5 |
+
import pickle
|
| 6 |
+
import warnings
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import tensorflow as tf
|
| 9 |
+
import tensorflow_hub as hub
|
| 10 |
+
import tensorflow_text as text
|
| 11 |
+
from pylab import rcParams
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 14 |
+
warnings.filterwarnings("ignore")
|
| 15 |
+
tf.get_logger().setLevel('ERROR')
|
| 16 |
+
|
| 17 |
+
class CaesarNLTrain:
|
| 18 |
+
def train(traindf,validdf,testdf,examples,history_filename = "history.png"):
|
| 19 |
+
intent_label_output_size = len(pd.unique(traindf["intent"]))
|
| 20 |
+
trainfeatures=traindf.copy()
|
| 21 |
+
trainlabels=trainfeatures.pop("intent")
|
| 22 |
+
|
| 23 |
+
trainfeatures=trainfeatures.values
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
"""One-Hot-Encoding of class-labels:"""
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
binarizer=LabelBinarizer()
|
| 32 |
+
trainlabels=binarizer.fit_transform(trainlabels.values)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
"""Preprocess test- and validation data in the same way as it has been done for training-data:"""
|
| 36 |
+
|
| 37 |
+
testfeatures=testdf.copy()
|
| 38 |
+
testlabels=testfeatures.pop("intent")
|
| 39 |
+
validfeatures=validdf.copy()
|
| 40 |
+
validlabels=validfeatures.pop("intent")
|
| 41 |
+
|
| 42 |
+
testfeatures=testfeatures.values
|
| 43 |
+
validfeatures=validfeatures.values
|
| 44 |
+
|
| 45 |
+
testlabels=binarizer.transform(testlabels.values)
|
| 46 |
+
validlabels=binarizer.transform(validlabels.values)
|
| 47 |
+
pickle.dump(binarizer, open('caesarmodel/labelbinarizer.pkl', 'wb'))
|
| 48 |
+
|
| 49 |
+
bert_model_name = 'small_bert/bert_en_uncased_L-8_H-512_A-8'
|
| 50 |
+
with open("caesarberthubmodels/bert_to_handle.json") as f:
|
| 51 |
+
map_name_to_handle = json.load(f)
|
| 52 |
+
with open("caesarberthubmodels/bert_to_preprocess.json") as f:
|
| 53 |
+
map_model_to_preprocess = json.load(f)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
tfhub_handle_encoder = map_name_to_handle[bert_model_name]
|
| 58 |
+
tfhub_handle_preprocess = map_model_to_preprocess[bert_model_name]
|
| 59 |
+
|
| 60 |
+
print(f'BERT model selected : {tfhub_handle_encoder}')
|
| 61 |
+
print(f'Preprocess model auto-selected: {tfhub_handle_preprocess}')
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
bert_preprocess_model = hub.KerasLayer(tfhub_handle_preprocess)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
trainfeatures[0]
|
| 69 |
+
|
| 70 |
+
text_test = trainfeatures[0]
|
| 71 |
+
text_preprocessed = bert_preprocess_model(text_test)
|
| 72 |
+
|
| 73 |
+
bert_model = hub.KerasLayer(tfhub_handle_encoder)
|
| 74 |
+
|
| 75 |
+
bert_results = bert_model(text_preprocessed)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def build_classifier_model():
|
| 80 |
+
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
|
| 81 |
+
preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, name='preprocessing')
|
| 82 |
+
encoder_inputs = preprocessing_layer(text_input)
|
| 83 |
+
encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')
|
| 84 |
+
outputs = encoder(encoder_inputs)
|
| 85 |
+
net = outputs['pooled_output']
|
| 86 |
+
net = tf.keras.layers.Dropout(0.1)(net)
|
| 87 |
+
net = tf.keras.layers.Dense(intent_label_output_size, activation=None, name='classifier')(net)
|
| 88 |
+
return tf.keras.Model(text_input, net)
|
| 89 |
+
|
| 90 |
+
"""Let's check that the model runs with the output of the preprocessing model."""
|
| 91 |
+
|
| 92 |
+
classifier_model = build_classifier_model()
|
| 93 |
+
bert_raw_result = classifier_model(tf.constant(trainfeatures[0]))
|
| 94 |
+
print(tf.keras.activations.softmax(bert_raw_result))
|
| 95 |
+
|
| 96 |
+
"""The output is meaningless, of course, because the model has not been trained yet.
|
| 97 |
+
|
| 98 |
+
Let's take a look at the model's structure.
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
classifier_model.summary()
|
| 102 |
+
|
| 103 |
+
"""## Model training
|
| 104 |
+
|
| 105 |
+
You now have all the pieces to train a model, including the preprocessing module, BERT encoder, data, and classifier.
|
| 106 |
+
|
| 107 |
+
Since this is a non-binary classification problem and the model outputs probabilities, you'll use `losses.CategoricalCrossentropy` loss function.
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
|
| 111 |
+
metrics = tf.metrics.CategoricalAccuracy()
|
| 112 |
+
|
| 113 |
+
"""### Loading the BERT model and training
|
| 114 |
+
|
| 115 |
+
Using the `classifier_model` you created earlier, you can compile the model with the loss, metric and optimizer.
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
epochs=5
|
| 119 |
+
optimizer=tf.keras.optimizers.Adam(1e-5)
|
| 120 |
+
classifier_model.compile(optimizer=optimizer,
|
| 121 |
+
loss=loss,
|
| 122 |
+
metrics=metrics)
|
| 123 |
+
|
| 124 |
+
"""Note: training time will vary depending on the complexity of the BERT model you have selected."""
|
| 125 |
+
|
| 126 |
+
print(f'Training model with {tfhub_handle_encoder}')
|
| 127 |
+
history = classifier_model.fit(x=trainfeatures,y=trainlabels,
|
| 128 |
+
validation_data=(validfeatures,validlabels),
|
| 129 |
+
batch_size=32,
|
| 130 |
+
epochs=epochs)
|
| 131 |
+
classifier_model.save("caesarmodel/caesarnl.h5")
|
| 132 |
+
|
| 133 |
+
"""### Evaluate the model
|
| 134 |
+
|
| 135 |
+
Let's see how the model performs. Two values will be returned. Loss (a number which represents the error, lower values are better), and accuracy.
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
loss, accuracy = classifier_model.evaluate(testfeatures,testlabels)
|
| 139 |
+
|
| 140 |
+
print(f'Loss: {loss}')
|
| 141 |
+
print(f'Accuracy: {accuracy}')
|
| 142 |
+
|
| 143 |
+
"""### Plot the accuracy and loss over time
|
| 144 |
+
|
| 145 |
+
Based on the `History` object returned by `model.fit()`. You can plot the training and validation loss for comparison, as well as the training and validation accuracy:
|
| 146 |
+
"""
|
| 147 |
+
|
| 148 |
+
history_dict = history.history
|
| 149 |
+
print(history_dict.keys())
|
| 150 |
+
|
| 151 |
+
acc = history_dict['categorical_accuracy']
|
| 152 |
+
val_acc = history_dict['val_categorical_accuracy']
|
| 153 |
+
loss = history_dict['loss']
|
| 154 |
+
val_loss = history_dict['val_loss']
|
| 155 |
+
|
| 156 |
+
epochs = range(1, len(acc) + 1)
|
| 157 |
+
fig = plt.figure(figsize=(10, 8))
|
| 158 |
+
fig.tight_layout()
|
| 159 |
+
|
| 160 |
+
plt.subplot(2, 1, 1)
|
| 161 |
+
# "bo" is for "blue dot"
|
| 162 |
+
plt.plot(epochs, loss, 'r', label='Training loss')
|
| 163 |
+
# b is for "solid blue line"
|
| 164 |
+
plt.plot(epochs, val_loss, 'b', label='Validation loss')
|
| 165 |
+
plt.title('Training and validation loss')
|
| 166 |
+
plt.grid(True)
|
| 167 |
+
# plt.xlabel('Epochs')
|
| 168 |
+
plt.ylabel('Loss')
|
| 169 |
+
plt.legend()
|
| 170 |
+
|
| 171 |
+
plt.subplot(2, 1, 2)
|
| 172 |
+
plt.plot(epochs, acc, 'r', label='Training acc')
|
| 173 |
+
plt.plot(epochs, val_acc, 'b', label='Validation acc')
|
| 174 |
+
plt.title('Training and validation accuracy')
|
| 175 |
+
plt.grid(True)
|
| 176 |
+
plt.xlabel('Epochs')
|
| 177 |
+
plt.ylabel('Accuracy')
|
| 178 |
+
|
| 179 |
+
plt.legend(loc='lower right')
|
| 180 |
+
plt.savefig(f"caesartrainperformance/{history_filename}")
|
| 181 |
+
|
| 182 |
+
"""In this plot, the red lines represents the training loss and accuracy, and the blue lines are the validation loss and accuracy.
|
| 183 |
+
|
| 184 |
+
Classifying arbitrary instructions:
|
| 185 |
+
"""
|
| 186 |
+
|
| 187 |
+
def print_my_examples(inputs, results):
|
| 188 |
+
result_for_printing = \
|
| 189 |
+
[f'input: {inputs[i]:<30} : estimated intent: {results[i]}'
|
| 190 |
+
for i in range(len(inputs))]
|
| 191 |
+
print(*result_for_printing, sep='\n')
|
| 192 |
+
print()
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
results = tf.nn.softmax(classifier_model(tf.constant(examples)))
|
| 198 |
+
|
| 199 |
+
binarizer.classes_
|
| 200 |
+
|
| 201 |
+
intents=binarizer.inverse_transform(results.numpy())
|
| 202 |
+
|
| 203 |
+
print_my_examples(examples, intents)
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
examples = [
|
| 206 |
+
'play a song from U2', # this is the same sentence tried earlier
|
| 207 |
+
'Will it rain tomorrow',
|
| 208 |
+
'I like to hear greatist hits from beastie boys',
|
| 209 |
+
'I like to book a table for 3 persons',
|
| 210 |
+
'5 stars for machines like me'
|
| 211 |
+
]
|
| 212 |
+
datafolder="intentdata/"
|
| 213 |
+
trainfile=datafolder+"train.csv"
|
| 214 |
+
testfile=datafolder+"test.csv"
|
| 215 |
+
validfile=datafolder+"valid.csv"
|
| 216 |
+
|
| 217 |
+
"""Next, the downloaded .csv-files for training, validation and test are imported into pandas dataframes:"""
|
| 218 |
+
|
| 219 |
+
traindf = pd.read_csv(trainfile)
|
| 220 |
+
validdf = pd.read_csv(validfile)
|
| 221 |
+
testdf = pd.read_csv(testfile)
|
| 222 |
+
|
| 223 |
+
CaesarNLTrain.train(traindf,validdf,testdf,examples)
|
| 224 |
+
|
caesartrainperformance/.png
ADDED
|
Binary file (55.3 kB). View file
|
|
|
caesartrainperformance/history.png
ADDED
|
data_aggregation.ipynb
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import pandas as pd\n",
|
| 10 |
+
"import json\n",
|
| 11 |
+
"smalltalkintent = pd.read_csv(\"intentdata/Small_talk_Intent.csv\").rename(columns={\"Utterances\":\"text\",\"Intent\":\"intent\"})\n",
|
| 12 |
+
"training = pd.read_csv(\"intentdata/train_command.csv\")\n",
|
| 13 |
+
"df3 = pd.concat([training,smalltalkintent], ignore_index=True)\n",
|
| 14 |
+
"df3.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)\n",
|
| 15 |
+
"# TODO Do NLPAugmentation\n",
|
| 16 |
+
"#df3 = pd.concat([training,smalltalkintent], ignore_index=True)\n",
|
| 17 |
+
"#with open(\"intentdata/Intent.json\",\"r\") as f:\n",
|
| 18 |
+
"# chatbotXresponseintent = json.load(f)\n",
|
| 19 |
+
"#chatbotXresponseintent[\"intents\"][0] \n"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "code",
|
| 24 |
+
"execution_count": null,
|
| 25 |
+
"metadata": {},
|
| 26 |
+
"outputs": [],
|
| 27 |
+
"source": [
|
| 28 |
+
"# Produce responses\n",
|
| 29 |
+
"import json \n",
|
| 30 |
+
"with open(\"intentdata/Intent.json\",\"r\") as f:\n",
|
| 31 |
+
" data = json.load(f)\n",
|
| 32 |
+
"intents = []\n",
|
| 33 |
+
"for intent in data[\"intents\"]:\n",
|
| 34 |
+
" intents.append({\"intent\":intent[\"intent\"],\"responses\":intent[\"responses\"]})\n",
|
| 35 |
+
"with open(\"intentdata/responses.json\",\"w+\") as f:\n",
|
| 36 |
+
" json.dump({\"response\":intents},f)\n",
|
| 37 |
+
" "
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"source": [
|
| 46 |
+
"with open(\"intentdata/responses.json\",\"r\") as f:\n",
|
| 47 |
+
" responses = json.load(f)[\"responses\"]\n",
|
| 48 |
+
"#print(intents[0] in greetings)\n",
|
| 49 |
+
"#if intents[0] in greetings:\n",
|
| 50 |
+
"print(responses[\"Greeting\"])"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"execution_count": null,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"import json\n",
|
| 60 |
+
"import pandas as pd \n",
|
| 61 |
+
"from IPython.display import display\n",
|
| 62 |
+
"with open(\"intentdata/aug/intent_aug_text.json\",\"r\") as f:\n",
|
| 63 |
+
" data = json.load(f)[\"intents\"]\n",
|
| 64 |
+
"columns_to_concat = []\n",
|
| 65 |
+
"for i in range(len(data)):\n",
|
| 66 |
+
" column1 = pd.DataFrame.from_dict({\"text\":data[i][\"text\"]})\n",
|
| 67 |
+
" #print(column1)\n",
|
| 68 |
+
" column2 = pd.DataFrame.from_dict({\"intent\":[data[i][\"intent\"] for j in range(len(data[i][\"text\"]))]})\n",
|
| 69 |
+
" #print(column2)\n",
|
| 70 |
+
" tent_concat= pd.concat([column1,column2],axis=1)\n",
|
| 71 |
+
" #print(tent_concat)\n",
|
| 72 |
+
" columns_to_concat.append(tent_concat)\n",
|
| 73 |
+
"intentdf = pd.concat(columns_to_concat,axis=0)\n",
|
| 74 |
+
"intentdf\n",
|
| 75 |
+
"df = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 76 |
+
"newdf = pd.concat([df,intentdf],axis=0)\n",
|
| 77 |
+
"newdf.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"\n"
|
| 80 |
+
]
|
| 81 |
+
},
|
| 82 |
+
{
|
| 83 |
+
"cell_type": "code",
|
| 84 |
+
"execution_count": null,
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"outputs": [],
|
| 87 |
+
"source": [
|
| 88 |
+
"import pandas as pd\n",
|
| 89 |
+
"data = pd.read_csv(\"intentdata/train.csv\")\n"
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"cell_type": "code",
|
| 94 |
+
"execution_count": null,
|
| 95 |
+
"metadata": {},
|
| 96 |
+
"outputs": [],
|
| 97 |
+
"source": [
|
| 98 |
+
"import pandas as pd\n",
|
| 99 |
+
"data = pd.read_csv(\"intentdata/train_no_half_response.csv\") # Original data is a (2000, 7) DataFrame\n",
|
| 100 |
+
"data = data.replace(\"smalltalk_agent_acquaintance\",\"CourtesyGreeting\")\n",
|
| 101 |
+
"data = data.replace(\"smalltalk_agent_age\",\"CurrentHumanQuery\")\n",
|
| 102 |
+
"data = data.replace(\"smalltalk_agent_annoying\",\"NotTalking2U\")\n",
|
| 103 |
+
"data = data.replace(\"smalltalk_agent_bad\",\"Swearing\")\n",
|
| 104 |
+
"data = data.replace(\"smalltalk_agent_boss\",\"CurrentHumanQuery\")\n",
|
| 105 |
+
"data = data.replace(\"smalltalk_agent_clever\",\"Clever\")\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"data = data.replace(\"smalltalk_agent_beautiful\",\"Clever\")\n",
|
| 108 |
+
"data = data.replace(\"smalltalk_agent_fired\",\"Shutup\")\n",
|
| 109 |
+
"data = data.replace(\"smalltalk_agent_good\",\"Thanks\")\n",
|
| 110 |
+
"data = data.replace(\"smalltalk_agent_chatbot\",\"SelfAware\")\n",
|
| 111 |
+
"data = data.replace(\"smalltalk_agent_real\",\"SelfAware\")\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"data.to_csv(\"intentdata/train.csv\",mode=\"w\",index=False)"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": null,
|
| 120 |
+
"metadata": {},
|
| 121 |
+
"outputs": [],
|
| 122 |
+
"source": [
|
| 123 |
+
"# TODO Checks label data balance\n",
|
| 124 |
+
"import pandas as pd\n",
|
| 125 |
+
"data = pd.read_csv(\"intentdata/train.csv\") # Original data is a (2000, 7) DataFrame\n",
|
| 126 |
+
"# data contains 6 feature columns and 1 target column.\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"# Separate the design matrix from the target labels.\n",
|
| 129 |
+
"X = data.iloc[:, :-1]\n",
|
| 130 |
+
"y = data['intent']\n",
|
| 131 |
+
"\n",
|
| 132 |
+
"y.value_counts().sort_index().plot.bar(x='Target Value', y='Number of Occurrences',figsize=(20,20))"
|
| 133 |
+
]
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"cell_type": "code",
|
| 137 |
+
"execution_count": null,
|
| 138 |
+
"metadata": {},
|
| 139 |
+
"outputs": [],
|
| 140 |
+
"source": [
|
| 141 |
+
"import torch\n",
|
| 142 |
+
"torch.cuda.is_available()"
|
| 143 |
+
]
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"cell_type": "code",
|
| 147 |
+
"execution_count": null,
|
| 148 |
+
"metadata": {},
|
| 149 |
+
"outputs": [],
|
| 150 |
+
"source": [
|
| 151 |
+
"import json \n",
|
| 152 |
+
"import nlpaug.augmenter.word as naw\n",
|
| 153 |
+
"import nlpaug.flow as naf\n",
|
| 154 |
+
"print(\"Loading Models...\")\n",
|
| 155 |
+
"TOPK=20 #default=100\n",
|
| 156 |
+
"ACT = 'insert' #\"substitute\"\n",
|
| 157 |
+
"aug_w2v= naw.WordEmbsAug(\n",
|
| 158 |
+
" model_type='glove', model_path='glove/glove.6B.300d.txt',\n",
|
| 159 |
+
" action=\"substitute\")\n",
|
| 160 |
+
"aug_bert = naw.ContextualWordEmbsAug(\n",
|
| 161 |
+
" model_path='distilbert-base-uncased', \n",
|
| 162 |
+
" \n",
|
| 163 |
+
" action=ACT, top_k=TOPK)\n",
|
| 164 |
+
"aug = naf.Sequential([\n",
|
| 165 |
+
" aug_bert,aug_w2v\n",
|
| 166 |
+
" ])\n",
|
| 167 |
+
"print(\"Models Loaded.\")\n",
|
| 168 |
+
"with open(\"intentdata/intent.json\") as f:\n",
|
| 169 |
+
" intentwhole = json.load(f)[\"intents\"]\n",
|
| 170 |
+
"#text = intent[0][\"text\"][3]\n",
|
| 171 |
+
"for intent in intentwhole:\n",
|
| 172 |
+
" for text in intent[\"text\"]:\n",
|
| 173 |
+
" augmented_texts = set()\n",
|
| 174 |
+
" for i in range(20):\n",
|
| 175 |
+
" #aug = naw.SynonymAug(aug_src='wordnet',aug_min=1, aug_max=10, aug_p=i/10)\n",
|
| 176 |
+
" augmented_text = str(aug.augment(text)[0])\n",
|
| 177 |
+
" print(augmented_text)\n",
|
| 178 |
+
" #print(augmented_text)\n",
|
| 179 |
+
" augmented_texts.add(augmented_text)\n",
|
| 180 |
+
" augmented_texts = augmented_texts.union(set(intent[\"text\"]))\n",
|
| 181 |
+
" intent[\"text\"] = list(augmented_texts)\n",
|
| 182 |
+
" def test():\n",
|
| 183 |
+
" if intent[\"intent\"] != \"Jokes\": \n",
|
| 184 |
+
" for response in intent[\"responses\"]:\n",
|
| 185 |
+
" augmented_responses = set()\n",
|
| 186 |
+
" for i in range(20):\n",
|
| 187 |
+
" #aug = naw.SynonymAug(aug_src='wordnet',aug_min=1, aug_max=10, aug_p=i/50,stopwords=[\"<HUMAN>\",\"<HUMAN>,\",\"<HUMAN>!\"])\n",
|
| 188 |
+
" augmented_response = str(aug.augment(response)[0])\n",
|
| 189 |
+
" print(augmented_response)\n",
|
| 190 |
+
" try:\n",
|
| 191 |
+
" augmented_response = augmented_response[:augmented_response.index(\"<\")] + \"<HUMAN\" + augmented_response[augmented_response.index(\">\"):]\n",
|
| 192 |
+
" except ValueError as vex:\n",
|
| 193 |
+
" pass\n",
|
| 194 |
+
" #print(augmented_text)\n",
|
| 195 |
+
" augmented_responses.add(augmented_response)\n",
|
| 196 |
+
" augmented_responses = augmented_responses.union(set(intent[\"responses\"]))\n",
|
| 197 |
+
" intent[\"responses\"] = list(augmented_responses) \n",
|
| 198 |
+
"with open(\"intentdata/intent_aug_text_test.json\",\"w+\") as f:\n",
|
| 199 |
+
" json.dump({\"intents\":intentwhole},f)\n",
|
| 200 |
+
"\n",
|
| 201 |
+
"\n",
|
| 202 |
+
"print(intentwhole[1][\"responses\"])"
|
| 203 |
+
]
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"cell_type": "code",
|
| 207 |
+
"execution_count": null,
|
| 208 |
+
"metadata": {},
|
| 209 |
+
"outputs": [],
|
| 210 |
+
"source": [
|
| 211 |
+
"df = pd.read_csv(\"intentdata/train.csv\")\n",
|
| 212 |
+
"len(list(pd.unique(df[\"intent\"])))"
|
| 213 |
+
]
|
| 214 |
+
},
|
| 215 |
+
{
|
| 216 |
+
"cell_type": "code",
|
| 217 |
+
"execution_count": 1,
|
| 218 |
+
"metadata": {},
|
| 219 |
+
"outputs": [
|
| 220 |
+
{
|
| 221 |
+
"data": {
|
| 222 |
+
"text/plain": [
|
| 223 |
+
"['20000-Utterances-Training-dataset-for-chatbots-virtual-assistant-Bitext-sample',\n",
|
| 224 |
+
" 'AskUbuntu Corpus.json',\n",
|
| 225 |
+
" 'Bitext_Sample_Customer_Service_Training_Dataset',\n",
|
| 226 |
+
" 'Chatbot Corpus.json',\n",
|
| 227 |
+
" 'Dataset-train.csv',\n",
|
| 228 |
+
" 'intent-corpus-basic.json',\n",
|
| 229 |
+
" 'intent-corpus-enrich-limit-20.json',\n",
|
| 230 |
+
" 'intents.json',\n",
|
| 231 |
+
" 'intent_classification.csv',\n",
|
| 232 |
+
" 'music_intent_entities.json',\n",
|
| 233 |
+
" 'restaurant_intent_entities.json',\n",
|
| 234 |
+
" 'Web App Corpus.json']"
|
| 235 |
+
]
|
| 236 |
+
},
|
| 237 |
+
"execution_count": 1,
|
| 238 |
+
"metadata": {},
|
| 239 |
+
"output_type": "execute_result"
|
| 240 |
+
}
|
| 241 |
+
],
|
| 242 |
+
"source": [
|
| 243 |
+
"import os\n",
|
| 244 |
+
"import json \n",
|
| 245 |
+
"import pandas as pd\n",
|
| 246 |
+
"os.listdir(\"new_intent_data\")"
|
| 247 |
+
]
|
| 248 |
+
}
|
| 249 |
+
],
|
| 250 |
+
"metadata": {
|
| 251 |
+
"kernelspec": {
|
| 252 |
+
"display_name": "Python 3",
|
| 253 |
+
"language": "python",
|
| 254 |
+
"name": "python3"
|
| 255 |
+
},
|
| 256 |
+
"language_info": {
|
| 257 |
+
"codemirror_mode": {
|
| 258 |
+
"name": "ipython",
|
| 259 |
+
"version": 3
|
| 260 |
+
},
|
| 261 |
+
"file_extension": ".py",
|
| 262 |
+
"mimetype": "text/x-python",
|
| 263 |
+
"name": "python",
|
| 264 |
+
"nbconvert_exporter": "python",
|
| 265 |
+
"pygments_lexer": "ipython3",
|
| 266 |
+
"version": "3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]"
|
| 267 |
+
},
|
| 268 |
+
"orig_nbformat": 4,
|
| 269 |
+
"vscode": {
|
| 270 |
+
"interpreter": {
|
| 271 |
+
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
|
| 272 |
+
}
|
| 273 |
+
}
|
| 274 |
+
},
|
| 275 |
+
"nbformat": 4,
|
| 276 |
+
"nbformat_minor": 2
|
| 277 |
+
}
|
glove/glove.6B.300d.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:91125602f730fea7ca768736c6f442e668b49db095682bf2aad375db061c21ed
|
| 3 |
+
size 1037962819
|
glove/glove.6B.300d_should_be_here.txt
ADDED
|
File without changes
|
intentdata/Intent.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aa7a26bc52c78129d9d8ee1b0869296dea221cee8ac9917eddfe3cf85ff314f0
|
| 3 |
+
size 69909
|
intentdata/Small_talk_Intent.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:21f77e92d68efbba58ef7f8d5e83c024ee424914e03e5263c117c6c4d4d57c02
|
| 3 |
+
size 112938
|
intentdata/aug/intent_aug.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e44a8f5bf7d0ef5461c2d5c456d41fb95a5e89243f89906db6b3bc4a887c769
|
| 3 |
+
size 956737
|
intentdata/aug/intent_aug_text.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:18fd098e8140e5634af282308e60c20d56532e6f93d2c53792ef08454ea67876
|
| 3 |
+
size 166271
|
intentdata/intent_aug_text_test.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e68f0837f43d08b5c816c18c0b4f7b06663eacf657db29d390de22f67fd6c30b
|
| 3 |
+
size 215508
|
intentdata/responses.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5e3ac745ff4639e958da57bf4a95a89ffd881841eebf9cd586c9fd011f61a08a
|
| 3 |
+
size 43596
|
intentdata/test.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:14238327adbc98959c324884d1690c06504ed1c751199c68489542f3fb6fbb86
|
| 3 |
+
size 43112
|
intentdata/train.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7bd871b9af6266016ad9ec8b68fa411c21774a647113f2c2da9aad752dc6f078
|
| 3 |
+
size 1017949
|
intentdata/train_prev/train copy.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7bd871b9af6266016ad9ec8b68fa411c21774a647113f2c2da9aad752dc6f078
|
| 3 |
+
size 1017949
|
intentdata/train_prev/train copy_recent.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:33922ccc5fb200af7cd21a3679ed57e2a04db3bda5f79deeeb96020e7dc27b5c
|
| 3 |
+
size 1837180
|
intentdata/train_prev/train_command.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:af3855f2000fa8ce9a9f2e81a7a92395e07b1695f9353f16765f4c7fb08a613c
|
| 3 |
+
size 798951
|
intentdata/train_prev/train_no_half_response.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:327d2145ccb4d62c85853339abcc17f1b66636135c3e101083b8577e9e174da4
|
| 3 |
+
size 927503
|
intentdata/valid.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:21201144b9832b8c6e0715331f8d3379ae49194311abfb43bb6df02abe61d0f9
|
| 3 |
+
size 43306
|
main.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
import uvicorn
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from fastapi.responses import FileResponse
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from caesarinfer import CaesarNL
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
CURRENT_DIR = os.path.realpath(__file__).replace(f"/main.py","")
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"], # can alter with time
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
class CaesarAINLModel(BaseModel):
|
| 21 |
+
caesarapi: str
|
| 22 |
+
@app.get("/")
|
| 23 |
+
def caesarhome():
|
| 24 |
+
return "Caeser: How can I help you sir?"
|
| 25 |
+
|
| 26 |
+
@app.get("/caesarapi")
|
| 27 |
+
def caesarapiget():
|
| 28 |
+
return "Caeser: Hello sir, this is the CaesarAIAPI"
|
| 29 |
+
|
| 30 |
+
@app.post("/caesarapi")
|
| 31 |
+
def caesarapi(caesarapijson: CaesarAINLModel):
|
| 32 |
+
try:
|
| 33 |
+
caesarapijson = dict(caesarapijson)
|
| 34 |
+
|
| 35 |
+
print("Caesar Processing...")
|
| 36 |
+
caesarResponse,intents = CaesarNL.run([caesarapijson["caesarapi"]])
|
| 37 |
+
print("Caesar Processed.")
|
| 38 |
+
print(caesarResponse,"intent:",intents)
|
| 39 |
+
|
| 40 |
+
return {"caesarmessage":{"caesarResponse":caesarResponse,"intent":intents}}
|
| 41 |
+
|
| 42 |
+
except Exception as ex:
|
| 43 |
+
return {"error":f"{type(ex)}-{ex}"}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
async def main():
|
| 50 |
+
config = uvicorn.Config("main:app", port=7860, log_level="info",host="0.0.0.0",reload=True)
|
| 51 |
+
server = uvicorn.Server(config)
|
| 52 |
+
await server.serve()
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
asyncio.run(main())
|