-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_chat.py
58 lines (41 loc) · 1.69 KB
/
04_chat.py
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
import tkinter
from tkinter import *
import json
import colorama
from colorama import Fore, Style, Back
import pickle
import numpy as np
from tensorflow import keras
wn = tkinter.Tk()
wn.geometry('500x500')
wn.resizable(width=False, height=False)
wn.title('AutoBot')
chat_log = Text(wn, bd=3, font=('Cambria', 12))
chat_log.config(state=DISABLED)
chat_log.place(x=0, y=0, width=490, height=400)
chat_area = Text(wn, bd=3, font=('Cambria', 12))
chat_area.place(x=0, y=400, width=400, height=100)
with open("01_intents.json") as f:
data = json.load(f)
model = keras.models.load_model('04_chatbot_model.h5')
with open("04_tokenizer.pkl", mode='rb') as t:
tokenizer = pickle.load(t)
with open("04_encoder.pkl", mode='rb') as e:
encoder = pickle.load(e)
def send():
user_input = chat_area.get(index1='1.0', index2='end-1c')
chat_area.delete(index1='0.0', index2=END)
if user_input != "":
predictions = model.predict(
keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences([user_input]),
truncating='post', maxlen=20))
tag = encoder.inverse_transform([np.argmax(predictions)])
for intent in data['intents']:
if intent['tag'] == tag:
res = np.random.choice(intent['response'])
chat_log.config(state=NORMAL)
chat_log.insert(END, "You: " + user_input + "\n\n")
chat_log.insert(END, "AutoBot: " + str(res) + "\n\n")
send_btn = Button(wn, text='Send', command=send, font=('Cambria', 12), bg='sky Blue', fg='Black')
send_btn.place(x=400, y=400, width=100, height=100)
wn.mainloop()