def process():
    bot_name = "Sam"
    sentence = request.form['user_input']
    question = sentence
    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"]:
                f = open("file.txt", "a")
                f.write(question + '\n' + random.choice(intent['responses']) +
                        '\n\n')
                return render_template('index.html',
                                       user_input=question,
                                       bot_response=random.choice(
                                           intent['responses']))
                #print(f"{bot_name}: {random.choice(intent['responses'])}")
    else:
        f = open("file.txt", "a")
        f.write(question + '\n' + 'I donot understand\n\n')
        return render_template('index.html',
                               user_input=question,
                               bot_response="I don't understand")
Exemple #2
0
def response(text):
    #sentence = input("You: ")
    sentence = text
    # 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'])}")
                return f"{bot_name}: {random.choice(intent['responses'])}"
    else:
        #print(f"{bot_name}: I do not understand...")
        return f"{bot_name}: I do not understand..."
Exemple #3
0
def bot_out(sentence):
    sentence = sentence
    sentence = tokenize(sentence) ## apply tokenization 
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(device) ## get our final format of user input 

    output = model(X)
    _, predicted = torch.max(output, dim=1) ## get the highst probabily of the output sentence 

    tag = tags[predicted.item()]

    probs = torch.softmax(output, dim=1) ## apply soft max on the output array 
    prob = probs[0][predicted.item()]
    out = ""
    ## check if this probabilty is sufficent for make reply to clint or not 
    if prob.item() > 0.75:
        for intent in intents['intents']:
            if tag == intent["tag"]:
                ## out send to web 
                out = random.choice(intent['responses'])
                out = f"{bot_name}: {out}"                
    else:
        ## chatbot out if the input word is far away from our data 
        out = f"{bot_name}: I do not understand..."
    return out
Exemple #4
0
def get_response(msg):
    msg = msg.lower()
    sentence = tokenize(msg)
    X = bag_of_words(sentence, all_words)
    # reshape to the shape required by the model ie 1 row and <len(all_words)> columns
    X = X.reshape(1, X.shape[0])
    # creates a tensor from X
    X = torch.from_numpy(X)

    # get the model output, which will basically contain the output values for all the tags
    output = model(X)
    # reduce to 1 dimension
    _, predicted = torch.max(output, dim=1)
    # (predicted is a tensor with index of the highest output valuethe and .item() gives the content inside it
    #  bascially what is the index of the tag with highest output
    tag = tags[predicted.item()]

    # convert the output value of the model to probablities using softmax activation function
    # dim = 1 to get 1 dimensional output, we're dealing with 1-D anyways
    probs = torch.softmax(output, dim=1)
    # now pick the highest probability
    prob = probs[0][predicted.item()]

    # check if there's a 50% probability that the user's input is similar to the patterns in the predicted tag
    # if yes, then return a random response from the tag, else return "do not understand"
    if prob.item() > 0.50:
        for intent in intents["intents"]:
            if tag == intent["tag"]:
                return random.choice(intent['responses']), tag

    else:
        return "I'm sorry, I do not understand you.", "noans"
Exemple #5
0
    def decide(self, spoken_text):
        """
        This function utilizes a neural network to determine
        which feature to run based on the input text.

        See for More: https://www.techwithtim.net/tutorials/ai-chatbot/
        """
        text = tokenize(spoken_text)
        x = bag_of_words(text, self.all_words)
        x = x.reshape(1, x.shape[0])
        x = torch.from_numpy(x).to(self.device)

        output = self.model(x)
        _, predicted = torch.max(output, dim=1)

        probs = torch.softmax(output, dim=1)
        prob = probs[0][predicted.item()]

        predicted_tag = self.tags[predicted.item()]
        predicted_tag_probability = prob.item()

        # if no accurate action is found from
        # spoken_text, default to chatbot feature.
        tag = predicted_tag if predicted_tag_probability >= 0.8 else 'chatbot'
        return tag
