--- a +++ b/.ipynb_checkpoints/final-chatbot-checkpoint.ipynb @@ -0,0 +1,246 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "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", + "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1 is available.\n", + "You should consider upgrading via the '/home/welcome/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install -r requirements.txt" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Training using fracture.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for fracture.yml\n", + "Training using fever.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for fever.yml\n", + "Training using headache.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for headache.yml\n", + "Training using generalhealth.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for generalhealth.yml\n", + "Training using botprofile.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for botprofile.yml\n", + "Training using personalinfo.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for personalinfo.yml\n", + "Training using doctor.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for doctor.yml\n", + "Training using cough.cold.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for cough.cold.yml\n", + "Training using new.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for new.yml\n", + "Training using greetings.yml\n", + "List Trainer: [####################] 100%\n", + "Training completed for greetings.yml\n" + ] + } + ], + "source": [ + "from chatterbot import ChatBot\n", + "from chatterbot.trainers import ListTrainer\n", + "import os\n", + "\n", + "\n", + "english_bot = ChatBot('Bot')\n", + "english_bot.set_trainer(ListTrainer)\n", + "for file in os.listdir('data'):\n", + " print('Training using '+file)\n", + " convData = open('data/' + file).readlines()\n", + " english_bot.train(convData)\n", + " print(\"Training completed for \"+file)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Okay Bro\n", + "You said: i have headache\n", + "\n", + "- 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", + "Okay Bro\n", + "You said: i have migraine headache\n", + "\n", + "- 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", + "Okay Bro\n", + "Your last instruction was unclear to me\n", + "Okay Bro\n", + "You said: yes i want to talk to a doctor\n", + "\n", + "- you shall be called once the doctor is available. Do you want to book an appointment?\n", + "Okay Bro\n", + "You said: yes i want to\n", + "\n", + "- ok, is there a swelling or a physical deformity or bleeding at the aching region.\n", + "Okay Bro\n", + "Your last instruction was unclear to me\n", + "Okay Bro\n", + "Your last instruction was unclear to me\n", + "Okay Bro\n", + "You said: yes i want to book an appointment\n", + "\n", + "- yes please state time and doctor's name in the following format; 'Patient Name: Date: Doctor: Hospital:'\n", + "Okay Bro\n" + ] + } + ], + "source": [ + "from flask import Flask, render_template, request\n", + "from chatterbot import ChatBot\n", + "from chatterbot.trainers import ChatterBotCorpusTrainer\n", + "import os\n", + "from gtts import gTTS\n", + "import speech_recognition as sr\n", + "import os\n", + "import re\n", + "import webbrowser\n", + "import smtplib\n", + "import requests\n", + "\n", + "\n", + "from chatterbot import ChatBot\n", + "from chatterbot.trainers import ListTrainer\n", + "\n", + "filenumber=int(os.listdir('saved_conversations')[-1])\n", + "filenumber=filenumber+1\n", + "file= open('saved_conversations/'+str(filenumber),\"w+\")\n", + "file.write('bot : Hi There! I am a medical chatbot. You can begin conversation by typing in a message and pressing enter.\\n')\n", + "file.close()\n", + "\n", + "\n", + "english_bot = ChatBot('Bot',\n", + " storage_adapter='chatterbot.storage.SQLStorageAdapter',\n", + " logic_adapters=[\n", + " {\n", + " 'import_path': 'chatterbot.logic.BestMatch'\n", + " },\n", + " \n", + "],\n", + "trainer='chatterbot.trainers.ListTrainer')\n", + "english_bot.set_trainer(ListTrainer)\n", + "\n", + "\n", + "def myCommand():\n", + " r = sr.Recognizer()\n", + "\n", + " with sr.Microphone() as source:\n", + " print('Okay Bro')\n", + " r.pause_treshold = 1\n", + " r.adjust_for_ambient_noise(source, duration = 1)\n", + " audio = r.listen(source)\t\n", + "\n", + " try:\n", + " command = r.recognize_google(audio).lower()\n", + " print('You said: '+ command + '\\n')\n", + "\n", + " except sr.UnknownValueError:\n", + " print('Your last instruction was unclear to me')\n", + " command = myCommand();\n", + "\n", + " return command \n", + "\n", + "\n", + "def get_bot_response(command):\n", + " #userText = request.args.get('msg')\n", + " response = str(english_bot.get_response(command))\n", + "\n", + " appendfile=os.listdir('saved_conversations')[-1]\n", + " appendfile= open('saved_conversations/'+str(filenumber),\"a\")\n", + " appendfile.write('user : '+command+'\\n')\n", + " appendfile.write('bot : '+response+'\\n')\n", + " appendfile.close()\n", + " return response\n", + "\n", + "\n", + "while True:\n", + " value=get_bot_response(myCommand())\n", + " print(value)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}