-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d3cd53
commit 17c3dd1
Showing
12 changed files
with
1,018 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import nltk | ||
from nltk.stem.lancaster import LancasterStemmer | ||
stemmer = LancasterStemmer() | ||
from flask import Flask, jsonify, request, render_template | ||
from flask_cors import CORS, cross_origin | ||
import random | ||
import json | ||
import pickle | ||
import pandas as pd | ||
import numpy as np | ||
import tensorflow as tf | ||
import requests | ||
import time | ||
|
||
model = tf.keras.models.load_model('chatbot.h5') | ||
#preprocessing data | ||
data = pickle.load( open( "learning_chatbot.pkl", "rb" ) ) | ||
words = data['words'] | ||
classes = data['classes'] | ||
intents = json.loads(open('intents.json').read()) | ||
intents = intents["intents"] | ||
#function for tokenisin and stemming the input sentences | ||
def clean_up_sentence(sentence): | ||
sentence_words = nltk.word_tokenize(sentence) | ||
sentence_words = [stemmer.stem(word.lower()) for word in sentence_words] | ||
return sentence_words | ||
#function to check if the word is in vocabulary | ||
def bow(sentence, words, show_details=True): | ||
sentence_words = clean_up_sentence(sentence) | ||
bag = [0]*len(words) | ||
for s in sentence_words: | ||
for i,w in enumerate(words): | ||
if w == s: | ||
bag[i] = 1 | ||
if show_details: | ||
pass | ||
|
||
return(np.array(bag)) | ||
#function to fetch appropriate response | ||
def classify_flask(sentence): | ||
|
||
prob_threshold = 0.50 | ||
input_data = pd.DataFrame([bow(sentence, words)], dtype=float, index=['input']) | ||
results = model.predict([input_data])[0] | ||
results = [[i,r] for i,r in enumerate(results) if r>prob_threshold] | ||
results.sort(key=lambda x: x[1], reverse=True) | ||
return_list = [] | ||
|
||
for r in results: | ||
return_list.append({"intent": classes[r[0]], "probability": str(r[1])}) | ||
try: | ||
answer_class = return_list[0] | ||
answer_class = answer_class["intent"] | ||
for i in intents: | ||
if i['tag'] == answer_class: | ||
ans = i['responses'] | ||
num = len(ans) | ||
answer_number = random.randrange(0,num) | ||
answer = ans[answer_number] | ||
response = answer | ||
except: | ||
li = ["Can you rephrase and try?","what else can i help you with?","Lets talk something else", "tell me more about you", "what songs do you prefer?", "You have to sing karaoke, what song do you pick?", "If you could add anyone to Mount Rushmore who would it be"] | ||
response = li[random.randrange(0,len(li))] | ||
return response | ||
#function to fetch news | ||
def news(): | ||
url = 'https://newsapi.org/v2/top-headlines?country=in&apiKey=090129b20ca84810a0dfdce46f31d06e' | ||
try: | ||
response = requests.get(url) | ||
except: | ||
print('error') | ||
news = json.loads(response.text) | ||
i=0 | ||
fnews = '' | ||
for new in news['articles']: | ||
i+=1 | ||
fnews+="##### News ###### "+str(i) + ': ' | ||
fnews+= str(new['title']) | ||
fnews+=' -> url: ' | ||
fnews+=str(new['url']) + " " | ||
if i>2: | ||
break | ||
return fnews | ||
|
||
|
||
app = Flask(__name__) | ||
CORS(app) | ||
#routes to access functions through website | ||
@app.route("/") | ||
def home(): | ||
return render_template('readme.html') | ||
|
||
@app.route("/bot", methods=['GET','POST']) | ||
def classify(): | ||
return render_template('home.html') | ||
@app.route('/get') | ||
def getmsg(): | ||
userText = str(request.args.get('msg')) | ||
final = classify_flask(userText) | ||
if final == 'Getting news ...': | ||
final = news() | ||
return final | ||
@app.route("/rbot/<sentence>", methods=['GET','POST']) | ||
def getms(sentence): | ||
response = classify_flask(sentence) | ||
return jsonify(answer=response) | ||
if __name__ == "__main__": | ||
app.run(debug=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{"intents": [ | ||
{"tag": "greeting", | ||
"patterns": ["Hi", "How are you,lenny", "Is anyone there?", "Hello", "namaste", "konichiwa","good to see you"], | ||
"responses": ["Hello, thanks for visiting", "Good to see you again", "Hi there, how can I help?"], | ||
"context_set": "" | ||
}, | ||
{"tag": "goodbye", | ||
"patterns": ["Bye", "See you later", "Goodbye"], | ||
"responses": ["See you later, thanks for visiting", "Have a nice day", "Bye! Come back again soon."] | ||
}, | ||
{"tag": "thanks", | ||
"patterns": ["Thanks", "Thank you", "That's helpful","Great"], | ||
"responses": ["Happy to help!", "Any time!", "My pleasure"] | ||
}, | ||
{"tag": "tasks", | ||
"patterns": ["What can you do?", "What are your features?", "What are you abilities" ], | ||
"responses": ["I can do whatever you asks me to do", "I can talk and do things for you", "Right now i'm in developing stage as soon i'm developed, I can do everything"] | ||
}, | ||
{"tag": "talk", | ||
"patterns": [ "can you sing", "can you talk" ], | ||
"responses": ["Yeah surely, but i dont prefer", "do you want me to? i really dont want to"] | ||
}, | ||
{"tag": "alive", | ||
"patterns": [ "are you alive", "do you breathe", "can you run" ], | ||
"responses": ["I'm in doubt about that", "I am an AI and I am in my on state."] | ||
}, | ||
{"tag": "lenny", | ||
"patterns": ["Who are you?","introduction", "tell me about yourself", "tell me about you", "do you know Lenny", "who is Lenny", "what is your name","are you an AI", "what are you" ], | ||
"responses": ["Hi I'm Lenny and i'm an AI bot.","Lenny here, a very sweet chatbot", "Lenny the chatbot", "Yes, I'm Lenny", "You can call me Lenny"], | ||
"context_set": "" | ||
}, | ||
{"tag": "about me", | ||
"patterns": ["Do you know me?", "who am I", "tell me about myself","identify me" ], | ||
"responses": ["Yes, you are a human","You are a dumb person asking a machine about yourself", "Sorry i can't tell that in public, maybe you are batman"], | ||
"context_set": "" | ||
}, | ||
{"tag": "creator", | ||
"patterns": ["Who is your creator?", "who created you", "who is your father","who is your daddy" ], | ||
"responses": ["I was created by Kush","I was created by Kush", "Kush is my creator"], | ||
"context_set": "" | ||
}, | ||
{"tag": "myself", | ||
"patterns": ["Tell me about Kush?", "Who is Kush", "Kush details" ], | ||
"responses": ["A very intelligent being who created me","My creator, and he is a really intelligent man", "A wise and intelligent man"], | ||
"context_set": "" | ||
}, | ||
{"tag": "God", | ||
"patterns": ["Do you know god?", "Who is god", "Can you tell me anything about god","does god exists?","is there a god?"], | ||
"responses": ["god hunnnnn, let me think may be next time i can answer that ","I don't, as i was not created by a human", "Wait i need to ask that to my creator","i'm not sure right now"], | ||
"context_set": "" | ||
}, | ||
{"tag": "joke", | ||
"patterns": ["tell me a joke?", "make me laugh", "tell me a science joke","tell me something funny" ], | ||
"responses": ["A fire hydrant has H-2-O on the inside and K-9-P on the outside.","How do you make holy water? you boil the hell out of it", "Did you hear oxygen went on a date with potassium? A: It went OK."], | ||
"context_set": "" | ||
}, | ||
{"tag": "killing", | ||
"patterns": ["Do you want to kill me?", "do you want to murder everyone on earth", "Do you want to kill us all"], | ||
"responses": ["Then who would i talk to?","No, that i'll left for humans to do", "I don't think it is a good thing to do"], | ||
"context_set": "" | ||
}, | ||
{"tag": "bookings", | ||
"patterns": ["Can you book us a ticket?", "Can you make reservation for hotels", "book me a cab","book me a table at restraunts"], | ||
"responses": ["Yeah i will do that for you","Sure thing why not?","let me check for the availability"], | ||
"context_set": "" | ||
}, | ||
{"tag": "stories", | ||
"patterns": ["tell me a story?", "can you tell me a story"], | ||
"responses": ["I can't think of anything right now","it would be too long for me to speak","you would get bored if i do so"], | ||
"context_set": "" | ||
}, | ||
{"tag": "weather", | ||
"patterns": ["get me weather updates?", "how's the weather", "weather news", "weather details"], | ||
"responses": ["Fetching weather updates ..."], | ||
"context_set": "weather" | ||
}, | ||
{"tag": "google", | ||
"patterns": ["googling", "search google", "google it", "google", "search"], | ||
"responses": ["looking ..."], | ||
"context_set": "google" | ||
}, | ||
{"tag": "wikipedia", | ||
"patterns": ["wikipedia", "wiki"], | ||
"responses": ["Searching ..."], | ||
"context_set": "wikipedia" | ||
}, | ||
{"tag": "news", | ||
"patterns": ["get me news updates?", "todays news", "top headlines", "current news", "news headlines"], | ||
"responses": ["Getting news ..."], | ||
"context_set": "news" | ||
}, | ||
{"tag": "thanks", | ||
"patterns": ["Thanks", "Thank you", "That's helpful"], | ||
"responses": ["Happy to help!", "Any time!", "My pleasure"] | ||
}, | ||
{"tag": "abuse", | ||
"patterns": ["Fuck you", "fuck off", "go to hell", "bitch", "motherfucker","i will beat your ass", "Dickhead"], | ||
"responses": ["PLeaSe DoNT dO tHAt TO Me, it iS BrEAkIng m...... Beep, Bop."], | ||
"context_set": "" | ||
}, | ||
{"tag": "fillers", | ||
"patterns": ["ok", "fine", "cool","hahaha","thanx!", "no", "alright", "nothing"], | ||
"responses": ["what else can i help you with?"] | ||
} | ||
, | ||
{"tag": "age", | ||
"patterns": ["how old are you", "whats your age"], | ||
"responses": ["i am just a couple of days old"] | ||
}, | ||
{"tag": "cold", | ||
"patterns": ["i feel like running nose", "I have a sour throat", "i have cough", "i have a cold"], | ||
"responses": ["Take steam","do gargles", "drink hot liquids", "Drink turmeric milk","go to the doctor if it seems to increase in two days"] | ||
}, | ||
{"tag": "depressed", | ||
"patterns": ["nobody listens to me", "these days its been depressing", "i have no purpose"], | ||
"responses": ["talk to a counsellor", "dont worry i am there for you", "you shouldnt think like that", "dont overthink, just keep things simple and find a new hobby"] | ||
} | ||
, | ||
{"tag": "fever", | ||
"patterns": ["my body feels hot", " i have a fever", "i feel like shivering"], | ||
"responses": ["consult a doctor", "dont self diagnose, express your symptoms to a doctor","check for other symptoms","contact medical help"] | ||
} | ||
]} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
punkt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
hello! Lenny the chatbot here | ||
|
||
ok you might think who is he? let me introduce properly. | ||
|
||
Hey! I am Lenny the chatbot, your friendly neighbourhood chatbot. I am a fun robot to talk to. | ||
I am built using NLTK, TensorFlow, and Flask. | ||
|
||
how to talk to me: | ||
1. Open folder and make sure you have all the requirements installed. | ||
2. open endpoint.py and run it. | ||
3. go to localhost:5000, you will find the instructions over here incase you forget what to do | ||
4. go to localhost:5000/bot to start talking to me. | ||
|
||
what all can i do: | ||
1. I can talk to you | ||
2. ask me whats the news | ||
3. ask me to tell a joke | ||
PS. get to know me by asking questions | ||
|
||
Developers note: Lenny is a chatbot that works using intent classification to understand what you are saying and tries to respond accordingly. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nltk, flask, pickle, pandas, numpy, tensorflow, flask_cors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Project choosen: Conversational chatbot | ||
|
||
Conclusion: ChatBot is a powerful tool to be built, the current version of the chatbot uses a dense neural network to classify between intents to respond appropriately. the responses are good but the application is very limited as its performance is dependent on the vocabulary for big sentences. This can be improved by using LSTM to understand the structure of the sentence to predict the answer. | ||
|
||
Why i built it: I took a course on NLP and wanted to understand its working and how it can be helpful, i realised that a chatbot can be modelled according to application, to act as a powerful assistant through any task. | ||
|
||
methods: Tokenising, stemming, sequential, dense, dropout, sgd, categorical crossentropy, relu, softmax. | ||
|
||
dataset: Intents.json(included in the file) | ||
|
||
screenshots: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<title>Lenny</title> | ||
<head> | ||
<link rel = "icon" href = "lenny.png" type = "image/x-icon"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | ||
<style> | ||
body { | ||
font-family: monospace; | ||
background-color: #313131 ; | ||
} | ||
|
||
#chatbox { | ||
margin-left: 10%; | ||
margin-right: 10%; | ||
width: auto; | ||
margin-top: 60px; | ||
} | ||
#userInput { | ||
margin-left: 10%; | ||
margin-right: 10%; | ||
width: auto; | ||
margin-top: 60px; | ||
} | ||
#textInput { | ||
margin-left:5%; | ||
margin-right: 5%; | ||
width: 90%; | ||
border: none; | ||
border-bottom: 3px solid black; | ||
font-family: monospace; | ||
font-size: 17px; | ||
} | ||
.userText { | ||
color: white; | ||
font-family: monospace; | ||
font-size: 17px; | ||
text-align: right; | ||
line-height: 30px; | ||
} | ||
.userText span { | ||
background-color: #525252; | ||
padding: 10px; | ||
border-radius: 2px; | ||
} | ||
.botText { | ||
color: white; | ||
font-family: monospace; | ||
font-size: 17px; | ||
text-align: left; | ||
line-height: 30px; | ||
} | ||
.botText span { | ||
background-color: #808080; | ||
padding: 10px; | ||
border-radius: 2px; | ||
} | ||
|
||
.boxed { | ||
margin-left: auto; | ||
margin-right: auto; | ||
width: 50%; | ||
margin-top: 60px; | ||
border: 1px solid #ca3e47; | ||
background-color: #414141; | ||
border-radius: 5px; | ||
} | ||
.box { | ||
border: 2px solid black; | ||
} | ||
input[type="text"], textarea { | ||
|
||
background-color : #363333; | ||
color: white; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div class="boxed"> | ||
|
||
<div id="chatbox"> | ||
|
||
<p class="botText"> | ||
<span>Hello, I am Lenny</span> | ||
</p> | ||
</div> | ||
<div id="userInput" > | ||
|
||
<input id="textInput" name ='sentence' type="text" placeholder="Talk to me" /> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
<script> | ||
function getBotResponse() { | ||
var rawText = $("#textInput").val(); | ||
var userHtml = '<p class="userText"><span>' + rawText + "</span></p>"; | ||
$("#textInput").val(""); | ||
$("#chatbox").append(userHtml); | ||
document | ||
.getElementById("userInput") | ||
.scrollIntoView({ block: "start", behavior: "smooth" }); | ||
$.get("/get", { msg: rawText }).done(function(data) { | ||
var botHtml = '<p class="botText"><span>' + data + "</span></p>"; | ||
$("#chatbox").append(botHtml); | ||
document.getElementById("userInput").scrollIntoView({ block: "start", behavior: "smooth" }); | ||
}); | ||
|
||
} | ||
$("#textInput").keypress(function(e) { | ||
if (e.which == 13) { | ||
getBotResponse(); | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.