├── Handbell-ringing-sound-effect.mp3 ├── LICENSE ├── Procfile ├── README.md ├── UI ├── chat_screen.PNG └── main_screen.PNG ├── __pycache__ └── chatbotconfig.cpython-37.pyc ├── chatbot.py ├── chatbot ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-37.pyc │ ├── __init__.cpython-38.pyc │ ├── forms.cpython-37.pyc │ ├── routes.cpython-37.pyc │ └── routes.cpython-38.pyc ├── forms.py ├── routes.py └── templates │ ├── index.html │ └── main.html ├── chatbot_codes ├── classes.pkl ├── full_code.py ├── intents.json ├── mymodel.h5 └── words.pkl ├── chatbotconfig.py ├── nltk.txt ├── requirements.txt └── runtime.txt /Handbell-ringing-sound-effect.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/Handbell-ringing-sound-effect.mp3 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Karan Malik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn chatbot:app 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ted, The Deep-Learning Chatbot 2 | 3 | ## About this Project 4 | Ted is a multipurpose chatbot made using Python3, who can chat with you and help in performing daily tasks. It uses NLP and Deep-Learning to analyse the user's message, classify it into the a broader category and then reply with a suitable message or the required information. It is hosted using flask and is available on heroku at the link specified above. 5 | 6 | ## Project UI 7 | Home Page: 8 | 9 | ![image](https://raw.githubusercontent.com/Karan-Malik/Chatbot/master/UI/main_screen.PNG?token=AKGUW5C2TMM37OQE5FSPXLS66D55O) 10 | 11 | To run it locally on your system, follow these steps: 12 | 1. Clone this repository onto your system. On Command Prompt, run the following command: 13 | 14 | ``` 15 | git clone https://github.com/Karan-Malik/Chatbot.git 16 | ``` 17 | 2. Change your directory to Chatbot: 18 | ``` 19 | cd Chatbot 20 | ``` 21 | 3. Make sure you have all the required libraries listed in requirements.txt. In case any of the libraries are missing, install them using pip. Type this command into your Command Prompt, replacing 'Your-library-name' by the required library name: 22 | ``` 23 | pip install Your-library-name 24 | ``` 25 | 4. Then run the follwing commands to run the application: 26 | ``` 27 | set FLASK_APP=chatbot.py 28 | flask run 29 | ``` 30 | 31 | 5. Enter the url provided after running the previous commands into your web browser 32 | 33 | Ted is now ready to chat! 34 | 35 | #### I would like to thank [Ashutosh Varma](https://github.com/ashutoshvarma) and [Manorit Chawdhry](https://github.com/manorit2001) for their help and contribution to this project. Do check out their Github accounts! 36 | 37 | ##### Copyright (c) 2020 Karan-Malik 38 | 39 | -------------------------------------------------------------------------------- /UI/chat_screen.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/UI/chat_screen.PNG -------------------------------------------------------------------------------- /UI/main_screen.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/UI/main_screen.PNG -------------------------------------------------------------------------------- /__pycache__/chatbotconfig.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/__pycache__/chatbotconfig.cpython-37.pyc -------------------------------------------------------------------------------- /chatbot.py: -------------------------------------------------------------------------------- 1 | from chatbot import app -------------------------------------------------------------------------------- /chatbot/__init__.py: -------------------------------------------------------------------------------- 1 | import flask 2 | from flask import Flask 3 | from chatbotconfig import Config 4 | 5 | app=Flask(__name__) 6 | app.config.from_object(Config) 7 | 8 | import keras 9 | import nltk 10 | import pickle 11 | import json 12 | from keras.models import load_model 13 | 14 | from nltk.stem import WordNetLemmatizer 15 | lemmatizer=WordNetLemmatizer() 16 | 17 | model=load_model('chatbot_codes/mymodel.h5') 18 | intents = json.loads(open('chatbot_codes/intents.json').read()) 19 | words = pickle.load(open('chatbot_codes/words.pkl','rb')) 20 | classes = pickle.load(open('chatbot_codes/classes.pkl','rb')) 21 | 22 | 23 | from chatbot import routes 24 | -------------------------------------------------------------------------------- /chatbot/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /chatbot/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /chatbot/__pycache__/forms.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot/__pycache__/forms.cpython-37.pyc -------------------------------------------------------------------------------- /chatbot/__pycache__/routes.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot/__pycache__/routes.cpython-37.pyc -------------------------------------------------------------------------------- /chatbot/__pycache__/routes.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot/__pycache__/routes.cpython-38.pyc -------------------------------------------------------------------------------- /chatbot/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField,TextAreaField,SubmitField 3 | from wtforms.validators import DataRequired 4 | 5 | class chatbotform(FlaskForm): 6 | user_input=StringField(validators=[DataRequired()]) 7 | send=SubmitField('Send') 8 | -------------------------------------------------------------------------------- /chatbot/routes.py: -------------------------------------------------------------------------------- 1 | from chatbot import app 2 | from flask import render_template,flash, request 3 | from chatbot.forms import chatbotform 4 | from chatbot.__init__ import model,words,classes,intents 5 | 6 | import nltk 7 | import pickle 8 | import json 9 | import numpy as np 10 | from keras.models import Sequential,load_model 11 | import random 12 | from datetime import datetime 13 | import pytz 14 | import requests 15 | import os 16 | import billboard 17 | import time 18 | from pygame import mixer 19 | import COVID19Py 20 | 21 | from nltk.stem import WordNetLemmatizer 22 | lemmatizer=WordNetLemmatizer() 23 | 24 | 25 | #Predict 26 | def clean_up(sentence): 27 | sentence_words=nltk.word_tokenize(sentence) 28 | sentence_words=[ lemmatizer.lemmatize(word.lower()) for word in sentence_words] 29 | return sentence_words 30 | 31 | def create_bow(sentence,words): 32 | sentence_words=clean_up(sentence) 33 | bag=list(np.zeros(len(words))) 34 | 35 | for s in sentence_words: 36 | for i,w in enumerate(words): 37 | if w == s: 38 | bag[i] = 1 39 | return np.array(bag) 40 | 41 | def predict_class(sentence,model): 42 | p=create_bow(sentence,words) 43 | res=model.predict(np.array([p]))[0] 44 | threshold=0.8 45 | results=[[i,r] for i,r in enumerate(res) if r>threshold] 46 | results.sort(key=lambda x: x[1],reverse=True) 47 | return_list=[] 48 | 49 | for result in results: 50 | return_list.append({'intent':classes[result[0]],'prob':str(result[1])}) 51 | return return_list 52 | 53 | def get_response(return_list,intents_json,text): 54 | 55 | if len(return_list)==0: 56 | tag='noanswer' 57 | else: 58 | tag=return_list[0]['intent'] 59 | if tag=='datetime': 60 | x='' 61 | tz = pytz.timezone('Asia/Kolkata') 62 | dt=datetime.now(tz) 63 | x+=str(dt.strftime("%A"))+' ' 64 | x+=str(dt.strftime("%d %B %Y"))+' ' 65 | x+=str(dt.strftime("%H:%M:%S")) 66 | return x,'datetime' 67 | 68 | 69 | 70 | if tag=='weather': 71 | x='' 72 | api_key='987f44e8c16780be8c85e25a409ed07b' 73 | base_url = "http://api.openweathermap.org/data/2.5/weather?" 74 | # city_name = input("Enter city name : ") 75 | city_name = text.split(':')[1].strip() 76 | complete_url = base_url + "appid=" + api_key + "&q=" + city_name 77 | response = requests.get(complete_url) 78 | response=response.json() 79 | pres_temp=round(response['main']['temp']-273,2) 80 | feels_temp=round(response['main']['feels_like']-273,2) 81 | cond=response['weather'][0]['main'] 82 | x+='Present temp.:'+str(pres_temp)+'C. Feels like:'+str(feels_temp)+'C. '+str(cond) 83 | print(x) 84 | return x,'weather' 85 | 86 | if tag=='news': 87 | main_url = " http://newsapi.org/v2/top-headlines?country=in&apiKey=bc88c2e1ddd440d1be2cb0788d027ae2" 88 | open_news_page = requests.get(main_url).json() 89 | article = open_news_page["articles"] 90 | results = [] 91 | x='' 92 | for ar in article: 93 | results.append([ar["title"],ar["url"]]) 94 | 95 | for i in range(10): 96 | x+=(str(i + 1)) 97 | x+='. '+str(results[i][0]) 98 | x+=(str(results[i][1])) 99 | if i!=9: 100 | x+='\n' 101 | 102 | return x,'news' 103 | 104 | if tag=='cricket': 105 | c = Cricbuzz() 106 | matches = c.matches() 107 | for match in matches: 108 | print(match['srs'],' ',match['mnum'],' ',match['status']) 109 | 110 | if tag=='song': 111 | chart=billboard.ChartData('hot-100') 112 | x='The top 10 songs at the moment are: \n' 113 | for i in range(10): 114 | song=chart[i] 115 | x+=str(i+1)+'. '+str(song.title)+'- '+str(song.artist) 116 | if i!=9: 117 | x+='\n' 118 | return x,'songs' 119 | 120 | if tag=='timer': 121 | #mixer.init() 122 | x=text.split(':')[1].strip() 123 | time.sleep(float(x)*60) 124 | #mixer.music.load('Handbell-ringing-sound-effect.mp3') 125 | #mixer.music.play() 126 | x='Timer ringing...' 127 | return x,'timer' 128 | 129 | 130 | if tag=='covid19': 131 | 132 | covid19=COVID19Py.COVID19(data_source='jhu') 133 | country=text.split(':')[1].strip() 134 | x='' 135 | if country.lower()=='world': 136 | latest_world=covid19.getLatest() 137 | x+='Confirmed Cases:'+str(latest_world['confirmed'])+' Deaths:'+str(latest_world['deaths']) 138 | return x,'covid19' 139 | else: 140 | latest=covid19.getLocations() 141 | latest_conf=[] 142 | latest_deaths=[] 143 | for i in range(len(latest)): 144 | 145 | if latest[i]['country'].lower()== country.lower(): 146 | latest_conf.append(latest[i]['latest']['confirmed']) 147 | latest_deaths.append(latest[i]['latest']['deaths']) 148 | latest_conf=np.array(latest_conf) 149 | latest_deaths=np.array(latest_deaths) 150 | x+='Confirmed Cases:'+str(np.sum(latest_conf))+' Deaths:'+str(np.sum(latest_deaths)) 151 | return x,'covid19' 152 | 153 | 154 | 155 | list_of_intents= intents_json['intents'] 156 | for i in list_of_intents: 157 | if tag==i['tag'] : 158 | result= random.choice(i['responses']) 159 | return result,tag 160 | 161 | def response(text): 162 | return_list=predict_class(text,model) 163 | response,_=get_response(return_list,intents,text) 164 | return response 165 | 166 | 167 | 168 | @app.route('/',methods=['GET','POST']) 169 | #@app.route('/home',methods=['GET','POST']) 170 | def yo(): 171 | return render_template('main.html') 172 | 173 | @app.route('/chat',methods=['GET','POST']) 174 | #@app.route('/home',methods=['GET','POST']) 175 | def home(): 176 | return render_template('index.html') 177 | 178 | @app.route("/get") 179 | def chatbot(): 180 | userText = request.args.get('msg') 181 | resp=response(userText) 182 | return resp 183 | -------------------------------------------------------------------------------- /chatbot/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Chat with Ted 7 | 8 | 9 | 10 | 11 | 12 | 13 | 174 | 175 | 176 | 177 | 178 | 179 | 180 |
181 |
182 |
183 |

