-2
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

1
  • 1
    So you are creating a chat bot with voice recognition but you got stuck at ModuleNotFoundError problem? Commented Oct 19, 2023 at 8:05

1 Answer 1

0

The error you see is explicit: No module named 'speech_recognition'.

You're missing a Python module.

To install it:

pip install SpeechRecognition

If you don't have pip, to install it on Windows:

py -3 -m ensurepip

Sources:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.