@sakshinikam2021 Please share the code that you have written in the joke.py file. Seems like there is an error in the code.
@sakshinikam2021 Thanks for pointing out the issue. input is an inbuilt variable. As the error suggests, we cannot use it for variable names as it is a reserved word in python. We will have to rename the variable.
I have made the changes in the training content. input is replaced with voice_input. This should solve the issue.
Here is the final code file:
import requests
import pyttsx3
import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone()
engine = pyttsx3.init()
engine.setProperty('rate', 175)
def take_input():
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
speech = r.recognize_google(audio)
return speech
def text_to_speech(text):
engine.say(text)
engine.runAndWait()
engine.stop()
print("Waiting for input:")
voice_input = take_input()
print(voice_input + "\n")
while True:
if "joke" in voice_input or "funny" in voice_input:
response = requests.get('https://icanhazdadjoke.com', headers={'Accept':'application/json'})
joke = response.json()['joke']
print(joke)
text_to_speech(joke)
continue
if "close" in voice_input or "quit" in voice_input:
engine.stop()
exit()
engine.stop()
@sakshinikam2021 it is not the same problem. This seems something linked with the audio. Please check this thread for the solution: Speech recoginition not working