def get_response(sentence):

    sentence = tokenize(sentence)
    sentence = [s_w.lower() for s_w in sentence]
    sentence = convert_and_pad(all_words, sentence)
    sentence.reshape(1, sentence.shape[0])
    sentence = torch.from_numpy(sentence)

    output = model(sentence.float())
    probs = torch.softmax(output, dim=0)
    ps, idx = torch.sort(probs, descending=True)
    ps = ps.detach().numpy()
    idx = idx.detach().numpy()
    ps, idx = ps[0], idx[0]
    tag = tags[idx]

    # Getting output probabilities
    # probs = torch.softmax( output, dim=0 )
    # prob = probs[ 0 ][ prediction.item() ]

    if ps >= 0.75:
        for intent in intents["intents"]:
            if tag == intent["tag"]:
                return random.choice(intent['responses'])

    else:
        return random.choice(response_1)
def send():
    msg = EntryBox.get("1.0", 'end-1c').strip()
    EntryBox.delete("0.0", END)

    if msg != '':
        ChatLog.config(state=NORMAL)
        ChatLog.insert(END, "You: " + msg + '\n\n')
        ChatLog.config(foreground="#442265", font=("Verdana", 12))

        sentence = tokenize(msg)
        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"]:
                    res = random.choice(intent['responses'])
        else:
            res = "Sorry, but I do not understand..."

        ChatLog.insert(END, bot_name + ": " + res + '\n\n')
        ChatLog.config(state=DISABLED)
        ChatLog.yview(END)
Exemple #8
0
    def __init__(self, intents_file_path, model_name='data'):
        self.model_name = model_name
        with open(intents_file_path, 'r') as f:
            intents = json.load(f)

        self.all_words = []
        self.tags = []
        self.xy = []
        self.x_train = []
        self.y_train = []
        # loop through each sentence in intents patterns
        for intent in intents['intents']:
            tag = intent['tag']
            self.tags.append(tag)
            for pattern in intent['patterns']:
                # tokenize each word in the sentence
                w = tokenize(pattern)
                # add to our words list
                self.all_words.extend(w)
                # add to xy pair
                self.xy.append((w, tag))

        # stem and lower each word
        ignore_words = ['?', '.', '!']
        self.all_words = [
            stem(w) for w in self.all_words if w not in ignore_words
        ]
        # remove duplicates and sort
        self.all_words = sorted(set(self.all_words))
        self.tags = sorted(set(self.tags))
Exemple #9
0
    async def on_message(self, message):
        if message.author.bot:
            return

        print(message.content)
   
    
        # sentence = "Merhaba"
        sentence = str(message.content)

        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'])}")
                    await message.channel.send(f"{random.choice(intent['responses'])}")
        else:
            print(f"{bot_name}: Anlayamadim. Tekrar sorar misin?")
            await message.channel.send(f"{random.choice(intent['responses'])}")
Exemple #10
0
def get_scarlet_renponse(mensaje):
    if mensaje == "quit":
        return 'Adiós putoooo'

    mensaje = tokenize(mensaje)
    x = bag_of_words(mensaje, 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"]:
                update_file_acierto(tag, prob.item(), mensaje)
                # Comprueba si es una funcion
                if "script" in tag:
                    return script_response(random.choice(intent['responses']))

                update_sentimiento(tag)
                print("Sentimiento actual: " + str(get_valor("sentimientos")))
                return random.choice(intent['responses'])

    update_file_error(tag, prob.item(), mensaje)
    return "Lo siento, no te he entendido"
Exemple #11
0
def fxn(input_str):
    # sentence = "do you use credit cards?"
    # sentence = input("You: ")
    sentence = input_str

    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()]
    x = None
    iflag = False

    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"]:
                x = (f"{random.choice(intent['responses'])}")
                if intent["tag"]== "ask_review":
                    iflag = True
    else:
        x = (f" I do not understand...")
    return x , iflag
