lsacy commited on
Commit
8029d8e
1 Parent(s): b3ff46b

minor changes

Browse files
..gitignore.un~ CHANGED
Binary files a/..gitignore.un~ and b/..gitignore.un~ differ
 
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
.Rhistory ADDED
File without changes
.gitignore CHANGED
@@ -4,4 +4,5 @@ openai_api_key.txt
4
  Dockerfile
5
  test.ipynb
6
  stream_app_minimum.py
7
- joy.py
 
 
4
  Dockerfile
5
  test.ipynb
6
  stream_app_minimum.py
7
+ joy.py
8
+ *~
.gitignore~ CHANGED
@@ -1,3 +1,7 @@
1
  data/
2
  .env
3
  openai_api_key.txt
 
 
 
 
 
1
  data/
2
  .env
3
  openai_api_key.txt
4
+ Dockerfile
5
+ test.ipynb
6
+ stream_app_minimum.py
7
+ joy.py
CICD/BUILD_TEST.png ADDED
CICD/githubActionBuild.png ADDED
CICD/githubActionPullRequest.png ADDED
CICD/githubActionWorkflow.png ADDED
README.md~ ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Advanced Software Engineering 2022/23
2
+ ### BHT with Prof. Dr. Edlich
3
+ # mentalHealth_Chatbot
4
+ <h3> an AI therapist, based on GPT3, fine-tuned with custom data. </h3>
5
+
6
+ ## Table of content
7
+ * [About the App](#About-the-app)
8
+ * [Checklist](#Checklist)
9
+ * [Technology](#Technology)
10
+ * [local Setup / webserver](#setup)
11
+
12
+ ## About the app
13
+
14
+
15
+ ## Checklist
16
+
17
+ ## Technology
18
+
19
+ ## Setup
20
+
README.md~ 2 ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Advanced Software Engineering 2022/23
2
+ ### BHT with Prof. Dr. Edlich
3
+ # mentalHealth_Chatbot
4
+ <h3> an AI therapist, based on GPT3, fine-tuned with custom data. </h3>
5
+
6
+ ## Table of content
7
+ * [About the App](#About-the-app)
8
+ * [Checklist](#Checklist)
9
+ * [Technology](#Technology)
10
+ * [local Setup / webserver](#setup)
11
+
12
+ ## About the app
13
+
14
+
15
+ ## Checklist
16
+
17
+ ## Technology
18
+
19
+ ## Setup
20
+
__pycache__/streamlit.cpython-310.pyc DELETED
Binary file (379 Bytes)
 
__pycache__/streamlit.cpython-38.pyc DELETED
Binary file (377 Bytes)
 
__pycache__/streamlit_app.cpython-38.pyc ADDED
Binary file (4.34 kB). View file
 
__pycache__/test_sample.cpython-38-pytest-7.2.1.pyc ADDED
Binary file (2.34 kB). View file
 
buildManagement/actionYAML.png ADDED
buildManagement/pyBuilder.png ADDED
code_snippit_tests 2.ipynb ADDED
@@ -0,0 +1,456 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 19,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "test = ''' Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
10
+ "\n",
11
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
12
+ "\n",
13
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
14
+ "\n",
15
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
16
+ "\n",
17
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
18
+ "\n",
19
+ "Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
20
+ "\n",
21
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
22
+ "\n",
23
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
24
+ "\n",
25
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
26
+ "\n",
27
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
28
+ "\n",
29
+ "Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
30
+ "\n",
31
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
32
+ "\n",
33
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
34
+ "\n",
35
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
36
+ "\n",
37
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
38
+ "\n",
39
+ "Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
40
+ "\n",
41
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
42
+ "\n",
43
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
44
+ "\n",
45
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
46
+ "\n",
47
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
48
+ "\n",
49
+ "\n",
50
+ "\n",
51
+ " '''\n",
52
+ "test3 = ['[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to.\\nJoy:I am Joy, your AI therapist. How are you feeling today?', \"\\n\\nPatient:im doing okay\\nJoy:That's good to hear. Is there anything in particular that you would like to talk about today?\", '\\n\\nPatient:no not really\\nJoy:No problem. Is there anything on your mind that you would like to share? I am here to listen and offer support.', \"\\n\\nPatient:maybe actually. I am afraid my spouse cheats on me\\nJoy:I'm sorry to hear that. What has been making you feel this way?\", '\\n\\nPatient:she is writing text messages constantly with people i dont know\\nJoy:That can certainly be concerning. Have you tried talking to your spouse about this?', '\\n\\nPatient:yea then she gets defensive and angry\\nJoy:I understand that this must be difficult for you. Are there any specific strategies that have helped when conversations with your spouse become heated?', \"\\n\\nPatient:i dont know\\nJoy:It's okay if you don't know right away. What do you feel comfortable doing in these situations? Are there any strategies that have helped in the past when things become tense between you and your spouse?\", '\\n\\nPatient:i just stopped talking to her or change the subject\\nJoy:It sounds like you are doing your best to find a way to manage this. Is there anything that has helped make the situation more comfortable for you?', '\\n\\nPatient:trying not thinking about it\\nJoy:It sounds like you are doing your best to handle this difficult situation. Do you think it would help to talk to a professional or find additional support?', \"\\n\\nPatient:no i havent\\nJoy:That's understandable. Is there someone who you feel comfortable talking to about this? How does having a conversation with them make you feel?\", \"\\n\\nPatient:i havent talked with anyone about this\\nJoy:That's understandable. Having someone to talk to can be beneficial in difficult times like this. Is there anyone who you trust that you feel comfortable speaking with about your concerns?\", '\\n\\nPatient:there is no one, except maybe my mom\\nJoy:Talking to your mom can be a great start. Is there anything in particular that you want to share with her about what is going on? How does the idea of having this conversation make you feel?', '\\n\\nPatient:yea like I said, about this topic that im not very comfortable with, do you still remember which?\\nJoy:Yes, you mentioned that your spouse may be cheating on you. How would you like to approach this conversation with your mom?', \"\\n\\nPatient:tell her about my suspicision i guess\\nJoy:It can be difficult to bring up a topic like this, but it's important that you have an open conversation with her. Would you like any advice on how to start the conversation?\", \"\\n\\nPatient:yes please\\nJoy:It can help to start by letting your mom know why you decided to talk to her about this. Sharing how you are feeling and asking for her advice might be a good way to open the conversation. I'm here if you need anything else before or after speaking with your mom.\"]"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": 20,
58
+ "metadata": {},
59
+ "outputs": [],
60
+ "source": [
61
+ "\n",
62
+ "def clean_chat_log(chat_log):\n",
63
+ " chat_log = ' '.join(chat_log)\n",
64
+ " # find the first /n\n",
65
+ " first_newline = chat_log.find('\\n')\n",
66
+ " chat_log = chat_log[first_newline:]\n",
67
+ " # remove all \\n\n",
68
+ " chat_log = chat_log.replace('\\n', ' ')\n",
69
+ " return chat_log"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": 32,
75
+ "metadata": {},
76
+ "outputs": [
77
+ {
78
+ "data": {
79
+ "text/plain": [
80
+ "['[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to. Joy:I am Joy, your AI therapist. How are you feeling today?',\n",
81
+ " \" Patient:im doing okay Joy:That's good to hear. Is there anything in particular that you would like to talk about today?\",\n",
82
+ " ' Patient:no not really Joy:No problem. Is there anything on your mind that you would like to share? I am here to listen and offer support.',\n",
83
+ " \" Patient:maybe actually. I am afraid my spouse cheats on me Joy:I'm sorry to hear that. What has been making you feel this way?\",\n",
84
+ " ' Patient:she is writing text messages constantly with people i dont know Joy:That can certainly be concerning. Have you tried talking to your spouse about this?',\n",
85
+ " ' Patient:yea then she gets defensive and angry Joy:I understand that this must be difficult for you. Are there any specific strategies that have helped when conversations with your spouse become heated?',\n",
86
+ " \" Patient:i dont know Joy:It's okay if you don't know right away. What do you feel comfortable doing in these situations? Are there any strategies that have helped in the past when things become tense between you and your spouse?\",\n",
87
+ " ' Patient:i just stopped talking to her or change the subject Joy:It sounds like you are doing your best to find a way to manage this. Is there anything that has helped make the situation more comfortable for you?',\n",
88
+ " ' Patient:trying not thinking about it Joy:It sounds like you are doing your best to handle this difficult situation. Do you think it would help to talk to a professional or find additional support?',\n",
89
+ " \" Patient:no i havent Joy:That's understandable. Is there someone who you feel comfortable talking to about this? How does having a conversation with them make you feel?\",\n",
90
+ " \" Patient:i havent talked with anyone about this Joy:That's understandable. Having someone to talk to can be beneficial in difficult times like this. Is there anyone who you trust that you feel comfortable speaking with about your concerns?\",\n",
91
+ " ' Patient:there is no one, except maybe my mom Joy:Talking to your mom can be a great start. Is there anything in particular that you want to share with her about what is going on? How does the idea of having this conversation make you feel?',\n",
92
+ " ' Patient:yea like I said, about this topic that im not very comfortable with, do you still remember which? Joy:Yes, you mentioned that your spouse may be cheating on you. How would you like to approach this conversation with your mom?',\n",
93
+ " \" Patient:tell her about my suspicision i guess Joy:It can be difficult to bring up a topic like this, but it's important that you have an open conversation with her. Would you like any advice on how to start the conversation?\",\n",
94
+ " \" Patient:yes please Joy:It can help to start by letting your mom know why you decided to talk to her about this. Sharing how you are feeling and asking for her advice might be a good way to open the conversation. I'm here if you need anything else before or after speaking with your mom.\"]"
95
+ ]
96
+ },
97
+ "execution_count": 32,
98
+ "metadata": {},
99
+ "output_type": "execute_result"
100
+ }
101
+ ],
102
+ "source": [
103
+ "def remove_backslashN(chat_log):\n",
104
+ " chat_log = [i.replace('\\n', ' ') for i in chat_log]\n",
105
+ " return chat_log\n",
106
+ "\n",
107
+ "remove_backslashN(test3)"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": 21,
113
+ "metadata": {},
114
+ "outputs": [
115
+ {
116
+ "name": "stderr",
117
+ "output_type": "stream",
118
+ "text": [
119
+ "Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
120
+ ]
121
+ },
122
+ {
123
+ "data": {
124
+ "text/plain": [
125
+ "682"
126
+ ]
127
+ },
128
+ "execution_count": 21,
129
+ "metadata": {},
130
+ "output_type": "execute_result"
131
+ }
132
+ ],
133
+ "source": [
134
+ "len(tokenizer(test, padding=True, truncation=True, return_tensors=\"pt\")[0])"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": 18,
140
+ "metadata": {},
141
+ "outputs": [
142
+ {
143
+ "name": "stderr",
144
+ "output_type": "stream",
145
+ "text": [
146
+ "Some weights of the model checkpoint at cardiffnlp/twitter-roberta-base-sentiment-latest were not used when initializing RobertaForSequenceClassification: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
147
+ "- This IS expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
148
+ "- This IS NOT expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
149
+ "Some weights of the model checkpoint at cardiffnlp/twitter-roberta-base-sentiment-latest were not used when initializing RobertaForSequenceClassification: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
150
+ "- This IS expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
151
+ "- This IS NOT expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
152
+ ]
153
+ }
154
+ ],
155
+ "source": [
156
+ "from transformers import AutoModelForSequenceClassification\n",
157
+ "from transformers import TFAutoModelForSequenceClassification\n",
158
+ "from transformers import AutoTokenizer, AutoConfig\n",
159
+ "from transformers import pipeline\n",
160
+ "MODEL = \"cardiffnlp/twitter-roberta-base-sentiment-latest\"\n",
161
+ "\n",
162
+ "tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
163
+ "config = AutoConfig.from_pretrained(MODEL)\n",
164
+ "model = AutoModelForSequenceClassification.from_pretrained(MODEL)\n",
165
+ "\n",
166
+ "sentiment_task = pipeline(\"sentiment-analysis\", model='cardiffnlp/twitter-roberta-base-sentiment-latest', tokenizer='cardiffnlp/twitter-roberta-base-sentiment-latest')\n"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": 56,
172
+ "metadata": {},
173
+ "outputs": [],
174
+ "source": [
175
+ "import numpy as np\n",
176
+ "tokenized = tokenizer(['this is great', 'this sucks'], return_tensors='pt', truncation=True, padding=True)\n",
177
+ "output = model(**tokenized)\n",
178
+ "scores = output[0][0].detach().numpy()\n",
179
+ "scores = np.exp(scores) / np.sum(np.exp(scores), axis=0)"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "execution_count": 58,
185
+ "metadata": {},
186
+ "outputs": [
187
+ {
188
+ "data": {
189
+ "text/plain": [
190
+ "SequenceClassifierOutput(loss=None, logits=tensor([[-2.1332, -0.7378, 2.8549],\n",
191
+ " [ 1.6230, -0.1552, -1.7155]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None)"
192
+ ]
193
+ },
194
+ "execution_count": 58,
195
+ "metadata": {},
196
+ "output_type": "execute_result"
197
+ }
198
+ ],
199
+ "source": [
200
+ "output"
201
+ ]
202
+ },
203
+ {
204
+ "cell_type": "code",
205
+ "execution_count": 57,
206
+ "metadata": {},
207
+ "outputs": [
208
+ {
209
+ "name": "stdout",
210
+ "output_type": "stream",
211
+ "text": [
212
+ "1) positive 0.9668\n",
213
+ "2) neutral 0.0266\n",
214
+ "3) negative 0.0066\n"
215
+ ]
216
+ }
217
+ ],
218
+ "source": [
219
+ "ranking = np.argsort(scores)\n",
220
+ "ranking = ranking[::-1]\n",
221
+ "for i in range(scores.shape[0]):\n",
222
+ " l = config.id2label[ranking[i]]\n",
223
+ " s = scores[ranking[i]]\n",
224
+ " print(f\"{i+1}) {l} {np.round(float(s), 4)}\")"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": 86,
230
+ "metadata": {},
231
+ "outputs": [
232
+ {
233
+ "name": "stdout",
234
+ "output_type": "stream",
235
+ "text": [
236
+ "0.6931471805599453\n",
237
+ "2.4849066497880004\n",
238
+ "3.091042453358316\n",
239
+ "3.4657359027997265\n",
240
+ "3.7376696182833684\n",
241
+ "3.9512437185814275\n",
242
+ "4.127134385045092\n",
243
+ "4.276666119016055\n",
244
+ "4.406719247264253\n",
245
+ "4.5217885770490405\n"
246
+ ]
247
+ }
248
+ ],
249
+ "source": [
250
+ "# import natural log function\n",
251
+ "from math import log\n",
252
+ "\n",
253
+ "for i in range(2, 100, 10):\n",
254
+ " print(log(i))"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": 27,
260
+ "metadata": {},
261
+ "outputs": [
262
+ {
263
+ "name": "stderr",
264
+ "output_type": "stream",
265
+ "text": [
266
+ "Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.\n"
267
+ ]
268
+ },
269
+ {
270
+ "name": "stdout",
271
+ "output_type": "stream",
272
+ "text": [
273
+ "[\" .J.o.y.:.I. .a.m. .J.o.y.,. .y.o.u.r. .A.I. .t.h.e.r.a.p.i.s.t... .H.o.w. .a.r.e. .y.o.u. .f.e.e.l.i.n.g. .t.o.d.a.y.?. . . .P.a.t.i.e.n.t.:.i.m. .d.o.i.n.g. .o.k.a.y. .J.o.y.:.T.h.a.t.'.s. .g.o.o.d. .t.o. .h.e.a.r... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .i.n. .p.a.r.t.i.c.u.l.a.r. .t.h.a.t. .y.o.u. .w.o.u.l.d. .l.i.k.e. .t.o. .t.a.l.k. .a.b.o.u.t. .t.o.d.a.y.?. . . .P.a.t.i.e.n.t.:.n.o. .n.o.t. .r.e.a.l.l.y. .J.o.y.:.N.o. .p.r.o.b.l.e.m... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .o.n. .y.o.u.r. .m.i.n.d. .t.h.a.t. .y.o.u. .w.o.u.l.d. .l.i.k.e. .t.o. .s.h.a.r.e.?. .I. .a.m. .h.e.r.e. .t.o. .l.i.s.t.e.n. .a.n.d. .o.f.f.e.r. .s.u.p.p.o.r.t... . . .P.a.t.i.e.n.t.:.m.a.y.b.e. .a.c.t.u.a.l.l.y... .I. .a.m. .a.f.r.a.i.d. .m.y. .s.p.o.u.s.e. .c.h.e.a.t.s. .o.n. .m.e. .J.o.y.:.I.'.m. .s.o.r.r.y. .t.o. .h.e.a.r. .t.h.a.t... .W.h.a.t. .h.a.s. .b.e.e.n. .m.a.k.i.n.g. .y.o.u. .f.e.e.l. .t.h.i.s. .w.a.y.?. . . .P.a.t.i.e.n.t.:.s.h.e. .i.s. .w.r.i.t.i.n.g. .t.e.x.t. .m.e.s.s.a.g.e.s. .c.o.n.s.t.a.n.t.l.y. .w.i.t.h. .p.e.o.p.l.e. .i. .d.o.n.t. .k.n.o.w. .J.o.y.:.T.h.a.t. .c.a.n. .c.e.r.t.a.i.n.l.y. .b.e. .c.o.n.c.e.r.n.i.n.g... .H.a.v.e. .y.o.u. .t.r.i.e.d. .t.a.l.k.i.n.g. .t.o. .y.o.u.r. .s.p.o.u.s.e. .a.b.o.u.t. .t.h.i.s.?. . . .P.a.t.i.e.n.t.:.y.e.a. .t.h.e.n. .s.h.e. .g.e.t.s. .d.e.f.e.n.s.i.v.e. .a.n.d. .a.n.g.r.y. .J.o.y.:.I. .u.n.d.e.r.s.t.a.n.d. .t.h.a.t. .t.h.i.s. .m.u.s.t. .b.e. .d.i.f.f.i.c.u.l.t. .f.o.r. .y.o.u... .A.r.e. .t.h.e.r.e. .a.n.y. .s.p.e.c.i.f.i.c. .s.t.r.a.t.e.g.i.e.s. .t.h.a.t. .h.a.v.e. .h.e.l.p.e.d. .w.h.e.n. .c.o.n.v.e.r.s.a.t.i.o.n.s. .w.i.t.h. .y.o.u.r. .s.p.o.u.s.e. .b.e.c.o.m.e. .h.e.a.t.e.d.?. . . .P.a.t.i.e.n.t.:.i. .d.o.n.t. .k.n.o.w. .J.o.y.:.I.t.'.s. .o.k.a.y. .i.f. .y.o.u. .d.o.n.'.t. .k.n.o.w. .r.i.g.h.t. .a.w.a.y... .W.h.a.t. .d.o. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .d.o.i.n.g. .i.n. .t.h.e.s.e. .s.i.t.u.a.t.i.o.n.s.?. .A.r.e. .t.h.e.r.e. .a.n.y. .s.t.r.a.t.e.g.i.e.s. .t.h.a.t. .h.a.v.e. .h.e.l.p.e.d. .i.n. .t.h.e. .p.a.s.t. .w.h.e.n. .t.h.i.n.g.s. .b.e.c.o.m.e. .t.e.n.s.e. .b.e.t.w.e.e.n. .y.o.u. .a.n.d. .y.o.u.r. .s.p.o.u.s.e.?. . . .P.a.t.i.e.n.t.:.i. .j.u.s.t. .s.t.o.p.p.e.d. .t.a.l.k.i.n.g. .t.o. .h.e.r. .o.r. .c.h.a.n.g.e. .t.h.e. .s.u.b.j.e.c.t. .J.o.y.:.I.t. .s.o.u.n.d.s. .l.i.k.e. .y.o.u. .a.r.e. .d.o.i.n.g. .y.o.u.r. .b.e.s.t. .t.o. .f.i.n.d. .a. .w.a.y. .t.o. .m.a.n.a.g.e. .t.h.i.s... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .t.h.a.t. .h.a.s. .h.e.l.p.e.d. .m.a.k.e. .t.h.e. .s.i.t.u.a.t.i.o.n. .m.o.r.e. .c.o.m.f.o.r.t.a.b.l.e. .f.o.r. .y.o.u.?. . . .P.a.t.i.e.n.t.:.t.r.y.i.n.g. .n.o.t. .t.h.i.n.k.i.n.g. .a.b.o.u.t. .i.t. .J.o.y.:.I.t. .s.o.u.n.d.s. .l.i.k.e. .y.o.u. .a.r.e. .d.o.i.n.g. .y.o.u.r. .b.e.s.t. .t.o. .h.a.n.d.l.e. .t.h.i.s. .d.i.f.f.i.c.u.l.t. .s.i.t.u.a.t.i.o.n... .D.o. .y.o.u. .t.h.i.n.k. .i.t. .w.o.u.l.d. .h.e.l.p. .t.o. .t.a.l.k. .t.o. .a. .p.r.o.f.e.s.s.i.o.n.a.l. .o.r. .f.i.n.d. .a.d.d.i.t.i.o.n.a.l. .s.u.p.p.o.r.t.?. . . .P.a.t.i.e.n.t.:.n.o. .i. .h.a.v.e.n.t. .J.o.y.:.T.h.a.t.'.s. .u.n.d.e.r.s.t.a.n.d.a.b.l.e... .I.s. .t.h.e.r.e. .s.o.m.e.o.n.e. .w.h.o. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .t.a.l.k.i.n.g. .t.o. .a.b.o.u.t. .t.h.i.s.?. .H.o.w. .d.o.e.s. .h.a.v.i.n.g. .a. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .t.h.e.m. .m.a.k.e. .y.o.u. .f.e.e.l.?. . . .P.a.t.i.e.n.t.:.i. .h.a.v.e.n.t. .t.a.l.k.e.d. .w.i.t.h. .a.n.y.o.n.e. .a.b.o.u.t. .t.h.i.s. .J.o.y.:.T.h.a.t.'.s. .u.n.d.e.r.s.t.a.n.d.a.b.l.e... .H.a.v.i.n.g. .s.o.m.e.o.n.e. .t.o. .t.a.l.k. .t.o. .c.a.n. .b.e. .b.e.n.e.f.i.c.i.a.l. .i.n. .d.i.f.f.i.c.u.l.t. .t.i.m.e.s. .l.i.k.e. .t.h.i.s... .I.s. .t.h.e.r.e. .a.n.y.o.n.e. .w.h.o. .y.o.u. .t.r.u.s.t. .t.h.a.t. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .s.p.e.a.k.i.n.g. .w.i.t.h. .a.b.o.u.t. .y.o.u.r. .c.o.n.c.e.r.n.s.?. . . .P.a.t.i.e.n.t.:.t.h.e.r.e. .i.s. .n.o. .o.n.e.,. .e.x.c.e.p.t. .m.a.y.b.e. .m.y. .m.o.m. .J.o.y.:.T.a.l.k.i.n.g. .t.o. .y.o.u.r. .m.o.m. .c.a.n. .b.e. .a. .g.r.e.a.t. .s.t.a.r.t... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .i.n. .p.a.r.t.i.c.u.l.a.r. .t.h.a.t. .y.o.u. .w.a.n.t. .t.o. .s.h.a.r.e. .w.i.t.h. .h.e.r. .a.b.o.u.t. .w.h.a.t. .i.s. .g.o.i.n.g. .o.n.?. .H.o.w. .d.o.e.s. .t.h.e. .i.d.e.a. .o.f. .h.a.v.i.n.g. .t.h.i.s. .c.o.n.v.e.r.s.a.t.i.o.n. .m.a.k.e. .y.o.u. .f.e.e.l.?. . . .P.a.t.i.e.n.t.:.y.e.a. .l.i.k.e. .I. .s.a.i.d.,. .a.b.o.u.t. .t.h.i.s. .t.o.p.i.c. .t.h.a.t. .i.m. .n.o.t. .v.e.r.y. .c.o.m.f.o.r.t.a.b.l.e. .w.i.t.h.,. .d.o. .y.o.u. .s.t.i.l.l. .r.e.m.e.m.b.e.r. .w.h.i.c.h.?. .J.o.y.:.Y.e.s.,. .y.o.u. .m.e.n.t.i.o.n.e.d. .t.h.a.t. .y.o.u.r. .s.p.o.u.s.e. .m.a.y. .b.e. .c.h.e.a.t.i.n.g. .o.n. .y.o.u... .H.o.w. .w.o.u.l.d. .y.o.u. .l.i.k.e. .t.o. .a.p.p.r.o.a.c.h. .t.h.i.s. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .y.o.u.r. .m.o.m.?. . . .P.a.t.i.e.n.t.:.t.e.l.l. .h.e.r. .a.b.o.u.t. .m.y. .s.u.s.p.i.c.i.s.i.o.n. .i. .g.u.e.s.s. .J.o.y.:.I.t. .c.a.n. .b.e. .d.i.f.f.i.c.u.l.t. .t.o. .b.r.i.n.g. .u.p. .a. .t.o.p.i.c. .l.i.k.e. .t.h.i.s.,. .b.u.t. .i.t.'.s. .i.m.p.o.r.t.a.n.t. .t.h.a.t. .y.o.u. .h.a.v.e. .a.n. .o.p.e.n. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .h.e.r... .W.o.u.l.d. .y.o.u. .l.i.k.e. .a.n.y. .a.d.v.i.c.e. .o.n. .h.o.w. .t.o. .s.t.a.r.t. .t.h.e. .c.o.n.v.e.r.s.a.t.i.o.n.?. . . .P.a.t.i.e.n.t.:.y.e.s. .p.l.e.a.s.e. .J.o.y.:.I.t. .c.a.n. .h.e.l.p. .t.o. .s.t.a.r.t. .b.y. .l.e.t.t.i.n.g. .y.o.u.r. .m.o.m. .k.n.o.w. .w.h.y. .y.o.u. .d.e.c.i.d.e.d. .t.o. .t.a.l.k. .t.o. .h.e.r. .a.b.o.u.t. .t.h.i.s... .S.h.a.r.i.n.g. .h.o.w. .y.o.u. .a.r.e. .f.e.e.l.i.n.g. .a.n.d. .a.s.k.i.n.g. .f.o.r. .h.e.r. .a.d.v.i.c.e. .m.i.g.h.t. .b.e. .a. .g.o.o.d. .w.a.y. .t.o. .o.p.e.n. .t.h.e. .c.o.n.v.e.r.s.a.t.i.o.n... .I.'.m. .h.e.r.e. .i.f. .y.o.u. .n.e.e.d. .a.n.y.t.h.i.n.g. .e.l.s.e. .b.e.f.o.r.e. .o.r. .a.f.t.e.r. .s.p.e.a.k.i.n.g. .w.i.t.h. .y.o.u.r. .m.o.m..\"]\n"
274
+ ]
275
+ }
276
+ ],
277
+ "source": [
278
+ "def split_input_string(input_string: list, tokenizer, max_length):\n",
279
+ " # Initialize the list to store the split strings\n",
280
+ " split_strings = []\n",
281
+ "\n",
282
+ " # Split the input string into sentences\n",
283
+ " sentences = input_string\n",
284
+ " #sentences = input_string.split(\".\")\n",
285
+ "\n",
286
+ " # Loop through each sentence\n",
287
+ " i = 0\n",
288
+ " while i < len(sentences):\n",
289
+ " # Initialize the current string with the first sentence\n",
290
+ " curr_string = sentences[i]\n",
291
+ "\n",
292
+ " # Keep adding sentences until the current string is longer than the maximum length\n",
293
+ " i += 1\n",
294
+ " while i < len(sentences) and len(tokenizer.encode(curr_string + \".\" + sentences[i], max_length=max_length)) <= max_length:\n",
295
+ " curr_string += \".\" + sentences[i]\n",
296
+ " i += 1\n",
297
+ "\n",
298
+ " # Add the current string to the list of split strings\n",
299
+ " split_strings.append(curr_string)\n",
300
+ "\n",
301
+ " # Return the list of split strings\n",
302
+ " return split_strings\n",
303
+ "\n",
304
+ "# Example usage\n",
305
+ "input_string = \"This is a sample input string. It can be quite long. This function splits the input string into parts, each of which is no longer than the maximum length defined by the tokenizer and each of which ends on a full stop.\"\n",
306
+ "max_length = 512\n",
307
+ "split_strings = split_input_string(test3_string, tokenizer, max_length)\n",
308
+ "print(split_strings)\n"
309
+ ]
310
+ },
311
+ {
312
+ "cell_type": "code",
313
+ "execution_count": 97,
314
+ "metadata": {},
315
+ "outputs": [
316
+ {
317
+ "data": {
318
+ "text/plain": [
319
+ "1"
320
+ ]
321
+ },
322
+ "execution_count": 97,
323
+ "metadata": {},
324
+ "output_type": "execute_result"
325
+ }
326
+ ],
327
+ "source": [
328
+ "len(split_strings)"
329
+ ]
330
+ },
331
+ {
332
+ "cell_type": "code",
333
+ "execution_count": 33,
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "import re \n",
338
+ "\n",
339
+ "def split(input: str) -> list:\n",
340
+ " tokenized = tokenizer(test, return_tensors='pt')\n",
341
+ " parts = (len(tokenized[0]) // 500) + 1\n",
342
+ " words_each_part = len(input.split()) // parts\n",
343
+ " print(words_each_part)\n",
344
+ " sentences = re.split(r'(?<=[.!?]) +', input)\n",
345
+ " # count the number of words in each sentence\n",
346
+ " words = [len(i.split()) for i in sentences]\n",
347
+ " print(words)\n",
348
+ "\n",
349
+ " return parts, sentences"
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": 64,
355
+ "metadata": {},
356
+ "outputs": [
357
+ {
358
+ "data": {
359
+ "text/plain": [
360
+ "{'input_ids': [0, 7568, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50140, 50140, 1437, 2], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}"
361
+ ]
362
+ },
363
+ "execution_count": 64,
364
+ "metadata": {},
365
+ "output_type": "execute_result"
366
+ }
367
+ ],
368
+ "source": [
369
+ "tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
370
+ "tokens = tokenizer(test)\n",
371
+ "tokens"
372
+ ]
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "execution_count": 55,
377
+ "metadata": {},
378
+ "outputs": [
379
+ {
380
+ "name": "stdout",
381
+ "output_type": "stream",
382
+ "text": [
383
+ "['ĠCancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', '��by', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'ĊĊ', 'ĊĊ', 'Ġ']\n"
384
+ ]
385
+ },
386
+ {
387
+ "ename": "KeyboardInterrupt",
388
+ "evalue": "",
389
+ "output_type": "error",
390
+ "traceback": [
391
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
392
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
393
+ "Cell \u001b[0;32mIn[55], line 26\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[39m# break\u001b[39;00m\n\u001b[1;32m 17\u001b[0m \u001b[39m# end += 1\u001b[39;00m\n\u001b[1;32m 18\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[39m# start = end + 1\u001b[39;00m\n\u001b[1;32m 22\u001b[0m \u001b[39m# end = start\u001b[39;00m\n\u001b[1;32m 24\u001b[0m \u001b[39mreturn\u001b[39;00m tokenized_parts\n\u001b[0;32m---> 26\u001b[0m tokenize_long_sentence(test, \u001b[39m500\u001b[39;49m)\n",
394
+ "Cell \u001b[0;32mIn[55], line 13\u001b[0m, in \u001b[0;36mtokenize_long_sentence\u001b[0;34m(long_sentence, max_tokens)\u001b[0m\n\u001b[1;32m 10\u001b[0m end \u001b[39m=\u001b[39m \u001b[39m0\u001b[39m\n\u001b[1;32m 12\u001b[0m \u001b[39mwhile\u001b[39;00m end \u001b[39m<\u001b[39m \u001b[39mlen\u001b[39m(tokens):\n\u001b[0;32m---> 13\u001b[0m \u001b[39mwhile\u001b[39;00m end \u001b[39m<\u001b[39m \u001b[39mlen\u001b[39m(tokens) \u001b[39mand\u001b[39;00m end \u001b[39m-\u001b[39;49m start \u001b[39m<\u001b[39;49m\u001b[39m=\u001b[39;49m max_tokens:\n\u001b[1;32m 14\u001b[0m \u001b[39mif\u001b[39;00m tokens[end] \u001b[39min\u001b[39;00m [\u001b[39m\"\u001b[39m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m?\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m!\u001b[39m\u001b[39m\"\u001b[39m]:\n\u001b[1;32m 15\u001b[0m \u001b[39mprint\u001b[39m(tokens[end])\n",
395
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
396
+ ]
397
+ }
398
+ ],
399
+ "source": [
400
+ "from transformers import AutoTokenizer\n",
401
+ "\n",
402
+ "def tokenize_long_sentence(long_sentence, max_tokens):\n",
403
+ " tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
404
+ " tokens = tokenizer.tokenize(long_sentence)\n",
405
+ " print(tokens)\n",
406
+ " tokenized_parts = []\n",
407
+ "\n",
408
+ " start = 0\n",
409
+ " end = 0\n",
410
+ "\n",
411
+ " while end < len(tokens):\n",
412
+ " while end < len(tokens) and end - start <= max_tokens:\n",
413
+ " if tokens[end] in [\".\", \"?\", \"!\"]:\n",
414
+ " print(tokens[end])\n",
415
+ " # break\n",
416
+ " # end += 1\n",
417
+ "\n",
418
+ " # part = tokens[start:end + 1]\n",
419
+ " # tokenized_parts.append(part)\n",
420
+ " # start = end + 1\n",
421
+ " # end = start\n",
422
+ "\n",
423
+ " return tokenized_parts\n",
424
+ "\n",
425
+ "tokenize_long_sentence(test, 500)"
426
+ ]
427
+ }
428
+ ],
429
+ "metadata": {
430
+ "kernelspec": {
431
+ "display_name": "chatbot",
432
+ "language": "python",
433
+ "name": "python3"
434
+ },
435
+ "language_info": {
436
+ "codemirror_mode": {
437
+ "name": "ipython",
438
+ "version": 3
439
+ },
440
+ "file_extension": ".py",
441
+ "mimetype": "text/x-python",
442
+ "name": "python",
443
+ "nbconvert_exporter": "python",
444
+ "pygments_lexer": "ipython3",
445
+ "version": "3.8.15"
446
+ },
447
+ "orig_nbformat": 4,
448
+ "vscode": {
449
+ "interpreter": {
450
+ "hash": "9798c78c52b861f9442ea63a21901b586ae2f2169fa92d94b4091cc5bab62e04"
451
+ }
452
+ }
453
+ },
454
+ "nbformat": 4,
455
+ "nbformat_minor": 2
456
+ }
code_snippit_tests.ipynb ADDED
@@ -0,0 +1,456 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 19,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "test = ''' Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
10
+ "\n",
11
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
12
+ "\n",
13
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
14
+ "\n",
15
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
16
+ "\n",
17
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
18
+ "\n",
19
+ "Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
20
+ "\n",
21
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
22
+ "\n",
23
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
24
+ "\n",
25
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
26
+ "\n",
27
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
28
+ "\n",
29
+ "Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
30
+ "\n",
31
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
32
+ "\n",
33
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
34
+ "\n",
35
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
36
+ "\n",
37
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
38
+ "\n",
39
+ "Cancer of the womb (uterus) is a common cancer that affects the female reproductive system. It's also called uterine cancer and endometrial cancer.\n",
40
+ "\n",
41
+ "Abnormal vaginal bleeding is the most common symptom of womb cancer.\n",
42
+ "\n",
43
+ "If you have been through the menopause, any vaginal bleeding is considered abnormal. If you have not yet been through the menopause, unusual bleeding may include bleeding between your periods.\n",
44
+ "\n",
45
+ "You should speak to your GP as soon as possible if you experience any unusual vaginal bleeding. While it's unlikely to be caused by womb cancer, it's best to be sure.\n",
46
+ "\n",
47
+ "Your GP will examine you and ask about your symptoms. They will refer you to a specialist for further tests if they suspect you may have a serious problem, or if they are unsure about a diagnosis.\n",
48
+ "\n",
49
+ "\n",
50
+ "\n",
51
+ " '''\n",
52
+ "test3 = ['[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to.\\nJoy:I am Joy, your AI therapist. How are you feeling today?', \"\\n\\nPatient:im doing okay\\nJoy:That's good to hear. Is there anything in particular that you would like to talk about today?\", '\\n\\nPatient:no not really\\nJoy:No problem. Is there anything on your mind that you would like to share? I am here to listen and offer support.', \"\\n\\nPatient:maybe actually. I am afraid my spouse cheats on me\\nJoy:I'm sorry to hear that. What has been making you feel this way?\", '\\n\\nPatient:she is writing text messages constantly with people i dont know\\nJoy:That can certainly be concerning. Have you tried talking to your spouse about this?', '\\n\\nPatient:yea then she gets defensive and angry\\nJoy:I understand that this must be difficult for you. Are there any specific strategies that have helped when conversations with your spouse become heated?', \"\\n\\nPatient:i dont know\\nJoy:It's okay if you don't know right away. What do you feel comfortable doing in these situations? Are there any strategies that have helped in the past when things become tense between you and your spouse?\", '\\n\\nPatient:i just stopped talking to her or change the subject\\nJoy:It sounds like you are doing your best to find a way to manage this. Is there anything that has helped make the situation more comfortable for you?', '\\n\\nPatient:trying not thinking about it\\nJoy:It sounds like you are doing your best to handle this difficult situation. Do you think it would help to talk to a professional or find additional support?', \"\\n\\nPatient:no i havent\\nJoy:That's understandable. Is there someone who you feel comfortable talking to about this? How does having a conversation with them make you feel?\", \"\\n\\nPatient:i havent talked with anyone about this\\nJoy:That's understandable. Having someone to talk to can be beneficial in difficult times like this. Is there anyone who you trust that you feel comfortable speaking with about your concerns?\", '\\n\\nPatient:there is no one, except maybe my mom\\nJoy:Talking to your mom can be a great start. Is there anything in particular that you want to share with her about what is going on? How does the idea of having this conversation make you feel?', '\\n\\nPatient:yea like I said, about this topic that im not very comfortable with, do you still remember which?\\nJoy:Yes, you mentioned that your spouse may be cheating on you. How would you like to approach this conversation with your mom?', \"\\n\\nPatient:tell her about my suspicision i guess\\nJoy:It can be difficult to bring up a topic like this, but it's important that you have an open conversation with her. Would you like any advice on how to start the conversation?\", \"\\n\\nPatient:yes please\\nJoy:It can help to start by letting your mom know why you decided to talk to her about this. Sharing how you are feeling and asking for her advice might be a good way to open the conversation. I'm here if you need anything else before or after speaking with your mom.\"]"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": 20,
58
+ "metadata": {},
59
+ "outputs": [],
60
+ "source": [
61
+ "\n",
62
+ "def clean_chat_log(chat_log):\n",
63
+ " chat_log = ' '.join(chat_log)\n",
64
+ " # find the first /n\n",
65
+ " first_newline = chat_log.find('\\n')\n",
66
+ " chat_log = chat_log[first_newline:]\n",
67
+ " # remove all \\n\n",
68
+ " chat_log = chat_log.replace('\\n', ' ')\n",
69
+ " return chat_log"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": 32,
75
+ "metadata": {},
76
+ "outputs": [
77
+ {
78
+ "data": {
79
+ "text/plain": [
80
+ "['[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to. Joy:I am Joy, your AI therapist. How are you feeling today?',\n",
81
+ " \" Patient:im doing okay Joy:That's good to hear. Is there anything in particular that you would like to talk about today?\",\n",
82
+ " ' Patient:no not really Joy:No problem. Is there anything on your mind that you would like to share? I am here to listen and offer support.',\n",
83
+ " \" Patient:maybe actually. I am afraid my spouse cheats on me Joy:I'm sorry to hear that. What has been making you feel this way?\",\n",
84
+ " ' Patient:she is writing text messages constantly with people i dont know Joy:That can certainly be concerning. Have you tried talking to your spouse about this?',\n",
85
+ " ' Patient:yea then she gets defensive and angry Joy:I understand that this must be difficult for you. Are there any specific strategies that have helped when conversations with your spouse become heated?',\n",
86
+ " \" Patient:i dont know Joy:It's okay if you don't know right away. What do you feel comfortable doing in these situations? Are there any strategies that have helped in the past when things become tense between you and your spouse?\",\n",
87
+ " ' Patient:i just stopped talking to her or change the subject Joy:It sounds like you are doing your best to find a way to manage this. Is there anything that has helped make the situation more comfortable for you?',\n",
88
+ " ' Patient:trying not thinking about it Joy:It sounds like you are doing your best to handle this difficult situation. Do you think it would help to talk to a professional or find additional support?',\n",
89
+ " \" Patient:no i havent Joy:That's understandable. Is there someone who you feel comfortable talking to about this? How does having a conversation with them make you feel?\",\n",
90
+ " \" Patient:i havent talked with anyone about this Joy:That's understandable. Having someone to talk to can be beneficial in difficult times like this. Is there anyone who you trust that you feel comfortable speaking with about your concerns?\",\n",
91
+ " ' Patient:there is no one, except maybe my mom Joy:Talking to your mom can be a great start. Is there anything in particular that you want to share with her about what is going on? How does the idea of having this conversation make you feel?',\n",
92
+ " ' Patient:yea like I said, about this topic that im not very comfortable with, do you still remember which? Joy:Yes, you mentioned that your spouse may be cheating on you. How would you like to approach this conversation with your mom?',\n",
93
+ " \" Patient:tell her about my suspicision i guess Joy:It can be difficult to bring up a topic like this, but it's important that you have an open conversation with her. Would you like any advice on how to start the conversation?\",\n",
94
+ " \" Patient:yes please Joy:It can help to start by letting your mom know why you decided to talk to her about this. Sharing how you are feeling and asking for her advice might be a good way to open the conversation. I'm here if you need anything else before or after speaking with your mom.\"]"
95
+ ]
96
+ },
97
+ "execution_count": 32,
98
+ "metadata": {},
99
+ "output_type": "execute_result"
100
+ }
101
+ ],
102
+ "source": [
103
+ "def remove_backslashN(chat_log):\n",
104
+ " chat_log = [i.replace('\\n', ' ') for i in chat_log]\n",
105
+ " return chat_log\n",
106
+ "\n",
107
+ "remove_backslashN(test3)"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": 21,
113
+ "metadata": {},
114
+ "outputs": [
115
+ {
116
+ "name": "stderr",
117
+ "output_type": "stream",
118
+ "text": [
119
+ "Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
120
+ ]
121
+ },
122
+ {
123
+ "data": {
124
+ "text/plain": [
125
+ "682"
126
+ ]
127
+ },
128
+ "execution_count": 21,
129
+ "metadata": {},
130
+ "output_type": "execute_result"
131
+ }
132
+ ],
133
+ "source": [
134
+ "len(tokenizer(test, padding=True, truncation=True, return_tensors=\"pt\")[0])"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": 18,
140
+ "metadata": {},
141
+ "outputs": [
142
+ {
143
+ "name": "stderr",
144
+ "output_type": "stream",
145
+ "text": [
146
+ "Some weights of the model checkpoint at cardiffnlp/twitter-roberta-base-sentiment-latest were not used when initializing RobertaForSequenceClassification: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
147
+ "- This IS expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
148
+ "- This IS NOT expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
149
+ "Some weights of the model checkpoint at cardiffnlp/twitter-roberta-base-sentiment-latest were not used when initializing RobertaForSequenceClassification: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
150
+ "- This IS expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
151
+ "- This IS NOT expected if you are initializing RobertaForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
152
+ ]
153
+ }
154
+ ],
155
+ "source": [
156
+ "from transformers import AutoModelForSequenceClassification\n",
157
+ "from transformers import TFAutoModelForSequenceClassification\n",
158
+ "from transformers import AutoTokenizer, AutoConfig\n",
159
+ "from transformers import pipeline\n",
160
+ "MODEL = \"cardiffnlp/twitter-roberta-base-sentiment-latest\"\n",
161
+ "\n",
162
+ "tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
163
+ "config = AutoConfig.from_pretrained(MODEL)\n",
164
+ "model = AutoModelForSequenceClassification.from_pretrained(MODEL)\n",
165
+ "\n",
166
+ "sentiment_task = pipeline(\"sentiment-analysis\", model='cardiffnlp/twitter-roberta-base-sentiment-latest', tokenizer='cardiffnlp/twitter-roberta-base-sentiment-latest')\n"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": 56,
172
+ "metadata": {},
173
+ "outputs": [],
174
+ "source": [
175
+ "import numpy as np\n",
176
+ "tokenized = tokenizer(['this is great', 'this sucks'], return_tensors='pt', truncation=True, padding=True)\n",
177
+ "output = model(**tokenized)\n",
178
+ "scores = output[0][0].detach().numpy()\n",
179
+ "scores = np.exp(scores) / np.sum(np.exp(scores), axis=0)"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "execution_count": 58,
185
+ "metadata": {},
186
+ "outputs": [
187
+ {
188
+ "data": {
189
+ "text/plain": [
190
+ "SequenceClassifierOutput(loss=None, logits=tensor([[-2.1332, -0.7378, 2.8549],\n",
191
+ " [ 1.6230, -0.1552, -1.7155]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None)"
192
+ ]
193
+ },
194
+ "execution_count": 58,
195
+ "metadata": {},
196
+ "output_type": "execute_result"
197
+ }
198
+ ],
199
+ "source": [
200
+ "output"
201
+ ]
202
+ },
203
+ {
204
+ "cell_type": "code",
205
+ "execution_count": 57,
206
+ "metadata": {},
207
+ "outputs": [
208
+ {
209
+ "name": "stdout",
210
+ "output_type": "stream",
211
+ "text": [
212
+ "1) positive 0.9668\n",
213
+ "2) neutral 0.0266\n",
214
+ "3) negative 0.0066\n"
215
+ ]
216
+ }
217
+ ],
218
+ "source": [
219
+ "ranking = np.argsort(scores)\n",
220
+ "ranking = ranking[::-1]\n",
221
+ "for i in range(scores.shape[0]):\n",
222
+ " l = config.id2label[ranking[i]]\n",
223
+ " s = scores[ranking[i]]\n",
224
+ " print(f\"{i+1}) {l} {np.round(float(s), 4)}\")"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": 86,
230
+ "metadata": {},
231
+ "outputs": [
232
+ {
233
+ "name": "stdout",
234
+ "output_type": "stream",
235
+ "text": [
236
+ "0.6931471805599453\n",
237
+ "2.4849066497880004\n",
238
+ "3.091042453358316\n",
239
+ "3.4657359027997265\n",
240
+ "3.7376696182833684\n",
241
+ "3.9512437185814275\n",
242
+ "4.127134385045092\n",
243
+ "4.276666119016055\n",
244
+ "4.406719247264253\n",
245
+ "4.5217885770490405\n"
246
+ ]
247
+ }
248
+ ],
249
+ "source": [
250
+ "# import natural log function\n",
251
+ "from math import log\n",
252
+ "\n",
253
+ "for i in range(2, 100, 10):\n",
254
+ " print(log(i))"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": 27,
260
+ "metadata": {},
261
+ "outputs": [
262
+ {
263
+ "name": "stderr",
264
+ "output_type": "stream",
265
+ "text": [
266
+ "Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.\n"
267
+ ]
268
+ },
269
+ {
270
+ "name": "stdout",
271
+ "output_type": "stream",
272
+ "text": [
273
+ "[\" .J.o.y.:.I. .a.m. .J.o.y.,. .y.o.u.r. .A.I. .t.h.e.r.a.p.i.s.t... .H.o.w. .a.r.e. .y.o.u. .f.e.e.l.i.n.g. .t.o.d.a.y.?. . . .P.a.t.i.e.n.t.:.i.m. .d.o.i.n.g. .o.k.a.y. .J.o.y.:.T.h.a.t.'.s. .g.o.o.d. .t.o. .h.e.a.r... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .i.n. .p.a.r.t.i.c.u.l.a.r. .t.h.a.t. .y.o.u. .w.o.u.l.d. .l.i.k.e. .t.o. .t.a.l.k. .a.b.o.u.t. .t.o.d.a.y.?. . . .P.a.t.i.e.n.t.:.n.o. .n.o.t. .r.e.a.l.l.y. .J.o.y.:.N.o. .p.r.o.b.l.e.m... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .o.n. .y.o.u.r. .m.i.n.d. .t.h.a.t. .y.o.u. .w.o.u.l.d. .l.i.k.e. .t.o. .s.h.a.r.e.?. .I. .a.m. .h.e.r.e. .t.o. .l.i.s.t.e.n. .a.n.d. .o.f.f.e.r. .s.u.p.p.o.r.t... . . .P.a.t.i.e.n.t.:.m.a.y.b.e. .a.c.t.u.a.l.l.y... .I. .a.m. .a.f.r.a.i.d. .m.y. .s.p.o.u.s.e. .c.h.e.a.t.s. .o.n. .m.e. .J.o.y.:.I.'.m. .s.o.r.r.y. .t.o. .h.e.a.r. .t.h.a.t... .W.h.a.t. .h.a.s. .b.e.e.n. .m.a.k.i.n.g. .y.o.u. .f.e.e.l. .t.h.i.s. .w.a.y.?. . . .P.a.t.i.e.n.t.:.s.h.e. .i.s. .w.r.i.t.i.n.g. .t.e.x.t. .m.e.s.s.a.g.e.s. .c.o.n.s.t.a.n.t.l.y. .w.i.t.h. .p.e.o.p.l.e. .i. .d.o.n.t. .k.n.o.w. .J.o.y.:.T.h.a.t. .c.a.n. .c.e.r.t.a.i.n.l.y. .b.e. .c.o.n.c.e.r.n.i.n.g... .H.a.v.e. .y.o.u. .t.r.i.e.d. .t.a.l.k.i.n.g. .t.o. .y.o.u.r. .s.p.o.u.s.e. .a.b.o.u.t. .t.h.i.s.?. . . .P.a.t.i.e.n.t.:.y.e.a. .t.h.e.n. .s.h.e. .g.e.t.s. .d.e.f.e.n.s.i.v.e. .a.n.d. .a.n.g.r.y. .J.o.y.:.I. .u.n.d.e.r.s.t.a.n.d. .t.h.a.t. .t.h.i.s. .m.u.s.t. .b.e. .d.i.f.f.i.c.u.l.t. .f.o.r. .y.o.u... .A.r.e. .t.h.e.r.e. .a.n.y. .s.p.e.c.i.f.i.c. .s.t.r.a.t.e.g.i.e.s. .t.h.a.t. .h.a.v.e. .h.e.l.p.e.d. .w.h.e.n. .c.o.n.v.e.r.s.a.t.i.o.n.s. .w.i.t.h. .y.o.u.r. .s.p.o.u.s.e. .b.e.c.o.m.e. .h.e.a.t.e.d.?. . . .P.a.t.i.e.n.t.:.i. .d.o.n.t. .k.n.o.w. .J.o.y.:.I.t.'.s. .o.k.a.y. .i.f. .y.o.u. .d.o.n.'.t. .k.n.o.w. .r.i.g.h.t. .a.w.a.y... .W.h.a.t. .d.o. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .d.o.i.n.g. .i.n. .t.h.e.s.e. .s.i.t.u.a.t.i.o.n.s.?. .A.r.e. .t.h.e.r.e. .a.n.y. .s.t.r.a.t.e.g.i.e.s. .t.h.a.t. .h.a.v.e. .h.e.l.p.e.d. .i.n. .t.h.e. .p.a.s.t. .w.h.e.n. .t.h.i.n.g.s. .b.e.c.o.m.e. .t.e.n.s.e. .b.e.t.w.e.e.n. .y.o.u. .a.n.d. .y.o.u.r. .s.p.o.u.s.e.?. . . .P.a.t.i.e.n.t.:.i. .j.u.s.t. .s.t.o.p.p.e.d. .t.a.l.k.i.n.g. .t.o. .h.e.r. .o.r. .c.h.a.n.g.e. .t.h.e. .s.u.b.j.e.c.t. .J.o.y.:.I.t. .s.o.u.n.d.s. .l.i.k.e. .y.o.u. .a.r.e. .d.o.i.n.g. .y.o.u.r. .b.e.s.t. .t.o. .f.i.n.d. .a. .w.a.y. .t.o. .m.a.n.a.g.e. .t.h.i.s... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .t.h.a.t. .h.a.s. .h.e.l.p.e.d. .m.a.k.e. .t.h.e. .s.i.t.u.a.t.i.o.n. .m.o.r.e. .c.o.m.f.o.r.t.a.b.l.e. .f.o.r. .y.o.u.?. . . .P.a.t.i.e.n.t.:.t.r.y.i.n.g. .n.o.t. .t.h.i.n.k.i.n.g. .a.b.o.u.t. .i.t. .J.o.y.:.I.t. .s.o.u.n.d.s. .l.i.k.e. .y.o.u. .a.r.e. .d.o.i.n.g. .y.o.u.r. .b.e.s.t. .t.o. .h.a.n.d.l.e. .t.h.i.s. .d.i.f.f.i.c.u.l.t. .s.i.t.u.a.t.i.o.n... .D.o. .y.o.u. .t.h.i.n.k. .i.t. .w.o.u.l.d. .h.e.l.p. .t.o. .t.a.l.k. .t.o. .a. .p.r.o.f.e.s.s.i.o.n.a.l. .o.r. .f.i.n.d. .a.d.d.i.t.i.o.n.a.l. .s.u.p.p.o.r.t.?. . . .P.a.t.i.e.n.t.:.n.o. .i. .h.a.v.e.n.t. .J.o.y.:.T.h.a.t.'.s. .u.n.d.e.r.s.t.a.n.d.a.b.l.e... .I.s. .t.h.e.r.e. .s.o.m.e.o.n.e. .w.h.o. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .t.a.l.k.i.n.g. .t.o. .a.b.o.u.t. .t.h.i.s.?. .H.o.w. .d.o.e.s. .h.a.v.i.n.g. .a. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .t.h.e.m. .m.a.k.e. .y.o.u. .f.e.e.l.?. . . .P.a.t.i.e.n.t.:.i. .h.a.v.e.n.t. .t.a.l.k.e.d. .w.i.t.h. .a.n.y.o.n.e. .a.b.o.u.t. .t.h.i.s. .J.o.y.:.T.h.a.t.'.s. .u.n.d.e.r.s.t.a.n.d.a.b.l.e... .H.a.v.i.n.g. .s.o.m.e.o.n.e. .t.o. .t.a.l.k. .t.o. .c.a.n. .b.e. .b.e.n.e.f.i.c.i.a.l. .i.n. .d.i.f.f.i.c.u.l.t. .t.i.m.e.s. .l.i.k.e. .t.h.i.s... .I.s. .t.h.e.r.e. .a.n.y.o.n.e. .w.h.o. .y.o.u. .t.r.u.s.t. .t.h.a.t. .y.o.u. .f.e.e.l. .c.o.m.f.o.r.t.a.b.l.e. .s.p.e.a.k.i.n.g. .w.i.t.h. .a.b.o.u.t. .y.o.u.r. .c.o.n.c.e.r.n.s.?. . . .P.a.t.i.e.n.t.:.t.h.e.r.e. .i.s. .n.o. .o.n.e.,. .e.x.c.e.p.t. .m.a.y.b.e. .m.y. .m.o.m. .J.o.y.:.T.a.l.k.i.n.g. .t.o. .y.o.u.r. .m.o.m. .c.a.n. .b.e. .a. .g.r.e.a.t. .s.t.a.r.t... .I.s. .t.h.e.r.e. .a.n.y.t.h.i.n.g. .i.n. .p.a.r.t.i.c.u.l.a.r. .t.h.a.t. .y.o.u. .w.a.n.t. .t.o. .s.h.a.r.e. .w.i.t.h. .h.e.r. .a.b.o.u.t. .w.h.a.t. .i.s. .g.o.i.n.g. .o.n.?. .H.o.w. .d.o.e.s. .t.h.e. .i.d.e.a. .o.f. .h.a.v.i.n.g. .t.h.i.s. .c.o.n.v.e.r.s.a.t.i.o.n. .m.a.k.e. .y.o.u. .f.e.e.l.?. . . .P.a.t.i.e.n.t.:.y.e.a. .l.i.k.e. .I. .s.a.i.d.,. .a.b.o.u.t. .t.h.i.s. .t.o.p.i.c. .t.h.a.t. .i.m. .n.o.t. .v.e.r.y. .c.o.m.f.o.r.t.a.b.l.e. .w.i.t.h.,. .d.o. .y.o.u. .s.t.i.l.l. .r.e.m.e.m.b.e.r. .w.h.i.c.h.?. .J.o.y.:.Y.e.s.,. .y.o.u. .m.e.n.t.i.o.n.e.d. .t.h.a.t. .y.o.u.r. .s.p.o.u.s.e. .m.a.y. .b.e. .c.h.e.a.t.i.n.g. .o.n. .y.o.u... .H.o.w. .w.o.u.l.d. .y.o.u. .l.i.k.e. .t.o. .a.p.p.r.o.a.c.h. .t.h.i.s. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .y.o.u.r. .m.o.m.?. . . .P.a.t.i.e.n.t.:.t.e.l.l. .h.e.r. .a.b.o.u.t. .m.y. .s.u.s.p.i.c.i.s.i.o.n. .i. .g.u.e.s.s. .J.o.y.:.I.t. .c.a.n. .b.e. .d.i.f.f.i.c.u.l.t. .t.o. .b.r.i.n.g. .u.p. .a. .t.o.p.i.c. .l.i.k.e. .t.h.i.s.,. .b.u.t. .i.t.'.s. .i.m.p.o.r.t.a.n.t. .t.h.a.t. .y.o.u. .h.a.v.e. .a.n. .o.p.e.n. .c.o.n.v.e.r.s.a.t.i.o.n. .w.i.t.h. .h.e.r... .W.o.u.l.d. .y.o.u. .l.i.k.e. .a.n.y. .a.d.v.i.c.e. .o.n. .h.o.w. .t.o. .s.t.a.r.t. .t.h.e. .c.o.n.v.e.r.s.a.t.i.o.n.?. . . .P.a.t.i.e.n.t.:.y.e.s. .p.l.e.a.s.e. .J.o.y.:.I.t. .c.a.n. .h.e.l.p. .t.o. .s.t.a.r.t. .b.y. .l.e.t.t.i.n.g. .y.o.u.r. .m.o.m. .k.n.o.w. .w.h.y. .y.o.u. .d.e.c.i.d.e.d. .t.o. .t.a.l.k. .t.o. .h.e.r. .a.b.o.u.t. .t.h.i.s... .S.h.a.r.i.n.g. .h.o.w. .y.o.u. .a.r.e. .f.e.e.l.i.n.g. .a.n.d. .a.s.k.i.n.g. .f.o.r. .h.e.r. .a.d.v.i.c.e. .m.i.g.h.t. .b.e. .a. .g.o.o.d. .w.a.y. .t.o. .o.p.e.n. .t.h.e. .c.o.n.v.e.r.s.a.t.i.o.n... .I.'.m. .h.e.r.e. .i.f. .y.o.u. .n.e.e.d. .a.n.y.t.h.i.n.g. .e.l.s.e. .b.e.f.o.r.e. .o.r. .a.f.t.e.r. .s.p.e.a.k.i.n.g. .w.i.t.h. .y.o.u.r. .m.o.m..\"]\n"
274
+ ]
275
+ }
276
+ ],
277
+ "source": [
278
+ "def split_input_string(input_string: list, tokenizer, max_length):\n",
279
+ " # Initialize the list to store the split strings\n",
280
+ " split_strings = []\n",
281
+ "\n",
282
+ " # Split the input string into sentences\n",
283
+ " sentences = input_string\n",
284
+ " #sentences = input_string.split(\".\")\n",
285
+ "\n",
286
+ " # Loop through each sentence\n",
287
+ " i = 0\n",
288
+ " while i < len(sentences):\n",
289
+ " # Initialize the current string with the first sentence\n",
290
+ " curr_string = sentences[i]\n",
291
+ "\n",
292
+ " # Keep adding sentences until the current string is longer than the maximum length\n",
293
+ " i += 1\n",
294
+ " while i < len(sentences) and len(tokenizer.encode(curr_string + \".\" + sentences[i], max_length=max_length)) <= max_length:\n",
295
+ " curr_string += \".\" + sentences[i]\n",
296
+ " i += 1\n",
297
+ "\n",
298
+ " # Add the current string to the list of split strings\n",
299
+ " split_strings.append(curr_string)\n",
300
+ "\n",
301
+ " # Return the list of split strings\n",
302
+ " return split_strings\n",
303
+ "\n",
304
+ "# Example usage\n",
305
+ "input_string = \"This is a sample input string. It can be quite long. This function splits the input string into parts, each of which is no longer than the maximum length defined by the tokenizer and each of which ends on a full stop.\"\n",
306
+ "max_length = 512\n",
307
+ "split_strings = split_input_string(test3_string, tokenizer, max_length)\n",
308
+ "print(split_strings)\n"
309
+ ]
310
+ },
311
+ {
312
+ "cell_type": "code",
313
+ "execution_count": 97,
314
+ "metadata": {},
315
+ "outputs": [
316
+ {
317
+ "data": {
318
+ "text/plain": [
319
+ "1"
320
+ ]
321
+ },
322
+ "execution_count": 97,
323
+ "metadata": {},
324
+ "output_type": "execute_result"
325
+ }
326
+ ],
327
+ "source": [
328
+ "len(split_strings)"
329
+ ]
330
+ },
331
+ {
332
+ "cell_type": "code",
333
+ "execution_count": 33,
334
+ "metadata": {},
335
+ "outputs": [],
336
+ "source": [
337
+ "import re \n",
338
+ "\n",
339
+ "def split(input: str) -> list:\n",
340
+ " tokenized = tokenizer(test, return_tensors='pt')\n",
341
+ " parts = (len(tokenized[0]) // 500) + 1\n",
342
+ " words_each_part = len(input.split()) // parts\n",
343
+ " print(words_each_part)\n",
344
+ " sentences = re.split(r'(?<=[.!?]) +', input)\n",
345
+ " # count the number of words in each sentence\n",
346
+ " words = [len(i.split()) for i in sentences]\n",
347
+ " print(words)\n",
348
+ "\n",
349
+ " return parts, sentences"
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": 64,
355
+ "metadata": {},
356
+ "outputs": [
357
+ {
358
+ "data": {
359
+ "text/plain": [
360
+ "{'input_ids': [0, 7568, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50118, 50118, 347, 20126, 9, 5, 31730, 36, 13080, 687, 43, 16, 10, 1537, 1668, 14, 8561, 5, 2182, 20111, 467, 4, 85, 18, 67, 373, 43683, 833, 1668, 8, 253, 11174, 13700, 1668, 4, 50118, 50118, 13112, 21113, 34126, 13162, 16, 5, 144, 1537, 28667, 9, 31730, 1668, 4, 50118, 50118, 1106, 47, 33, 57, 149, 5, 604, 1517, 17498, 6, 143, 34126, 13162, 16, 1687, 27034, 4, 318, 47, 33, 45, 648, 57, 149, 5, 604, 1517, 17498, 6, 5425, 13162, 189, 680, 13162, 227, 110, 5788, 4, 50118, 50118, 1185, 197, 1994, 7, 110, 12571, 25, 1010, 25, 678, 114, 47, 676, 143, 5425, 34126, 13162, 4, 616, 24, 18, 3752, 7, 28, 1726, 30, 31730, 1668, 6, 24, 18, 275, 7, 28, 686, 4, 50118, 50118, 12861, 12571, 40, 10154, 47, 8, 1394, 59, 110, 5298, 4, 252, 40, 9115, 47, 7, 10, 6857, 13, 617, 3457, 114, 51, 1985, 47, 189, 33, 10, 1473, 936, 6, 50, 114, 51, 32, 17118, 59, 10, 9726, 4, 50140, 50140, 1437, 2], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}"
361
+ ]
362
+ },
363
+ "execution_count": 64,
364
+ "metadata": {},
365
+ "output_type": "execute_result"
366
+ }
367
+ ],
368
+ "source": [
369
+ "tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
370
+ "tokens = tokenizer(test)\n",
371
+ "tokens"
372
+ ]
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "execution_count": 55,
377
+ "metadata": {},
378
+ "outputs": [
379
+ {
380
+ "name": "stdout",
381
+ "output_type": "stream",
382
+ "text": [
383
+ "['ĠCancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', '��by', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'Ċ', 'Ċ', 'C', 'ancer', 'Ġof', 'Ġthe', 'Ġwomb', 'Ġ(', 'uter', 'us', ')', 'Ġis', 'Ġa', 'Ġcommon', 'Ġcancer', 'Ġthat', 'Ġaffects', 'Ġthe', 'Ġfemale', 'Ġreproductive', 'Ġsystem', '.', 'ĠIt', \"'s\", 'Ġalso', 'Ġcalled', 'Ġuter', 'ine', 'Ġcancer', 'Ġand', 'Ġend', 'omet', 'rial', 'Ġcancer', '.', 'Ċ', 'Ċ', 'Ab', 'normal', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġthe', 'Ġmost', 'Ġcommon', 'Ġsymptom', 'Ġof', 'Ġwomb', 'Ġcancer', '.', 'Ċ', 'Ċ', 'If', 'Ġyou', 'Ġhave', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġany', 'Ġvaginal', 'Ġbleeding', 'Ġis', 'Ġconsidered', 'Ġabnormal', '.', 'ĠIf', 'Ġyou', 'Ġhave', 'Ġnot', 'Ġyet', 'Ġbeen', 'Ġthrough', 'Ġthe', 'Ġmen', 'op', 'ause', ',', 'Ġunusual', 'Ġbleeding', 'Ġmay', 'Ġinclude', 'Ġbleeding', 'Ġbetween', 'Ġyour', 'Ġperiods', '.', 'Ċ', 'Ċ', 'You', 'Ġshould', 'Ġspeak', 'Ġto', 'Ġyour', 'ĠGP', 'Ġas', 'Ġsoon', 'Ġas', 'Ġpossible', 'Ġif', 'Ġyou', 'Ġexperience', 'Ġany', 'Ġunusual', 'Ġvaginal', 'Ġbleeding', '.', 'ĠWhile', 'Ġit', \"'s\", 'Ġunlikely', 'Ġto', 'Ġbe', 'Ġcaused', 'Ġby', 'Ġwomb', 'Ġcancer', ',', 'Ġit', \"'s\", 'Ġbest', 'Ġto', 'Ġbe', 'Ġsure', '.', 'Ċ', 'Ċ', 'Your', 'ĠGP', 'Ġwill', 'Ġexamine', 'Ġyou', 'Ġand', 'Ġask', 'Ġabout', 'Ġyour', 'Ġsymptoms', '.', 'ĠThey', 'Ġwill', 'Ġrefer', 'Ġyou', 'Ġto', 'Ġa', 'Ġspecialist', 'Ġfor', 'Ġfurther', 'Ġtests', 'Ġif', 'Ġthey', 'Ġsuspect', 'Ġyou', 'Ġmay', 'Ġhave', 'Ġa', 'Ġserious', 'Ġproblem', ',', 'Ġor', 'Ġif', 'Ġthey', 'Ġare', 'Ġunsure', 'Ġabout', 'Ġa', 'Ġdiagnosis', '.', 'ĊĊ', 'ĊĊ', 'Ġ']\n"
384
+ ]
385
+ },
386
+ {
387
+ "ename": "KeyboardInterrupt",
388
+ "evalue": "",
389
+ "output_type": "error",
390
+ "traceback": [
391
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
392
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
393
+ "Cell \u001b[0;32mIn[55], line 26\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[39m# break\u001b[39;00m\n\u001b[1;32m 17\u001b[0m \u001b[39m# end += 1\u001b[39;00m\n\u001b[1;32m 18\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[39m# start = end + 1\u001b[39;00m\n\u001b[1;32m 22\u001b[0m \u001b[39m# end = start\u001b[39;00m\n\u001b[1;32m 24\u001b[0m \u001b[39mreturn\u001b[39;00m tokenized_parts\n\u001b[0;32m---> 26\u001b[0m tokenize_long_sentence(test, \u001b[39m500\u001b[39;49m)\n",
394
+ "Cell \u001b[0;32mIn[55], line 13\u001b[0m, in \u001b[0;36mtokenize_long_sentence\u001b[0;34m(long_sentence, max_tokens)\u001b[0m\n\u001b[1;32m 10\u001b[0m end \u001b[39m=\u001b[39m \u001b[39m0\u001b[39m\n\u001b[1;32m 12\u001b[0m \u001b[39mwhile\u001b[39;00m end \u001b[39m<\u001b[39m \u001b[39mlen\u001b[39m(tokens):\n\u001b[0;32m---> 13\u001b[0m \u001b[39mwhile\u001b[39;00m end \u001b[39m<\u001b[39m \u001b[39mlen\u001b[39m(tokens) \u001b[39mand\u001b[39;00m end \u001b[39m-\u001b[39;49m start \u001b[39m<\u001b[39;49m\u001b[39m=\u001b[39;49m max_tokens:\n\u001b[1;32m 14\u001b[0m \u001b[39mif\u001b[39;00m tokens[end] \u001b[39min\u001b[39;00m [\u001b[39m\"\u001b[39m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m?\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m!\u001b[39m\u001b[39m\"\u001b[39m]:\n\u001b[1;32m 15\u001b[0m \u001b[39mprint\u001b[39m(tokens[end])\n",
395
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
396
+ ]
397
+ }
398
+ ],
399
+ "source": [
400
+ "from transformers import AutoTokenizer\n",
401
+ "\n",
402
+ "def tokenize_long_sentence(long_sentence, max_tokens):\n",
403
+ " tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
404
+ " tokens = tokenizer.tokenize(long_sentence)\n",
405
+ " print(tokens)\n",
406
+ " tokenized_parts = []\n",
407
+ "\n",
408
+ " start = 0\n",
409
+ " end = 0\n",
410
+ "\n",
411
+ " while end < len(tokens):\n",
412
+ " while end < len(tokens) and end - start <= max_tokens:\n",
413
+ " if tokens[end] in [\".\", \"?\", \"!\"]:\n",
414
+ " print(tokens[end])\n",
415
+ " # break\n",
416
+ " # end += 1\n",
417
+ "\n",
418
+ " # part = tokens[start:end + 1]\n",
419
+ " # tokenized_parts.append(part)\n",
420
+ " # start = end + 1\n",
421
+ " # end = start\n",
422
+ "\n",
423
+ " return tokenized_parts\n",
424
+ "\n",
425
+ "tokenize_long_sentence(test, 500)"
426
+ ]
427
+ }
428
+ ],
429
+ "metadata": {
430
+ "kernelspec": {
431
+ "display_name": "chatbot",
432
+ "language": "python",
433
+ "name": "python3"
434
+ },
435
+ "language_info": {
436
+ "codemirror_mode": {
437
+ "name": "ipython",
438
+ "version": 3
439
+ },
440
+ "file_extension": ".py",
441
+ "mimetype": "text/x-python",
442
+ "name": "python",
443
+ "nbconvert_exporter": "python",
444
+ "pygments_lexer": "ipython3",
445
+ "version": "3.8.15"
446
+ },
447
+ "orig_nbformat": 4,
448
+ "vscode": {
449
+ "interpreter": {
450
+ "hash": "9798c78c52b861f9442ea63a21901b586ae2f2169fa92d94b4091cc5bab62e04"
451
+ }
452
+ }
453
+ },
454
+ "nbformat": 4,
455
+ "nbformat_minor": 2
456
+ }
ddd/ddd.png ADDED
metrics/pylint1.png ADDED
metrics/pylint2.png ADDED
metrics/sonarLint.png ADDED
metrics/sonarQubeCloud.png ADDED
streamlit_app.py CHANGED
@@ -1,42 +1,54 @@
 
1
  import openai
2
  import streamlit as st
3
  from streamlit_chat import message
4
  from transformers import pipeline
 
 
5
  summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
6
- sentiment_task = pipeline("sentiment-analysis", model='cardiffnlp/twitter-roberta-base-sentiment-latest', tokenizer='cardiffnlp/twitter-roberta-base-sentiment-latest')
7
- openai.api_key = st.secrets["openai_api_key"]
 
8
 
9
- from math import log
10
 
11
  completion = openai.Completion()
12
 
 
 
 
 
 
 
13
 
14
- start_prompt = '[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to.'
15
- start_message = 'I am Joy, your AI therapist. How are you feeling today?'
16
- start_sequence = "\nJoy:"
17
- restart_sequence = "\n\nPatient:"
18
-
19
  def ask(question: str, chat_log: str, model='text-davinci-003', temp=0.9) -> (str, str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- prompt = f'{chat_log}{restart_sequence} {question}{start_sequence}'
22
-
23
- response = completion.create(
24
- prompt = prompt,
25
- model = model,
26
- stop = ["Patient:",'Joy:'],
27
- temperature = temp, #the higher the more creative
28
- frequency_penalty = 0.9, #prevents word repetition, larger -> higher penalty
29
- presence_penalty = 1, #prevents topic repetition, larger -> higher penalty
30
- top_p =1,
31
- best_of=1,
32
- max_tokens=170
33
- )
34
-
35
- answer = response.choices[0].text.strip()
36
- log = f'{restart_sequence}{question}{start_sequence}{answer}'
37
- return str(answer), str(log)
38
-
39
- def clean_chat_log(chat_log):
40
  chat_log = ' '.join(chat_log)
41
  # find the first /n
42
  first_newline = chat_log.find('\n')
@@ -45,38 +57,41 @@ def clean_chat_log(chat_log):
45
  chat_log = chat_log.replace('\n', ' ')
46
  return chat_log
47
 
48
- def summarize(chat_log):
 
 
49
  chat_log = clean_chat_log(chat_log)
50
- summary = summarizer(chat_log, max_length=150, do_sample=False)[0]['summary_text']
51
  return summary
52
 
53
- def analyze_sentiment(chat_log):
54
- # split chat_log into smaller chunks
55
-
56
- # analyze each chunk
57
-
58
- # return the average sentiment
59
-
60
 
61
- chat_log = clean_chat_log(chat_log)
62
- sentiment = sentiment_task(chat_log)
 
63
  return sentiment
64
 
65
- def remove_backslashN(chat_log: list) -> list:
 
 
66
  chat_log = [i.replace('\n', ' ') for i in chat_log]
67
  return chat_log
68
 
69
 
70
 
71
  def main():
 
 
72
  st.title("Chat with Joy - the AI therapist!")
73
  col1, col2 = st.columns(2)
74
  temp = col1.slider("Bot-Creativeness", 0.0, 1.0, 0.9, 0.1)
75
- model = col2.selectbox("Model", ["text-davinci-003", "text-curie-001", "curie:ft-personal-2023-02-03-17-06-53"])
 
76
 
77
  if 'generated' not in st.session_state:
78
- st.session_state['generated'] = [start_message]
79
-
80
  if 'past' not in st.session_state:
81
  st.session_state['past'] = []
82
 
@@ -84,8 +99,8 @@ def main():
84
  st.session_state['summary'] = []
85
 
86
  if 'chat_log' not in st.session_state:
87
- st.session_state['chat_log'] = [start_prompt+start_sequence+start_message]
88
-
89
 
90
  if len(st.session_state['generated']) > 2:
91
  if st.button("Clear and summerize", key='clear'):
@@ -93,11 +108,11 @@ def main():
93
  summary = summarizer(chat_log, max_length=100, min_length=30, do_sample=False)
94
  st.write(summary)
95
  user_sentiment = st.session_state['past']
96
- user_sentiment = remove_backslashN(user_sentiment)
97
- st.write(sentiment_task(user_sentiment))
98
- st.session_state['generated'] = [start_message]
99
  st.session_state['past'] = []
100
- st.session_state['chat_log'] = [start_prompt+start_sequence+start_message]
101
  st.session_state['summary'] = []
102
 
103
  user_input=st.text_input("You:",key='input')
@@ -107,9 +122,6 @@ def main():
107
  st.session_state['chat_log'].append(chat_log)
108
  st.session_state['past'].append(user_input)
109
  st.session_state['generated'].append(output)
110
- print(model)
111
- print(temp)
112
- print(st.session_state['chat_log'])
113
  if st.session_state['generated']:
114
  for i in range(len(st.session_state['generated'])-1, -1, -1):
115
  if i < len(st.session_state['past']):
@@ -120,12 +132,3 @@ def main():
120
 
121
  if __name__ == "__main__":
122
  main()
123
- # save the user's input and the model's output to the database and analyze the user's input and the model's output
124
-
125
- # if len(st.seesion_state['generated']) :
126
- # save the user's input and the model's output to the database
127
- # analyze the user's input and the model's output
128
-
129
-
130
-
131
-
 
1
+ """OpenAI GPT-3 Chatbot with Streamlit"""
2
  import openai
3
  import streamlit as st
4
  from streamlit_chat import message
5
  from transformers import pipeline
6
+
7
+
8
  summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
9
+ sentiment_task = pipeline("sentiment-analysis",
10
+ model='cardiffnlp/twitter-roberta-base-sentiment-latest',
11
+ tokenizer='cardiffnlp/twitter-roberta-base-sentiment-latest')
12
 
13
+ openai.api_key = st.secrets["openai_api_key"]
14
 
15
  completion = openai.Completion()
16
 
17
+ START_PROMPT = '[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI \
18
+ therapist named Joy. Joy listens and offers advices. \
19
+ End the conversation when the patient wishes to.'
20
+ START_MESSAGE = 'I am Joy, your AI therapist. How are you feeling today?'
21
+ START_SEQUENCE = "\nJoy:"
22
+ RESTART_SEQUENCE = "\n\nPatient:"
23
 
 
 
 
 
 
24
  def ask(question: str, chat_log: str, model='text-davinci-003', temp=0.9) -> (str, str):
25
+ ''' funtion takes a input string and the preview chat_log,
26
+ returns the model's repsonse/answer and the dialog
27
+ by using the preview chat_log, gold fish memory effect can be prevented,
28
+ the chat_log is used to summarize and analyze the sentiment of the user's input '''
29
+
30
+ prompt = f'{chat_log}{RESTART_SEQUENCE} {question}{START_SEQUENCE}'
31
+ response = completion.create(
32
+ prompt = prompt,
33
+ model = model,
34
+ stop = ["Patient:",'Joy:'],
35
+ temperature = temp, #the higher the more creative
36
+ frequency_penalty = 0.9, #prevents word repetition, larger -> higher penalty
37
+ presence_penalty = 1, #prevents topic repetition, larger -> higher penalty
38
+ top_p =1,
39
+ best_of=1,
40
+ max_tokens=170,
41
+ )
42
+
43
+ answer = response.choices[0].text.strip()
44
+ log = f'{RESTART_SEQUENCE}{question}{START_SEQUENCE}{answer}'
45
+ return str(answer), str(log)
46
+
47
+ def clean_chat_log(chat_log: list) -> str:
48
+ ''' cleans the chat log by joining list items,
49
+ removing everything before the first \n and replace all other
50
+ \n with empty space.'''
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  chat_log = ' '.join(chat_log)
53
  # find the first /n
54
  first_newline = chat_log.find('\n')
 
57
  chat_log = chat_log.replace('\n', ' ')
58
  return chat_log
59
 
60
+ def summarize(chat_log: list) -> str:
61
+ ''' returns a summary of the chat log '''
62
+
63
  chat_log = clean_chat_log(chat_log)
64
+ summary = summarizer(chat_log, do_sample=False)[0]['summary_text']
65
  return summary
66
 
67
+ def analyze_sentiment(user_input: list) -> str:
68
+ ''' returns user sentiment based on the users input'''
 
 
 
 
 
69
 
70
+ user_input = clean_chat_log(user_input)
71
+ summary = summarizer(user_input, do_sample=False)[0]['summary_text']
72
+ sentiment = sentiment_task(summary)
73
  return sentiment
74
 
75
+ def remove_backslash(chat_log: list) -> list:
76
+ ''' removes the backslash from the chat log '''
77
+
78
  chat_log = [i.replace('\n', ' ') for i in chat_log]
79
  return chat_log
80
 
81
 
82
 
83
  def main():
84
+ ''' main function '''
85
+
86
  st.title("Chat with Joy - the AI therapist!")
87
  col1, col2 = st.columns(2)
88
  temp = col1.slider("Bot-Creativeness", 0.0, 1.0, 0.9, 0.1)
89
+ model = col2.selectbox("Model", ["text-davinci-003",
90
+ "text-curie-001", "curie:ft-personal-2023-02-03-17-06-53"])
91
 
92
  if 'generated' not in st.session_state:
93
+ st.session_state['generated'] = [START_MESSAGE]
94
+
95
  if 'past' not in st.session_state:
96
  st.session_state['past'] = []
97
 
 
99
  st.session_state['summary'] = []
100
 
101
  if 'chat_log' not in st.session_state:
102
+ st.session_state['chat_log'] = [START_PROMPT+START_SEQUENCE+START_MESSAGE]
103
+
104
 
105
  if len(st.session_state['generated']) > 2:
106
  if st.button("Clear and summerize", key='clear'):
 
108
  summary = summarizer(chat_log, max_length=100, min_length=30, do_sample=False)
109
  st.write(summary)
110
  user_sentiment = st.session_state['past']
111
+ user_sentiment = remove_backslash(user_sentiment)
112
+ st.write(analyze_sentiment(user_sentiment))
113
+ st.session_state['generated'] = [START_MESSAGE]
114
  st.session_state['past'] = []
115
+ st.session_state['chat_log'] = [START_PROMPT+START_SEQUENCE+START_MESSAGE]
116
  st.session_state['summary'] = []
117
 
118
  user_input=st.text_input("You:",key='input')
 
122
  st.session_state['chat_log'].append(chat_log)
123
  st.session_state['past'].append(user_input)
124
  st.session_state['generated'].append(output)
 
 
 
125
  if st.session_state['generated']:
126
  for i in range(len(st.session_state['generated'])-1, -1, -1):
127
  if i < len(st.session_state['past']):
 
132
 
133
  if __name__ == "__main__":
134
  main()
 
 
 
 
 
 
 
 
 
streamlit_app.py_backup ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import streamlit as st
3
+ from streamlit_chat import message
4
+ from transformers import pipeline
5
+ summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
6
+ sentiment_task = pipeline("sentiment-analysis", model='cardiffnlp/twitter-roberta-base-sentiment-latest', tokenizer='cardiffnlp/twitter-roberta-base-sentiment-latest')
7
+ openai.api_key = st.secrets["openai_api_key"]
8
+
9
+ from math import log
10
+
11
+ completion = openai.Completion()
12
+
13
+
14
+ start_prompt = '[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens and offers advices. End the conversation when the patient wishes to.'
15
+ start_message = 'I am Joy, your AI therapist. How are you feeling today?'
16
+ start_sequence = "\nJoy:"
17
+ restart_sequence = "\n\nPatient:"
18
+
19
+ def ask(question: str, chat_log: str, model='text-davinci-003', temp=0.9) -> (str, str):
20
+
21
+ prompt = f'{chat_log}{restart_sequence} {question}{start_sequence}'
22
+
23
+ response = completion.create(
24
+ prompt = prompt,
25
+ model = model,
26
+ stop = ["Patient:",'Joy:'],
27
+ temperature = temp, #the higher the more creative
28
+ frequency_penalty = 0.9, #prevents word repetition, larger -> higher penalty
29
+ presence_penalty = 1, #prevents topic repetition, larger -> higher penalty
30
+ top_p =1,
31
+ best_of=1,
32
+ max_tokens=170
33
+ )
34
+
35
+ answer = response.choices[0].text.strip()
36
+ log = f'{restart_sequence}{question}{start_sequence}{answer}'
37
+ return str(answer), str(log)
38
+
39
+ def clean_chat_log(chat_log):
40
+ chat_log = ' '.join(chat_log)
41
+ # find the first /n
42
+ first_newline = chat_log.find('\n')
43
+ chat_log = chat_log[first_newline:]
44
+ # remove all \n
45
+ chat_log = chat_log.replace('\n', ' ')
46
+ return chat_log
47
+
48
+ def summarize(chat_log):
49
+ chat_log = clean_chat_log(chat_log)
50
+ summary = summarizer(chat_log, max_length=150, do_sample=False)[0]['summary_text']
51
+ return summary
52
+
53
+ def analyze_sentiment(chat_log):
54
+ # split chat_log into smaller chunks
55
+
56
+ # analyze each chunk
57
+
58
+ # return the average sentiment
59
+
60
+
61
+ chat_log = clean_chat_log(chat_log)
62
+ sentiment = sentiment_task(chat_log)
63
+ return sentiment
64
+
65
+ def remove_backslashN(chat_log: list) -> list:
66
+ chat_log = [i.replace('\n', ' ') for i in chat_log]
67
+ return chat_log
68
+
69
+
70
+
71
+ def main():
72
+ st.title("Chat with Joy - the AI therapist!")
73
+ col1, col2 = st.columns(2)
74
+ temp = col1.slider("Bot-Creativeness", 0.0, 1.0, 0.9, 0.1)
75
+ model = col2.selectbox("Model", ["text-davinci-003", "text-curie-001", "curie:ft-personal-2023-02-03-17-06-53"])
76
+
77
+ if 'generated' not in st.session_state:
78
+ st.session_state['generated'] = [start_message]
79
+
80
+ if 'past' not in st.session_state:
81
+ st.session_state['past'] = []
82
+
83
+ if 'summary' not in st.session_state:
84
+ st.session_state['summary'] = []
85
+
86
+ if 'chat_log' not in st.session_state:
87
+ st.session_state['chat_log'] = [start_prompt+start_sequence+start_message]
88
+
89
+
90
+ if len(st.session_state['generated']) > 2:
91
+ if st.button("Clear and summerize", key='clear'):
92
+ chat_log = clean_chat_log(st.session_state['chat_log'])
93
+ summary = summarizer(chat_log, max_length=100, min_length=30, do_sample=False)
94
+ st.write(summary)
95
+ user_sentiment = st.session_state['past']
96
+ user_sentiment = remove_backslashN(user_sentiment)
97
+ st.write(sentiment_task(user_sentiment))
98
+ st.session_state['generated'] = [start_message]
99
+ st.session_state['past'] = []
100
+ st.session_state['chat_log'] = [start_prompt+start_sequence+start_message]
101
+ st.session_state['summary'] = []
102
+
103
+ user_input=st.text_input("You:",key='input')
104
+
105
+ if user_input:
106
+ output, chat_log = ask(user_input, st.session_state['chat_log'], model=model, temp=temp)
107
+ st.session_state['chat_log'].append(chat_log)
108
+ st.session_state['past'].append(user_input)
109
+ st.session_state['generated'].append(output)
110
+ print(model)
111
+ print(temp)
112
+ print(st.session_state['chat_log'])
113
+ if st.session_state['generated']:
114
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
115
+ if i < len(st.session_state['past']):
116
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
117
+ message(st.session_state["generated"][i], key=str(i))
118
+
119
+
120
+
121
+ if __name__ == "__main__":
122
+ main()
123
+ # save the user's input and the model's output to the database and analyze the user's input and the model's output
124
+
125
+ # if len(st.seesion_state['generated']) :
126
+ # save the user's input and the model's output to the database
127
+ # analyze the user's input and the model's output
128
+
129
+
130
+
131
+
streamlit_app_minimum.py DELETED
@@ -1,83 +0,0 @@
1
- import openai
2
- openai.api_key_path = './openai_api_key.txt'
3
- import streamlit as st
4
- from streamlit_chat import message
5
-
6
-
7
- completion = openai.Completion()
8
-
9
-
10
- start_prompt = '[Instruction] Act as a friendly, compasionate, insightful, and empathetic AI therapist named Joy. Joy listens, asks for details and offers detailed advices once a while. End the conversation when you wishes to.'
11
- start_message = 'I am Joy, your AI therapist. How are you feeling today?'
12
-
13
- start_sequence = "\nJoy:"
14
- restart_sequence = "\n\nYou:"
15
-
16
- # to do:
17
- # let the user choose between models (curie, davinci, curie-finetuned, davinci-finetuned)
18
- # let the user choose between different temperatures, frequency_penalty, presence_penalty
19
-
20
- # save the user's input and the model's output to the database
21
- # analyze the user's input and the model's output
22
- # sentiment/mood analysis / topic analysis of the user's input
23
-
24
- # embed the user's input and look for therapy catalogue that is similar to the user's input
25
- # push the therapy catalogue to the user
26
-
27
-
28
- def ask(question: str, chat_log: str) -> (str, str):
29
-
30
- prompt = f'{chat_log}{restart_sequence} {question}{start_sequence}'
31
-
32
- response = completion.create(
33
- prompt = prompt,
34
- model = model,
35
- stop = ["You:",'Joy:'],
36
- temperature = temp, #the higher the more creative
37
- frequency_penalty = 0.3, #prevents word repetition, larger -> higher penalty
38
- presence_penalty = 0.6, #prevents topic repetition, larger -> higher penalty
39
- top_p =1,
40
- best_of=1,
41
- max_tokens=170
42
- )
43
-
44
- answer = response.choices[0].text.strip()
45
- log = f'{restart_sequence}{question}{start_sequence}{answer}'
46
- return str(answer), str(log)
47
-
48
-
49
- # button for starting a new conversation
50
-
51
- st.title("Chat with Joy - the AI therapist!")
52
- temp = st.slider("Creativity", 0.0, 1.0, 0.7, 0.1)
53
- model = st.selectbox("Model", ["text-davinci-003", "text-curie-001", "curie:ft-personal-2023-02-03-17-06-53"])
54
-
55
- if 'generated' not in st.session_state:
56
- st.session_state['generated'] = [start_message]
57
-
58
- if 'past' not in st.session_state:
59
- st.session_state['past'] = []
60
-
61
-
62
-
63
- if 'chat_log' not in st.session_state:
64
- st.session_state['chat_log'] = [start_prompt+start_sequence+start_message]
65
-
66
- user_input=st.text_input("You:",key='input')
67
-
68
- if user_input:
69
- output, chat_log = ask(user_input, st.session_state['chat_log'])
70
- st.session_state['chat_log'].append(chat_log)
71
- st.session_state['past'].append(user_input)
72
- st.session_state['generated'].append(output)
73
- print(st.session_state['chat_log'])
74
- if st.session_state['generated']:
75
- for i in range(len(st.session_state['generated'])-1, -1, -1):
76
- if i < len(st.session_state['past']):
77
- message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
78
- message(st.session_state["generated"][i], key=str(i))
79
-
80
- # save the user's input and the model's output to the database and analyze the user's input and the model's output
81
-
82
-
83
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
test_sample 2.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from streamlit_app import ask, clean_chat_log
3
+
4
+ def test_ask():
5
+ question = "What's your name?"
6
+ chat_log = ""
7
+ model = 'text-davinci-003'
8
+ temp = 0.9
9
+
10
+ answer, log = ask(question, chat_log, model, temp)
11
+
12
+ assert isinstance(answer, str)
13
+ assert isinstance(log, str)
14
+
15
+ def test_clean_chat_log():
16
+ # Test case 1
17
+ input_chat_log = ['hello', 'world\n', 'how', 'are', 'you\n']
18
+ expected_output = ' how are you '
19
+ assert clean_chat_log(input_chat_log) == expected_output
20
+
21
+ # Test case 2
22
+ input_chat_log = ['\n', 'this', 'is', 'a', 'new', 'line\n', '\n']
23
+ expected_output = ' this is a new line '
24
+ output = clean_chat_log(input_chat_log)
25
+ assert output == expected_output
26
+ assert isinstance(output, str)
27
+
test_sample.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from streamlit_app import ask, clean_chat_log
3
+
4
+ def test_ask():
5
+ question = "What's your name?"
6
+ chat_log = ""
7
+ model = 'text-davinci-003'
8
+ temp = 0.9
9
+
10
+ answer, log = ask(question, chat_log, model, temp)
11
+
12
+ assert isinstance(answer, str)
13
+ assert isinstance(log, str)
14
+
15
+ def test_clean_chat_log():
16
+ # Test case 1
17
+ input_chat_log = ['hello', 'world\n', 'how', 'are', 'you\n']
18
+ expected_output = ' how are you '
19
+ assert clean_chat_log(input_chat_log) == expected_output
20
+
21
+ # Test case 2
22
+ input_chat_log = ['\n', 'this', 'is', 'a', 'new', 'line\n', '\n']
23
+ expected_output = ' this is a new line '
24
+ output = clean_chat_log(input_chat_log)
25
+ assert output == expected_output
26
+ assert isinstance(output, str)
27
+
uml/@startuml.wsd ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @startuml
2
+ actor User
3
+ participant AI
4
+ participant Analyzer
5
+
6
+ loop n times
7
+ User -> AI: request
8
+ AI -> User: response
9
+ User -> AI: feedback
10
+ end
11
+
12
+ AI -> Analyzer: aggregates chat logs
13
+ Analyzer -> AI: statistics / custom responses
14
+ AI -> User: customized therapy suggestions
15
+ @enduml
uml/UseCase.png ADDED
uml/flowDiagram.png ADDED
uml/sequence.png ADDED
unitTest/testFailed.png ADDED
unitTest/unitTest.png ADDED