I am building a voice assistant that can tell stories. While the bot is telling stories I want to interrupt in between and ask it to stop or go backward or to end the story. I tried few ways, but they are not working I am not able to listen while it is speaking cause after the speaking part gets ended it goes to listening part.
Thanks in advance
Here is my code
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
try:
inp=r.recognize_google(audio_text, language = "en-IN")
print("Text: "+inp)
except:
inp="sorry"
print("Sorry, I did not get that")
if inp.lower() == "quit":
bot_voice("Ok bye see you later")
break
if inp.lower() == "sorry":
bot_voice("Sorry, I did not get that")
if (deter==0):
y=-1
deter=1
for x in stories:
m=x
y=m.find(inp)
if(y>-1):
filename = 'Stories/data/'+x+'.txt'
with open(filename) as f_in:
for line in f_in:
bot_voice(line)
break
else:
results = model.predict([bag_of_words(inp, words)])
results_index = numpy.argmax(results)
tag = labels[results_index]
for tg in data["intents"]:
if tg['tag'] == tag:
responses = tg['responses']
reply=random.choice(responses)
if(reply=='7417'):
filename = "Stories/list.txt"
bot_voice("I know quite a few stories, they are")
with open(filename) as f_in:
for x,line in enumerate(f_in):
bot_voice(line)
bot_voice("which one you want")
deter=0
else:
print("bot:",reply)
bot_voice(reply)
This isn't possible with the speech recognition you are using. This speech recognition takes input and doesn't provide output. With your output system, which I assume is something like pyttsx, would just read as it is told. You would require a stop system and you would need to do this with a machine learning-based program that is capable of conversations and can stop when told to stop and take keywords as commands.
I recommend Pytorch as a starter for Python Machine Learning. Here is an article on conversational AI.