Upload 10 files
Browse files- Added pre-trained BERT model files (config, checkpoints, vocab).
- Uploaded fine-tuned BERT model and weights.
- Included requirements.txt with specific dependencies.
- Added README.md with instructions on model usage and dependencies.
- Added Python script for running the model and making predictions.
- .gitattributes +1 -0
- Intent predictor.ipynb +194 -0
- README.md +45 -0
- bert_200_max_seq_len_model.h5 +3 -0
- bert_adv_synthetic_weights_v2(14).h5 +3 -0
- bert_config.json +13 -0
- bert_model.ckpt.data-00000-of-00001 +3 -0
- bert_model.ckpt.index +0 -0
- bert_model.ckpt.meta +0 -0
- requirements.txt +7 -0
- vocab.txt +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
bert_model.ckpt.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
Intent predictor.ipynb
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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13 |
+
"from tqdm import tqdm\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"import pandas as pd\n",
|
| 16 |
+
"import numpy as np\n",
|
| 17 |
+
"\n",
|
| 18 |
+
"import tensorflow as tf\n",
|
| 19 |
+
"from tensorflow import keras\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"import bert\n",
|
| 22 |
+
"from bert import BertModelLayer\n",
|
| 23 |
+
"from bert.loader import StockBertConfig, map_stock_config_to_params, load_stock_weights\n",
|
| 24 |
+
"from bert.tokenization.bert_tokenization import FullTokenizer"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"execution_count": 2,
|
| 30 |
+
"metadata": {},
|
| 31 |
+
"outputs": [],
|
| 32 |
+
"source": [
|
| 33 |
+
"bert_model_name=\"uncased_L-12_H-768_A-12\"\n",
|
| 34 |
+
"\n",
|
| 35 |
+
"bert_ckpt_dir = os.path.join(\"model/\", bert_model_name)\n",
|
| 36 |
+
"bert_ckpt_file = os.path.join(bert_ckpt_dir, \"bert_model.ckpt\")\n",
|
| 37 |
+
"bert_config_file = os.path.join(bert_ckpt_dir, \"bert_config.json\")"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": 3,
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"source": [
|
| 46 |
+
"tokenizer = FullTokenizer(vocab_file=os.path.join(bert_ckpt_dir, \"vocab.txt\"))"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "code",
|
| 51 |
+
"execution_count": 4,
|
| 52 |
+
"metadata": {},
|
| 53 |
+
"outputs": [],
|
| 54 |
+
"source": [
|
| 55 |
+
"classes = ['service_availability_check',\n",
|
| 56 |
+
" 'billing_inquiry', \n",
|
| 57 |
+
" 'order_cancellation',\n",
|
| 58 |
+
" 'address_verification',\n",
|
| 59 |
+
" 'user_authentication',\n",
|
| 60 |
+
" 'account_information_update',\n",
|
| 61 |
+
" 'call_divert', \n",
|
| 62 |
+
" 'customer_service_escalation',\n",
|
| 63 |
+
" 'appointment_scheduling',\n",
|
| 64 |
+
" 'order_status_inquiry',\n",
|
| 65 |
+
" 'product_information_request',\n",
|
| 66 |
+
" 'complaint_registration',\n",
|
| 67 |
+
" 'call_disconnect',\n",
|
| 68 |
+
" 'appointment_confirmation', \n",
|
| 69 |
+
" 'appointment_cancellation']\n",
|
| 70 |
+
"\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"max_seq_len = 200"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 5,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": [
|
| 81 |
+
"def create_model(max_seq_len, bert_ckpt_file):\n",
|
| 82 |
+
" with tf.io.gfile.GFile(bert_config_file, \"r\") as reader:\n",
|
| 83 |
+
" bc = StockBertConfig.from_json_string(reader.read())\n",
|
| 84 |
+
" bert_params = map_stock_config_to_params(bc)\n",
|
| 85 |
+
" bert_params.adapter_size = None\n",
|
| 86 |
+
" bert = BertModelLayer.from_params(bert_params, name=\"bert\")\n",
|
| 87 |
+
" input_ids = keras.layers.Input(\n",
|
| 88 |
+
" shape=(max_seq_len, ),\n",
|
| 89 |
+
" dtype='int32',\n",
|
| 90 |
+
" name=\"input_ids\"\n",
|
| 91 |
+
" )\n",
|
| 92 |
+
" bert_output = bert(input_ids)\n",
|
| 93 |
+
" print(\"bert shape\", bert_output.shape)\n",
|
| 94 |
+
" cls_out = keras.layers.Lambda(lambda seq: seq[:, 0, :])(bert_output)\n",
|
| 95 |
+
" cls_out = keras.layers.Dropout(0.5)(cls_out)\n",
|
| 96 |
+
" logits = keras.layers.Dense(units=768, activation=\"tanh\")(cls_out)\n",
|
| 97 |
+
" logits = keras.layers.Dropout(0.5)(logits)\n",
|
| 98 |
+
" logits = keras.layers.Dense(\n",
|
| 99 |
+
" units=len(classes),\n",
|
| 100 |
+
" activation=\"softmax\"\n",
|
| 101 |
+
" )(logits)\n",
|
| 102 |
+
" model = keras.Model(inputs=input_ids, outputs=logits)\n",
|
| 103 |
+
" model.build(input_shape=(None, max_seq_len))\n",
|
| 104 |
+
" load_stock_weights(bert, bert_ckpt_file)\n",
|
| 105 |
+
" return model\n"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"execution_count": null,
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"outputs": [],
|
| 113 |
+
"source": [
|
| 114 |
+
"model = create_model(max_seq_len, bert_ckpt_file)"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": 7,
|
| 120 |
+
"metadata": {},
|
| 121 |
+
"outputs": [],
|
| 122 |
+
"source": [
|
| 123 |
+
"model.load_weights('bert_adv_synthetic_weights_v2(14).h5') "
|
| 124 |
+
]
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"cell_type": "code",
|
| 128 |
+
"execution_count": 17,
|
| 129 |
+
"metadata": {},
|
| 130 |
+
"outputs": [],
|
| 131 |
+
"source": [
|
| 132 |
+
"# Run this if you change the max_seq_len\n",
|
| 133 |
+
"#model.save('bert_200_max_seq_len_model.h5') \n",
|
| 134 |
+
" "
|
| 135 |
+
]
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
"cell_type": "code",
|
| 139 |
+
"execution_count": 8,
|
| 140 |
+
"metadata": {},
|
| 141 |
+
"outputs": [],
|
| 142 |
+
"source": [
|
| 143 |
+
"def intent_recognizer(sentences):\n",
|
| 144 |
+
" pred_tokens = map(tokenizer.tokenize, sentences)\n",
|
| 145 |
+
" pred_tokens = map(lambda tok: [\"[CLS]\"] + tok + [\"[SEP]\"], pred_tokens)\n",
|
| 146 |
+
" pred_token_ids = list(map(tokenizer.convert_tokens_to_ids, pred_tokens))\n",
|
| 147 |
+
" pred_token_ids = map(lambda tids: tids +[0]*(max_seq_len-len(tids)),pred_token_ids)\n",
|
| 148 |
+
" pred_token_ids = np.array(list(pred_token_ids))\n",
|
| 149 |
+
" predictions = model.predict(pred_token_ids).argmax(axis=-1)\n",
|
| 150 |
+
" for text, label in zip(sentences, predictions):\n",
|
| 151 |
+
" print(\"text:\", text, \"\\nintent:\", classes[label])\n",
|
| 152 |
+
" print()"
|
| 153 |
+
]
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"cell_type": "code",
|
| 157 |
+
"execution_count": null,
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"outputs": [],
|
| 160 |
+
"source": [
|
| 161 |
+
"sentences = [\"While I’ve been speaking with you, I realized that my particular request might be better handled by someone with a more specialized focus. I believe there’s another team, maybe the one that deals with customer inquiries more closely, that would be better suited to help me. It seems like my issue falls outside the general scope, and I’d really appreciate being put in touch with the department or individual who is more equipped to provide the support I’m looking for. I’m thinking maybe the sales or technical support team could be more in line with what I need.\"]\n",
|
| 162 |
+
"intent_recognizer(sentences)"
|
| 163 |
+
]
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"cell_type": "code",
|
| 167 |
+
"execution_count": null,
|
| 168 |
+
"metadata": {},
|
| 169 |
+
"outputs": [],
|
| 170 |
+
"source": []
|
| 171 |
+
}
|
| 172 |
+
],
|
| 173 |
+
"metadata": {
|
| 174 |
+
"kernelspec": {
|
| 175 |
+
"display_name": "base",
|
| 176 |
+
"language": "python",
|
| 177 |
+
"name": "python3"
|
| 178 |
+
},
|
| 179 |
+
"language_info": {
|
| 180 |
+
"codemirror_mode": {
|
| 181 |
+
"name": "ipython",
|
| 182 |
+
"version": 3
|
| 183 |
+
},
|
| 184 |
+
"file_extension": ".py",
|
| 185 |
+
"mimetype": "text/x-python",
|
| 186 |
+
"name": "python",
|
| 187 |
+
"nbconvert_exporter": "python",
|
| 188 |
+
"pygments_lexer": "ipython3",
|
| 189 |
+
"version": "3.7.4"
|
| 190 |
+
}
|
| 191 |
+
},
|
| 192 |
+
"nbformat": 4,
|
| 193 |
+
"nbformat_minor": 2
|
| 194 |
+
}
|
README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Custom BERT Model for Intent Recognition
|
| 3 |
+
|
| 4 |
+
This repository contains a custom fine-tuned BERT model for intent recognition. The model was trained to recognize a set of customer service-related intents, and it's based on the pre-trained BERT architecture (uncased_L-12_H-768_A-12).
|
| 5 |
+
|
| 6 |
+
## Python Version
|
| 7 |
+
|
| 8 |
+
This project is compatible with **Python 3.7.4**. It is recommended to use this version for compatibility with the listed dependencies.
|
| 9 |
+
|
| 10 |
+
## Model Information
|
| 11 |
+
|
| 12 |
+
- **Base Architecture**: BERT (uncased_L-12_H-768_A-12)
|
| 13 |
+
- **Max Sequence Length**: 200
|
| 14 |
+
- **Number of Intents**: 15
|
| 15 |
+
|
| 16 |
+
## Classes
|
| 17 |
+
|
| 18 |
+
The model is trained to classify the following customer service-related intents:
|
| 19 |
+
don't change the order while intializing
|
| 20 |
+
|
| 21 |
+
1. `service_availability_check`
|
| 22 |
+
2. `billing_inquiry`
|
| 23 |
+
3. `order_cancellation`
|
| 24 |
+
4. `address_verification`
|
| 25 |
+
5. `user_authentication`
|
| 26 |
+
6. `account_information_update`
|
| 27 |
+
7. `call_divert`
|
| 28 |
+
8. `customer_service_escalation`
|
| 29 |
+
9. `appointment_scheduling`
|
| 30 |
+
10. `order_status_inquiry`
|
| 31 |
+
11. `product_information_request`
|
| 32 |
+
12. `complaint_registration`
|
| 33 |
+
13. `call_disconnect`
|
| 34 |
+
14. `appointment_confirmation`
|
| 35 |
+
15. `appointment_cancellation`
|
| 36 |
+
|
| 37 |
+
## How to Use
|
| 38 |
+
|
| 39 |
+
To use the model, load the configuration file (bert_config.json), the checkpoint files (bert_model.ckpt*), and the vocabulary file (vocab.txt). Along with these, load the saved fine-tuned model or weights (if you plan to modify layers or change the max_seq_len [the length of input sentences]). This ensures that the model is correctly configured and functions as expected for your custom use case.
|
| 40 |
+
|
| 41 |
+
## Intended Use
|
| 42 |
+
|
| 43 |
+
This model is designed for intent recognition in customer service applications and supports a variety of queries such as billing inquiries, order cancellations, service availability checks, and more.
|
| 44 |
+
|
| 45 |
+
.
|
bert_200_max_seq_len_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5caa23740f8446cb18470b329be05337a7f6f9ff044cf9150beefa48e491ef7f
|
| 3 |
+
size 438221296
|
bert_adv_synthetic_weights_v2(14).h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f2733aa63247a3f1e7671c8283521e94d552a12f0ad79cebd66502cd46902c7b
|
| 3 |
+
size 438219568
|
bert_config.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"attention_probs_dropout_prob": 0.1,
|
| 3 |
+
"hidden_act": "gelu",
|
| 4 |
+
"hidden_dropout_prob": 0.1,
|
| 5 |
+
"hidden_size": 768,
|
| 6 |
+
"initializer_range": 0.02,
|
| 7 |
+
"intermediate_size": 3072,
|
| 8 |
+
"max_position_embeddings": 512,
|
| 9 |
+
"num_attention_heads": 12,
|
| 10 |
+
"num_hidden_layers": 12,
|
| 11 |
+
"type_vocab_size": 2,
|
| 12 |
+
"vocab_size": 30522
|
| 13 |
+
}
|
bert_model.ckpt.data-00000-of-00001
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:58580dc5e0bf0ae0d2efd51d0e8272b2f808857f0a43a88aaf7549da6d7a8a84
|
| 3 |
+
size 440425712
|
bert_model.ckpt.index
ADDED
|
Binary file (8.53 kB). View file
|
|
|
bert_model.ckpt.meta
ADDED
|
Binary file (904 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Python 3.7.4 required
|
| 3 |
+
tensorflow==2.2.0
|
| 4 |
+
keras==2.3.0-tf
|
| 5 |
+
bert-for-tf2==0.14.9
|
| 6 |
+
pandas==1.3.5
|
| 7 |
+
numpy==1.19.5
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|