import speech_recognition as sr
import os
import webbrowser
import pyttsx3
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.adjust_for_ambient_noise(source) # Adjust for ambient noise
audio = r.listen(source)
try:
print("Recognizing....")
query = r.recognize_google(audio, language='en-in')
print(f"user said: {query}\n")
return query.lower()
except sr.UnknownValueError:
print("Sorry, I couldn't understand. Can you please repeat?")
return "None"
except sr.RequestError as e:
print(f"Could not request results; {e}")
return "None"
except Exception as e:
print(f"An error occurred: {e}")
return "None"
def open_application(app_name):
if "chrome" in app_name:
speak("Opening Chrome")
os.system("start chrome")
else:
speak(f"Sorry, {app_name} is not supported.")
def search_youtube(query):
speak(f"Searching for {query} on YouTube")
url = f"https://www.youtube.com/results?search_query={'+'.join(query.split())}"
webbrowser.open(url)
def minimize_chrome():
speak("Minimizing Chrome")
os.system("powershell -command \"$wshell = New-Object -ComObject wscript.shell;$wshell.AppActivate('Chrome');$wshell.SendKeys('% ');\"")
if __name__ == "__main__":
speak("Welcome, how may I help you?")
while True:
query = take_command()
if query == "None":
continue # Ignore if no command is recognized
if "open" in query:
open_application(query)
elif "youtube" in query:
if "search" in query:
search_query = query.split("search")[-1].strip()
search_youtube(search_query)
else:
open_application("YouTube")
elif "minimize" in query:
minimize_chrome()
elif "exit" in query or "quit" in query:
speak("Goodbye!")
break
i write this code after run show welcome message and after that show this error message
Listening...
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\tts.py", line 15, in take_command
r.adjust_for_ambient_noise(source) # Adjust for ambient noise
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\speech_recognition\__init__.py", line 383, in adjust_for_ambient_noise
assert source.stream is not None, "Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
AssertionError: Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\tts.py", line 52, in <module>
query = take_command()
File "C:\Users\Administrator\Desktop\tts.py", line 13, in take_command
with sr.Microphone() as source:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\speech_recognition\__init__.py", line 189, in __exit__
self.stream.close()
AttributeError: 'NoneType' object has no attribute 'close'