-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechOpenAI.py
96 lines (74 loc) · 3.12 KB
/
speechOpenAI.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
import time
import multiprocessing
import speech_recognition as sr
from openai import OpenAI
import json
import os
"""
-Turn on the Nao Robot and wait for it to connect to the wifi network. The robot will provide an IP address after the chest button is pressed.
-Update the IP address of the Nao robot throughout both files of python code
-Open a terminal window. In the terminal, change the directory to your project folder
-Run the Nao file by typing: python2 nao_tts.py
-While the previous file is running, open a new terminal window and change the directory to the root level of your project folder
-In the terminal, activate the virtual environment:
-For macOS/Linux: source myenv/bin/activate
-For Windows: myenv\Scripts\activate
-In the virtual environment, run the ChatGPT file by typing: python speechOpenAI.py
-Speak to your computer, and wait for the Nao robot to give a response
-Play with changing the prompt in the speechOpenAI.py if you would like.
"""
# code to figure out the microphone indexes for multi-microphone use
microphones = sr.Microphone.list_microphone_names()
for index, name in enumerate(microphones):
print(f"Microphone with index {index} and name \"{name}\" found")
openAIKey = os.environ["OPENAI_API_KEY"]
# Initialize the recognizer
r = sr.Recognizer()
r.energy_threshold = 300 # Adjust as needed
# Initialize the OpenAI client
client = OpenAI(api_key=openAIKey)
MODEL = "gpt-4"
chat_history = [{"role": "system", "content": "You are a NAO robot that provides appropiate gestures while answering my questions breifly. Provide the response in this example format: First part of response ^start(animations/Stand/Gestures/Hey_1) second part of response. "}]
with open("history.txt", "w") as f:
json.dump(chat_history,f)
def speak(mic,person):
while True:
with sr.Microphone(device_index=mic) as source:
r.adjust_for_ambient_noise(source,duration=1)
print("Listening...")
audio = r.listen(source)
print("Stop Listening")
try:
# using google to transcribe the audio file to text
text = r.recognize_google(audio)
print("mic " + str(mic) + " " + person + " said: " + text)
# read current chat history
with open("history.txt", "r") as f:
chat_history = json.load(f)
# keeps the chat history with ChatGPT
chat_history.append({'role': 'user', 'content': text})
completion = client.chat.completions.create(
model= MODEL,
messages= chat_history
)
response = completion.choices[0].message.content
print("Assistant: " + response)
# Add the assistant's response to the chat history
chat_history.append({"role": "assistant", "content": response})
# Save the updated chat history back to the file
with open("history.txt", "w") as f:
json.dump(chat_history, f)
with open("response.txt", "w") as f:
f.write(response)
while True:
with open("listen.txt", "r") as f:
result = f.read()
if result == "yes":
with open("listen.txt", "w") as f:
f.write("no")
break
except Exception as e:
print(f"An error occurred: {e}")
time.sleep(1)
# replace the parameters accordingly
speak(2,"Human")