import speech_recognition as sr
import pyttsx3
import requests
def listen_to_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
return r.recognize_google(audio)
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def main():
speak("Hello! How can I assist you today?")
while True:
try:
user_input = listen_to_audio().lower()
print("You said:", user_input)
if "weather" in user_input:
speak("I can check the weather for you. Please provide the location.")
location = listen_to_audio()
weather_url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid=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"
response = requests.get(weather_url)
data = response.json()
temperature = data["main"]["temp"]
weather_description = data["weather"][0]["description"]
speak(f"The temperature in {location} is {temperature} Kelvin with {weather_description} weather.")
elif "exit" in user_input:
speak("Goodbye!")
break
else:
speak("I didn't understand. Please try again.")
except sr.UnknownValueError:
speak("Sorry, I didn't catch that. Please repeat.")
except sr.RequestError:
speak("I'm sorry, there was an issue with my speech recognition.")
if __name__ == "__main__":
main()
Traceback (most recent call last): File "C:/Users/sathy/OneDrive/Desktop/Chatbot.py", line 1, in import speech_recognition as sr ModuleNotFoundError: No module named 'speech_recognition'
To make a chat bot with voice recognition
ModuleNotFoundErrorproblem?