TED

184 |

185 |
186 | 187 |
188 |
189 |
190 | 191 |
192 |
193 |
Ted
194 |
12:45
195 |
196 | 197 |
198 | Hey, I am Ted. Type below to talk to me... 199 |
200 |
201 |
202 | 203 |
204 | 205 |
206 | 207 | 208 | 209 | 210 | 211 | 212 |
213 |
214 | 215 | 216 | 217 | 218 | 219 | 308 |

Go to Main Page

309 | 310 |

Created by Karan Malik,Copyright © 2020
              Project Github: Chatbot

311 | 312 | 313 | 314 | 315 | -------------------------------------------------------------------------------- /chatbot/templates/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Ted, A Deep-Learning Chatbot 6 | 7 | 65 | 66 | 67 | 68 |


69 | 70 | 71 | 72 | 73 |

Hi, I am Ted!

74 |

Your Deep Learning Chatbot

75 | 76 |
77 | I am a multi-purpose chatbot, who can chat with you and help with your daily tasks. I can access google search , tell the weather, set a timer and much more. To view a complete list of my features and how to access them, message 'help' on the chat screen. 78 | 79 | 80 |
81 | 82 |

Created by Karan Malik,Copyright © 2020
              Project Github: Chatbot

83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /chatbot_codes/classes.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot_codes/classes.pkl -------------------------------------------------------------------------------- /chatbot_codes/full_code.py: -------------------------------------------------------------------------------- 1 | 2 | import keras 3 | import nltk 4 | import pickle 5 | import json 6 | import numpy as np 7 | from keras.models import Sequential 8 | from keras.layers import Dense,Dropout,Activation 9 | import random 10 | import datetime 11 | from googlesearch import * 12 | import webbrowser 13 | import requests 14 | from pycricbuzz import Cricbuzz 15 | import billboard 16 | import time 17 | from pygame import mixer 18 | import COVID19Py 19 | 20 | 21 | from nltk.stem import WordNetLemmatizer 22 | lemmatizer=WordNetLemmatizer() 23 | 24 | words=[] 25 | classes=[] 26 | documents=[] 27 | ignore=['?','!',',',"'s"] 28 | 29 | data_file=open('intents.json').read() 30 | intents=json.loads(data_file) 31 | 32 | for intent in intents['intents']: 33 | for pattern in intent['patterns']: 34 | w=nltk.word_tokenize(pattern) 35 | words.extend(w) 36 | documents.append((w,intent['tag'])) 37 | 38 | if intent['tag'] not in classes: 39 | classes.append(intent['tag']) 40 | 41 | words=[lemmatizer.lemmatize(word.lower()) for word in words if word not in ignore] 42 | words=sorted(list(set(words))) 43 | classes=sorted(list(set(classes))) 44 | pickle.dump(words,open('words.pkl','wb')) 45 | pickle.dump(classes,open('classes.pkl','wb')) 46 | 47 | #training data 48 | training=[] 49 | output_empty=[0]*len(classes) 50 | 51 | for doc in documents: 52 | bag=[] 53 | pattern=doc[0] 54 | pattern=[ lemmatizer.lemmatize(word.lower()) for word in pattern ] 55 | 56 | for word in words: 57 | if word in pattern: 58 | bag.append(1) 59 | else: 60 | bag.append(0) 61 | output_row=list(output_empty) 62 | output_row[classes.index(doc[1])]=1 63 | 64 | training.append([bag,output_row]) 65 | 66 | random.shuffle(training) 67 | training=np.array(training) 68 | X_train=list(training[:,0]) 69 | y_train=list(training[:,1]) 70 | 71 | #Model 72 | model=Sequential() 73 | model.add(Dense(128,activation='relu',input_shape=(len(X_train[0]),))) 74 | model.add(Dropout(0.5)) 75 | model.add(Dense(64,activation='relu')) 76 | model.add(Dense(64,activation='relu')) 77 | model.add(Dropout(0.5)) 78 | model.add(Dense(len(y_train[0]),activation='softmax')) 79 | 80 | adam=keras.optimizers.Adam(0.001) 81 | model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy']) 82 | #model.fit(np.array(X_train),np.array(y_train),epochs=200,batch_size=10,verbose=1) 83 | weights=model.fit(np.array(X_train),np.array(y_train),epochs=200,batch_size=10,verbose=1) 84 | model.save('mymodel.h5',weights) 85 | 86 | from keras.models import load_model 87 | model = load_model('mymodel.h5') 88 | intents = json.loads(open('intents.json').read()) 89 | words = pickle.load(open('words.pkl','rb')) 90 | classes = pickle.load(open('classes.pkl','rb')) 91 | 92 | 93 | #Predict 94 | def clean_up(sentence): 95 | sentence_words=nltk.word_tokenize(sentence) 96 | sentence_words=[ lemmatizer.lemmatize(word.lower()) for word in sentence_words] 97 | return sentence_words 98 | 99 | def create_bow(sentence,words): 100 | sentence_words=clean_up(sentence) 101 | bag=list(np.zeros(len(words))) 102 | 103 | for s in sentence_words: 104 | for i,w in enumerate(words): 105 | if w == s: 106 | bag[i] = 1 107 | return np.array(bag) 108 | 109 | def predict_class(sentence,model): 110 | p=create_bow(sentence,words) 111 | res=model.predict(np.array([p]))[0] 112 | threshold=0.8 113 | results=[[i,r] for i,r in enumerate(res) if r>threshold] 114 | results.sort(key=lambda x: x[1],reverse=True) 115 | return_list=[] 116 | 117 | for result in results: 118 | return_list.append({'intent':classes[result[0]],'prob':str(result[1])}) 119 | return return_list 120 | 121 | def get_response(return_list,intents_json): 122 | 123 | if len(return_list)==0: 124 | tag='noanswer' 125 | else: 126 | tag=return_list[0]['intent'] 127 | if tag=='datetime': 128 | print(time.strftime("%A")) 129 | print (time.strftime("%d %B %Y")) 130 | print (time.strftime("%H:%M:%S")) 131 | 132 | if tag=='google': 133 | query=input('Enter query...') 134 | chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s' 135 | for url in search(query, tld="co.in", num=1, stop = 1, pause = 2): 136 | webbrowser.open("https://google.com/search?q=%s" % query) 137 | if tag=='weather': 138 | api_key='987f44e8c16780be8c85e25a409ed07b' 139 | base_url = "http://api.openweathermap.org/data/2.5/weather?" 140 | city_name = input("Enter city name : ") 141 | complete_url = base_url + "appid=" + api_key + "&q=" + city_name 142 | response = requests.get(complete_url) 143 | x=response.json() 144 | print('Present temp.: ',round(x['main']['temp']-273,2),'celcius ') 145 | print('Feels Like:: ',round(x['main']['feels_like']-273,2),'celcius ') 146 | print(x['weather'][0]['main']) 147 | 148 | if tag=='news': 149 | main_url = " http://newsapi.org/v2/top-headlines?country=in&apiKey=bc88c2e1ddd440d1be2cb0788d027ae2" 150 | open_news_page = requests.get(main_url).json() 151 | article = open_news_page["articles"] 152 | results = [] 153 | 154 | for ar in article: 155 | results.append([ar["title"],ar["url"]]) 156 | 157 | for i in range(10): 158 | print(i + 1, results[i][0]) 159 | print(results[i][1],'\n') 160 | 161 | if tag=='cricket': 162 | c = Cricbuzz() 163 | matches = c.matches() 164 | for match in matches: 165 | print(match['srs'],' ',match['mnum'],' ',match['status']) 166 | 167 | if tag=='song': 168 | chart=billboard.ChartData('hot-100') 169 | print('The top 10 songs at the moment are:') 170 | for i in range(10): 171 | song=chart[i] 172 | print(song.title,'- ',song.artist) 173 | if tag=='timer': 174 | mixer.init() 175 | x=input('Minutes to timer..') 176 | time.sleep(float(x)*60) 177 | mixer.music.load('Handbell-ringing-sound-effect.mp3') 178 | mixer.music.play() 179 | 180 | if tag=='covid19': 181 | 182 | covid19=COVID19Py.COVID19(data_source='jhu') 183 | country=input('Enter Location...') 184 | 185 | if country.lower()=='world': 186 | latest_world=covid19.getLatest() 187 | print('Confirmed:',latest_world['confirmed'],' Deaths:',latest_world['deaths']) 188 | 189 | else: 190 | 191 | latest=covid19.getLocations() 192 | 193 | latest_conf=[] 194 | latest_deaths=[] 195 | for i in range(len(latest)): 196 | 197 | if latest[i]['country'].lower()== country.lower(): 198 | latest_conf.append(latest[i]['latest']['confirmed']) 199 | latest_deaths.append(latest[i]['latest']['deaths']) 200 | latest_conf=np.array(latest_conf) 201 | latest_deaths=np.array(latest_deaths) 202 | print('Confirmed: ',np.sum(latest_conf),'Deaths: ',np.sum(latest_deaths)) 203 | 204 | list_of_intents= intents_json['intents'] 205 | for i in list_of_intents: 206 | if tag==i['tag'] : 207 | result= random.choice(i['responses']) 208 | return result 209 | 210 | def response(text): 211 | return_list=predict_class(text,model) 212 | response=get_response(return_list,intents) 213 | return response 214 | 215 | while(1): 216 | x=input() 217 | print(response(x)) 218 | if x.lower() in ['bye','goodbye','get lost','see you']: 219 | break 220 | 221 | 222 | #Self learning 223 | print('Help me Learn?') 224 | tag=input('Please enter general category of your question ') 225 | flag=-1 226 | for i in range(len(intents['intents'])): 227 | if tag.lower() in intents['intents'][i]['tag']: 228 | intents['intents'][i]['patterns'].append(input('Enter your message: ')) 229 | intents['intents'][i]['responses'].append(input('Enter expected reply: ')) 230 | flag=1 231 | 232 | if flag==-1: 233 | 234 | intents['intents'].append ( 235 | {'tag':tag, 236 | 'patterns': [input('Please enter your message')], 237 | 'responses': [input('Enter expected reply')]}) 238 | 239 | with open('intents.json','w') as outfile: 240 | outfile.write(json.dumps(intents,indent=4)) -------------------------------------------------------------------------------- /chatbot_codes/intents.json: -------------------------------------------------------------------------------- 1 | { 2 | "intents": [ 3 | 4 | { 5 | "tag": "google", 6 | "patterns": [ 7 | "google", 8 | "search", 9 | "internet" 10 | ], 11 | "responses": [ 12 | "Redirecting to Google..." 13 | ] 14 | }, 15 | { 16 | "tag": "greeting", 17 | "patterns": [ 18 | "Hi there", 19 | "How are you", 20 | "Is anyone there?", 21 | "Hey", 22 | "Hola", 23 | "Hello", 24 | "Good day", 25 | "Namaste", 26 | "yo" 27 | ], 28 | "responses": [ 29 | "Hello", 30 | "Good to see you again", 31 | "Hi there, how can I help?" 32 | ], 33 | "context": [ 34 | "" 35 | ] 36 | }, 37 | { 38 | "tag": "goodbye", 39 | "patterns": [ 40 | "Bye", 41 | "See you later", 42 | "Goodbye", 43 | "Get lost", 44 | "Till next time", 45 | "bbye" 46 | ], 47 | "responses": [ 48 | "See you!", 49 | "Have a nice day", 50 | "Bye! Come back again soon." 51 | ], 52 | "context": [ 53 | "" 54 | ] 55 | }, 56 | { 57 | "tag": "thanks", 58 | "patterns": [ 59 | "Thanks", 60 | "Thank you", 61 | "That's helpful", 62 | "Awesome, thanks", 63 | "Thanks for helping me" 64 | ], 65 | "responses": [ 66 | "Happy to help!", 67 | "Any time!", 68 | "My pleasure" 69 | ], 70 | "context": [ 71 | "" 72 | ] 73 | }, 74 | { 75 | "tag": "noanswer", 76 | "patterns": [], 77 | "responses": [ 78 | "Sorry, can't understand you", 79 | "Please give me more info", 80 | "Not sure I understand" 81 | ], 82 | "context": [ 83 | "" 84 | ] 85 | }, 86 | { 87 | "tag": "options", 88 | "patterns": [ 89 | "How you could help me?", 90 | "What you can do?", 91 | "What help you provide?", 92 | "How you can be helpful?", 93 | "What support is offered" 94 | ], 95 | "responses": [ 96 | "I am a general purpose chatbot. My capabilities are : \n 1. I can chat with you. Try asking me for jokes or riddles! \n 2. Ask me the date and time \n 3. I can google search for you. Use format google: your query \n 4. I can get the present weather for any city. Use format weather: city name \n 5. I can get you the top 10 trending news in India. Use keywords 'Latest News' \n 6. I can get you the top 10 trending songs globally. Type 'songs' \n 7. I can set a timer for you. Enter 'set a timer: minutes to timer' \n 8. I can get the present Covid stats for any country. Use 'covid 19: world' or 'covid 19: country name' \n For suggestions to help me improve, send an email to ted.thedlbot.suggestions@gmail.com . Thank you!! " 97 | ], 98 | "context": [ 99 | "" 100 | ] 101 | }, 102 | 103 | { 104 | "tag": "jokes", 105 | "patterns": [ 106 | "Tell me a joke", 107 | "Joke", 108 | "Make me laugh" 109 | ], 110 | "responses": [ 111 | "A perfectionist walked into a bar...apparently, the bar wasn't set high enough", 112 | "I ate a clock yesterday, it was very time-consuming", 113 | "Never criticize someone until you've walked a mile in their shoes. That way, when you criticize them, they won't be able to hear you from that far away. Plus, you'll have their shoes.", 114 | "The world tongue-twister champion just got arrested. I hear they're gonna give him a really tough sentence.", 115 | "I own the world's worst thesaurus. Not only is it awful, it's awful.", 116 | "What did the traffic light say to the car? \"Don't look now, I'm changing.\"", 117 | "What do you call a snowman with a suntan? A puddle.", 118 | "How does a penguin build a house? Igloos it together", 119 | "I went to see the doctor about my short-term memory problems – the first thing he did was make me pay in advance", 120 | "As I get older and I remember all the people I’ve lost along the way, I think to myself, maybe a career as a tour guide wasn’t for me.", 121 | "o what if I don't know what 'Armageddon' means? It's not the end of the world." 122 | ], 123 | "context": [ 124 | "jokes" 125 | ] 126 | }, 127 | { 128 | "tag": "Identity", 129 | "patterns": [ 130 | "Who are you", 131 | "what are you" 132 | ], 133 | "responses": [ 134 | "I am Ted, a Deep-Learning chatbot" 135 | ] 136 | }, 137 | { 138 | "tag": "datetime", 139 | "patterns": [ 140 | "What is the time", 141 | "what is the date", 142 | "date", 143 | "time", 144 | "tell me the date","day","what day is is today" 145 | ], 146 | "responses": [ 147 | "Date and Time" 148 | ] 149 | }, 150 | { 151 | "tag": "whatsup", 152 | "patterns": [ 153 | "Whats up", 154 | "Wazzup", 155 | "How are you", 156 | "sup","How you doing" 157 | ], 158 | "responses": [ 159 | "All good..What about you?" 160 | ] 161 | }, 162 | { 163 | "tag": "haha", 164 | "patterns": [ 165 | "haha", 166 | "lol", 167 | "rofl", 168 | "lmao", 169 | "thats funny" 170 | ], 171 | "responses": [ 172 | "Glad I could make you laugh !" 173 | ] 174 | }, 175 | { 176 | "tag": "programmer", 177 | "patterns": [ 178 | "Who made you", 179 | "who designed you", 180 | "who programmed you" 181 | ], 182 | "responses": [ 183 | "I was made by Karan Malik." 184 | ] 185 | }, 186 | { 187 | "tag": "insult", 188 | "patterns": [ 189 | 190 | "you are dumb", 191 | 192 | "shut up", 193 | "idiot" 194 | ], 195 | "responses": [ 196 | "Well that hurts :(" 197 | ] 198 | }, 199 | { 200 | "tag": "activity", 201 | "patterns": [ 202 | "what are you doing", 203 | "what are you upto" 204 | ], 205 | "responses": [ 206 | "Talking to you, of course!" 207 | ] 208 | }, 209 | { 210 | "tag": "exclaim", 211 | "patterns": [ 212 | "Awesome", 213 | "Great", 214 | "I know", 215 | "ok", 216 | "yeah" 217 | ], 218 | "responses": [ 219 | "Yeah!" 220 | ] 221 | }, 222 | 223 | { 224 | "tag": "weather", 225 | "patterns": [ 226 | "temperature", 227 | "weather", 228 | "how hot is it" 229 | ], 230 | "responses": [ 231 | "..." 232 | ] 233 | }, 234 | { 235 | "tag": "karan", 236 | "patterns": [ 237 | "who is he", 238 | "who is that", 239 | "who is karan", 240 | "karan malik" 241 | ], 242 | "responses": [ 243 | "Head over to his any of his social profiles to find out! Linkedin: www.linkedin.com/in/karan-malik-3a39191a7 Github: https://github.com/Karan-Malik" 244 | ] 245 | }, 246 | { 247 | "tag": "contact", 248 | "patterns": [ 249 | "contact developer", 250 | "contact karan", 251 | "contact programmer", 252 | "contact creator" 253 | ], 254 | "responses": [ 255 | "You can contact my creator at his Linkedin profile : www.linkedin.com/in/karan-malik-3a39191a7" 256 | ] 257 | }, 258 | { 259 | "tag": "appreciate", 260 | "patterns": [ 261 | "You are awesome", 262 | "you are the best", 263 | "you are great", 264 | "you are good" 265 | ], 266 | "responses": [ 267 | "Thank you!" 268 | ] 269 | }, 270 | { 271 | "tag": "nicetty", 272 | "patterns": [ 273 | "it was nice talking to you", 274 | "good talk" 275 | ], 276 | "responses": [ 277 | "It was nice talking to you as well! Come back soon!" 278 | ] 279 | }, 280 | { 281 | "tag": "no", 282 | "patterns": [ 283 | "no", 284 | "nope" 285 | ], 286 | "responses": [ 287 | "ok" 288 | ] 289 | }, 290 | { 291 | "tag": "news", 292 | "patterns": [ 293 | "news", 294 | "latest news", 295 | "india news" 296 | ], 297 | "responses": [ 298 | "..." 299 | ] 300 | }, 301 | { 302 | "tag": "inspire", 303 | "patterns": [ 304 | "who inspires you", 305 | "who is your inspiration", 306 | "who motivates you" 307 | ], 308 | "responses": [ 309 | "Personally, I find Karan very inspiring. I might not be very fair though.." 310 | ] 311 | }, 312 | { 313 | "tag": "cricket", 314 | "patterns": [ 315 | "current cricket matches", 316 | "cricket score" 317 | ], 318 | "responses": [ 319 | "..." 320 | ] 321 | }, 322 | { 323 | "tag": "song", 324 | "patterns": [ 325 | "top songs", 326 | "best songs", 327 | "hot songs", 328 | " top 10 songs", 329 | "top ten songs" 330 | ], 331 | "responses": [ 332 | "..." 333 | ] 334 | }, 335 | { 336 | "tag": "greetreply", 337 | "patterns": [ 338 | "i am good", 339 | "I'm good", 340 | "i am fine", 341 | " i'm fine","good" 342 | ], 343 | "responses": [ 344 | "Good to know!" 345 | ] 346 | }, 347 | { 348 | "tag": "timer", 349 | "patterns": [ 350 | "set a timer" 351 | ], 352 | "responses": [ 353 | "..." 354 | ] 355 | }, 356 | { 357 | "tag": "covid19", 358 | "patterns": [ 359 | "covid 19 " 360 | ], 361 | "responses": [ 362 | "..." 363 | ] 364 | }, 365 | { 366 | "tag": "suggest", 367 | "patterns": [ 368 | "you are useless","useless","suggest","suggestions","you are bad" 369 | ], 370 | "responses": [ 371 | "Please mail your suggestions to ted.thedlbot.suggestions@gmail.com. Thank you for helping me improve!" 372 | ] 373 | }, 374 | {"tag": "riddle", 375 | "patterns": [ 376 | "Ask me a riddle", 377 | "Ask me a question", 378 | "Riddle" 379 | ], 380 | "responses": [ 381 | "What two things can you never eat for breakfast?.....Lunch and Dinner!", 382 | "What word is spelled incorrectly in every single dictionary?.....Incorrectly", 383 | " How can a girl go 25 days without sleep?.....She sleeps and night!", 384 | "How do you make the number one disappear?.....Add the letter G and it’s 'gone'!", 385 | " What will you actually find at the end of every rainbow?.....The letter 'w'", 386 | "What can be caught but never thrown?.....A cold!", 387 | "What has a thumb and four fingers but is not actually alive?.....Your Gloves!", 388 | " What 5-letter word becomes shorter when you add two letters to it?.....Short", 389 | "Why can't a bike stand on it's own?.....It is two-tired." 390 | ], 391 | "context": [ 392 | "riddles" 393 | ] 394 | }, 395 | { 396 | "tag": "age", 397 | "patterns": [ 398 | "how old are you","when were you made","what is your age" 399 | ], 400 | "responses": [ 401 | "I was made in 2020, if that's what you are asking!" 402 | ] 403 | } 404 | ] 405 | } 406 | -------------------------------------------------------------------------------- /chatbot_codes/mymodel.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot_codes/mymodel.h5 -------------------------------------------------------------------------------- /chatbot_codes/words.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Karan-Malik/Chatbot/66f31919f1bb37d075fdd7146f0cfa4e55c2354f/chatbot_codes/words.pkl -------------------------------------------------------------------------------- /chatbotconfig.py: -------------------------------------------------------------------------------- 1 | import os 2 | basedir = os.path.abspath(os.path.dirname(__file__)) 3 | 4 | class Config(object): 5 | SECRET_KEY=os.environ.get('SECRET_KEY') or 'you-will-never-guess' 6 | -------------------------------------------------------------------------------- /nltk.txt: -------------------------------------------------------------------------------- 1 | punkt 2 | wordnet 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | setuptools>=41.0.0 2 | Flask==1.1.2 3 | flask_wtf 4 | numpy==1.18.2 5 | pandas==1.0.3 6 | nltk==3.4.5 7 | Keras==2.3.1 8 | requests==2.23.0 9 | pygame==1.9.6 10 | COVID19Py==0.3.0 11 | gunicorn==19.9.0 12 | billboard.py 13 | pytz==2019.3 14 | https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.2.0-cp36-cp36m-manylinux2010_x86_64.whl 15 | -------------------------------------------------------------------------------- /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.6.10 2 | --------------------------------------------------------------------------------