forked from ElliotGestrin/NAOChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTalker.py
More file actions
77 lines (67 loc) · 2.5 KB
/
Copy pathTalker.py
File metadata and controls
77 lines (67 loc) · 2.5 KB
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
from pydub import AudioSegment, playback
from io import BytesIO
from gtts import gTTS
class Talker:
def __init__(self, language: str = "en"):
self.language = language
def __call__(self, to_say: str):
"""
Speaks the string/string Generator to_say. If it's a generator
It speaks via stream_say, if string via say
Args:
to_say (str): The string to speak
"""
if isinstance(to_say, str):
print(f"'{to_say}'")
self.say(to_say)
else: # If not a string, it's a string generator
self.stream_say(to_say)
def say(self, to_say: str):
"""
Speaks the string to_say
Args:
to_say (str): The string to speak
"""
raise NotImplementedError
def stream_say(self, to_say: "Generator<str,None,None>"):
"""
Speaks the string generated by to_say line by line.
Args:
to_say (str): The string to speak
"""
res = ""
for mes in to_say:
res += mes
while res.count('\n') >= 1:
pos = res.find('\n')
print(res[0:pos])
self.say(res[0:pos])
res = res[pos+1:]
print(res)
self.say(res)
class LocalTalker(Talker):
def say(self, to_say: str):
try:
audio = gTTS(to_say,lang=self.language)
fp = BytesIO()
audio.write_to_fp(fp)
fp.seek(0) # Return to start of file
segment = AudioSegment.from_file(fp,format="mp3")
playback.play(segment)
except:
return
if __name__ == "__main__":
from Chatter import Chatter
from Listener import Listener
name = "Alice"
swe_intro = f"Du är den mänskliga roboten {name}. En NAO-modellrobot byggd av Softbank och programmerad av FIA Robotics. Din uppgift är att hålla en intressant konversation med en grupp människor."
eng_intro = f"You are the humanoid robot {name}. A NAO model robot built by Softbank and programmed by FIA Robotics. Your task is to hold an interesting conversation with a group of humans."
chatter = Chatter(swe_intro, stream=True,chat_horison=5,filt_horison=3,name=name)
listener = Listener("sv",use_whisper=False) # Change to 'en' for english
talker = LocalTalker("sv")
while(True):
heard = listener()
print(f"Heard: {heard}")
if heard != "":
response = chatter(heard)
talker(response)