Exemple #12
0
def chat():
    
    user_responses = []
    bot_responses = []
    sentence1 = request.form['user_input']
    user_responses.append(sentence1)
    sentence = tokenize(sentence1)
    stemmed_words = [ stem(w) for w in sentence]
    no_of_pizza=0
    order_id = 0
    sts = ['Your food is being prepared', 'Our executive is out for delivery', 'Sorry for the inconvinence.. you will get you order within 10 mins']
    order_sts = sts[random.randint(0,2)]
    for w in stemmed_words:
        if w == 'order' or w =='want' or w == 'need':
            for wrd in stemmed_words:
                if re.match('^[0-9]*$', wrd):
                    no_of_pizza = int(wrd)
                    choices = list(range(100))
                    random.shuffle(choices)
                    order_id = choices.pop()
                    sts = ['Your food is being prepared', 'Our executive is out for delivery', 'Sorry for the inconvinence.. you will get you order within 10 mins']
                    order_sts = sts[random.randint(0,2)]
                    order_details = {'_id': order_id , 'Address':'none', 'Status': order_sts}
                    collection.insert_one(order_details)
                    bot = "your order has been recorded and your order id is {order_id}, kindly provide us delivery details"
                    return render_template('index.html', user_input=sentence1, bot_response = bot )
        elif w == 'address':
            result = collection.update({"_id":order_id}, {"$set":{"Address": sentence1}})
            bot = "your delivery details are recorded status of your order: {order_sts}"
            return render_template('index.html', user_input=sentence1, bot_response = bot )
        elif w == 'status':
            results = collection.find_one({"_id":order_id})
            if results == 'None':
                bot = 'No orderFound in this id'
                return render_template('index.html', user_input=sentence1, bot_response = bot )
            else:
                bot = results.get('Status')
                return render_template('index.html', user_input=sentence1, bot_response = bot )
                
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X)
    
    output = model(X)
    _, predicted = torch.max(output, dim=1)
    tag = tags[predicted.item()]
    
    probs = torch.softmax(output, dim =1)
    prob = probs[0][predicted.item()] 
    
       
    for intent in intents['intents']:
            if tag == intent['tag']:
                bot = random.choice(intent["responses"])
                return render_template('index.html', user_input=sentence1, bot_response = bot )
Exemple #13
0
def sms_reply():
    with open('dis.json', 'r') as f:
        intents = json.load(f)
    if torch.cuda.is_available():
        map_location = lambda storage, loc: storage.cuda()
    else:
        map_location = 'cpu'

    FILE = "data.pth"
    data = torch.load(FILE, map_location=map_location)

    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)
    model.load_state_dict(model_state)
    model.eval()
    while True:
        # Fetch the message
        msg = request.form.get('Body')
        #sentence = input(msg)
        #if sentence == "quit":
        #    break

        #sentence = tokenize(sentence)
        msg = tokenize(msg)
        X = bag_of_words(msg, all_words)
        X = X.reshape(1, X.shape[0])
        X = torch.from_numpy(X)

        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"]:
                    # Create reply
                    resp = MessagingResponse()
                    resp.message(
                        random.choice(intent['responses']).format(msg))

        else:
            resp = MessagingResponse()
            resp.message("I do not understand...".format(msg))

        return str(resp)
Exemple #14
0
def input_process(user_input):
    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 = "Ash"
    print("Let's chat! (type 'quit' to exit)")
    while True:
        #sentence = input("You: ")
        sentence = user_input
        #if sentence == "quit":
        #break

        sentence = tokenize(sentence)
        print(sentence)
        print(all_words)
        X = bag_of_words(sentence, all_words)
        print(X)
        X = X.reshape(1, X.shape[0])
        print(X)
        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'])}")
                    return random.choice(intent['responses'])
        else:
            #print(f"{bot_name}: I do not understand...")
            return "I do not understand..."
Exemple #15
0
def nlu(sentence):
    query = sentence
    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_wordsh)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(device)  #for neuralnetwoek
    output = modelh(X)
    _, predicted = torch.max(output, dim=1)
    tag = tagsh[predicted.item()]
    print(tag)
    result = ""
    probs = torch.softmax(output, dim=1)
    prob = probs[0][predicted.item()]
    if prob.item() > 0.7:
        for intent in history['intents']:
            if tag == intent["tag"]:
                webbrowser.open(random.choice(intent['responses']))
                return (f"{random.choice(intent['responses'])}")
    else:
        sentence = tokenize(query)
        X = bag_of_words(sentence, all_words)
        X = X.reshape(1, X.shape[0])
        # X = torch.from_numpy(X).to(dtype=torch.long).to(device) for lstm
        X = torch.from_numpy(X).to(device)
        output = model(X)
        _, predicted = torch.max(output, dim=1)

        tag = tags[predicted.item()]
        result = ""
        probs = torch.softmax(output, dim=1)
        prob = probs[0][predicted.item()]
        if prob.item() > 0.6:
            pass
        else:
            # print(f"{bot_name}: I do not understand...")
            tag = "search on the web"

        result = answer(tag, query)
        return result
