-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
executable file
·124 lines (90 loc) · 3.05 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
#!/usr/bin/env python3
#// global modules \\\\\\\\#
import argparse
import openai
import os
import speech_recognition as sr
#\\\\\\\\ global modules //#
#// local modules \\\\\\\\#
import functions
import errors
import colors
import voice
import cursor
#\\\\\\\\ local modules //#
#// argument parsing \\\\\\\\#
parser = argparse.ArgumentParser(description='Sapphire - A simple, actually helpful personal assistant leveraging Google Speech Recognition and GPT-3, for programmers')
parser.add_argument('-c', '--calibrate', action='store_true', help="Calibrate for ambient noise on startup")
parser.add_argument('-k', '--keyboard', action='store_true', help="Use keyboard input instead of speech recognition")
args = parser.parse_args()
#\\\\\\\\ argument parsing //#
def main():
openai.api_key = os.environ['OPENAI_KEY']
cache_path = os.path.expanduser('~') + '/.cache/sapphire/'
#? get name of distribution from /etc/os-release
operating_system = functions.get_os()
#? initialize the microphone and speech recognition engine
r = sr.Recognizer()
m = sr.Microphone()
print(cursor.clear, end='')
#? shorten pause_threshold from 0.8s to 0.5s
r.pause_threshold = 0.5
#? calibrate for ambient noise level
if not args.keyboard:
if not os.path.exists(cache_path + 'noise_calibration'):
try:
os.makedirs(cache_path)
except:
None
r.energy_threshold = functions.calibrate(m, r)
with open(cache_path + 'noise_calibration', "w") as f:
f.write(str(r.energy_threshold))
if args.calibrate:
r.energy_threshold = functions.calibrate(m, r)
with open(cache_path + 'noise_calibration', "w") as f:
f.write(str(r.energy_threshold))
else:
with open(cache_path + 'noise_calibration', 'r') as f:
r.energy_threshold = float(f.readline())
print(cursor.clear, end='')
#? start logging the current conversation, overwriting the previous
log = open('/tmp/sapphire.log', 'w')
#? initialize variables
reply = ''
request = ''
previous_reply = reply
previous_request = request
#// main loop \\\\\\\\#
while True:
if request != None:
log.write(request + '\n' + reply + '\n\n')
previous_request = request + '\n\n'
previous_reply = reply + '\n\n'
#? use speech recognition to prompt the user for a request
request = functions.listen(m, r, args.keyboard)
#? getting the request failed, restart the loop
if request == None:
continue
#? check if the user has requested to exit
elif functions.should_exit(request):
break
#? play processing sound
else:
voice.play('processing')
#? generate the prompt to send to OpenAI
prompt = functions.get_header() + previous_request + previous_reply + request
#? request a completion of the prompt and perform any necessary actions
if request != '':
try:
reply = functions.complete(m, r, request, prompt, args.keyboard)
except KeyboardInterrupt:
continue
if reply == None:
reply = ''
#? stop logging and exit with the exit sound
log.close()
functions.exit()
#\\\\\\\\ main loop //#
#? run main()
if __name__ == '__main__':
main()