Spaces:
Sleeping
Sleeping
File size: 2,185 Bytes
70c7b07 dc60757 70c7b07 |
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 |
import torch as tr
import numpy as np
from Model.model import WordModel
import string
import json
import random
import re
import nltk
from nltk.stem import PorterStemmer
nltk.download('punkt')
with open('./Model/intents.json', 'r', encoding='UTF-8') as fp:
intents = json.load(fp)
CONFIG = tr.load('./Model/save.pth')
def load_chat_model():
model = WordModel(IN_DIMS=CONFIG['input_size'], HIDDEN_DIMS=CONFIG['hidden_size'], VOCAB_SIZE=CONFIG['output_size'])
model.load_state_dict(CONFIG['model_state'])
model.eval()
return model
class Agent:
MODEL_NAME = 'Garmento Agent'
stemmer = PorterStemmer()
model = load_chat_model()
def preprocess_text(self, text:str)->list[str]:
text = text.lower()
text = re.sub(pattern=f'[{string.punctuation}]', repl='', string=text)
text = nltk.tokenize.word_tokenize(text)
return text
def stemming(self, world_list:list[str])->list[str]:
return list(map(self.stemmer.stem, world_list))
def bag_of_words(self, tokens:list[str], vocab:list[str])->list[str]:
tokens = self.stemming(tokens)
bow = np.zeros(shape=(len(vocab)), dtype=np.float32)
for idx, token in enumerate(vocab):
if token in tokens:
bow[idx] = 1
return bow
def agent_response(self, user_input:str)->str:
tokens = self.preprocess_text(user_input)
vector = self.bag_of_words(tokens=tokens, vocab=CONFIG['VOCAB'])
vector = tr.from_numpy(vector[None, :])
logits = self.model(vector)
tag_id = tr.argmax(logits, dim=-1)
tag = CONFIG['tags'][tag_id.item()]
confidence = tr.softmax(logits, dim=-1)
for intent in intents['intents']:
if intent['tag'] == tag:
response = f"{random.choice(intent['responses'])}"
if confidence.max() < 0.7:
response += ". <br> I am an AI-agent trained to represent my organisation. If my response is not relevant to your query. Kindly connect to a our customer support team. ☎️ Contact information : +91-999-9999-999 "
return response, confidence.max() |