Jul 19, 2024
python -m venv venv
``
.\venv\Scripts\activate.ps1
``
pip install SpeechRecognition pyttsx3 webbrowser openai
import speech_recognition as sr
import pyttsx3
import webbrowser
import openai
from datetime import datetime
``
recognizer = sr.Recognizer()
engine = pyttsx3.init()
def speak(text):
engine.say(text)
engine.runAndWait()
def listen_for_wake_word():
try:
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio).lower()
if 'jarvis' in command:
speak("Yes, I'm listening")
command = recognizer.listen(source)
return recognizer.recognize_google(command).lower()
except sr.UnknownValueError:
pass
def process_command(command):
if 'open google' in command:
webbrowser.open('https://google.com')
elif 'open youtube' in command:
webbrowser.open('https://youtube.com')
elif 'open facebook' in command:
webbrowser.open('https://facebook.com')
elif 'play music' in command:
# function to play music
pass
elif 'news' in command:
# function to fetch news
pass
else:
respond_openai(command)
openai.api_key = 'YOUR_OPENAI_API_KEY'
def respond_openai(prompt):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=100
)
speak(response.choices[0].text.strip())
if __name__ == "__main__":
while True:
command = listen_for_wake_word()
if command:
process_command(command)