From d0d0d1e002749ad3c4c8fa88507ebcb401721aac Mon Sep 17 00:00:00 2001 From: Sree Vidya Date: Tue, 23 Jul 2024 17:56:30 +0530 Subject: [PATCH] Add files via upload --- AI_CHATBOT/README.md | 4 ++ AI_CHATBOT/chat.py | 53 +++++++++++++++ AI_CHATBOT/data.pth | Bin 0 -> 5674 bytes AI_CHATBOT/intents.json | 138 +++++++++++++++++++++++++++++++++++++++ AI_CHATBOT/model.py | 19 ++++++ AI_CHATBOT/nitkutils.py | 0 AI_CHATBOT/nltk_utils.py | 23 +++++++ AI_CHATBOT/train.py | 108 ++++++++++++++++++++++++++++++ 8 files changed, 345 insertions(+) create mode 100644 AI_CHATBOT/README.md create mode 100644 AI_CHATBOT/chat.py create mode 100644 AI_CHATBOT/data.pth create mode 100644 AI_CHATBOT/intents.json create mode 100644 AI_CHATBOT/model.py create mode 100644 AI_CHATBOT/nitkutils.py create mode 100644 AI_CHATBOT/nltk_utils.py create mode 100644 AI_CHATBOT/train.py diff --git a/AI_CHATBOT/README.md b/AI_CHATBOT/README.md new file mode 100644 index 0000000..4880f4d --- /dev/null +++ b/AI_CHATBOT/README.md @@ -0,0 +1,4 @@ +# AI_CHATBOT +It is an Ai chatbot developed using natural language toolkit NLTK, pytorch +I have developed this bot for a Petari website +Feel free to update the intents.json to make the domain of the chatbot wider. diff --git a/AI_CHATBOT/chat.py b/AI_CHATBOT/chat.py new file mode 100644 index 0000000..333859d --- /dev/null +++ b/AI_CHATBOT/chat.py @@ -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...") \ No newline at end of file diff --git a/AI_CHATBOT/data.pth b/AI_CHATBOT/data.pth new file mode 100644 index 0000000000000000000000000000000000000000..c3fea7a6a57575a2ad8924c1ee133eab7cbb79d7 GIT binary patch literal 5674 zcmb7I30PCd77ic~Q9uM01QmCz$`()%?ioM?G$@FuxMCm|6AcNITtHM@0TmZqS(Vlu z+^cox&c#+M?rYuk`4rr$*4ACDzI$)f0`Ik-?|$Eqb5G9ypMPdfW|)bVn>CV1q*BR$ zJ=T&I5~YGscnE*oQzmM{LhK|X8$L5kGqJ=it_crM)+uRCyn#_LG&V_0)M+$yBBR!6 z4OUTlC9S8Gq3T4&3Y&I|!Deo8jd_H|%RQY|k5@6cQ6i($C#u}zQyI0!U=^>Y6H?V0 zWjsS`4LW^%nzt1;cXNxgOvUF?S*VO$Uf536 zST1F7bCDt&+sjR|v4aubg263ga4TbPyg&EGu^xdpcH#+w7~I;ZL7Ib|c`3XG7b8g< z2DgpD?Ti|{jT&6*QOIz6qlONjG;|a+bTYy_Gq_6(?rPK^`>esO9)S<;#uNB4_#1)1 zQqF7WZq(4jNYazR?lIWIs6l4b;8~Bt7klv(mJIe5DYCIl(BNZ)`!cv!4DOwpiTy+! z@yRr!5DEwTi^JT|1%f*^f;K(=&o?nreNT)D(V4S(|qDe?i z#)FKuHiPd_lvsEQJ&mJ9D#VB<+&gjz7c!U_phpAKW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