import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import pyjokes
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def take_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio)
return command.lower()
def run_assistant():
speak("Hello! I am your voice assistant. How can I help you today?")
while True:
query = take_command()
if 'wikipedia' in query:
speak("Searching Wikipedia...")
query = query.replace("wikipedia", "")
result = wikipedia.summary(query, sentences=2)
speak(result)
elif 'open youtube' in query:
webbrowser.open("https://www.youtube.com")
speak("Opening YouTube...")
elif 'time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"The current time is {strTime}")
elif 'joke' in query:
joke = pyjokes.get_joke()
speak(joke)
elif 'exit' in query:
speak("Goodbye!")
break
else:
speak("Sorry, I didn't understand that.")
run_assistant()