-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathmain.py
130 lines (116 loc) · 4.17 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
from os import system
from EdgeGPT.EdgeUtils import Query
import speech_recognition as sr
import sys, whisper, warnings, time, openai
# Wake word variables
BING_WAKE_WORD = "bing"
GPT_WAKE_WORD = "gpt"
# Initialize the OpenAI API
openai.api_key = "[paste your OpenAI API key here]"
r = sr.Recognizer()
tiny_model = whisper.load_model('tiny')
base_model = whisper.load_model('base')
listening_for_wake_word = True
bing_engine = True
source = sr.Microphone()
warnings.filterwarnings("ignore", category=UserWarning, module='whisper.transcribe', lineno=114)
if sys.platform != 'darwin':
import pyttsx3
engine = pyttsx3.init()
def speak(text):
if sys.platform == 'darwin':
ALLOWED_CHARS = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?!-_$:+-/ ")
clean_text = ''.join(c for c in text if c in ALLOWED_CHARS)
system(f"say '{clean_text}'")
else:
engine.say(text)
engine.runAndWait()
def listen_for_wake_word(audio):
global listening_for_wake_word
global bing_engine
with open("wake_detect.wav", "wb") as f:
f.write(audio.get_wav_data())
result = tiny_model.transcribe('wake_detect.wav')
text_input = result['text']
if BING_WAKE_WORD in text_input.lower().strip():
print("Speak your prompt to Bing.")
speak('Listening')
listening_for_wake_word = False
elif GPT_WAKE_WORD in text_input.lower().strip():
print("Speak your prompt to GPT 3.5 Turbo.")
speak('Listening')
bing_engine = False
listening_for_wake_word = False
def prompt_bing(audio):
global listening_for_wake_word
global bing_engine
try:
with open("prompt.wav", "wb") as f:
f.write(audio.get_wav_data())
result = base_model.transcribe('prompt.wav')
prompt_text = result['text']
if len(prompt_text.strip()) == 0:
print("Empty prompt. Please speak again.")
speak("Empty prompt. Please speak again.")
listening_for_wake_word = True
else:
print('User: ' + prompt_text)
output = Query(prompt_text)
print('Bing: ' + str(output))
speak(str(output))
print('\nSay Ok Bing or Ok GPT to wake me up. \n')
bing_engine = True
listening_for_wake_word = True
except Exception as e:
print("Prompt error: ", e)
def prompt_gpt(audio):
global listening_for_wake_word
global bing_engine
try:
with open("prompt.wav", "wb") as f:
f.write(audio.get_wav_data())
result = base_model.transcribe('prompt.wav')
prompt_text = result['text']
if len(prompt_text.strip()) == 0:
print("Empty prompt. Please speak again.")
speak("Empty prompt. Please speak again.")
listening_for_wake_word = True
else:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content":
"You are a helpful assistant."},
{"role": "user", "content": prompt_text},
],
temperature=0.5,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
n=1,
stop=["\nUser:"],
)
bot_response = response["choices"][0]["message"]["content"]
listening_for_wake_word = True
bing_engine = True
except Exception as e:
print("Prompt error: ", e)
def callback(recognizer, audio):
global listening_for_wake_word
global bing_engine
if listening_for_wake_word:
listen_for_wake_word(audio)
elif bing_engine == True:
prompt_bing(audio)
elif bing_engine == False:
prompt_gpt(audio)
def start_listening():
with source as s:
r.adjust_for_ambient_noise(s, duration=2)
print('\nSay Ok Bing or Ok GPT to wake me up. \n')
r.listen_in_background(source, callback)
while True:
time.sleep(1)
if __name__ == '__main__':
start_listening()