-
Notifications
You must be signed in to change notification settings - Fork 0
/
voiceassistant.py
49 lines (38 loc) · 1.41 KB
/
voiceassistant.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
import os
import time
import pyaudio
import speech_recognition as sr
import playsound
from gtts import gTTS
import openai
# Replace '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' with your actual API key
api_key = "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"
lang = 'en'
openai.api_key = api_key
guy = ""
while True:
def get_audio():
r = sr.Recognizer()
with sr.Microphone(device_index=1) as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
global guy
guy = said
if "Hey Rex" in said: # Adjusted the wake word
words = said.split()
new_string = ' '.join(words[2:]) # Skip "Hey Rex"
print(new_string)
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content":new_string}])
text = completion.choices[0].message.content
speech = gTTS(text=text, lang=lang, slow=False, tld="com.au")
speech.save("welcome1.mp3")
playsound.playsound("welcome1.mp3")
except Exception:
print("Exception")
return said
if "stop" in guy:
break
get_audio()