|
a |
|
b/final-chatbot.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 1, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [ |
|
|
8 |
{ |
|
|
9 |
"name": "stdout", |
|
|
10 |
"output_type": "stream", |
|
|
11 |
"text": [ |
|
|
12 |
"Requirement already satisfied: Flask==0.12.3 in /home/welcome/anaconda3/lib/python3.6/site-packages (from -r requirements.txt (line 4)) (0.12.3)\n", |
|
|
13 |
"Requirement already satisfied: chatterbot==0.8.4 in /home/welcome/anaconda3/lib/python3.6/site-packages (from -r requirements.txt (line 5)) (0.8.4)\n", |
|
|
14 |
"Requirement already satisfied: SQLAlchemy==1.1.11 in /home/welcome/anaconda3/lib/python3.6/site-packages (from -r requirements.txt (line 6)) (1.1.11)\n", |
|
|
15 |
"Requirement already satisfied: Jinja2>=2.4 in /home/welcome/anaconda3/lib/python3.6/site-packages (from Flask==0.12.3->-r requirements.txt (line 4)) (2.9.6)\n", |
|
|
16 |
"Requirement already satisfied: itsdangerous>=0.21 in /home/welcome/anaconda3/lib/python3.6/site-packages (from Flask==0.12.3->-r requirements.txt (line 4)) (0.24)\n", |
|
|
17 |
"Requirement already satisfied: Werkzeug>=0.7 in /home/welcome/anaconda3/lib/python3.6/site-packages (from Flask==0.12.3->-r requirements.txt (line 4)) (0.12.2)\n", |
|
|
18 |
"Requirement already satisfied: click>=2.0 in /home/welcome/anaconda3/lib/python3.6/site-packages (from Flask==0.12.3->-r requirements.txt (line 4)) (6.7)\n", |
|
|
19 |
"Requirement already satisfied: chatterbot-corpus<1.2,>=1.1 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot==0.8.4->-r requirements.txt (line 5)) (1.1.4)\n", |
|
|
20 |
"Requirement already satisfied: python-dateutil<2.7,>=2.6 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot==0.8.4->-r requirements.txt (line 5)) (2.6.1)\n", |
|
|
21 |
"Requirement already satisfied: python-twitter<4.0,>=3.0 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot==0.8.4->-r requirements.txt (line 5)) (3.5)\n", |
|
|
22 |
"Requirement already satisfied: pymongo<4.0,>=3.3 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot==0.8.4->-r requirements.txt (line 5)) (3.10.1)\n", |
|
|
23 |
"Requirement already satisfied: nltk<4.0,>=3.2 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot==0.8.4->-r requirements.txt (line 5)) (3.2.4)\n", |
|
|
24 |
"Requirement already satisfied: mathparse<0.2,>=0.1 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot==0.8.4->-r requirements.txt (line 5)) (0.1.2)\n", |
|
|
25 |
"Requirement already satisfied: MarkupSafe>=0.23 in /home/welcome/anaconda3/lib/python3.6/site-packages (from Jinja2>=2.4->Flask==0.12.3->-r requirements.txt (line 4)) (1.0)\n", |
|
|
26 |
"Requirement already satisfied: PyYAML<4.0,>=3.12 in /home/welcome/anaconda3/lib/python3.6/site-packages (from chatterbot-corpus<1.2,>=1.1->chatterbot==0.8.4->-r requirements.txt (line 5)) (3.12)\n", |
|
|
27 |
"Requirement already satisfied: six>=1.5 in /home/welcome/anaconda3/lib/python3.6/site-packages (from python-dateutil<2.7,>=2.6->chatterbot==0.8.4->-r requirements.txt (line 5)) (1.11.0)\n", |
|
|
28 |
"Requirement already satisfied: future in /home/welcome/anaconda3/lib/python3.6/site-packages (from python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (0.18.2)\n", |
|
|
29 |
"Requirement already satisfied: requests in /home/welcome/anaconda3/lib/python3.6/site-packages (from python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (2.18.4)\n", |
|
|
30 |
"Requirement already satisfied: requests-oauthlib in /home/welcome/anaconda3/lib/python3.6/site-packages (from python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (1.3.0)\n", |
|
|
31 |
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/welcome/anaconda3/lib/python3.6/site-packages (from requests->python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (3.0.4)\n", |
|
|
32 |
"Requirement already satisfied: idna<2.7,>=2.5 in /home/welcome/anaconda3/lib/python3.6/site-packages (from requests->python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (2.6)\n", |
|
|
33 |
"Requirement already satisfied: urllib3<1.23,>=1.21.1 in /home/welcome/anaconda3/lib/python3.6/site-packages (from requests->python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (1.22)\n", |
|
|
34 |
"Requirement already satisfied: certifi>=2017.4.17 in /home/welcome/anaconda3/lib/python3.6/site-packages (from requests->python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (2017.7.27.1)\n", |
|
|
35 |
"Requirement already satisfied: oauthlib>=3.0.0 in /home/welcome/anaconda3/lib/python3.6/site-packages (from requests-oauthlib->python-twitter<4.0,>=3.0->chatterbot==0.8.4->-r requirements.txt (line 5)) (3.1.0)\n", |
|
|
36 |
"\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1 is available.\n", |
|
|
37 |
"You should consider upgrading via the '/home/welcome/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n" |
|
|
38 |
] |
|
|
39 |
} |
|
|
40 |
], |
|
|
41 |
"source": [ |
|
|
42 |
"!pip install -r requirements.txt" |
|
|
43 |
] |
|
|
44 |
}, |
|
|
45 |
{ |
|
|
46 |
"cell_type": "code", |
|
|
47 |
"execution_count": 2, |
|
|
48 |
"metadata": {}, |
|
|
49 |
"outputs": [ |
|
|
50 |
{ |
|
|
51 |
"name": "stdout", |
|
|
52 |
"output_type": "stream", |
|
|
53 |
"text": [ |
|
|
54 |
"Training using fracture.yml\n", |
|
|
55 |
"List Trainer: [####################] 100%\n", |
|
|
56 |
"Training completed for fracture.yml\n", |
|
|
57 |
"Training using fever.yml\n", |
|
|
58 |
"List Trainer: [####################] 100%\n", |
|
|
59 |
"Training completed for fever.yml\n", |
|
|
60 |
"Training using headache.yml\n", |
|
|
61 |
"List Trainer: [####################] 100%\n", |
|
|
62 |
"Training completed for headache.yml\n", |
|
|
63 |
"Training using generalhealth.yml\n", |
|
|
64 |
"List Trainer: [####################] 100%\n", |
|
|
65 |
"Training completed for generalhealth.yml\n", |
|
|
66 |
"Training using botprofile.yml\n", |
|
|
67 |
"List Trainer: [####################] 100%\n", |
|
|
68 |
"Training completed for botprofile.yml\n", |
|
|
69 |
"Training using personalinfo.yml\n", |
|
|
70 |
"List Trainer: [####################] 100%\n", |
|
|
71 |
"Training completed for personalinfo.yml\n", |
|
|
72 |
"Training using doctor.yml\n", |
|
|
73 |
"List Trainer: [####################] 100%\n", |
|
|
74 |
"Training completed for doctor.yml\n", |
|
|
75 |
"Training using cough.cold.yml\n", |
|
|
76 |
"List Trainer: [####################] 100%\n", |
|
|
77 |
"Training completed for cough.cold.yml\n", |
|
|
78 |
"Training using new.yml\n", |
|
|
79 |
"List Trainer: [####################] 100%\n", |
|
|
80 |
"Training completed for new.yml\n", |
|
|
81 |
"Training using greetings.yml\n", |
|
|
82 |
"List Trainer: [####################] 100%\n", |
|
|
83 |
"Training completed for greetings.yml\n" |
|
|
84 |
] |
|
|
85 |
} |
|
|
86 |
], |
|
|
87 |
"source": [ |
|
|
88 |
"from chatterbot import ChatBot\n", |
|
|
89 |
"from chatterbot.trainers import ListTrainer\n", |
|
|
90 |
"import os\n", |
|
|
91 |
"\n", |
|
|
92 |
"\n", |
|
|
93 |
"english_bot = ChatBot('Bot')\n", |
|
|
94 |
"english_bot.set_trainer(ListTrainer)\n", |
|
|
95 |
"for file in os.listdir('data'):\n", |
|
|
96 |
" print('Training using '+file)\n", |
|
|
97 |
" convData = open('data/' + file).readlines()\n", |
|
|
98 |
" english_bot.train(convData)\n", |
|
|
99 |
" print(\"Training completed for \"+file)\n", |
|
|
100 |
" \n" |
|
|
101 |
] |
|
|
102 |
}, |
|
|
103 |
{ |
|
|
104 |
"cell_type": "code", |
|
|
105 |
"execution_count": null, |
|
|
106 |
"metadata": {}, |
|
|
107 |
"outputs": [ |
|
|
108 |
{ |
|
|
109 |
"name": "stdout", |
|
|
110 |
"output_type": "stream", |
|
|
111 |
"text": [ |
|
|
112 |
"Okay Bro\n", |
|
|
113 |
"You said: i have headache\n", |
|
|
114 |
"\n", |
|
|
115 |
"- what kind of headache do you have : 1. Migrane headache - intense throbbing pain on just one side of the head. 2. Cluster headaches - severe and recurrent headaches 3. Tension Headache - Ocassional;\n", |
|
|
116 |
"Okay Bro\n", |
|
|
117 |
"You said: i have migraine headache\n", |
|
|
118 |
"\n", |
|
|
119 |
"- symptom noted. Migraines tend to be recurrent, and each attack may last up to 3 days. would you like to talk to a doctor?\n", |
|
|
120 |
"Okay Bro\n", |
|
|
121 |
"Your last instruction was unclear to me\n", |
|
|
122 |
"Okay Bro\n", |
|
|
123 |
"You said: yes i want to talk to a doctor\n", |
|
|
124 |
"\n", |
|
|
125 |
"- you shall be called once the doctor is available. Do you want to book an appointment?\n", |
|
|
126 |
"Okay Bro\n", |
|
|
127 |
"You said: yes i want to\n", |
|
|
128 |
"\n", |
|
|
129 |
"- ok, is there a swelling or a physical deformity or bleeding at the aching region.\n", |
|
|
130 |
"Okay Bro\n", |
|
|
131 |
"Your last instruction was unclear to me\n", |
|
|
132 |
"Okay Bro\n", |
|
|
133 |
"Your last instruction was unclear to me\n", |
|
|
134 |
"Okay Bro\n", |
|
|
135 |
"You said: yes i want to book an appointment\n", |
|
|
136 |
"\n", |
|
|
137 |
"- yes please state time and doctor's name in the following format; 'Patient Name: Date: Doctor: Hospital:'\n", |
|
|
138 |
"Okay Bro\n" |
|
|
139 |
] |
|
|
140 |
} |
|
|
141 |
], |
|
|
142 |
"source": [ |
|
|
143 |
"from flask import Flask, render_template, request\n", |
|
|
144 |
"from chatterbot import ChatBot\n", |
|
|
145 |
"from chatterbot.trainers import ChatterBotCorpusTrainer\n", |
|
|
146 |
"import os\n", |
|
|
147 |
"from gtts import gTTS\n", |
|
|
148 |
"import speech_recognition as sr\n", |
|
|
149 |
"import os\n", |
|
|
150 |
"import re\n", |
|
|
151 |
"import webbrowser\n", |
|
|
152 |
"import smtplib\n", |
|
|
153 |
"import requests\n", |
|
|
154 |
"\n", |
|
|
155 |
"\n", |
|
|
156 |
"from chatterbot import ChatBot\n", |
|
|
157 |
"from chatterbot.trainers import ListTrainer\n", |
|
|
158 |
"\n", |
|
|
159 |
"filenumber=int(os.listdir('saved_conversations')[-1])\n", |
|
|
160 |
"filenumber=filenumber+1\n", |
|
|
161 |
"file= open('saved_conversations/'+str(filenumber),\"w+\")\n", |
|
|
162 |
"file.write('bot : Hi There! I am a medical chatbot. You can begin conversation by typing in a message and pressing enter.\\n')\n", |
|
|
163 |
"file.close()\n", |
|
|
164 |
"\n", |
|
|
165 |
"\n", |
|
|
166 |
"english_bot = ChatBot('Bot',\n", |
|
|
167 |
" storage_adapter='chatterbot.storage.SQLStorageAdapter',\n", |
|
|
168 |
" logic_adapters=[\n", |
|
|
169 |
" {\n", |
|
|
170 |
" 'import_path': 'chatterbot.logic.BestMatch'\n", |
|
|
171 |
" },\n", |
|
|
172 |
" \n", |
|
|
173 |
"],\n", |
|
|
174 |
"trainer='chatterbot.trainers.ListTrainer')\n", |
|
|
175 |
"english_bot.set_trainer(ListTrainer)\n", |
|
|
176 |
"\n", |
|
|
177 |
"\n", |
|
|
178 |
"def myCommand():\n", |
|
|
179 |
" r = sr.Recognizer()\n", |
|
|
180 |
"\n", |
|
|
181 |
" with sr.Microphone() as source:\n", |
|
|
182 |
" print('Okay Bro')\n", |
|
|
183 |
" r.pause_treshold = 1\n", |
|
|
184 |
" r.adjust_for_ambient_noise(source, duration = 1)\n", |
|
|
185 |
" audio = r.listen(source)\t\n", |
|
|
186 |
"\n", |
|
|
187 |
" try:\n", |
|
|
188 |
" command = r.recognize_google(audio).lower()\n", |
|
|
189 |
" print('You said: '+ command + '\\n')\n", |
|
|
190 |
"\n", |
|
|
191 |
" except sr.UnknownValueError:\n", |
|
|
192 |
" print('Your last instruction was unclear to me')\n", |
|
|
193 |
" command = myCommand();\n", |
|
|
194 |
"\n", |
|
|
195 |
" return command \n", |
|
|
196 |
"\n", |
|
|
197 |
"\n", |
|
|
198 |
"def get_bot_response(command):\n", |
|
|
199 |
" #userText = request.args.get('msg')\n", |
|
|
200 |
" response = str(english_bot.get_response(command))\n", |
|
|
201 |
"\n", |
|
|
202 |
" appendfile=os.listdir('saved_conversations')[-1]\n", |
|
|
203 |
" appendfile= open('saved_conversations/'+str(filenumber),\"a\")\n", |
|
|
204 |
" appendfile.write('user : '+command+'\\n')\n", |
|
|
205 |
" appendfile.write('bot : '+response+'\\n')\n", |
|
|
206 |
" appendfile.close()\n", |
|
|
207 |
" return response\n", |
|
|
208 |
"\n", |
|
|
209 |
"\n", |
|
|
210 |
"while True:\n", |
|
|
211 |
" value=get_bot_response(myCommand())\n", |
|
|
212 |
" print(value)" |
|
|
213 |
] |
|
|
214 |
}, |
|
|
215 |
{ |
|
|
216 |
"cell_type": "code", |
|
|
217 |
"execution_count": null, |
|
|
218 |
"metadata": { |
|
|
219 |
"collapsed": true |
|
|
220 |
}, |
|
|
221 |
"outputs": [], |
|
|
222 |
"source": [] |
|
|
223 |
} |
|
|
224 |
], |
|
|
225 |
"metadata": { |
|
|
226 |
"kernelspec": { |
|
|
227 |
"display_name": "Python 3", |
|
|
228 |
"language": "python", |
|
|
229 |
"name": "python3" |
|
|
230 |
}, |
|
|
231 |
"language_info": { |
|
|
232 |
"codemirror_mode": { |
|
|
233 |
"name": "ipython", |
|
|
234 |
"version": 3 |
|
|
235 |
}, |
|
|
236 |
"file_extension": ".py", |
|
|
237 |
"mimetype": "text/x-python", |
|
|
238 |
"name": "python", |
|
|
239 |
"nbconvert_exporter": "python", |
|
|
240 |
"pygments_lexer": "ipython3", |
|
|
241 |
"version": "3.7.6" |
|
|
242 |
} |
|
|
243 |
}, |
|
|
244 |
"nbformat": 4, |
|
|
245 |
"nbformat_minor": 2 |
|
|
246 |
} |