-
Notifications
You must be signed in to change notification settings - Fork 264
/
Chatbot.py
101 lines (73 loc) · 2.03 KB
/
Chatbot.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import chatterbot
from chatterbot.trainers import ListTrainer
from tkinter import *
from PIL import ImageTk, Image
import pyttsx3 as pp
engine = pp.init()
voices = engine.getProperty('voices')
print(voices)
engine.setProperty('voice', voices[1].id)
def speak(word):
engine.say(word)
engine.runAndWait()
# pyttsx3
bot = chatterbot.ChatBot("My Bot")
convo = [
'hello',
'hi there !',
'what is your name?',
'My name is Bot, I am created by Chhavi',
'How are you?',
'I am doing great these days',
'thank you',
'In which city you live?',
'I live in lucknow',
'In which language you talk?',
'I mostly talk in english'
]
trainer = ListTrainer(bot)
# now training the bot with the help of trainer
trainer.train(convo)
# answer = bot.get_response("what is you name?")
# print(answer)
#
# print("Talk to bot")
# while True:
# query = input()
# if query == 'exit':
# break
# answer = bot.get_response(query)
# print("bot: ", answer)
main = Tk()
main.geometry("600x750")
main.title("My Chat bot")
canvas = Canvas(main, width=200, height=200)
canvas.pack()
img = ImageTk.PhotoImage(Image.open("bot1.jpg"))
canvas.create_image(10, 10, anchor=NW, image=img)
def ask_from_bot():
query = textF.get()
answer_from_bot = bot.get_response(query)
msgs.insert(END, "you: " + query)
print(type(answer_from_bot))
msgs.insert(END, "bot: " + str(answer_from_bot))
speak(answer_from_bot)
textF.delete(0, END)
msgs.yview(END)
frame = Frame(main)
sc = Scrollbar(frame)
msgs = Listbox(frame, width=80, height=20, yscrollcommand=sc.set)
sc.pack(side=RIGHT, fill=Y)
msgs.pack(side=LEFT, fill=BOTH, pady=10)
frame.pack()
# creating text field
textF = Entry(main, font=("Verdana", 20))
textF.pack(fill=X, pady=10)
btn = Button(main, text="Ask from bot", font=("Verdana", 20), command=ask_from_bot)
btn.pack()
# creating a function
def enter_function(event):
btn.invoke()
# going to bind main window with enter key..
main.bind('<Return>', enter_function)
main.mainloop()