-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobit.py
executable file
·71 lines (57 loc) · 2.79 KB
/
robit.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
#!/usr/bin/env python3
import json
import logging
import os
import tempfile
import time
import dotenv
dotenv.load_dotenv()
from helpers import AIHelper, AudioHelper
class Robit():
def __init__(self):
logging.basicConfig()
self.logger = logging.getLogger("robit")
self.logger.setLevel(os.getenv("ROBIT_LOG_LEVEL", "INFO").upper())
self.ai = AIHelper.AIHelper()
def start(self):
#TODO: Background constant listening, with interrupt (separate listen_loop func probably)
#TODO: Eventually, wake word
while True:
try:
timestats = {}
start = time.perf_counter()
tf = tempfile.NamedTemporaryFile(suffix=".wav")
self.logger.debug("Listening...")
AudioHelper.AudioHelper(dst_filename=tf.name).get_soundbite()
self.logger.debug("Transcribing...")
with open(tf.name, 'rb') as ofile:
user_message = self.ai.transcribe(ofile.read())
timestats['transcription'] = time.perf_counter() - start
tf.close()
self.logger.debug(f"Transcription [{round(timestats['transcription'], 2)}s]: {user_message}")
self.logger.debug("Thinking...")
bot_message = self.ai.chat(user_message)
timestats['chat'] = time.perf_counter() - (start + timestats['transcription'])
self.logger.debug(f"Response [{round(timestats['chat'], 2)}s]: {bot_message}")
self.logger.debug("Generating voice...")
audio = self.ai.text_to_speech(bot_message)
timestats['tts'] = time.perf_counter() - (start + timestats['chat'] + timestats['transcription'])
timestats['total'] = time.perf_counter() - start
self.logger.debug(f"Speaking [{round(timestats['tts'], 2)}s]: [...]")
AudioHelper.AudioHelper().say(audio, format="mp3")
self.logger.info(json.dumps({k:round(v, 2) for k,v in timestats.items()}, indent=2))
except KeyboardInterrupt:
self.logger.info("Shutting down from console request")
message_count = len(self.ai.ChatHelper.get_messages())
if message_count != 0:
self.logger.info(f"Generating summary [{message_count} messages]...")
self.logger.info(f"Summary: // {self.ai.create_summary()} //")
quit()
except AIHelper.TranscriptionHelper.TranscriptionError as e:
self.logger.error(f"There was an error transcribing: {e}")
except Exception as e:
self.logger.critical(f"Unhandled exception! {e}")
finally:
tf.close()
if __name__ == "__main__":
Robit().start()