|
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_kel import * |
|
import json |
|
from tokenizer import * |
|
|
|
|
|
with open(dataset_file, "r") as f: |
|
dset = json.load(f) |
|
|
|
with open(responses_file, "r") as f: |
|
lines = [x.rstrip("\n") for x in f.readlines()] |
|
|
|
fit_on_texts(list(dset.keys())) |
|
|
|
model = load_model("chatbot_kel.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] |
|
|
|
def generate(text, verbose=1): |
|
tokens = list(tokenize(text)) |
|
tokens = (tokens+[0,]*inp_len)[:inp_len] |
|
prediction = model.predict(np.array([tokens,]), verbose=verbose)[0] |
|
line = find_line_number(prediction) |
|
return lines[line] |
|
|
|
if __name__ == "__main__": |
|
while True: |
|
inp = input("User: ") |
|
gen = generate(inp) |
|
if gen != "<null>": print(f"Bot: {gen}") |
|
|