Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added chatbot functionality #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ build
# Dependency directories
node_modules/
jspm_packages/
Lib
Scripts


# TypeScript v1 declaration files
typings/
Expand Down Expand Up @@ -97,4 +100,7 @@ typings/
.tern-port

# vscode settings
.vscode
.vscode

#Ignored package-lock.json
package-lock.json
10 changes: 5 additions & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"semi": false,
"trailingComma": "es5",
"singleQuote": true
}
{
"semi": false,
"trailingComma": "es5",
"singleQuote": true
}
Binary file added database.sqlite3
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"lodash": "^4.17.20",
"mongoose": "^5.10.3",
"morgan": "^1.9.1",
"python-shell": "^2.0.2",
"socket.io": "^2.3.0"
},
"devDependencies": {
Expand Down
86 changes: 86 additions & 0 deletions src/chatBot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import sys
import logging
logger = logging.getLogger()
logger.setLevel(logging.CRITICAL)
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

chatbot = ChatBot(
'Darkrai-Bot',
preprocessors=[
'chatterbot.preprocessors.clean_whitespace'
],
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
'chatterbot.logic.BestMatch',
'chatterbot.logic.MathematicalEvaluation',
],
database_uri='sqlite:///database.sqlite3'
)
general = [
"Hi",
"Hi",
"Heya",
"Fine",
"I'm good what about you",
"How are you?",
"I'm good what about you",
"What are you doing?",
"Nothing just talking with you",
"Are you happy",
"Yes i'm Happy",
"Hello",
"Hi there!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome."
]

project =[
'What is Darkrai?',
'Darkrai is a browser extension that will enable the user to chat with any other user visiting the same website as themselves.',
'How to use',
'''
Download the Extension from the Chrome and Open any Website and then you can chat with any other person or
group of person visitin the website
''',
'Who built it?',
'It is built by students of DA-IICT college',
'What features does it have?',
'Darkrai is a browser extension through which we chat with other people visiting the same website',
'Is it available for FireFox',
'No currently it is available for Chrome only',
'Is it a full fledged application',
'It is still in development and new features are added continuously',
'Is it applicable for any website',
'Yes it is available for any websites',
'Can i use it?',
'Yes you can use it you just have to download the extension from the chrome',
'Can i get the Github link of this project',
r'Yes you can go to this repository https://github.com/darkraichat',
'Is it open-source or close source',
'Darkrai Chat is an Open Source Application',
'Which stack Darkrai uses?',
'MERN stack',
'What is the Stack of Darkrai',
'It uses MERN stack',
'This is a Nice App',
'Thanks do share with your friends'
]

trainer = ListTrainer(chatbot)

for item in (general, project):
trainer.train(item)


user_input = sys.argv[1]
bot_response = chatbot.get_response(user_input)
if(bot_response.confidence < 0.1):
print('I am sorry, but I do not understand.')
else:
print(bot_response)

#print(sys.argv[1])
1 change: 0 additions & 1 deletion src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Message = require('../models/message')
const Room = require('../models/room')

const addMessage = function (username, message, website) {
const newMessage = {
username,
Expand Down
55 changes: 41 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const jwt = require('jsonwebtoken')
const Room = require('./models/room')
const indexRoutes = require('./routes')
const controllers = require('./controllers')
const toxicity = require('@tensorflow-models/toxicity')

const {PythonShell} =require('python-shell')
//const toxicity = require('@tensorflow-models/toxicity')
// tfjs node backend initialization
require('@tensorflow/tfjs-node')
// require('@tensorflow/tfjs-node')

// Loading .env
require('dotenv').config()
Expand Down Expand Up @@ -52,6 +52,7 @@ const io = socket(server)
app.use(express.json())
app.use(express.urlencoded({ extended: true }))


// Users count for each room
const rooms = {}

Expand Down Expand Up @@ -95,24 +96,50 @@ io.sockets
})

socket.on('send_message', (data) => {
//console.log(socket.id);
// tfjs toxicity model prediction
toxicity.load().then((model) => {
model.classify(data.message).then((predictions) => {
if (predictions[predictions.length - 1].results[0].match) {
console.log('Toxic message detected. Deleting now...')
io.sockets.in(socket.room).emit('delete_message', {
message: data.message,
})
controllers.updateMessage(data.message)
}
})
})
// toxicity.load().then((model) => {
// model.classify(data.message).then((predictions) => {
// if (predictions[predictions.length - 1].results[0].match) {
// console.log('Toxic message detected. Deleting now...')
// io.sockets.in(socket.room).emit('delete_message', {
// message: data.message,
// })
// controllers.updateMessage(data.message)
// }
// })
// })

controllers.addMessage(socket.username, data.message, socket.room)
io.sockets.in(socket.room).emit('receive_message', {
username: socket.username,
message: data.message,
})

// ChatBot code
const check = data.message.toString();
if(check[0].toLowerCase()==='c' && check[1].toLowerCase()==='b' && check[2]==='-'){
const search = check.slice(3);
let options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
scriptPath: 'src/', //If you are having python_test.py script in same folder, then it's optional.
args: [search] //An argument which can be accessed in the script using sys.argv[1]
};
let msg='' // Chatbot response
PythonShell.run('chatBot.py', options, function (err, result){
if (err) res.send(err.message);
const len = result.length;
const response = result[len-1];
msg = response.toString();
console.log('result: ', msg);
io.sockets.in(socket.room).emit('receive_message',{
username: 'ChatBot',
message: msg
})
// res.send(response.toString())
})
}
})

socket.on('disconnect', (data) => {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ router.post('/login', async (req, res) => {
if (user) {
const token = jwt.sign(
{ username: user.username },
process.env.SERVER_SECRET,
`${process.env.SERVER_SECRET}`,
{
expiresIn: '30d',
}
Expand Down Expand Up @@ -49,7 +49,7 @@ router.post('/register', (req, res) => {
}).then(async (user) => {
const token = jwt.sign(
{ username: user.username },
process.env.SERVER_SECRET,
"process.env.SERVER_SECRET",
{
expiresIn: '30d',
}
Expand Down