Commit caa6e3f1 authored by IT18174236(Gunarathna D.M.G.S)'s avatar IT18174236(Gunarathna D.M.G.S)

Merge branch 'IT18174236_Update_Component' into 'master'

Project update 2

See merge request !11
parents 091b5265 61d1aac3
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ChatBot.iml" filepath="$PROJECT_DIR$/.idea/ChatBot.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import random
import json
import torch
from ChatBot.model import NeuralNet
from ChatBot.nltk_utils import bag_of_words, tokenize
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open('intents.json', 'r') as json_data:
intents = json.load(json_data)
FILE = "last_model.pth"
data = torch.load(FILE)
input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data['all_words']
tags = data['tags']
model_state = data["model_state"]
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
bot_name = "Bot"
print("Let's chat! (type 'quit' to exit)")
while True:
# sentence = "do you use credit cards?"
sentence = input("You: ")
if sentence == "quit":
break
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(device)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
print(f"{bot_name}: {random.choice(intent['responses'])}")
else:
print(f"{bot_name}: I do not understand...")
\ No newline at end of file
{
"intents": [
{
"tag": "greeting",
"patterns": [
"Greetings",
"Hi",
"Hey",
"Hello", "helloo", "Howdy", "Hola",
"How are you", "How are you doing", "How u doing?", "How're you doing",
"Whats up", "Yo",
"Sup",
"Is anyone there",
"Are you there", "You there",
"Need some help"
],
"responses": [
"Botson reporting! ;-) What's on your mind?",
"Hello mate! Feeling creative?",
"Hey, It's your favourite Botson ;-) Any thoughts?",
"Hello sir! got any new ideas?",
"Hey! Good day to be creative. Isn't it?",
"Hello master creator! How can I help?"
]
},
{
"tag": "Morning",
"patterns": [
"Good morning", "Morning", "Gd morning", "G'morning", "G morning", "Gm", "Gummorning",
"Good morning Botson", "Morning Botson", "Gd morning Botson", "G'morning Botson", "G morning Botson", "Gm Botson", "Gummorning Botson",
"Hey good morning", "Hey gummorning", "Hey gd morning", "Hey g'morning", "Hey gm",
"Hi good morning", "Hi gummorning", "Hi gd morning", "Hi g'morning", "Hi gm",
"Hello good morning", "Hello gummorning", "Hello gd morning", "Hello g'morning", "Hello gm",
"Howdy good morning", "Howdy gummorning", "Howdy gd morning", "Howdy g'morning", "Howdy gm",
"Very good morning", "Very gd morning", "Very gummorning", "Very g'morning", " Vgm"
],
"responses": [
"Good morning mate. What brings you up here?",
"It is now. How may I assist you?",
"You just made it better. How can I repay you?",
"Thanks to you! What do you seek?",
"Hello master creator! How can I help?"
]
},
{
"tag": "day",
"patterns": [
"Good afternoon", "afternoon", "Gd afternoon", "D'afternoon", "D afternoon",
"Good afternoon Botson", "afternoon Botson", "Gd afternoon Botson", "D'afternoon Botson", "D afternoon Botson",
"Hi good afternoon", "Hi gd afternoon", "Hi d'afternoon", "Hi d'afternoon",
"Hey good afternoon", "Hey gd afternoon", "Hey d'afternoon", "Hey d'afternoon",
"Hello good afternoon", "Hello gd afternoon", "Hello d'afternoon", "Hello d'afternoon"
],
"responses": [
"Good afternoon mate. What brings you up here?",
"It is now. How may I assist you?",
"You just made it better. How can I repay you?",
"Thanks to you! What do you seek?",
"Hello master creator! How can I help?"
]
},
{
"tag": "night",
"patterns": [
"Good night", "night night", "Gd night", "Gd nyt", "Gn",
"Good night Botson", "night night Botson", "Gd night Botson", "Gd nyt Botson", "Gn Botson",
"Sweet dreams"
],
"responses": [
"Good night pal!",
"Sweet dreams!",
"Sleep well mate."
]
},
{
"tag": "goodbye",
"patterns": [
"Bye", "See you later", "Goodbye", "Later", "Catch you later",
"Good day", "G'day", "G day"],
"responses": [
"Thanks for visiting!",
"Have a nice day!",
"Bye! Come again soon.",
"Bye! Have a great day.",
"Goodbye! Hope to see you soon.", "Goodbye! Take care"
]
},
{
"tag": "thanks",
"patterns": [
"Thanks", "Thank you", "Great help","That's helpful", "Thank's a lot", "Thank you very much",
"Äppreciate it", "Such a life saver"],
"responses": ["Happy to help!", "Any time!", "My pleasure!", "Cool ;-)", "Sure buddy!", "No sweat...", "Glad to help"]
},
{
"tag": "funny",
"patterns": [
"Joke", "Funny", "laugh",
"Tell me a joke",
"Tell me something funny",
"Do you know a joke", "Do you know any jokes",
"I want to hear a joke",
"I would like to hear a joke",
"I could use a joke",
"Please tell a joke",
"Can you say something funny",
"Could use a laugh right now",
"Wanna laugh", "Need a laugh", "Make me laugh", "Any jokes"
],
"responses": [
"Why did the hipster burn his mouth? He drank the coffee before it was cool.",
"What did the buffalo say when his son left for college? Bison.",
"What’s the best thing about Switzerland? The flag is a big plus.",
"I invented a new word! Plagiarism!",
"Did you hear about the mathematician who’s afraid of negative numbers? He’ll stop at nothing to avoid them.",
"Why do we tell actors to “break a leg?” Because every play has a cast.",
"Yesterday I saw a guy spill all his Scrabble letters on the road. I asked him, “What’s the word on the street?",
"A woman in labor suddenly shouted, “Shouldn’t! Wouldn’t! Couldn’t! Didn’t! Can’t!”. “Don’t worry,” said the doc. “Those are just contractions.",
"A bear walks into a bar and says, “Give me a whiskey and … cola”. “Why the big pause?” asks the bartender. The bear shrugged. “I’m not sure; I was born with them.”",
"Did you hear about the actor who fell through the floorboards? He was just going through a stage."
]
},
{
"tag": "Tips",
"patterns": [
"Any tips",
"Tell me something I don't know",
"General knowledge"
],
"responses": [
"Did you know that youtube has 37M+ channels as of 2021?",
"Did you know that Bella Poarch is a Philipino?",
"Did you know that you get $18 per 1000 views for a video?",
"Did you know that you could request human review for a video?",
"Here are some tips to boost your channel ==> (https://digitalmarketinginstitute.com/blog/10-ways-to-grow-your-youtube-channel-in-2018)",
"How to startup a new channel ==> (https://www.wix.com/blog/2017/03/how-to-start-a-successful-youtube-channel-for-your-business/)"
]
},
{
"tag": "Idea Generation",
"patterns": [
"Trends", "What's trending", "Ideas", "Any ideas?", "New ideas", "New trends", "Trends graph", "Updates", "Whats new?", "New topics", "Topics", "Concepts", "New concepts",
"Trending concepts", "Show trends", "Weekly trends", "This week", "Analytics", "Report", "Weekly report"
],
"responses": [
"Here you go ;-)"
]
},
{
"tag": "time",
"patterns": [
"time?",
"What time it is",
"can you tell me time?",
"Do u know the time?"
],
"responses": [
"time"
]
},
{
"tag": "forecasting",
"patterns": [
"forecast next week",
"what is best topic in next week?",
"trending topic in next week?",
"what should I do next week?",
"next week"
],
"responses": [
"forecast"
]
}
]
}
\ No newline at end of file
File added
import torch
import torch.nn as nn
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).__init__()
self.l1 = nn.Linear(input_size, hidden_size)
self.l2 = nn.Linear(hidden_size, hidden_size)
self.l3 = nn.Linear(hidden_size, num_classes)
self.relu = nn.ReLU()
def forward(self, x):
out = self.l1(x)
out = self.relu(out)
out = self.l2(out)
out = self.relu(out)
out = self.l3(out)
# no activation and no softmax at the end
return out
\ No newline at end of file
import numpy as np
import nltk
# nltk.download('punkt')
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()
def tokenize(sentence):
"""
split sentence into array of words/tokens
a token can be a word or punctuation character, or number
"""
return nltk.word_tokenize(sentence)
def stem(word):
"""
stemming = find the root form of the word
examples:
words = ["organize", "organizes", "organizing"]
words = [stem(w) for w in words]
-> ["organ", "organ", "organ"]
"""
return stemmer.stem(word.lower())
def bag_of_words(tokenized_sentence, words):
"""
return bag of words array:
1 for each known word that exists in the sentence, 0 otherwise
example:
sentence = ["hello", "how", "are", "you"]
words = ["hi", "hello", "I", "you", "bye", "thank", "cool"]
bog = [ 0 , 1 , 0 , 1 , 0 , 0 , 0]
"""
# stem each word
sentence_words = [stem(word) for word in tokenized_sentence]
# initialize bag with 0 for each word
bag = np.zeros(len(words), dtype=np.float32)
for idx, w in enumerate(words):
if w in sentence_words:
bag[idx] = 1
return bag
\ No newline at end of file
import numpy as np
import json
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from ChatBot.nltk_utils import bag_of_words, tokenize, stem
from ChatBot.model import NeuralNet
with open('intents.json', 'r') as f:
intents = json.load(f)
all_words = []
tags = []
xy = []
# loop through each sentence in our intents patterns
for intent in intents['intents']:
tag = intent['tag']
# add to tag list
tags.append(tag)
for pattern in intent['patterns']:
# tokenize each word in the sentence
w = tokenize(pattern)
# add to our words list
all_words.extend(w)
# add to xy pair
xy.append((w, tag))
# stem and lower each word
ignore_words = ['?', '.', '!']
all_words = [stem(w) for w in all_words if w not in ignore_words]
# remove duplicates and sort
all_words = sorted(set(all_words))
tags = sorted(set(tags))
print(len(xy), "patterns")
print(len(tags), "tags:", tags)
print(len(all_words), "unique stemmed words:", all_words)
# create training data
X_train = []
y_train = []
for (pattern_sentence, tag) in xy:
# X: bag of words for each pattern_sentence
bag = bag_of_words(pattern_sentence, all_words)
X_train.append(bag)
# y: PyTorch CrossEntropyLoss needs only class labels, not one-hot
label = tags.index(tag)
y_train.append(label)
X_train = np.array(X_train)
y_train = np.array(y_train)
# Hyper-parameters
num_epochs = 1000
batch_size = 8
learning_rate = 0.001
input_size = len(X_train[0])
hidden_size = 8
output_size = len(tags)
print(input_size, output_size)
class ChatDataset(Dataset):
def __init__(self):
self.n_samples = len(X_train)
self.x_data = X_train
self.y_data = y_train
# support indexing such that dataset[i] can be used to get i-th sample
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
# we can call len(dataset) to return the size
def __len__(self):
return self.n_samples
dataset = ChatDataset()
train_loader = DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True,
num_workers=0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = NeuralNet(input_size, hidden_size, output_size).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
for epoch in range(num_epochs):
for (words, labels) in train_loader:
words = words.to(device)
labels = labels.to(dtype=torch.long).to(device)
# Forward pass
outputs = model(words)
# if y would be one-hot, we must apply
# labels = torch.max(labels, 1)[1]
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 100 == 0:
print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')
print(f'final loss: {loss.item():.4f}')
data = {
"model_state": model.state_dict(),
"input_size": input_size,
"hidden_size": hidden_size,
"output_size": output_size,
"all_words": all_words,
"tags": tags
}
FILE = "last_model.pth"
torch.save(data, FILE)
print(f'training complete. file saved to {FILE}')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment