-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (165 loc) · 6.26 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import configparser
import logging
import os
import tempfile
import time
import openai
import pvporcupine
import pyttsx4
import speech_recognition as sr
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts import MessagesPlaceholder
from langchain.schema import SystemMessage
from pvrecorder import PvRecorder
DEFAULT_KEYWORD = "jarvis"
DEFAULT_CONFIG = {
"paths": {
"keyword_path": pvporcupine.KEYWORD_PATHS[DEFAULT_KEYWORD],
},
"settings": {
"chat_model": "gpt-3.5-turbo",
"system_prompt": "You are a helpful assistant.",
"temperature": "0.7",
"tools": "serpapi",
},
}
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
)
class PorcupineWakeWordListener:
def __init__(self, access_key, keyword_paths, model_path):
self.porcupine = pvporcupine.create(
access_key=access_key, keyword_paths=keyword_paths, model_path=model_path
)
audio_device_index = -1
self.recorder = PvRecorder(
device_index=audio_device_index, frame_length=self.porcupine.frame_length
)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def listen(self):
try:
self.recorder.start()
logging.info("Listening wake word ...")
while True:
pcm = self.recorder.read()
keyword_idx = self.porcupine.process(pcm)
if keyword_idx >= 0:
logging.info("Detected keyword")
return keyword_idx
logging.debug("Not detected")
finally:
self.recorder.stop()
def close(self):
self.recorder.delete()
self.porcupine.delete()
class SpeechSynthesizer:
def __init__(self, voice=None):
self.engine = pyttsx4.init()
if voice:
self.engine.setProperty("voice", voice)
def speak(self, text):
self.engine.say(text)
# If we use runAndWait, there is an issue that the program terminates
# abnormally.
self.engine.startLoop(False)
self.engine.iterate()
while self.engine.isBusy():
time.sleep(0.1)
self.engine.endLoop()
class WhisperTranscriber:
def __init__(self, language):
# language: ISO-639-1 format
self.language = language
def transcribe(self, audio_file_name):
with open(audio_file_name, "rb") as f:
transcription = openai.Audio.transcribe(
"whisper-1", f, language=self.language
)
command_text = transcription.get("text")
logging.info("Transcribed command text: %s", command_text)
return command_text
class VoiceActivityDetector:
def __init__(self):
self.recognizer = sr.Recognizer()
# Adjust the noise level
with sr.Microphone() as source:
logging.info("Adjusting for noise...")
self.recognizer.adjust_for_ambient_noise(source)
logging.info("Done")
def listen(self, audio_file_name):
# Listen for a voice command until it ends
with sr.Microphone() as source:
logging.info("Listening...")
audio = self.recognizer.listen(source, timeout=5, phrase_time_limit=10)
logging.info("Finished listening")
# Save audio as WAV file
with open(audio_file_name, "wb") as out:
out.write(audio.get_wav_data())
return audio_file_name
class Iris:
def __init__(self, config, wake, vad, stt, agent, tts):
self.config = config
self.wake = wake
self.vad = vad
self.stt = stt
self.agent = agent
self.tts = tts
def run(self):
try:
while True:
self.wake.listen()
with tempfile.NamedTemporaryFile(suffix=".wav") as temp_file:
self.vad.listen(temp_file.name)
command_text = self.stt.transcribe(temp_file.name)
if not command_text:
logging.error("Failed to transcribe audio")
continue
response_message = self.agent.run(command_text)
logging.info("Agent Response: %s", response_message)
self.tts.speak(response_message)
except KeyboardInterrupt:
logging.info("Stopping...")
def parse_list(list_of_str):
return [item.strip() for item in list_of_str.split(",")]
if __name__ == "__main__":
config = configparser.ConfigParser()
config.read_dict(DEFAULT_CONFIG)
config.read("config.ini")
with PorcupineWakeWordListener(
config.get("api", "picovoice_access_key"),
[config.get("paths", "keyword_path")],
config.get("paths", "wake_model_path", fallback=None),
) as wake:
vad = VoiceActivityDetector()
openai.api_key = config.get("api", "openai_api_key")
stt = WhisperTranscriber(config.get("settings", "language", fallback=None))
system_prompt = SystemMessage(content=config.get("settings", "system_prompt"))
agent_kwargs = {
"extra_prompt_messages": [MessagesPlaceholder(variable_name="memory")],
"system_message": system_prompt,
}
memory = ConversationBufferMemory(memory_key="memory", return_messages=True)
chat = ChatOpenAI(
model=config.get("settings", "chat_model"),
temperature=float(config.get("settings", "temperature")),
openai_api_key=config.get("api", "openai_api_key"),
)
if not os.environ.get("SERPAPI_API_KEY"):
os.environ["SERPAPI_API_KEY"] = config.get("api", "serpapi_api_key")
tools = load_tools(parse_list(config.get("settings", "tools")))
agent = initialize_agent(
tools,
chat,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
agent_kwargs=agent_kwargs,
memory=memory,
)
tts = SpeechSynthesizer(config.get("settings", "voice", fallback=None))
iris = Iris(config, wake, vad, stt, agent, tts)
iris.run()