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

Add ChatBot #1075

Open
wants to merge 3 commits into
base: main
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
5 changes: 5 additions & 0 deletions AI_CHATBOT_METAVERSE/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# AI_CHATBOT
It is an Ai chatbot developed using natural language toolkit NLTK, pytorch
I have developed this bot.

Feel free to update the intents.json to make the domain of the chatbot wider.
53 changes: 53 additions & 0 deletions AI_CHATBOT_METAVERSE/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random
import json

import torch

from model import NeuralNet
from 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 = "data.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 = "Snapitizer"
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...")
Binary file added AI_CHATBOT_METAVERSE/data.pth
Binary file not shown.
115 changes: 115 additions & 0 deletions AI_CHATBOT_METAVERSE/intents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"intents": [
{
"tag": "greeting",
"patterns": [
"Hi",
"Hello",
"Hey",
"How are you",
"Is anyone there?"
],
"responses": [
"Hello! Welcome to the Metaverse Front-End Playground. How can I assist you today?",
"Hi there! How can I help you with the Metaverse Front-End Playground today?",
"Hello! How can we make your experience better with the Metaverse Front-End Playground?"
]
},
{
"tag": "goodbye",
"patterns": ["Bye", "Goodbye", "See you later"],
"responses": [
"Goodbye! Thank you for visiting the Metaverse Front-End Playground.",
"See you later! Have a great day!",
"Bye! Come back again soon to the Metaverse Front-End Playground."
]
},
{
"tag": "thanks",
"patterns": ["Thanks", "Thank you", "That's helpful", "Thank you so much"],
"responses": [
"You're welcome! Happy to help.",
"Anytime! We're here to assist you.",
"My pleasure! If you have more questions, feel free to ask."
]
},
{
"tag": "mission",
"patterns": [
"What is the Metaverse Front-End Playground?",
"Tell me about the Metaverse Front-End Playground",
"What does the Metaverse Front-End Playground do?",
"What is the mission of the Metaverse Front-End Playground?"
],
"responses": [
"The Metaverse Front-End Playground is an open-source repository created to empower developers to explore, innovate, and collaborate in the metaverse space. Our mission is to provide a platform for developers to showcase their projects and dive into the virtual realm."
]
},
{
"tag": "features",
"patterns": [
"What features does the Metaverse Front-End Playground offer?",
"Tell me about the features of the Metaverse Front-End Playground",
"What can the Metaverse Front-End Playground do?"
],
"responses": [
"The Metaverse Front-End Playground offers several features, including a platform for developers to showcase their metaverse projects, tools for innovation and collaboration, and a dynamic community for networking and learning."
]
},
{
"tag": "community",
"patterns": [
"What is the community like?",
"Can I join the community?",
"Tell me about the Metaverse Front-End Playground community"
],
"responses": [
"The Metaverse Front-End Playground community is a dynamic and inclusive group of developers from all backgrounds. Whether you're a seasoned developer or a newcomer, you're invited to join us and collaborate on exciting projects."
]
},
{
"tag": "collaboration",
"patterns": [
"How can I collaborate on projects?",
"Can I contribute to existing projects?",
"Tell me about collaboration opportunities"
],
"responses": [
"You can collaborate on projects by joining our repository on GitHub and participating in ongoing projects. Feel free to contribute your ideas, code, and expertise to help us innovate in the metaverse space."
]
},
{
"tag": "projects",
"patterns": [
"What projects are available?",
"Can I see some projects?",
"Tell me about the projects in the Metaverse Front-End Playground"
],
"responses": [
"You can explore various projects on our GitHub repository. We have a wide range of projects showcasing different aspects of metaverse development, from virtual reality experiences to interactive web applications."
]
},
{
"tag": "volunteer",
"patterns": [
"How can I volunteer?",
"Can I volunteer for the Metaverse Front-End Playground?",
"Tell me about volunteering opportunities"
],
"responses": [
"Thank you for your interest in volunteering! Please visit our GitHub repository and get involved in our projects. Your contributions are valuable and appreciated."
]
},
{
"tag": "donate",
"patterns": [
"How can I donate?",
"Can I donate to the Metaverse Front-End Playground?",
"Tell me about donation options"
],
"responses": [
"Thank you for considering a donation! You can support our projects by contributing to our GitHub repository. Your donations help us continue our mission to innovate and collaborate in the metaverse space."
]
}
]
}
19 changes: 19 additions & 0 deletions AI_CHATBOT_METAVERSE/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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)
return out
Empty file.
23 changes: 23 additions & 0 deletions AI_CHATBOT_METAVERSE/nltk_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import nltk
nltk.download('punkt')
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()

def tokenize(sentence):
return nltk.word_tokenize(sentence)


def stem(word):
return stemmer.stem(word.lower())


def bag_of_words(tokenized_sentence, words):

sentence_words = [stem(word) for word in tokenized_sentence]
bag = np.zeros(len(words), dtype=np.float32)
for idx, w in enumerate(words):
if w in sentence_words:
bag[idx] = 1

return bag
108 changes: 108 additions & 0 deletions AI_CHATBOT_METAVERSE/train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import numpy as np
import random
import json

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader

from nltk_utils import bag_of_words, tokenize, stem
from model import NeuralNet

with open('intents.json', 'r') as f:
intents = json.load(f)

all_words = []
tags = []
xy = []
for intent in intents['intents']:
tag = intent['tag']
tags.append(tag)
for pattern in intent['patterns']:
w = tokenize(pattern)
all_words.extend(w)
xy.append((w, tag))

ignore_words = ['?', '.', '!']
all_words = [stem(w) for w in all_words if w not in ignore_words]
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)

X_train = []
y_train = []
for (pattern_sentence, tag) in xy:
bag = bag_of_words(pattern_sentence, all_words)
X_train.append(bag)
label = tags.index(tag)
y_train.append(label)

X_train = np.array(X_train)
y_train = np.array(y_train)

num_epochs = 1000
batch_size = 8
learning_rate = 0.01
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

def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
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)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
for (words, labels) in train_loader:
words = words.to(device)
labels = labels.to(dtype=torch.long).to(device)
outputs = model(words)

loss = criterion(outputs, labels)
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 = "data.pth"
torch.save(data, FILE)

print(f'training complete. file saved to {FILE}')