Exemple #16
0
def predict_class(sentence, model):
    # filter out predictions below a threshold
    sentence = tokenize(sentence)
    p = bag_of_words(sentence, words)
    res = model.predict(np.array([p]))[0]
    ERROR_THRESHOLD = 0.25
    results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
    # sort by strength of probability
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []
    for r in results:
        return_list.append({"intent": tags[r[0]], "probability": str(r[1])})
    return return_list
Exemple #17
0
def get_tag_probability(message):
    sentence = tokenize(message.content)
    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()]
    return (tag, prob)
Exemple #18
0
def solve(msg):
    with open('intents.json', 'r', encoding='utf-8', errors='ignore') as f:
        intents = json.load(f)

    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()
    global btags
    global bques
    sentence = msg
    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(dtype=torch.float).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"]:
                if tag not in btags:
                    btags += tag + " - "
                    response = random.choice(intent['responses'])
                    return (response) + "\n\n"
                else:
                    return ""
            else:
                continue
    else:
        if msg not in bques:
            bques += msg + " - "
            global bnon
            bnon = True
            return msg + ": Mình tạm thời chưa có đáp án \n Bạn hãy để lại sđt và câu hỏi tại mục report hoặc liên hệ trực tiếp số điện thoại 0868355415" + "\n\n"
        else:
            return ""
Exemple #19
0
def intentDetection(sentence):
    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()]

    #print(prob.item())

    return tag, prob.item()
def prepare_train_data(): 
    import numpy as np
    import json
    from nltk.corpus import stopwords
    from nltk_utils import tokenize, stem, convert_and_pad

    with open( "intents.json", "r" ) as f:
        data = json.load( f )

    all_words = [ ]
    tags = [ ]
    x_y = [ ]

    for d_point in data[ "intents" ]:
        tag = d_point[ 'tag' ]
        tags.append( tag )
        
        for pattern in d_point[ "patterns" ]:
            word = tokenize( pattern )
            word = [ s_w.lower() for s_w in word ]
            all_words.extend( word )
            x_y.append( (word, tag) )

    signs = [ '!', '?', ',', '.']
    all_words = [ stem( w ) for w in all_words if w not in signs ]
    all_words = sorted( set(all_words) )
    tags = sorted( set(tags) )

    X_train = []
    y_train = []

    for ( pattern_sentence, tag ) in x_y:
        bag_of_words = convert_and_pad( all_words, pattern_sentence )
        X_train.append( bag_of_words )

        label = tags.index( tag )
        y_train.append( float(label) )
    y_train = np.array( y_train )
    
    ## Testing fuction/
    # sents = ['hi', 'hello', 'i', 'you', 'bye', 'thank', 'cool']
    # ss = ['hello', 'how', 'are', 'you']
    # print(convert_and_pad(sents, ss))

    return X_train, y_train, [ all_words, tags, x_y ]
Exemple #21
0
def bot_response(msg):
    sentence = tokenize(msg)
    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"]:
                return random.choice(intent['responses'])
    return "I do not understand..."
Exemple #22
0
def chatbot(question):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    with open('intents.json', 'r',encoding="utf8") as json_data:
        intents = json.load(json_data)

    FILE = "data.pth"
    data = torch.load(FILE,map_location='cpu')

    input_size = data["input_size"]
    hidden_size00 = data["hidden_size00"]
    hidden_size01 = data["hidden_size01"]
    hidden_size02 = data["hidden_size02"]
    output_size = data["output_size"]
    all_words = data['all_words']
    tags = data['tags']
    model_state = data["model_state"]

    model = NeuralNet(input_size, hidden_size00, hidden_size01, hidden_size02, output_size).to(device)
    model.load_state_dict(model_state)
    model.eval()
    
    while True:
        # sentence = "do you use credit cards?"
        sentence = question
        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"]:
                    return(random.choice(intent['responses']))
        else:
            return('Sorry I do not understand..')
def get_response(sentence):
    sentence = tokenize(sentence)
    bag = bag_of_words(sentence, words)
    bag = bag.reshape(1, bag.shape[0])
    bag = torch.from_numpy(bag).to(device)

    output = model(bag)
    _, predicted = torch.max(output, dim=1)
    tag = tags[predicted.item()]

    probabilities = torch.softmax(output, dim=1)
    prob = probabilities[0][predicted.item()]

    if (prob.item() >= probability):
        for intent in intents["intents"]:
            if tag == intent["tag"]:
                return random.choice(intent["responses"])
    else:
        return (f"I don't understand that, sorry...")
