FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PySimpleGUI/DemoPrograms/Demo_Chatterbot_With_TTS.py at master · PySimpleGUI/PySimpleGUI · GitHub
PySimpleGUI
/
PySimpleGUI
Public
Notifications
You must be signed in to change notification settings
Fork
1.8k
Star
13.8k
Code
Issues
708
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PySimpleGUI
/
DemoPrograms
/
Demo_Chatterbot_With_TTS.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
105 lines (83 loc) · 3.24 KB
Breadcrumbs
PySimpleGUI
/
DemoPrograms
/
Demo_Chatterbot_With_TTS.py
Copy path
File metadata and controls
105 lines (83 loc) · 3.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
102
103
104
105
#!/usr/bin/env python
import
PySimpleGUI
as
sg
from
chatterbot
import
ChatBot
import
chatterbot
.
utils
from
gtts
import
gTTS
from
pygame
import
mixer
import
time
import
os
'''
Demo_Chatterbot.py
Note - this code was written using version 0.8.7 of Chatterbot... to install:
python -m pip install chatterbot==0.8.7
It still runs fine with the old version.
A GUI wrapped around the Chatterbot package.
The GUI is used to show progress bars during the training process and
to collect user input that is sent to the chatbot. The reply is displayed in the GUI window
Copyright 2018-2026 PySimpleGUI. All rights reserved.
'''
# Create the 'Trainer GUI'
# The Trainer GUI consists of a lot of progress bars stacked on top of each other
sg
.
theme
(
'NeutralBlue'
)
# sg.DebugWin()
MAX_PROG_BARS
=
20
# number of training sessions
bars
=
[]
texts
=
[]
training_layout
=
[[
sg
.
Text
(
'TRAINING PROGRESS'
,
size
=
(
20
,
1
),
font
=
(
'Helvetica'
,
17
))], ]
for
i
in
range
(
MAX_PROG_BARS
):
bars
.
append
(
sg
.
ProgressBar
(
100
,
size
=
(
30
,
4
)))
texts
.
append
(
sg
.
Text
(
' '
*
20
,
size
=
(
20
,
1
),
justification
=
'right'
))
training_layout
+=
[[
texts
[
i
],
bars
[
i
]],]
# add a single row
training_window
=
sg
.
Window
(
'Training'
,
training_layout
)
current_bar
=
0
# callback function for training runs
def
print_progress_bar
(
description
,
iteration_counter
,
total_items
,
progress_bar_length
=
20
):
global
current_bar
global
bars
global
texts
global
training_window
# update the window and the bars
button
,
values
=
training_window
.
read
(
timeout
=
0
)
if
button
is
None
:
# if user closed the window on us, exit
return
if
bars
[
current_bar
].
update_bar
(
iteration_counter
,
max
=
total_items
)
is
False
:
return
texts
[
current_bar
].
update
(
description
)
# show the training dataset name
if
iteration_counter
==
total_items
:
current_bar
+=
1
def
speak
(
text
):
global
i
tts
=
gTTS
(
text
=
text
,
lang
=
'en'
,
slow
=
False
)
tts
.
save
(
'speech{}.mp3'
.
format
(
i
%
2
))
# playback the speech
mixer
.
music
.
load
(
'speech{}.mp3'
.
format
(
i
%
2
))
mixer
.
music
.
play
()
# wait for playback to end
while
mixer
.
music
.
get_busy
():
time
.
sleep
(
.1
)
mixer
.
stop
()
i
+=
1
i
=
0
mixer
.
init
()
# redefine the chatbot text based progress bar with a graphical one
chatterbot
.
utils
.
print_progress_bar
=
print_progress_bar
chatbot
=
ChatBot
(
'Ron Obvious'
,
trainer
=
'chatterbot.trainers.ChatterBotCorpusTrainer'
)
# Train based on the english corpus
chatbot
.
train
(
"chatterbot.corpus.english"
)
################# GUI #################
layout
=
[[
sg
.
Multiline
(
size
=
(
80
,
20
),
reroute_stdout
=
True
,
echo_stdout_stderr
=
True
)],
[
sg
.
MLine
(
size
=
(
70
,
5
),
key
=
'-MLINE IN-'
,
enter_submits
=
True
,
do_not_clear
=
False
),
sg
.
Button
(
'SEND'
,
bind_return_key
=
True
),
sg
.
Button
(
'EXIT'
)]]
window
=
sg
.
Window
(
'Chat Window'
,
layout
,
default_element_size
=
(
30
,
2
))
# ---===--- Loop taking in user input and using it to query HowDoI web oracle --- #
while
True
:
event
,
values
=
window
.
read
()
if
event
!=
'SEND'
:
break
string
=
values
[
'-MLINE IN-'
].
rstrip
()
print
(
' '
+
string
)
# send the user input to chatbot to get a response
response
=
chatbot
.
get_response
(
values
[
'-MLINE IN-'
].
rstrip
())
print
(
response
)
window
.
close
()
Back
|
FazBrowse Home
|
New Git URL