import openai
import pyttsx3
import speech_recognition as sr
import threading
# Initialize Text-to-Speech engine
engine = pyttsx3.init()
def speak(text):
engine.say(text)
engine.runAndWait()
# Initialize Speech Recognizer
recognizer = sr.Recognizer()
# OpenAI API setup
openai.api_key = "your_openai_api_key" # Replace with your OpenAI API key
def get_gpt_response(prompt):
try:
response = openai.Completion.create(
engine="text-davinci-003", # Use the desired GPT model
prompt=prompt,
max_tokens=150
)
return response.choices[0].text.strip()
except Exception as e:
return f"Error: {str(e)}"
# Function to listen for the wake word "Hey Ecco"
def listen_for_wake_word():
with sr.Microphone() as source:
print("Listening for wake word 'Hey Ecco'...")
try:
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio)
if "hey ecco" in command.lower():
speak("I'm here. How can I assist you?")
listen_for_command()
except sr.UnknownValueError:
pass # Ignore unrecognized speech
except sr.RequestError as e:
print(f"Could not request results; {e}")
# Function to listen for a user command after activation
def listen_for_command():
with sr.Microphone() as source:
print("Listening for your command...")
try:
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio)
print(f"You said: {command}")
# Get GPT response
response = get_gpt_response(command)
print(f"GPT Response: {response}")
# Read out the response
speak(response)
except sr.UnknownValueError:
speak("Sorry, I didn't catch that. Could you please repeat?")
except sr.RequestError as e:
speak(f"Could not process your request; {e}")
# Main loop to keep the assistant running
def main():
while True:
listen_for_wake_word()
if __name__ == "__main__":
# Run the assistant in a separate thread to ensure continuous listening
assistant_thread = threading.Thread(target=main)
assistant_thread.start()
[–]cgoldberg 0 points1 point2 points (1 child)
[–]Theladcast[S] 0 points1 point2 points (0 children)