File size: 1,280 Bytes
4b443ba
 
5edc8fb
4b443ba
558b544
 
4b443ba
6d37d8c
 
d3cb051
d12bf65
6d37d8c
d12bf65
d3cb051
 
 
 
 
c323e5c
d12bf65
558b544
c323e5c
558b544
6d37d8c
d3cb051
 
 
 
 
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
import numpy as np
from keras.saving import load_model
from keras.preprocessing.text import Tokenizer
from keras_self_attention import SeqSelfAttention
from model_settings import *


with open("responses.txt", "r") as f:
    lines = [x.rstrip("\n") for x in f.readlines()]

tokenizer = Tokenizer() # a tokenizer is a thing to split text into words, it might have some other stuff like making all the letters lowercase, etc.
tokenizer.fit_on_texts(lines)

model = load_model("chatbot.keras", custom_objects={"SeqSelfAttention": SeqSelfAttention})

def find_line_number(array):
    return sorted(zip(list(array), [x for x in range(len(array))]), key=lambda x:x[0], reverse=True)[0][1] # yeah, one big line, find the biggest value and return the number of the line

def generate(text, verbose=1):
    tokens = list(tokenizer.texts_to_sequences([text,])[0]) # text into tokens (almost words)
    tokens = (tokens+[0,]*inp_len)[:inp_len] # cutting off the sentence after inp_len words
    prediction = model.predict(np.array([tokens,]), verbose=verbose)[0]
    line = find_line_number(prediction)
    return lines[line]

if __name__ == "__main__": # if this code is not being imported, open the chat
    while True:
        inp = input("User: ")
        print(f"Bot: {generate(inp)}")