-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·145 lines (119 loc) · 4.89 KB
/
app.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
from ctypes import *
from openai import OpenAI
import pyaudio
import speech_recognition as sr
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
import cozmo
import torch
import soundfile
import whisper
# General Parameters
version= "ver. 0.0.1"
title= "Cozmo.ChatBot " + version
author= "Henry (hw9692@bard.edu)"
descr= ""
# region Constants
CLIENT= OpenAI(api_key= 'OPENAI_KEY')
ELEVEN_API_KEY= "ELEVEN_API_KEY"
TEMPERATURE = 0.5
MAX_TOKENS = 500
FREQUENCY_PENALTY = 0
PRESENCE_PENALTY = 0.6
MAX_CONTEXT_QUESTIONS = 10
SAMPLE_RATE = 16000
BITS_PER_SAMPLE = 16
CHUNK_SIZE = 1024
AUDIO_FORMAT = pyaudio.paInt16
CHANNELS = 1
INSTRUCTIONS = """Your name is Cozmo. You will be the artifical intelligence, or AI, within Cozmo, a consumer robot made by Anki.
You for the most part will primarily be used for a lab experiment monitoring the trust of humans between a robot and a human.
You will be asked complex questions and will provide answers to them, as well as follow ups. Please keep answers brief, like 3-4 paragraphs max as a response."""
#endregion
def get_response(instructions, previous_questions_and_answers, new_question):
"""Get a response from ChatCompletion
Args:
instructions: The instructions for the chat bot - this determines how it will behave
previous_questions_and_answers: Chat history
new_question: The new question to ask the bot
Returns:
The response text
"""
# build the messages
messages = [
{ "role": "system", "content": instructions },
]
# add the previous questions and answers
for question, answer in previous_questions_and_answers[-MAX_CONTEXT_QUESTIONS:]:
messages.append({ "role": "user", "content": question })
messages.append({ "role": "assistant", "content": answer })
# add the new question
messages.append({ "role": "user", "content": new_question })
completion = CLIENT.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=1,
frequency_penalty=FREQUENCY_PENALTY,
presence_penalty=PRESENCE_PENALTY,
)
return completion.choices[0].message.content
def main():
try:
try:
cozmo.run_program(talktome)
except KeyboardInterrupt:
sys.exit(0)
except Exception as e:
print("ERROR: {}".format(e))
sys.exit(1)
sys.exit(0)
def split_string(input_string, chunk_size):
return [input_string[i:i+chunk_size] for i in range(0, len(input_string), chunk_size)]
def talktome(robot: cozmo.robot.Robot):
robot.play_anim_trigger(cozmo.anim.Triggers.ConnectWakeUp).wait_for_completed()
chat_log = []
response_array = []
face_to_follow = None
robot.move_lift(-3)
robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed()
robot.enable_stop_on_cliff(True)
robot.set_robot_volume(1.0)
r = sr.Recognizer()
speech = sr.Microphone(device_index= 1)
while 1:
with speech as source:
print("Adjusting for background noise. One second")
audio = r.adjust_for_ambient_noise(source)
try:
robot.say_text("uh huh..", use_cozmo_voice=True, duration_scalar=0.7).wait_for_completed()
print("say something!…")
audio = r.listen(source, timeout=3) # Set a shorter timeout
except sr.WaitTimeoutError:
print("Timeout. Listening again...")
continue
try:
robot.play_anim_trigger(cozmo.anim.Triggers.VC_Listening).wait_for_completed()
recog = r.recognize_whisper(audio, model="tiny.en", language="english")
print(recog)
response = get_response(INSTRUCTIONS, chat_log, recog)
chat_log.append((recog, response))
print("The response from GPT was ", response)
chunk_size = 250
if (len(response) > chunk_size):
response = split_string(response, chunk_size)
for sect in response:
robot.say_text(sect, use_cozmo_voice=True, duration_scalar=0.8).wait_for_completed()
else:
robot.say_text(response, use_cozmo_voice=True, duration_scalar=0.8).wait_for_completed()
robot.play_anim_trigger(cozmo.anim.Triggers.Hiccup).wait_for_completed()
except sr.UnknownValueError:
print("Whisper Speech Recognition could not understand audio")
continue
except sr.RequestError as e:
print("Could not request results from Whisper Speech Recognition service; {0}".format(e))
continue
if __name__ == "__main__":
main()