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22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import tensorflow_addons as tfa\n",
    "from tensorflow.keras import layers\n",
    "import transformers\n",
    "import sentencepiece as spm\n",
    "#show the version of the package imported with text instructions\\\n",
    "print(\"Tensorflow version: \", tf.__version__)\n",
    "print(\"Tensorflow Addons version: \", tfa.__version__)\n",
    "print(\"Transformers version: \", transformers.__version__)\n",
    "print(\"Sentencepiece version: \", spm.__version__)\n",
    "print(\"Numpy version: \", np.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MeanPool(tf.keras.layers.Layer):\n",
    "    def call(self, inputs, mask=None):\n",
    "        broadcast_mask = tf.expand_dims(tf.cast(mask, \"float32\"), -1)\n",
    "        embedding_sum = tf.reduce_sum(inputs * broadcast_mask, axis=1)\n",
    "        mask_sum = tf.reduce_sum(broadcast_mask, axis=1)\n",
    "        mask_sum = tf.math.maximum(mask_sum, tf.constant([1e-9]))\n",
    "        return embedding_sum / mask_sum\n",
    "class WeightsSumOne(tf.keras.constraints.Constraint):\n",
    "    def __call__(self, w):\n",
    "        return tf.nn.softmax(w, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = transformers.AutoTokenizer.from_pretrained(\"microsoft/deberta-v3-large\"\n",
    ")\n",
    "tokenizer.save_pretrained('./tokenizer/')\n",
    "\n",
    "cfg = transformers.AutoConfig.from_pretrained(\"microsoft/deberta-v3-large\", output_hidden_states=True)\n",
    "cfg.hidden_dropout_prob = 0\n",
    "cfg.attention_probs_dropout_prob = 0\n",
    "cfg.save_pretrained('./tokenizer/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def deberta_encode(texts, tokenizer=tokenizer):\n",
    "    input_ids = []\n",
    "    attention_mask = []\n",
    "    \n",
    "    for text in texts:\n",
    "        token = tokenizer(text, \n",
    "                          add_special_tokens=True, \n",
    "                          max_length=512, \n",
    "                          return_attention_mask=True, \n",
    "                          return_tensors=\"np\", \n",
    "                          truncation=True, \n",
    "                          padding='max_length')\n",
    "        input_ids.append(token['input_ids'][0])\n",
    "        attention_mask.append(token['attention_mask'][0])\n",
    "    \n",
    "    return np.array(input_ids, dtype=\"int32\"), np.array(attention_mask, dtype=\"int32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MAX_LENGTH=512\n",
    "BATCH_SIZE=8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model():\n",
    "    input_ids = tf.keras.layers.Input(\n",
    "        shape=(MAX_LENGTH,), dtype=tf.int32, name=\"input_ids\"\n",
    "    )\n",
    "    \n",
    "    attention_masks = tf.keras.layers.Input(\n",
    "        shape=(MAX_LENGTH,), dtype=tf.int32, name=\"attention_masks\"\n",
    "    )\n",
    "   \n",
    "    deberta_model = transformers.TFAutoModel.from_pretrained(\"microsoft/deberta-v3-large\", config=cfg)\n",
    "    \n",
    "        \n",
    "    REINIT_LAYERS = 1\n",
    "    normal_initializer = tf.keras.initializers.GlorotUniform()\n",
    "    zeros_initializer = tf.keras.initializers.Zeros()\n",
    "    ones_initializer = tf.keras.initializers.Ones()\n",
    "\n",
    "#     print(f'\\nRe-initializing encoder block:')\n",
    "    for encoder_block in deberta_model.deberta.encoder.layer[-REINIT_LAYERS:]:\n",
    "#         print(f'{encoder_block}')\n",
    "        for layer in encoder_block.submodules:\n",
    "            if isinstance(layer, tf.keras.layers.Dense):\n",
    "                layer.kernel.assign(normal_initializer(shape=layer.kernel.shape, dtype=layer.kernel.dtype))\n",
    "                if layer.bias is not None:\n",
    "                    layer.bias.assign(zeros_initializer(shape=layer.bias.shape, dtype=layer.bias.dtype))\n",
    "\n",
    "            elif isinstance(layer, tf.keras.layers.LayerNormalization):\n",
    "                layer.beta.assign(zeros_initializer(shape=layer.beta.shape, dtype=layer.beta.dtype))\n",
    "                layer.gamma.assign(ones_initializer(shape=layer.gamma.shape, dtype=layer.gamma.dtype))\n",
    "\n",
    "    deberta_output = deberta_model.deberta(\n",
    "        input_ids, attention_mask=attention_masks\n",
    "    )\n",
    "    hidden_states = deberta_output.hidden_states\n",
    "    \n",
    "    #WeightedLayerPool + MeanPool of the last 4 hidden states\n",
    "    stack_meanpool = tf.stack(\n",
    "        [MeanPool()(hidden_s, mask=attention_masks) for hidden_s in hidden_states[-4:]], \n",
    "        axis=2)\n",
    "    \n",
    "    weighted_layer_pool = layers.Dense(1,\n",
    "                                       use_bias=False,\n",
    "                                       kernel_constraint=WeightsSumOne())(stack_meanpool)\n",
    "    \n",
    "    weighted_layer_pool = tf.squeeze(weighted_layer_pool, axis=-1)\n",
    "    output=layers.Dense(15,activation='linear')(weighted_layer_pool)\n",
    "    #x = layers.Dense(6, activation='linear')(x)\n",
    "    \n",
    "    #output = layers.Rescaling(scale=4.0, offset=1.0)(x)\n",
    "    model = tf.keras.Model(inputs=[input_ids, attention_masks], outputs=output)\n",
    "    \n",
    "    #Compile model with Layer-wise Learning Rate Decay\n",
    "    layer_list = [deberta_model.deberta.embeddings] + list(deberta_model.deberta.encoder.layer)\n",
    "    layer_list.reverse()\n",
    "    \n",
    "    INIT_LR = 1e-5\n",
    "    LLRDR = 0.9\n",
    "    LR_SCH_DECAY_STEPS = 1600\n",
    "    \n",
    "    lr_schedules = [tf.keras.optimizers.schedules.ExponentialDecay(\n",
    "        initial_learning_rate=INIT_LR * LLRDR ** i, \n",
    "        decay_steps=LR_SCH_DECAY_STEPS, \n",
    "        decay_rate=0.3) for i in range(len(layer_list))]\n",
    "    lr_schedule_head = tf.keras.optimizers.schedules.ExponentialDecay(\n",
    "        initial_learning_rate=1e-4, \n",
    "        decay_steps=LR_SCH_DECAY_STEPS, \n",
    "        decay_rate=0.3)\n",
    "    \n",
    "    optimizers = [tf.keras.optimizers.Adam(learning_rate=lr_sch) for lr_sch in lr_schedules]\n",
    "    \n",
    "    optimizers_and_layers = [(tf.keras.optimizers.Adam(learning_rate=lr_schedule_head), model.layers[-4:])] +\\\n",
    "        list(zip(optimizers, layer_list))\n",
    "    \n",
    "    optimizer = tfa.optimizers.MultiOptimizer(optimizers_and_layers)\n",
    "    \n",
    "    model.compile(optimizer=optimizer,\n",
    "                 loss='mse',\n",
    "                 metrics=[tf.keras.metrics.RootMeanSquaredError()],\n",
    "                 )\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.keras.backend.clear_session()\n",
    "model = get_model()\n",
    "model.load_weights('./best_model_fold2.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# map the integer labels to their original string representation\n",
    "label_mapping = {\n",
    "    0: 'Greeting',\n",
    "    1: 'Curiosity',\n",
    "    2: 'Interest',\n",
    "    3: 'Obscene',\n",
    "    4: 'Annoyed',\n",
    "    5: 'Openness',\n",
    "    6: 'Anxious',\n",
    "    7: 'Acceptance',\n",
    "    8: 'Uninterested',\n",
    "    9: 'Informative',\n",
    "    10: 'Accusatory',\n",
    "    11: 'Denial',\n",
    "    12: 'Confused',\n",
    "    13: 'Disapproval',\n",
    "    14: 'Remorse'\n",
    "}\n",
    "\n",
    "#label_strings = [label_mapping[label] for label in labels]\n",
    "\n",
    "#print(label_strings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def inference(texts):\n",
    "    prediction = model.predict(deberta_encode([texts]))\n",
    "    labels = np.argmax(prediction, axis=1)\n",
    "    label_strings = [label_mapping[label] for label in labels]\n",
    "    return label_strings[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "import pandas as pd\n",
    "import gradio as gr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "openai.organization = os.environ['org_id']\n",
    "openai.api_key = os.environ['openai_api']\n",
    "model_version = \"gpt-3.5-turbo\"\n",
    "model_token_limit = 10\n",
    "model_temperature = 0.1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generatePrompt () :\n",
    "    labels = [\"Openness\", \n",
    "    \"Anxious\",\n",
    "    \"Confused\",\n",
    "    \"Disapproval\",\n",
    "    \"Remorse\",\n",
    "    \"Uninterested\",\n",
    "    \"Accusatory\",\n",
    "    \"Annoyed\",\n",
    "    \"Interest\",\n",
    "    \"Curiosity\",\n",
    "    \"Acceptance\",\n",
    "    \"Obscene\",\n",
    "    \"Denial\",\n",
    "    \"Informative\",\n",
    "    \"Greeting\"]\n",
    "\n",
    "    formatted_labels = ', '.join(labels[:-1]) + ', or ' + labels[-1] + '.'\n",
    "\n",
    "    label_set = [\"Openness\", \"Anxious\", \"Confused\", \"Disapproval\", \"Remorse\", \"Accusatory\",\n",
    "          \"Denial\", \"Obscene\", \"Uninterested\", \"Annoyed\", \"Informative\", \"Greeting\",\n",
    "          \"Interest\", \"Curiosity\", \"Acceptance\"]\n",
    "\n",
    "    formatted_labels = ', '.join(label_set[:-1]) + ', or ' + label_set[-1] + '.\\n'\n",
    "\n",
    "    # The basic task to assign GPT (in natural language)\n",
    "    base_task = \"Classify the following text messages into one of the following categories using one word: \" + formatted_labels\n",
    "    base_task += \"Provide only a one word response. Use only the labels provided.\\n\"\n",
    "\n",
    "    return base_task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(message):\n",
    "    \n",
    "    prompt = [{\"role\": \"user\", \"content\": generatePrompt () + \"Text: \"+ message}]\n",
    "    \n",
    "    response = openai.ChatCompletion.create(\n",
    "                    model=model_version,\n",
    "                    temperature=model_temperature,\n",
    "                    max_tokens=model_token_limit,\n",
    "                    messages=prompt\n",
    "                )\n",
    "        \n",
    "    return response[\"choices\"][0][\"message\"][\"content\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_version = \"gpt-3.5-turbo\"\n",
    "model_token_limit = 2000\n",
    "model_temperature = 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def revision(message):\n",
    "    base_prompt = \"Here is a conversation between a Caller and a Volunteer. The Volunteer is trying to be as non-accusatory as possible but also wants to get as much information about the caller as possible. What should the volunteer say next in this exchange? Proved 3 possible responses.\"\n",
    "\n",
    "    prompt = [{\"role\": \"user\", \"content\": base_prompt + message}]\n",
    "    \n",
    "    response = openai.ChatCompletion.create(\n",
    "                    model=model_version,\n",
    "                    temperature=model_temperature,\n",
    "                    max_tokens=model_token_limit,\n",
    "                    messages=prompt\n",
    "                )\n",
    "\n",
    "    return response[\"choices\"][0][\"message\"][\"content\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "\n",
    "def combine(a):\n",
    "    return a + \"hello\"\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "with gr.Blocks() as demo:\n",
    "    gr.Markdown(\"## DeBERTa Sentiment Analysis\")\n",
    "    gr.Markdown(\"This is a custom DeBERTa model architecture for sentiment analysis with 15 labels: Openness, Anxiety, Confusion, Disapproval, Remorse, Accusation, Denial, Obscenity, Disinterest, Annoyance, Information, Greeting, Interest, Curiosity, or Acceptance.<br />Please enter your sentence(s) in the input box below and click the Submit button. The model will then process the input and provide the sentiment in one of the labels.<br/>The Test Example section below provides some input examples. Click on them and submit them to the model to see how it works.\")\n",
    "\n",
    "    txt = gr.Textbox(label=\"Input\", lines=2)\n",
    "    txt_1 = gr.Textbox(value=\"\", label=\"Output\")\n",
    "    btn = gr.Button(value=\"Submit\")\n",
    "    btn.click(inference, inputs=txt, outputs= txt_1)\n",
    "\n",
    "    demoExample = [\n",
    "        \"Hello, how are you?\",\n",
    "        \"I am so happy to be here!\",\n",
    "        \"i don't have time for u\"\n",
    "    ]\n",
    "\n",
    "    gr.Markdown(\"## Text Examples\")\n",
    "    gr.Examples(\n",
    "        demoExample,\n",
    "        txt,\n",
    "        txt_1,\n",
    "        inference\n",
    "    )\n",
    "\n",
    "with gr.Blocks() as gptdemo:\n",
    "\n",
    "    gr.Markdown(\"## GPT Sentiment Analysis\")\n",
    "    gr.Markdown(\"This a custom GPT model for sentiment analysis with 15 labels: Openness, Anxiety, Confusion, Disapproval, Remorse, Accusation, Denial, Obscenity, Disinterest, Annoyance, Information, Greeting, Interest, Curiosity, or Acceptance.<br />Please enter your sentence(s) in the input box below and click the Submit button. The model will then process the input and provide the sentiment in one of the labels.<br />The Test Example section below provides some input examples. Click on them and submit them to the model to see how it works.Please note that the input may be collected by service providers.\")\n",
    "    txt = gr.Textbox(label=\"Input\", lines=2)\n",
    "    txt_1 = gr.Textbox(value=\"\", label=\"Output\")\n",
    "    btn = gr.Button(value=\"Submit\")\n",
    "    btn.click(predict, inputs=txt, outputs= txt_1)\n",
    "\n",
    "    gptExample = [\n",
    "        \"Hello, how are you?\",\n",
    "        \"Are you busy at the moment?\",\n",
    "        \"I'm doing real good\"\n",
    "    ]\n",
    "\n",
    "    gr.Markdown(\"## Text Examples\")\n",
    "    gr.Examples(\n",
    "        gptExample,\n",
    "        txt,\n",
    "        txt_1,\n",
    "        predict\n",
    "    )\n",
    "\n",
    "\n",
    "with gr.Blocks() as revisiondemo:\n",
    "    gr.Markdown(\"## Conversation Revision\")\n",
    "    gr.Markdown(\"This is a custom GPT model designed to generate possible response texts based on previous contexts. You can input a conversation between a caller and a volunteer, and the model will provide three possible responses based on the input. <br />The Test Example section below provides some input examples. Click on them and submit them to the model to see how it works. Please note that the input may be collected by service providers.\")\n",
    "    txt = gr.Textbox(label=\"Input\", lines=2)\n",
    "    txt_1 = gr.Textbox(value=\"\", label=\"Output\",lines=4)\n",
    "    btn = gr.Button(value=\"Submit\")\n",
    "    btn.click(revision, inputs=txt, outputs= txt_1)\n",
    "\n",
    "    revisionExample = [\"Caller: sup\\nVolunteer: Hey, how's it going?\\nCaller: not very well, actually\\nVolunteer: What's the matter?\\nCaller: it's my wife, don't worry about it\"]\n",
    "\n",
    "    with gr.Column():\n",
    "        gr.Markdown(\"## Text Examples\")\n",
    "        gr.Examples(\n",
    "            revisionExample,\n",
    "            [txt],\n",
    "            txt_1,\n",
    "            revision\n",
    "        )\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "gr.TabbedInterface([demo, gptdemo,revisiondemo], [\"Model\", \"GPT\",\"Text Revision\"]\n",
    ").launch(inline=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "76d9096663e4677afe736ff46b3dcdaff586dfdb471519f50b872333a086db78"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}