forked from enenmia/BugWithNPD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac3.py
124 lines (106 loc) · 5.2 KB
/
ac3.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
import serial
import threading
from gtts import gTTS
import time
import os
import random
import speech_recognition as sr
from openai import OpenAI
interrupt_sentences = ["hmm", "yes", "ok", "well"]
arduino_port = '/dev/cu.usbserial-120' # Change this to the arduino port, sometimes 1100 sometimes 110
baud_rate = 9600
ser = serial.Serial(arduino_port, baud_rate, timeout=1)
ser_lock = threading.Lock()
# 用锁包裹串口写操作
def write_to_serial(message):
with ser_lock:
ser.write(message)
# def write_to_serial_and_print(message):
# print(message)
# for char in message:
# ser.write(char.encode())
# time.sleep(0.01) # Small delay between characters
# ser.write('\n'.encode()) # Send a newline character as the end of the message
def generate_response_with_gpt3(text, api_key):
client = OpenAI(api_key=api_key)
try:
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a friend of mine with Toxic Narcissistic personality disorder, which means you have a pattern of grandiosity, are in need for admiration, and lack of empathy. Pretend to be such a person and give a reaction to what the user just said. Limit your answer within 2 sentences and remember to showcase your toxic personality. Say it in first person perspective.Don't show too obciously your nature. Express it in a more subtle way and use some passive-aggresiveness.Also talk about yourself."},
{"role": "user", "content": text}
],
model="gpt-3.5-turbo",
)
if hasattr(chat_completion, 'choices') and chat_completion.choices:
return chat_completion.choices[0].message.content
else:
return "No response generated."
except Exception as e:
print(f"Error in generating response: {e}")
return "I'm sorry, I couldn't generate a response."
import uuid
def speak_text(text):
language = 'en'
temp_filename = "temp_" + str(uuid.uuid4()) + ".mp3"
myobj = gTTS(text=text, lang=language, slow=False)
myobj.save(temp_filename)
os.system(f"afplay {temp_filename}")
os.remove(temp_filename)
def interrupter(stop_listening):
last_time_spoken = time.time()
while not stop_listening.is_set():
current_time = time.time()
# Check if 4 seconds have passed since the last interruption
if current_time - last_time_spoken >= 4:
sentence_to_speak = random.choice(interrupt_sentences)
print(sentence_to_speak)
speak_text(sentence_to_speak)
last_time_spoken = current_time
time.sleep(0.1) # Short sleep for responsive stopping
def main():
recognized_hello = False
while not recognized_hello:
write_to_serial(b'y')
speak_text("Are you ok? If not, remember I am always here for help")
print("Are you ok? If not, remember I am always here for help")
recognizer = sr.Recognizer()
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
try:
print("Listening for 'hello'...")
audio = recognizer.listen(source, timeout=5) # Adjust the timeout as needed
recognized_text = recognizer.recognize_google(audio)
if "hello" in recognized_text.lower():
recognized_hello = True
print("Hello recognized. Listening for speech...")
stop_listening = threading.Event()
interrupt_thread = threading.Thread(target=interrupter, args=(stop_listening,))
interrupt_thread.start()
conversation = []
while True:
try:
audio = recognizer.listen(source, timeout=5) # Adjust the timeout as needed
recognized_text = recognizer.recognize_google(audio)
conversation.append(recognized_text)
if "do you have any suggestion" in recognized_text.lower():
stop_listening.set()
conversation_string = " ".join(conversation).replace("do you have any suggestion", "").strip()
print(f"Recognized: {conversation_string}")
gpt_response = generate_response_with_gpt3(conversation_string, "apikey")
print(gpt_response)
write_to_serial(b'g')
speak_text(gpt_response)
time.sleep(5)
break
except sr.UnknownValueError:
continue
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
continue
except sr.UnknownValueError:
continue
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
if __name__ == "__main__":
while True:
main()