Exemple #24
0
def echo(update, context):
    sentence = tokenize(update.message.text)
    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']:
                update.message.reply_text(random.choice(intent['responses']))
    else:
        update.message.reply_text('Mohon maaf saya tidak mengerti ...')
Exemple #25
0
def get_answer(sentence: str) -> str:
    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])

    output = model(X)
    tag = tags[np.argmax(output)]
    # print(tag)

    softmax = tf.keras.layers.Softmax()
    probs = softmax(output).numpy()
    # print(probs)
    prob = probs[0][np.argmax(output)]
    if prob > 0.75:
        for intent in intents['intents']:
            if tag == intent['tag']:
                return random.choice(intent['responses'])
    else:
        return "Desculpe, não sei nada sobre isso..."
Exemple #26
0
def chat():
    sentence = tokenize(request.args.get('msg'))
    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"]:
                return random.choice(intent['responses'])
    else:
        return "I do not understand... <br/> <br/> In case of any queries please contact [email protected]"
Exemple #27
0
def respond(input_value, message_type):
    append('messages', input_value)

    global all_words, ids, model, nodes

    input_value = tokenize(input_value)
    X = bag_of_words(input_value, all_words)

    # check if the input matched any words
    all_weights_zero = all(w == 0 for w in X)

    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(device)

    output = model(X)
    _, predicted = torch.max(output, dim=1)

    id = ids[predicted.item()]

    probs = torch.softmax(output, dim=1)
    prob = probs[0][predicted.item()]
    if prob.item() > 0.7 and all_weights_zero is False:
        matched_nodes = []
        for node in nodes:
            if id == node['id']:

                node['options'] = []

                for subNode in node['subNodes']:
                    for node2 in nodes:
                        if subNode['to'] == node2['id']:
                            node['options'].append(node2['patterns'][0])

                matched_nodes.append(node)
        return matched_nodes
    else:
        response = dict()
        response['type'] = "text"
        response['messageType'] = "informational"
        response['answer'] = "Ik begrijp niet wat ik moet doen.. 😢"
        response['validResponse'] = False
        return [response]
Exemple #28
0
def get_bot_response():
    #userText =request.args.get("msg")
    while True:
        # sentence = "do you use credit cards?"
        #userText = input("You: ")
        userText = request.args.get("msg")
        if userText == "quit":
            break

        userText = tokenize(userText)
        X = bag_of_words(userText, 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"]:
                    bot_choice = random.choice(intent['responses'])
                    #print(f"{bot_name}:", bot_choice)
                    #return str(bot_choice.get_response(userText))
                    return str(bot_choice)

                    chat_saved = {
                        "chats": [{
                            "tag": tag,
                            "patterns": userText,
                            "responses": bot_choice
                        }]
                    }
                    with open('saved_chat.json', 'a') as json_file:
                        json.dump(chat_saved, json_file)
        else:
            #print(f"{bot_name}: I do not understand...")
            return str("I do not understand")
Exemple #29
0
def get_bot_response():
    userText = request.args.get('msg')
    sentence = userText
    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"]:
                return random.choice(intent['responses'])
    else:
        return "I don't understand"
Exemple #30
0
def process(s):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    with open('intents.json', 'r') as f:
        intents = json.load(f)

    FILE = "data.pth"
    data = torch.load(FILE)

    input_size = data["input_size"]
    output_size = data["output_size"]
    all_words = data["all_words"]
    tags = data["tags"]
    model_state = data["model_state"]
    hidden_size = data["hidden_size"]

    model = NeuralNet(input_size, hidden_size, output_size).to(device)
    model.load_state_dict(model_state)
    model.eval()

    bot_name = "Zoey"

    sentance = tokenize(s)
    x = bag_of_words(sentance, all_words)
    x = x.reshape(1, x.shape[0])
    x = torch.from_numpy(x)
    output = model(x)
    _, predicted = torch.max(output, dim=1)
    probability = torch.softmax(output, dim=1)
    probability = probability[0][predicted.item()]
    if (probability < .45):
        return "sorry"
    tag = tags[predicted.item()]
    return tag


#	for intent in intents["intents"]:
#		if tag == intent["tag"]:
#			choice = random.choice(intent["responses"])
#			print(f"{bot_name}: {choice}")