import speech_recognition as sr
import time
import os
import openai
import serial
# Set up your OpenAI API key
#openai.api_key = 'sk-proj-9wGxyYGvulHuFnh50CPgT3BlbkFJYj6XPjbLKnZUrDsSEGzA'
#api_key = "sk-proj-9wGxyYGvulHuFnh50CPgT3BlbkFJYj6XPjbLKnZUrDsSEGzA"
client = openai.OpenAI(
# This is the default and can be omitted
api_key=os.getenv("sk-proj-9wGxyYGvulHuFnh50CPgT3BlbkFJYj6XPjbLKnZUrDsSEGzA")
)
# Serial port for communication with Arduino Nano
SERIAL_PORT = '/dev/ttyUSB0' # Adjust as needed
SERIAL_BAUDRATE = 9600
# Function to send command to Arduino Nano
def send_command_to_robot(command):
try:
arduino = serial.Serial(SERIAL_PORT, SERIAL_BAUDRATE)
arduino.write(command.encode())
arduino.close()
print(f"Sent command to Arduino: {command}")
except Exception as e:
print(f"Error sending command to Arduino: {e}")
# Function to recognize speech from microphone
def recognize_speech_from_mic(recognizer, microphone, timeout=5):
# Adjust the recognizer sensitivity to ambient noise and record audio
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
print("Listening...")
try:
audio = recognizer.listen(source, timeout=timeout)
except sr.WaitTimeoutError:
return {"success": False, "error": "TimeoutError", "transcription": None}
# Initialize response
response = {
"success": True,
"error": None,
"transcription": None
}
# Try recognizing the speech in the recording
try:
response["transcription"] = recognizer.recognize_google(audio)
except sr.RequestError:
# API was unreachable or unresponsive
response["success"] = False
response["error"] = "API unavailable"
except sr.UnknownValueError:
# Speech was unintelligible
response["error"] = "Unable to recognize speech"
return response
# Function to understand command using ChatGPT
def understand_command_with_chatgpt(command):
try:
response = client.completions.create(
model="gpt-3.5-turbo",
#engine="text-davinci-002",
prompt=f"What is the standard command for '{command}'?",
temperature=0.5,
max_tokens=50
)
standardized_command = response.choices[0].text.strip()
return standardized_command
except Exception as e:
print(f"Error understanding command with ChatGPT: {e}")
return None
# Main function
def main():
# Create recognizer and mic instances
recognizer = sr.Recognizer()
microphone = sr.Microphone()
print("Say 'Hey' to start controlling the robot.")
activated = False
last_command = None
while True:
response = recognize_speech_from_mic(recognizer, microphone)
if response["success"]:
transcription = response["transcription"]
if transcription:
transcription = transcription.lower()
print(f"You said: {transcription}")
if not activated:
if "hey" in transcription:
print("Voice control activated. Please say a command: 'forward', 'backward', 'left', 'right', 'stop'.")
activated = True
else:
print("Say 'Hey' to start controlling the robot.")
else:
if "exit" in transcription:
print("Voice control deactivated. Say 'Hey' to activate again.")
activated = False
else:
# Replace synonyms with standard commands using ChatGPT
for word in transcription.split():
standardized_command = understand_command_with_chatgpt(word)
if standardized_command:
transcription = transcription.replace(word, standardized_command)
print(f"Standardized command: {transcription}")
if "forward" in transcription:
send_command_to_robot('F')
last_command = 'F'
elif "backward" in transcription:
send_command_to_robot('B')
last_command = 'B'
elif "left" in transcription:
send_command_to_robot('L')
last_command = 'L'
elif "right" in transcription:
send_command_to_robot('R')
last_command = 'R'
elif "stop" in transcription:
send_command_to_robot('S')
last_command = 'S'
else:
print("Unrecognized command. Please try again.")
else:
print("Didn't catch that. Say 'Hey' to start controlling the robot.")
if response["error"]:
print(f"Error: {response['error']}")
if not activated:
print("Listening for activation keyword...")
# Add a short delay to avoid too fast looping
time.sleep(1)
if __name__ == "__main__":
main()
- Sorry if I done mistake in this code, I am a newbie with only basic coding knowledge. I am using ChatGPT to code this for me.
I am try to create a code to do natural language processing which will change the voice command to something a robot can understand. The old code work fine without the ChatGPT but after I add in the ChatGPT function, there is a bunch error and the ChatGPT cannot fix it for me.
The error show after I run the code:
Traceback (most recent call last):
File "C:UsersganshDesktopRaspberry piVoice_Command_GPT.py", line 10, in <module>
client = openai.OpenAI(
File "C:UsersganshAppDataRoamingPythonPython310site-packagesopenai_client.py", line 104, in __init__
raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
I try to use env on the API key but still not working.
Please help me to fix this error. Appreciate your help.
Hope I can run the code after your help.
Gan Shao Yu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.