Skip to content

Commit 309b315

Browse files
committed
Initial Commit "Some minor Changes and Improvements"
1 parent b712600 commit 309b315

File tree

9 files changed

+158
-99
lines changed

9 files changed

+158
-99
lines changed

build/lib/neurolink/Scripts/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import os
2-
3-
from neurolink.Scripts.__tflearn__ import chat
1+
from neurolink.Scripts.Engines.__tflearn__ import chat
42

53

64
class initialize:

build/lib/neurolink/neurolink.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
from neurolink import initialize as neurolink

neurolink/Scripts/Engines/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
1+
import json
12
import numpy as np
23
from tensorflow import keras
34
from nltk.stem.lancaster import LancasterStemmer
45
import nltk
56
import os
67
import pickle
7-
from neurolink.include import __load__
8-
from neurolink.include.__process__ import *
98

109

11-
# Build and compile the model
10+
def preprocess_data(data):
11+
words = []
12+
labels = []
13+
docs_x = []
14+
docs_y = []
15+
for intent in data["intents"]:
16+
for pattern in intent["patterns"]:
17+
wrds = nltk.word_tokenize(pattern)
18+
words.extend(wrds)
19+
docs_x.append(wrds)
20+
docs_y.append(intent["tag"])
21+
if intent["tag"] not in labels:
22+
labels.append(intent["tag"])
23+
words = [LancasterStemmer().stem(w.lower()) for w in words if w != "?"]
24+
words = sorted(list(set(words)))
25+
labels = sorted(labels)
26+
training = []
27+
output = []
28+
out_empty = [0 for _ in range(len(labels))]
29+
for x, doc in enumerate(docs_x):
30+
bag = []
31+
wrds = [LancasterStemmer().stem(w) for w in doc]
32+
for w in words:
33+
if w in wrds:
34+
bag.append(1)
35+
else:
36+
bag.append(0)
37+
output_row = out_empty[:]
38+
output_row[labels.index(docs_y[x])] = 1
39+
training.append(bag)
40+
output.append(output_row)
41+
training = np.array(training)
42+
output = np.array(output)
43+
return words, labels, training, output
44+
45+
1246
def build_model(input_shape, output_shape):
1347
model = keras.Sequential([
1448
keras.layers.Dense(8, input_shape=input_shape, activation='relu'),
@@ -18,12 +52,10 @@ def build_model(input_shape, output_shape):
1852
return model
1953

2054

21-
# Train the model
2255
def train_model(model, training, output, epochs, verbose=1):
2356
model.fit(training, output, epochs=epochs, verbose=verbose)
2457

2558

26-
# Predict the intent
2759
def predict_intent(sentence, model, words, labels):
2860
sentence_words = nltk.word_tokenize(sentence)
2961
sentence_words = [LancasterStemmer().stem(word.lower()) for word in sentence_words]
@@ -39,11 +71,11 @@ def predict_intent(sentence, model, words, labels):
3971
return intent
4072

4173

42-
# Use the model to predict intents
4374
def chat(message, confidence_threshold, intents_path, model_path, data_path, should_train_model):
4475
if should_train_model:
4576
# Load and preprocess the data
46-
data = __load__.load_data(intents_path)
77+
with open(intents_path, "r") as file:
78+
data = json.load(file)
4779
words, labels, training, output = preprocess_data(data)
4880

4981
# Build and compile the model
@@ -55,15 +87,15 @@ def chat(message, confidence_threshold, intents_path, model_path, data_path, sho
5587
epochs = 1000
5688
train_model(model, training, output, epochs, verbose=0)
5789

58-
# Save the model
59-
model.save(model_path)
90+
# Save the model in the native Keras format
91+
model.save(model_path + '.keras')
6092

6193
# Save words and labels using pickle
6294
with open(data_path, 'wb') as file:
6395
pickle.dump((words, labels), file)
6496

6597
else:
66-
model = keras.models.load_model(model_path)
98+
model = keras.models.load_model(model_path + '.keras')
6799
with open(data_path, 'rb') as file:
68100
words, labels = pickle.load(file)
69101

@@ -74,23 +106,36 @@ def chat(message, confidence_threshold, intents_path, model_path, data_path, sho
74106
return "Sorry, I don't understand. Please try again."
75107

76108

77-
class botModel_keras:
109+
class initialize_keras_model:
78110
"""
79-
these are the test case for the chat function
80-
intents_path = os.path.abspath("intents.json")
81-
model_path = os.path.abspath("model.tflearn")
82-
data_path = os.path.abspath("data.pickle")
83-
84-
response = chat("Hi", 0.5, intents_path, model_path, data_path, train_model=False)
111+
these are the parameters that are required to initialize the chatbot
112+
intents_path = os.path.abspath("../../intents.json")
113+
model_dir = os.path.dirname(os.path.abspath(__file__))
114+
model_path = os.path.join(model_dir, "model")
115+
data_path = os.path.join(model_dir, "data.pickle")
116+
117+
initialize = initialize(intents_path, model_path, data_path, should_train_model=True)
118+
response = initialize.keras("Hi", 0.5)
85119
print(response)
86-
"""
87-
88-
def __init__(self, intents_path, model_path, data_path, train_model):
120+
"""
121+
def __init__(self, intents_path, model_path, data_path, should_train_model):
89122
self.intents_path = intents_path
90123
self.model_path = model_path
91124
self.data_path = data_path
92-
self.train_model = train_model
93-
94-
def chat(self, message, accuracy):
95-
response = chat(message, accuracy, self.intents_path, self.model_path, self.data_path, self.train_model)
125+
self.should_train_model = should_train_model
126+
127+
def keras(self, message, confidence_threshold):
128+
response = chat(
129+
message,
130+
confidence_threshold,
131+
self.intents_path,
132+
self.model_path,
133+
self.data_path,
134+
self.should_train_model
135+
)
96136
return response
137+
138+
139+
140+
141+
Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1+
import json
12
import os
23
import random
3-
import json
4+
45
import nltk
56
import numpy as np
67
import torch
78
import torch.nn as nn
8-
from nltk import LancasterStemmer
9-
from torch.utils.data import Dataset, DataLoader
109
from nltk.stem.porter import PorterStemmer
11-
12-
from neurolink.include import __load__
10+
from torch.utils.data import DataLoader, Dataset
1311

1412
stemmer = PorterStemmer()
1513

16-
model_path = os.path.abspath("../../intents.json")
17-
18-
intents = __load__.load_data(model_path)
19-
20-
21-
def bag_of_words(tokenized_sentence, words):
22-
# Stem each word
23-
sentence_words = [stem(word) for word in tokenized_sentence]
24-
# Initialize bag with 0 for each word
25-
bag = np.zeros(len(words), dtype=np.float32)
26-
for idx, w in enumerate(words):
27-
if w in sentence_words:
28-
bag[idx] = 1
29-
return bag
30-
31-
32-
def tokenize(sentence):
33-
return nltk.word_tokenize(sentence)
34-
35-
36-
def stem(word):
37-
return stemmer.stem(word.lower())
38-
39-
4014
class ChatDataset(Dataset):
4115
def __init__(self, X, y):
4216
self.n_samples = len(X)
@@ -50,6 +24,16 @@ def __len__(self):
5024
return self.n_samples
5125

5226

27+
def bag_of_words(tokenized_sentence, words):
28+
# Stem each word
29+
sentence_words = [stem(word) for word in tokenized_sentence]
30+
# Initialize bag with 0 for each word
31+
bag = np.zeros(len(words), dtype=np.float32)
32+
for idx, w in enumerate(words):
33+
if w in sentence_words:
34+
bag[idx] = 1
35+
return bag
36+
5337
class NeuralNet(nn.Module):
5438
def __init__(self, input_size, hidden_size, num_classes):
5539
super(NeuralNet, self).__init__()
@@ -69,7 +53,18 @@ def forward(self, x):
6953

7054

7155

72-
def __preprocess_data__(intents):
56+
def tokenize(sentence):
57+
return nltk.word_tokenize(sentence)
58+
59+
60+
def stem(word):
61+
return stemmer.stem(word.lower())
62+
63+
64+
65+
66+
67+
def preprocess_data(intents):
7368
all_words = []
7469
tags = []
7570
xy = []
@@ -101,18 +96,22 @@ def __preprocess_data__(intents):
10196
return X_train, y_train, all_words, tags
10297

10398

104-
X_train, y_train, all_words, tags = __preprocess_data__(intents)
99+
def train(intents_path, data_path):
105100

106-
num_epochs = 1000
107-
batch_size = 8
108-
learning_rate = 0.001
109-
input_size = len(X_train[0])
110-
hidden_size = 8
111-
output_size = len(tags)
112-
print(input_size, output_size)
101+
model_path = os.path.abspath(intents_path)
113102

103+
with open(model_path, 'r') as file:
104+
intents = json.load(file)
114105

115-
def train():
106+
X_train, y_train, all_words, tags = preprocess_data(intents)
107+
108+
num_epochs = 1000
109+
batch_size = 8
110+
learning_rate = 0.001
111+
input_size = len(X_train[0])
112+
hidden_size = 8
113+
output_size = len(tags)
114+
print(input_size, output_size)
116115
dataset = ChatDataset(X_train, y_train)
117116
train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=0)
118117
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
@@ -144,13 +143,12 @@ def train():
144143
"all_words": all_words,
145144
"tags": tags
146145
}
147-
data_path = os.path.abspath("data.pth")
148-
FILE = data_path
146+
FILE = os.path.abspath(data_path)
149147
torch.save(data, FILE)
150148
print(f'Training complete. File saved to {FILE}')
151149

152150

153-
def chat(message, threshold, intents, data_path):
151+
def chat(message, threshold, intents_path, data_path):
154152
# Load other data
155153
data = torch.load(data_path)
156154
input_size = data["input_size"]
@@ -174,29 +172,45 @@ def chat(message, threshold, intents, data_path):
174172
probs = torch.softmax(output, dim=1)
175173
prob = probs[0][predicted.item()]
176174
if prob.item() > threshold:
177-
for intent in intents:
175+
with open(intents_path, 'r') as file:
176+
intents = json.load(file)
177+
for intent in intents['intents']:
178178
if tag == intent["tag"]:
179179
return random.choice(intent['responses'])
180180
else:
181181
return "I do not understand... Could you please rephrase?"
182182

183183

184-
train_model = True
184+
class Initialize:
185+
def __init__(self, intents_path, data_path, train_model):
186+
self.intents_path = intents_path
187+
self.data_path = data_path
188+
self.train_model = train_model
189+
self.intents = None
185190

186-
if train_model:
187-
train()
191+
def train(self):
192+
if self.train_model:
193+
train(self.intents_path, self.data_path)
188194

189-
intents_path = os.path.abspath("../../intents.json")
190-
data_path = os.path.abspath("data.pth")
191-
threshold = 0.75
195+
def load_intents(self):
196+
with open(self.intents_path, 'r') as file:
197+
self.intents = json.load(file)
192198

193-
# Load intents file
194-
with open(intents_path, 'r') as file:
195-
intents = json.load(file)["intents"]
199+
def chat(self, message, accuracy):
200+
self.train()
201+
if not self.intents:
202+
self.load_intents()
196203

197-
while True:
198-
message = input("User: ")
199-
if message.lower() == "quit":
200-
break
201-
response = chat(message, threshold, intents, data_path)
202-
print("Assistant:", response)
204+
response = chat(message, accuracy, self.intents_path, self.data_path)
205+
return response
206+
207+
"""
208+
These are some just test case for development.
209+
"""
210+
train_model = True
211+
intents_path = "../../../intents.json"
212+
data_path = "../../data.pth"
213+
threshold = 0.75
214+
initialize_instance = Initialize(intents_path, data_path, train_model)
215+
response = initialize_instance.chat("Hi", threshold)
216+
print(response)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ def __init__(self, intents_path, model_path, data_path, train_model):
122122
self.data_path = data_path
123123
self.train_model = train_model
124124

125-
def chat(self, message, accuracy):
125+
def tfl(self, message, accuracy):
126126
response = chat(message, accuracy, self.intents_path, self.model_path, self.data_path, self.train_model)
127127
return response

neurolink/Scripts/__init__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import os
2-
3-
from neurolink.Scripts.__tflearn__ import chat
4-
1+
from neurolink.Scripts.Engines.__tflearn__ import initialize as tfl
2+
from neurolink.Scripts.Engines.__keras__ import initialize_keras_model
53

64
class initialize:
75
"""
@@ -15,14 +13,19 @@ class initialize:
1513
print(response)
1614
1715
"""
16+
1817
def __init__(self, intents_path, model_path, data_path, train_model=True):
1918
self.intents_path = intents_path
2019
self.model_path = model_path
2120
self.data_path = data_path
2221
self.train_model = train_model
2322

24-
def chat(self, message, accuracy):
25-
response = chat(message, accuracy, self.intents_path, self.model_path, self.data_path, self.train_model)
26-
return response
27-
28-
23+
def chat(self, message, accuracy, model):
24+
if model == "tfl":
25+
response = tfl(self.intents_path, self.model_path, self.data_path, self.train_model)
26+
response = response.tfl(message, accuracy)
27+
return response
28+
elif model == "keras":
29+
response = initialize_keras_model(self.intents_path, self.model_path, self.data_path, self.train_model)
30+
response = response.keras(message, accuracy)
31+
return response

0 commit comments

Comments
 (0)