-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot_model.py
More file actions
121 lines (98 loc) · 3.92 KB
/
chatbot_model.py
File metadata and controls
121 lines (98 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import nltk
from nltk.stem import WordNetLemmatizer
import numpy as np
import json
import pickle
import random
# Tensorflow and Keras for Neural Network (AI Model)
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout # <--- YE LINE ZAROORI HAI
from tensorflow.keras.optimizers import Adam
print("All libraries imported successfully.") # <--- YE LINE ADD KAREIN
# Initialisation
# ... (rest of the initialization code) ...
# Initialisation
lemmatizer = WordNetLemmatizer()
words = []
classes = []
documents = []
ignore_words = ['?', '!', '.', ',']
# 1. Load Data
try:
with open('intents.json', encoding='utf-8') as file:
data = json.load(file)
print("Intents file loaded successfully.")
except Exception as e:
# Yeh 'Exception as e' sabhi tarah ke errors ko pakadta hai (JSON, File Not Found, etc.)
print("\n--- CRITICAL ERROR ---")
print(f"Chatbot failed to start due to: {e}")
print("Please check intents.json format and location.")
print("------------------------\n")
exit() # Agar error aaye to code yahaan ruk jaayega
# 2. Pre-processing
for intent in data['intents']:
for pattern in intent['patterns']:
# Tokenize (split into words)
w = nltk.word_tokenize(pattern)
words.extend(w)
# Add to documents list
documents.append((w, intent['tag']))
# Add to classes list
if intent['tag'] not in classes:
classes.append(intent['tag'])
# Lemmatize (reduce word to its root) and clean up
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
words = sorted(list(set(words)))
classes = sorted(list(set(classes)))
print(f"{len(documents)} documents (patterns) loaded")
print(f"{len(classes)} classes (tags) loaded: {classes}")
# 3. Create Training Data (Bag of Words)
training = []
output_empty = [0] * len(classes)
for doc in documents:
bag = []
pattern_words = doc[0]
pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words]
# Create the Bag of Words (BoW) array
for w in words:
bag.append(1) if w in pattern_words else bag.append(0)
# Output row: one-hot encoding for the intent tag
output_row = list(output_empty)
output_row[classes.index(doc[1])] = 1
training.append([bag, output_row])
# Shuffle and convert to NumPy array
random.shuffle(training)
training = np.array(training, dtype=object)
# Create train_x (patterns) and train_y (intents)
train_x = list(training[:,0])
train_y = list(training[:,1])
# 4. Neural Network Model Definition (The AI Brain)
model = Sequential()
# Input layer and first hidden layer
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
# Second hidden layer
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
# Output layer (predicts the class/tag)
model.add(Dense(len(train_y[0]), activation='softmax'))
# Compile the model (Optimizer helps adjust weights)
adam = Adam(learning_rate=0.01)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
# 5. Train the Model
print("\n--- Model Training Started ---")
# Adjust epochs/batch_size based on your computer's speed/memory
hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
print("--- Model Training Complete ---")
# 6. Save Model and Data
model.save('chatbot_model.h5',)
pickle.dump({'words':words, 'classes':classes, 'train_x':train_x, 'train_y':train_y}, open( "training_data.pkl", "wb" ))
print("\nModel and data files saved successfully!")
# Model Training Complete hone ke baad:
# Model ko save karein (Sahi tareeka: sirf file ka naam)
model.save('chatbot_model.h5')
# Data ko save karein
pickle.dump({'words':words, 'classes':classes, 'train_x':train_x, 'train_y':train_y}, open( "training_data.pkl", "wb" ))
# Success message print karein
print("\nModel and data files saved successfully!")