Spaces:
Sleeping
Sleeping
File size: 48,847 Bytes
c03de54 |
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
import gradio as gr
import numpy as np
import json
import re
import random
from typing import List, Dict, Tuple, Optional
import os
import time
import matplotlib.pyplot as plt
from io import BytesIO
import base64
from datetime import datetime
# Assuming all the classes (ActivationFunctions, LossFunctions, Layer, DenseLayer,
# DropoutLayer, NeuralNetwork, TextProcessor, Chatbot) are defined as in your uploaded code
# I'm not repeating them here for brevity
class ActivationFunctions:
"""Class containing various activation functions and their derivatives."""
@staticmethod
def sigmoid(z: np.ndarray) -> np.ndarray:
"""Sigmoid activation function."""
z = np.clip(z, -500, 500)
return 1 / (1 + np.exp(-z))
@staticmethod
def sigmoid_derivative(z: np.ndarray) -> np.ndarray:
"""Derivative of the sigmoid function."""
s = ActivationFunctions.sigmoid(z)
return s * (1 - s)
@staticmethod
def relu(z: np.ndarray) -> np.ndarray:
"""ReLU activation function."""
return np.maximum(0, z)
@staticmethod
def relu_derivative(z: np.ndarray) -> np.ndarray:
"""Derivative of the ReLU function."""
return np.where(z > 0, 1, 0)
@staticmethod
def softmax(z: np.ndarray) -> np.ndarray:
"""Softmax activation function."""
exp_z = np.exp(z - np.max(z))
return exp_z / exp_z.sum(axis=0, keepdims=True)
class LossFunctions:
"""Class containing various loss functions and their derivatives."""
@staticmethod
def mse(output: np.ndarray, target: np.ndarray) -> float:
"""Mean Squared Error loss."""
return np.mean((output - target) ** 2)
@staticmethod
def mse_derivative(output: np.ndarray, target: np.ndarray) -> np.ndarray:
"""Derivative of MSE loss."""
return 2 * (output - target) / output.size
@staticmethod
def cross_entropy(output: np.ndarray, target: np.ndarray) -> float:
"""Cross Entropy loss for multi-class classification."""
epsilon = 1e-15
output = np.clip(output, epsilon, 1 - epsilon)
return -np.sum(target * np.log(output)) / output.shape[1]
@staticmethod
def cross_entropy_derivative(output: np.ndarray, target: np.ndarray) -> np.ndarray:
"""Derivative of Cross Entropy loss."""
epsilon = 1e-15
output = np.clip(output, epsilon, 1 - epsilon)
return -target / output / output.shape[1]
class Layer:
"""Base class for neural network layers."""
def forward(self, inputs: np.ndarray) -> np.ndarray:
"""Forward pass through the layer."""
raise NotImplementedError
def backward(self, grad: np.ndarray) -> np.ndarray:
"""Backward pass through the layer."""
raise NotImplementedError
def update(self, learning_rate: float) -> None:
"""Update layer parameters."""
pass
def get_parameters(self) -> List:
"""Get layer parameters."""
return []
class DenseLayer(Layer):
"""Fully connected layer with improved numerical stability."""
def __init__(self, input_size: int, output_size: int, activation: str = "sigmoid"):
"""Initialize the dense layer with more stable parameters."""
self.input_size = input_size
self.output_size = output_size
# Use smaller initialization to prevent exploding gradients
# Xavier/Glorot initialization with smaller scale factor
self.weights = np.random.randn(output_size, input_size) * np.sqrt(
1 / (input_size + output_size)
)
self.biases = np.zeros((output_size, 1))
# Set activation function
if activation == "sigmoid":
self.activation_fn = ActivationFunctions.sigmoid
self.activation_derivative = ActivationFunctions.sigmoid_derivative
elif activation == "relu":
self.activation_fn = ActivationFunctions.relu
self.activation_derivative = ActivationFunctions.relu_derivative
elif activation == "softmax":
self.activation_fn = ActivationFunctions.softmax
self.activation_derivative = None
else:
raise ValueError(f"Unsupported activation function: {activation}")
self.activation_name = activation
# Cache for backward pass
self.inputs = None
self.z = None
self.output = None
# Gradients
self.dW = None
self.db = None
def forward(self, inputs: np.ndarray) -> np.ndarray:
"""Forward pass through the layer with improved numerical stability."""
self.inputs = inputs
# Use dot product with better numerical stability
self.z = np.dot(self.weights, inputs) + self.biases
# Clip values to prevent overflow in activations
if self.activation_name == "sigmoid":
self.z = np.clip(self.z, -15, 15) # Prevent overflow in sigmoid
self.output = self.activation_fn(self.z)
# Add small epsilon to prevent exact zeros or ones
if self.activation_name == "softmax":
epsilon = 1e-10
self.output = np.clip(self.output, epsilon, 1.0 - epsilon)
return self.output
def backward(self, grad: np.ndarray) -> np.ndarray:
"""Backward pass through the layer with gradient clipping."""
if self.activation_name == "softmax":
# Special case for softmax + cross-entropy
delta = grad
else:
delta = grad * self.activation_derivative(self.z)
# Compute gradients
self.dW = np.dot(delta, self.inputs.T)
self.db = np.sum(delta, axis=1, keepdims=True)
# Clip gradients to prevent exploding gradients
max_grad_norm = 5.0
self.dW = np.clip(self.dW, -max_grad_norm, max_grad_norm)
self.db = np.clip(self.db, -max_grad_norm, max_grad_norm)
# Gradient to pass to the previous layer
return np.dot(self.weights.T, delta)
def update(self, learning_rate: float) -> None:
"""Update layer parameters using gradient descent with weight decay."""
# Add small weight decay to prevent overfitting
weight_decay = 1e-4
weight_decay_term = weight_decay * self.weights
self.weights -= learning_rate * (self.dW + weight_decay_term)
self.biases -= learning_rate * self.db
class DropoutLayer(Layer):
"""Dropout layer for regularization."""
def __init__(self, dropout_rate: float = 0.5):
"""Initialize the dropout layer."""
self.dropout_rate = dropout_rate
self.mask = None
def forward(self, inputs: np.ndarray, training: bool = True) -> np.ndarray:
"""Forward pass through the layer."""
if not training:
return inputs
# Create dropout mask
self.mask = np.random.binomial(1, 1 - self.dropout_rate, size=inputs.shape) / (
1 - self.dropout_rate
)
return inputs * self.mask
def backward(self, grad: np.ndarray) -> np.ndarray:
"""Backward pass through the layer."""
return grad * self.mask
class NeuralNetwork:
"""Neural network with multiple layers."""
def __init__(self):
"""Initialize the neural network."""
self.layers = []
self.loss_fn = None
self.loss_derivative = None
def add(self, layer: Layer) -> None:
"""Add a layer to the network."""
self.layers.append(layer)
def set_loss(self, loss_type: str) -> None:
"""Set the loss function."""
if loss_type == "mse":
self.loss_fn = LossFunctions.mse
self.loss_derivative = LossFunctions.mse_derivative
elif loss_type == "cross_entropy":
self.loss_fn = LossFunctions.cross_entropy
self.loss_derivative = LossFunctions.cross_entropy_derivative
else:
raise ValueError(f"Unsupported loss function: {loss_type}")
def forward(self, x: np.ndarray, training: bool = True) -> np.ndarray:
"""Forward pass through the network."""
output = x
for layer in self.layers:
if isinstance(layer, DropoutLayer):
output = layer.forward(output, training)
else:
output = layer.forward(output)
return output
def compute_loss(self, y_pred: np.ndarray, y_true: np.ndarray) -> float:
"""Compute the loss."""
return self.loss_fn(y_pred, y_true)
def backward(self, y_pred: np.ndarray, y_true: np.ndarray) -> None:
"""Backward pass through the network."""
# Initial gradient from the loss function
grad = self.loss_derivative(y_pred, y_true)
# Propagate gradient through layers in reverse order
for layer in reversed(self.layers):
grad = layer.backward(grad)
def update(self, learning_rate: float) -> None:
"""Update network parameters."""
for layer in self.layers:
layer.update(learning_rate)
def predict(self, x: np.ndarray) -> np.ndarray:
"""Make predictions."""
return self.forward(x, training=False)
@classmethod
def load(cls, filename: str) -> "NeuralNetwork":
"""Load a model from a file."""
with open(filename, "r") as f:
model_data = json.load(f)
network = cls()
network.set_loss(model_data.get("loss_type", "cross_entropy"))
for layer_data in model_data["layers"]:
if layer_data["type"] == "dense":
layer = DenseLayer(
layer_data["input_size"],
layer_data["output_size"],
layer_data["activation"],
)
layer.weights = np.array(layer_data["weights"])
layer.biases = np.array(layer_data["biases"])
network.add(layer)
elif layer_data["type"] == "dropout":
layer = DropoutLayer(layer_data["dropout_rate"])
network.add(layer)
return network
def save(self, filename: str) -> None:
"""Save the model to a file."""
model_data = {"layers": []}
for layer in self.layers:
if isinstance(layer, DenseLayer):
layer_data = {
"type": "dense",
"input_size": layer.input_size,
"output_size": layer.output_size,
"activation": layer.activation_name,
"weights": layer.weights.tolist(),
"biases": layer.biases.tolist(),
}
model_data["layers"].append(layer_data)
elif isinstance(layer, DropoutLayer):
layer_data = {"type": "dropout", "dropout_rate": layer.dropout_rate}
model_data["layers"].append(layer_data)
with open(filename, "w") as f:
json.dump(model_data, f)
class TextProcessor:
"""Class for processing text data."""
def __init__(self):
"""Initialize the text processor."""
self.vocabulary = []
self.vocabulary_size = 0
def tokenize(self, sentence: str) -> List[str]:
"""Tokenize a sentence."""
return re.findall(r"\w+", sentence.lower())
def build_vocabulary(self, sentences: List[str]) -> None:
"""Build the vocabulary from a list of sentences."""
vocabulary = set()
for sentence in sentences:
tokens = self.tokenize(sentence)
vocabulary.update(tokens)
self.vocabulary = sorted(list(vocabulary))
self.vocabulary_size = len(self.vocabulary)
def sentence_to_bow(self, sentence: str) -> np.ndarray:
"""Convert a sentence to a bag-of-words vector."""
tokens = self.tokenize(sentence)
vector = np.zeros((self.vocabulary_size, 1))
for token in tokens:
if token in self.vocabulary:
idx = self.vocabulary.index(token)
vector[idx, 0] = 1
return vector
def save(self, filename: str) -> None:
"""Save the text processor to a file."""
processor_data = {
"vocabulary": self.vocabulary,
"vocabulary_size": self.vocabulary_size,
}
with open(filename, "w") as f:
json.dump(processor_data, f)
@classmethod
def load(cls, filename: str) -> "TextProcessor":
"""Load a text processor from a file."""
with open(filename, "r") as f:
processor_data = json.load(f)
processor = cls()
processor.vocabulary = processor_data["vocabulary"]
processor.vocabulary_size = processor_data["vocabulary_size"]
return processor
class Chatbot:
"""Neural network based chatbot."""
def __init__(self):
"""Initialize the chatbot."""
self.intents = {}
self.text_processor = TextProcessor()
self.model = NeuralNetwork()
self.intent_names = []
self.confidence_threshold = 0.5
self.default_response = "I'm not sure I understand. Could you rephrase that?"
self.training_history = None
def load_intents(self, intents_data: Dict) -> None:
"""Load intents data."""
self.intents = intents_data
self.intent_names = list(self.intents.keys())
# Extract all patterns for building vocabulary
all_patterns = []
for intent in self.intents.values():
all_patterns.extend(intent["patterns"])
# Build vocabulary from patterns
self.text_processor.build_vocabulary(all_patterns)
def load_intents_from_file(self, filename: str) -> None:
"""Load intents from a JSON file."""
with open(filename, "r") as f:
intents_data = json.load(f)
self.load_intents(intents_data)
def save_intents(self, filename: str) -> None:
"""Save intents to a JSON file."""
with open(filename, "w") as f:
json.dump(self.intents, f, indent=4)
def load_model(self, filename: str) -> None:
"""Load a model from a file."""
self.model = NeuralNetwork.load(filename)
def save_model(self, filename: str) -> None:
"""Save the model to a file."""
self.model.save(filename)
# Also save the text processor and intent names
self.text_processor.save(filename.replace(".json", "_processor.json"))
# Save intent names
with open(filename.replace(".json", "_intents.json"), "w") as f:
json.dump(
{
"intent_names": self.intent_names,
"confidence_threshold": self.confidence_threshold,
"default_response": self.default_response,
},
f,
)
def build_model(
self, hidden_layers: List[int] = [8], dropout_rate: float = 0.0
) -> None:
"""Build the neural network model."""
# Input layer size is the vocabulary size
input_size = self.text_processor.vocabulary_size
# Output layer size is the number of intents
output_size = len(self.intent_names)
if output_size == 0:
raise ValueError("No intents loaded. Please load intents first.")
# Create the model
self.model = NeuralNetwork()
# Add first hidden layer
self.model.add(DenseLayer(input_size, hidden_layers[0], "relu"))
# Add dropout if needed
if dropout_rate > 0:
self.model.add(DropoutLayer(dropout_rate))
# Add additional hidden layers
for i in range(1, len(hidden_layers)):
self.model.add(DenseLayer(hidden_layers[i - 1], hidden_layers[i], "relu"))
# Add dropout if needed
if dropout_rate > 0:
self.model.add(DropoutLayer(dropout_rate))
# Add output layer with softmax activation for classification
self.model.add(DenseLayer(hidden_layers[-1], output_size, "softmax"))
# Set cross-entropy loss for classification
self.model.set_loss("cross_entropy")
def train(
self,
epochs: int = 1000,
learning_rate: float = 0.01,
batch_size: int = None,
verbose: bool = True,
) -> Dict:
"""Train the model with numerical stability fixes."""
# Prepare training data
X_train = []
y_train = []
for idx, intent in enumerate(self.intent_names):
for pattern in self.intents[intent]["patterns"]:
# Convert pattern to bag-of-words
X_train.append(self.text_processor.sentence_to_bow(pattern))
# Create one-hot encoded target
target = np.zeros((len(self.intent_names), 1))
target[idx, 0] = 1
y_train.append(target)
# Convert to numpy arrays
X_train = np.hstack(X_train)
y_train = np.hstack(y_train)
# Training history
history = {"loss": [], "accuracy": []}
# Apply gradient clipping to prevent exploding gradients
max_grad_norm = 1.0
# Training loop
for epoch in range(epochs):
# Forward pass
outputs = self.model.forward(X_train)
# Add small epsilon to prevent log(0)
epsilon = 1e-10
outputs = np.clip(outputs, epsilon, 1.0 - epsilon)
# Compute loss
loss = self.model.compute_loss(outputs, y_train)
# Check for NaN and if found, break training
if np.isnan(loss):
if verbose:
print(f"NaN loss detected at epoch {epoch+1}. Stopping training.")
# If we have previous good values, use those
if epoch > 0:
break
else:
# Otherwise, return with error
return {"loss": [0], "accuracy": [0]}
# Backward pass
self.model.backward(outputs, y_train)
# Apply gradient clipping to each layer
for layer in self.model.layers:
if hasattr(layer, "dW") and layer.dW is not None:
# Clip gradients
layer.dW = np.clip(layer.dW, -max_grad_norm, max_grad_norm)
if hasattr(layer, "db") and layer.db is not None:
layer.db = np.clip(layer.db, -max_grad_norm, max_grad_norm)
# Update parameters
self.model.update(learning_rate)
# Compute accuracy
predictions = np.argmax(outputs, axis=0)
targets = np.argmax(y_train, axis=0)
accuracy = np.mean(predictions == targets)
# Save history
history["loss"].append(
float(loss)
) # Convert to Python float to ensure it's serializable
history["accuracy"].append(float(accuracy))
# Print progress
if verbose and (epoch + 1) % 100 == 0:
print(
f"Epoch {epoch + 1}/{epochs}, Loss: {loss:.4f}, Accuracy: {accuracy:.4f}"
)
self.training_history = history
return history
def predict(self, sentence: str) -> Tuple[str, float]:
"""Predict the intent of a sentence."""
# Convert to bag-of-words
bow = self.text_processor.sentence_to_bow(sentence)
# Get prediction
prediction = self.model.predict(bow)
# Get predicted intent and confidence
intent_idx = np.argmax(prediction)
confidence = prediction[intent_idx, 0]
return self.intent_names[intent_idx], confidence
def get_response(self, sentence: str) -> Tuple[str, str, float]:
"""Get a response for a user input."""
intent, confidence = self.predict(sentence)
# Use default response if confidence is below threshold
if confidence < self.confidence_threshold:
return "unknown", self.default_response, confidence
# Get a random response for the predicted intent
responses = self.intents[intent]["responses"]
response = random.choice(responses)
return intent, response, confidence
def plot_training_history(self, history: Dict = None) -> None:
"""Plot the training history."""
if history is None:
history = self.training_history
if history is None:
print("No training history available.")
return
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(history["loss"])
plt.title("Model Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.subplot(1, 2, 2)
plt.plot(history["accuracy"])
plt.title("Model Accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.tight_layout()
plt.show()
def get_training_plot_as_base64(self, history: Dict = None) -> str:
"""Generate a base64 encoded image of the training history plot with improved error handling."""
if history is None:
history = self.training_history
if history is None or "loss" not in history or len(history["loss"]) == 0:
return None
try:
plt.figure(figsize=(12, 5))
# Check for NaN values and filter them out
loss_values = [x for x in history["loss"] if not np.isnan(x)]
acc_values = [x for x in history["accuracy"] if not np.isnan(x)]
if len(loss_values) == 0 or len(acc_values) == 0:
return None
# Plot loss (with error handling)
plt.subplot(1, 2, 1)
plt.plot(loss_values)
plt.title("Model Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
# Plot accuracy (with error handling)
plt.subplot(1, 2, 2)
plt.plot(acc_values)
plt.title("Model Accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.tight_layout()
# Save plot to a BytesIO object
buf = BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
# Encode to base64
img_str = base64.b64encode(buf.read()).decode("utf-8")
plt.close()
# Save the image to a file instead of returning the base64 string directly
# This avoids the file name too long error
img_path = "training_plot.png"
with open(img_path, "wb") as f:
f.write(base64.b64decode(img_str))
return img_path
except Exception as e:
print(f"Error generating training plot: {str(e)}")
return None
def chat(self):
"""Start a chat session in the console."""
print("Chatbot: Hello! Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() in ["quit", "exit", "bye"]:
print("Chatbot: Goodbye!")
break
intent, response, confidence = self.get_response(user_input)
print(f"Chatbot ({intent}, {confidence:.2f}): {response}")
# Initialize the chatbot
chatbot = Chatbot()
# Default intents
default_intents = {
"greeting": {
"patterns": ["Hi", "Hello", "Hey", "Good morning", "What's up"],
"responses": ["Hello!", "Hi there!", "Greetings!", "Hey! How can I help you?"],
},
"farewell": {
"patterns": ["Bye", "See you", "Goodbye", "Later", "I'm leaving"],
"responses": ["Goodbye!", "See you later!", "Farewell!", "Take care!"],
},
"thanks": {
"patterns": ["Thanks", "Thank you", "Much appreciated", "Appreciate it"],
"responses": ["You're welcome!", "No problem!", "Anytime!", "Glad to help!"],
},
"help": {
"patterns": ["Help", "I need help", "Can you help me", "Support"],
"responses": [
"How can I help you?",
"I'm here to assist you.",
"What do you need help with?",
],
},
}
# Function to initialize the chatbot
def initialize_chatbot():
global chatbot
# Check if model exists
model_path = "chatbot_model.json"
processor_path = "chatbot_model_processor.json"
intents_names_path = "chatbot_model_intents.json"
intents_path = "intents.json"
# Check if intents file exists
if os.path.exists(intents_path):
try:
chatbot.load_intents_from_file(intents_path)
print(f"Loaded intents from {intents_path}")
except Exception as e:
print(f"Error loading intents: {e}")
print("Loading default intents")
chatbot.load_intents(default_intents)
else:
print("No intents file found. Loading default intents")
chatbot.load_intents(default_intents)
# Save default intents
chatbot.save_intents(intents_path)
# Check if all model files exist
if (
os.path.exists(model_path)
and os.path.exists(processor_path)
and os.path.exists(intents_names_path)
):
try:
# Load the model
chatbot.load_model(model_path)
# Load the text processor
chatbot.text_processor = TextProcessor.load(processor_path)
# Load intent names and settings
with open(intents_names_path, "r") as f:
intents_data = json.load(f)
chatbot.intent_names = intents_data["intent_names"]
chatbot.confidence_threshold = intents_data.get(
"confidence_threshold", 0.5
)
chatbot.default_response = intents_data.get(
"default_response",
"I'm not sure I understand. Could you rephrase that?",
)
print(f"Loaded existing model from {model_path}")
except Exception as e:
print(f"Error loading model: {e}")
print("A new model will be built and trained")
chatbot.build_model(hidden_layers=[32, 16])
else:
print(
"No model found or incomplete model files. A new model will be built and trained"
)
chatbot.build_model(hidden_layers=[32, 16])
# Call initialize
initialize_chatbot()
# Chat history for the interface
chat_history = []
# Function to respond to user messages
def respond(message, history):
if not message:
return "Please type a message."
# Get response from chatbot
intent, response, confidence = chatbot.get_response(message)
# Add thinking animation (simulate processing)
time.sleep(0.5)
# Return the response
return response
# Function to get intent and confidence
def get_intent_info(message):
if not message:
return "N/A", 0.0
# Get intent and confidence
intent, confidence = chatbot.predict(message)
return intent, float(confidence)
# Function to add a new intent
def add_intent(intent_name, patterns, responses):
if not intent_name or not patterns or not responses:
return "Please fill all fields"
# Split patterns and responses
pattern_list = [p.strip() for p in patterns.split("\n") if p.strip()]
response_list = [r.strip() for r in responses.split("\n") if r.strip()]
if not pattern_list or not response_list:
return "Please provide at least one pattern and one response"
# Check if intent already exists
if intent_name in chatbot.intents:
# Update existing intent
chatbot.intents[intent_name]["patterns"].extend(pattern_list)
chatbot.intents[intent_name]["responses"].extend(response_list)
else:
# Add new intent
chatbot.intents[intent_name] = {
"patterns": pattern_list,
"responses": response_list,
}
chatbot.intent_names.append(intent_name)
# Save intents
chatbot.save_intents("intents.json")
return f"Intent '{intent_name}' added/updated successfully"
# Fixed train_model function with corrected format string
def train_model(epochs, learning_rate, hidden_layers_str, dropout_rate):
try:
# Parse hidden layers
hidden_layers = [
int(x.strip()) for x in hidden_layers_str.split(",") if x.strip()
]
if not hidden_layers:
return (
"Error: Invalid hidden layer format. Use comma-separated numbers, e.g. '32,16'",
None,
)
# Convert to float/int and use lower learning rate for stability
epochs = int(epochs)
learning_rate = min(
float(learning_rate), 0.005
) # Cap learning rate for stability
dropout_rate = float(dropout_rate)
# Validate intents and vocabulary
if len(chatbot.intent_names) < 2:
return (
"Error: Need at least 2 intents for training. Please add more intents.",
None,
)
if chatbot.text_processor.vocabulary_size == 0:
return (
"Error: No vocabulary built. Please add more patterns to your intents.",
None,
)
# Rebuild model with new architecture
chatbot.build_model(hidden_layers=hidden_layers, dropout_rate=dropout_rate)
# Train the model
history = chatbot.train(
epochs=epochs, learning_rate=learning_rate, verbose=True
)
# Check if training was successful
if not history or "loss" not in history or not history["loss"]:
return "Training failed - no history data returned", None
# Format final loss and accuracy safely
final_loss = history["loss"][-1] if history["loss"] else 0
final_accuracy = history["accuracy"][-1] if history["accuracy"] else 0
if np.isnan(final_loss):
final_loss_str = "NaN"
else:
final_loss_str = f"{final_loss:.4f}"
if np.isnan(final_accuracy):
final_accuracy_str = "NaN"
else:
final_accuracy_str = f"{final_accuracy:.4f}"
# Save the model
chatbot.save_model("chatbot_model.json")
# Generate plot image
img_str = chatbot.get_training_plot_as_base64(history)
return (
f"Model trained successfully with:\n"
f"- Epochs: {epochs}\n"
f"- Learning Rate: {learning_rate}\n"
f"- Hidden Layers: {hidden_layers}\n"
f"- Dropout Rate: {dropout_rate}\n"
f"- Final Loss: {final_loss_str}\n"
f"- Final Accuracy: {final_accuracy_str}"
), img_str
except Exception as e:
import traceback
error_details = traceback.format_exc()
return f"Error training model: {str(e)}\n\nDetails:\n{error_details}", None
# Function to load an existing model
def load_model_from_file(file_obj):
if not file_obj:
return "No file uploaded"
try:
file_path = file_obj.name
# Check file extension
if not file_path.endswith(".json"):
return "Please upload a JSON model file"
# Load the model
chatbot.load_model(file_path)
# Get the base name without extension for related files
base_name = os.path.splitext(file_path)[0]
processor_path = f"{base_name}_processor.json"
intents_names_path = f"{base_name}_intents.json"
# Check for related files
if os.path.exists(processor_path):
chatbot.text_processor = TextProcessor.load(processor_path)
if os.path.exists(intents_names_path):
with open(intents_names_path, "r") as f:
intents_data = json.load(f)
chatbot.intent_names = intents_data["intent_names"]
chatbot.confidence_threshold = intents_data.get(
"confidence_threshold", 0.5
)
chatbot.default_response = intents_data.get(
"default_response",
"I'm not sure I understand. Could you rephrase that?",
)
return f"Model loaded successfully from {file_path}"
except Exception as e:
return f"Error loading model: {str(e)}"
# Function to save the current model
def save_model():
try:
# Get timestamp for filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"chatbot_model_{timestamp}.json"
# Save the model
chatbot.save_model(filename)
return f"Model saved as {filename}"
except Exception as e:
return f"Error saving model: {str(e)}"
# Function to update settings
def update_settings(threshold, default_response):
try:
# Update settings
chatbot.confidence_threshold = float(threshold)
chatbot.default_response = default_response
# Save settings to the model intents file
with open("chatbot_model_intents.json", "w") as f:
json.dump(
{
"intent_names": chatbot.intent_names,
"confidence_threshold": chatbot.confidence_threshold,
"default_response": chatbot.default_response,
},
f,
)
return "Settings updated successfully"
except Exception as e:
return f"Error updating settings: {str(e)}"
# Function to list intents
def list_intents():
if not chatbot.intents:
return "No intents available"
intents_info = ""
for intent_name, intent_data in chatbot.intents.items():
patterns = ", ".join(intent_data["patterns"][:3])
if len(intent_data["patterns"]) > 3:
patterns += "..."
responses = ", ".join(intent_data["responses"][:3])
if len(intent_data["responses"]) > 3:
responses += "..."
intents_info += f"**Intent**: {intent_name}\n"
intents_info += f"**Patterns**: {patterns}\n"
intents_info += f"**Responses**: {responses}\n\n"
return intents_info
# Function to edit an intent
def edit_intent(intent_name, new_patterns, new_responses):
if not intent_name or intent_name not in chatbot.intents:
return f"Intent '{intent_name}' not found"
# Split patterns and responses
if new_patterns:
pattern_list = [p.strip() for p in new_patterns.split("\n") if p.strip()]
if pattern_list:
chatbot.intents[intent_name]["patterns"] = pattern_list
if new_responses:
response_list = [r.strip() for r in new_responses.split("\n") if r.strip()]
if response_list:
chatbot.intents[intent_name]["responses"] = response_list
# Save intents
chatbot.save_intents("intents.json")
return f"Intent '{intent_name}' updated successfully"
# Function to delete an intent
def delete_intent(intent_name):
if not intent_name or intent_name not in chatbot.intents:
return f"Intent '{intent_name}' not found"
# Delete intent
del chatbot.intents[intent_name]
chatbot.intent_names.remove(intent_name)
# Save intents
chatbot.save_intents("intents.json")
return f"Intent '{intent_name}' deleted successfully"
# Get the list of intents for dropdown
def get_intent_list():
return chatbot.intent_names
# Function to export intents
def export_intents():
try:
# Get timestamp for filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"intents_{timestamp}.json"
# Save intents
with open(filename, "w") as f:
json.dump(chatbot.intents, f, indent=4)
return f"Intents exported as {filename}"
except Exception as e:
return f"Error exporting intents: {str(e)}"
# Function to import intents
def import_intents_from_file(file_obj):
if not file_obj:
return "No file uploaded"
try:
file_path = file_obj.name
# Check file extension
if not file_path.endswith(".json"):
return "Please upload a JSON intents file"
# Load intents
with open(file_path, "r") as f:
intents_data = json.load(f)
# Validate intents format
for intent_name, intent_data in intents_data.items():
if (
not isinstance(intent_data, dict)
or "patterns" not in intent_data
or "responses" not in intent_data
):
return f"Invalid intent format for '{intent_name}'"
# Update chatbot intents
chatbot.load_intents(intents_data)
# Save intents
chatbot.save_intents("intents.json")
return f"Imported {len(intents_data)} intents from {file_path}"
except Exception as e:
return f"Error importing intents: {str(e)}"
# Function to get intent details
def get_intent_details(intent_name):
if not intent_name or intent_name not in chatbot.intents:
return "", ""
patterns = "\n".join(chatbot.intents[intent_name]["patterns"])
responses = "\n".join(chatbot.intents[intent_name]["responses"])
return patterns, responses
# Create the Gradio interface with multiple tabs
with gr.Blocks(title="Neural Network Chatbot", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 Neural Network Chatbot")
gr.Markdown(
""" This chatbot uses a neural network to understand and respond to your messages.
This chatbot application was developed by:
| **Name** | **Student ID** | **Email** |
|----------|----------------|-----------|
| AARJEYAN SHRESTHA | C0927422 | C0927422@mylambton.ca |
| PRAJWAL LUITEL | C0927658 | C0927658@mylambton.ca |
| RAJAN GHIMIRE | C0924991 | C0924991@mylambton.ca |
| RISHABH JHA | C0923563 | C0923563@mylambton.ca |
| SUDIP CHAUDHARY | C0922310 | C0922310@mylambton.ca |
- **Course**: Software Tools and Emerging Technologies for AI and ML
- **Term**: 3rd
- **Instructor**: [Peter Sigurdson](https://www.linkedin.com/in/petersigurdson/)
"""
)
with gr.Tabs():
# Chat tab
with gr.Tab("Chat"):
with gr.Row():
with gr.Column(scale=3):
chatbot_interface = gr.Chatbot(label="Conversation", height=400)
with gr.Row():
msg = gr.Textbox(
placeholder="Type your message here...",
label="Your message",
lines=2,
show_label=False,
)
send_btn = gr.Button("Send", variant="primary")
with gr.Accordion("Examples", open=False):
gr.Examples(
examples=[
"Hello!",
"How are you?",
"What can you help me with?",
"Thank you",
"Goodbye",
],
inputs=msg,
)
with gr.Column(scale=1):
gr.Markdown("### Analysis")
intent_label = gr.Label(label="Predicted Intent")
confidence_score = gr.Number(label="Confidence Score")
gr.Markdown("### Settings")
confidence_slider = gr.Slider(
label="Confidence Threshold",
minimum=0.0,
maximum=1.0,
step=0.05,
value=chatbot.confidence_threshold,
)
default_resp = gr.Textbox(
label="Default Response",
value=chatbot.default_response,
lines=2,
)
update_settings_btn = gr.Button("Update Settings")
# Event handlers for chat
def user_message(user_message, history):
return "", history + [[user_message, None]]
def bot_message(history):
if history:
user_message = history[-1][0]
intent, response, confidence = chatbot.get_response(user_message)
history[-1][1] = response
return history, intent, confidence
return history, "N/A", 0.0
msg.submit(
user_message,
[msg, chatbot_interface],
[msg, chatbot_interface],
queue=False,
).then(
bot_message,
chatbot_interface,
[chatbot_interface, intent_label, confidence_score],
)
send_btn.click(
user_message,
[msg, chatbot_interface],
[msg, chatbot_interface],
queue=False,
).then(
bot_message,
chatbot_interface,
[chatbot_interface, intent_label, confidence_score],
)
update_settings_btn.click(
update_settings,
[confidence_slider, default_resp],
gr.Textbox(label="Status"),
)
# Intents Management tab
with gr.Tab("Intents Management"):
with gr.Row():
with gr.Column():
gr.Markdown("### Add New Intent")
new_intent_name = gr.Textbox(label="Intent Name")
new_patterns = gr.Textbox(label="Patterns (one per line)", lines=5)
new_responses = gr.Textbox(
label="Responses (one per line)", lines=5
)
add_intent_btn = gr.Button("Add Intent", variant="primary")
add_intent_status = gr.Textbox(label="Status")
with gr.Column():
gr.Markdown("### Edit Intent")
edit_intent_dropdown = gr.Dropdown(
label="Select Intent to Edit",
choices=get_intent_list(),
interactive=True,
)
edit_patterns = gr.Textbox(label="Patterns (one per line)", lines=5)
edit_responses = gr.Textbox(
label="Responses (one per line)", lines=5
)
with gr.Row():
update_intent_btn = gr.Button("Update Intent")
delete_intent_btn = gr.Button("Delete Intent", variant="stop")
edit_intent_status = gr.Textbox(label="Status")
with gr.Row():
with gr.Column():
gr.Markdown("### Import/Export Intents")
with gr.Row():
export_intents_btn = gr.Button("Export Intents")
import_intents_file = gr.File(
label="Import Intents (JSON file)"
)
import_export_status = gr.Textbox(label="Status")
with gr.Column():
gr.Markdown("### Current Intents")
refresh_intents_btn = gr.Button("Refresh Intents List")
intents_list = gr.Markdown()
# Event handlers for intents management
add_intent_btn.click(
add_intent,
[new_intent_name, new_patterns, new_responses],
add_intent_status,
)
# Update dropdown when adding/deleting intents
add_intent_btn.click(get_intent_list, [], edit_intent_dropdown)
edit_intent_dropdown.change(
get_intent_details,
edit_intent_dropdown,
[edit_patterns, edit_responses],
)
update_intent_btn.click(
edit_intent,
[edit_intent_dropdown, edit_patterns, edit_responses],
edit_intent_status,
)
delete_intent_btn.click(
delete_intent, edit_intent_dropdown, edit_intent_status
).then(get_intent_list, [], edit_intent_dropdown)
export_intents_btn.click(export_intents, [], import_export_status)
import_intents_file.change(
import_intents_from_file, import_intents_file, import_export_status
).then(get_intent_list, [], edit_intent_dropdown)
refresh_intents_btn.click(list_intents, [], intents_list)
# Training tab
with gr.Tab("Training"):
with gr.Row():
with gr.Column():
gr.Markdown("### Train Model")
epochs_input = gr.Number(
label="Epochs", value=500, minimum=100, maximum=5000, step=100
)
learning_rate_input = gr.Number(
label="Learning Rate",
value=0.01,
minimum=0.0001,
maximum=0.1,
step=0.001,
)
hidden_layers_input = gr.Textbox(
label="Hidden Layers (comma-separated)", value="32, 16"
)
dropout_rate_input = gr.Number(
label="Dropout Rate",
value=0.2,
minimum=0.0,
maximum=0.5,
step=0.05,
)
train_btn = gr.Button("Train Model", variant="primary")
with gr.Column():
training_status = gr.Textbox(label="Training Status", lines=6)
training_plot = gr.Image(label="Training History")
with gr.Row():
with gr.Column():
gr.Markdown("### Model Management")
save_model_btn = gr.Button("Save Current Model")
load_model_file = gr.File(label="Load Model (JSON file)")
model_status = gr.Textbox(label="Status")
# Event handlers for training
train_btn.click(
train_model,
[
epochs_input,
learning_rate_input,
hidden_layers_input,
dropout_rate_input,
],
[training_status, training_plot],
)
save_model_btn.click(save_model, [], model_status)
load_model_file.change(load_model_from_file, load_model_file, model_status)
# About tab
with gr.Tab("About"):
gr.Markdown(
"""
## Neural Network Chatbot
This chatbot uses a neural network to understand and respond to user messages.
The model is trained on a set of intents, each with patterns and responses.
### Features:
- **Neural Network Backend**: The chatbot uses a fully-connected neural network with configurable layers.
- **Intent Recognition**: Recognizes user intents based on trained patterns.
- **Customizable Responses**: Each intent has multiple possible responses for variety.
- **Training Interface**: Train the model directly from the web interface.
- **Intent Management**: Add, edit, delete, import, and export intents.
- **Model Management**: Save and load models for future use.
### How to Use:
1. **Chat Tab**: Interact with the chatbot.
2. **Intents Management Tab**: Manage the chatbot's knowledge.
3. **Training Tab**: Train the neural network model.
4. **About Tab**: Learn about the chatbot and its features.
### Technical Details:
- Built with Python, NumPy, and Gradio.
- Uses a bag-of-words approach for text representation.
- Neural network with configurable hidden layers and activation functions.
- Cross-entropy loss for multi-class classification.
Created for deployment on Hugging Face Spaces.
"""
)
# Call initialize again after defining the UI
# to make sure dropdown is populated
chat_intents = get_intent_list()
# Launch the app
if __name__ == "__main__":
demo.launch()
|