-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
74 lines (62 loc) · 2.02 KB
/
recognize.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
from openai import OpenAI
from pathlib import Path
import playsound
import json
import sys
import os
import re
from recorder import live_speech
chatgpt = OpenAI()
messages = [
{
"role": "system",
"content": "You are a voice controlled assistant. Answer the user's prompts as best you can. Answer in 20 words or less. If the question requires a longer answer, ask the user first if they would like to know more. After confirmation, you can provide a full answer."
},
]
def detect_wakeup(command: str, wakeup_words: list[str]):
command = re.sub(r"[,\.!?]", "", command.lower())
for word in wakeup_words:
word = re.sub(r"[,\.!?]", "", word.lower())
if word in command:
return True
return False
if not os.path.exists("wakeup_words.json"):
print("You must run init.py first!")
sys.exit(1)
with open("wakeup_words.json", "r") as f:
wakeup_words = json.load(f)
while True:
for message in live_speech():
if detect_wakeup(message, wakeup_words):
print(f"Detected: {message}")
playsound.playsound(Path(__file__).parent / "sounds" / "detected.mp3")
break
for message in live_speech(50):
playsound.playsound(Path(__file__).parent / "sounds" / "processing.mp3")
messages.append(
{
"role": "user",
"content": message
}
)
response = chatgpt.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages
)
response_text = response.choices[0].message.content
print(f"ChatGPT: {response_text}")
messages.append(
{
"role": "assistant",
"content": response_text
}
)
voice = chatgpt.audio.speech.create(
input=response_text,
model="tts-1",
voice="alloy",
)
voice.stream_to_file("audio.mp3")
playsound.playsound("audio.mp3")
os.remove("audio.mp3")
break