pythontext-to-speechvoice-recognitionvoicegtts

I made a voice assistant with Python. How can I execute only one when I use two different voice commands in the same sentence?


I am making a voice assistant with Python. I'm giving voice commands to the assistant. There are several predefined sentences and answers. However, if there are two commands defined in the code before, they start working at the same time. How can I prevent this. For example, if the commands "what time is it (saat kaç)" and "assistant shutdown (asistan kapan)" occur in a sentence, they do both. Instead I perceived two commands as assistant, "what time is it" and "assistant shut down". How can I get me to ask questions like which one would you like me to do?

import ... 
r = sr.Recognizer()
def record(ask=False):
    with sr.Microphone() as source:
        if ask:
            speak(ask)
        audio = r.listen(source)
        voice = ''
        try:
            voice = r.recognize_google(audio, language='tr-TR')
        except sr.UnknownValueError:
            print("anlayamadım") #anlayamadım = i couldn't understand
        except sr.RequestError:
            speak("sistem çalışmıyor") #sistem çalışmıyor= system is not work
        return voice

def response(voice):
    if 'saat kaç' in voice: #saat kaç = what time is it
        speak(datetime.now().strftime('%H:%M:%S'))
    if 'Asistan kapan' in voice: #asistan kapan = assistant shutdown
        speak("görüşürüz") #görüşürüz = bye
        exit()
#The command that consists of voice responses in the range of 1, 10000 files and deletes after the answer is answered
def speak(string):
    tts = gTTS(string, lang='tr')
    rand = random.randint(1, 10000)
    file = 'audio' + str(rand) + '.mp3'
    tts.save(file)
    playsound(file)
    os.remove(file)

speak("nasıl yardımcı olabilirim") #nasıl yardımcı olabilirim = how can i help you
time.sleep(1)
while True:
    voice = record()
    print(voice)
    response(voice)

Solution

  • A brute solution would be to check if both of the commands are in the variable voice

    def response(voice):
        if 'saat kaç' in voice and 'Asistan kapan' in voice:
        speak("which one would you like me to do first?")
        new_voice = record()
        response(new_voice)
        else:
            if 'saat kaç' in voice: #saat kaç = what time is it
                speak(datetime.now().strftime('%H:%M:%S'))
            if 'Asistan kapan' in voice: #asistan kapan = assistant shutdown
                speak("görüşürüz") #görüşürüz = bye
                exit()
        
    

    But i wouldn't recommend this because as you add new commands you have to keep updating the new if statement that i've added.

    Instead make the user say words like "and" when giving multiple commands. Now you can split the command based on that word and run each command.

    def split_command(voice):
        commands = voice.split(" and ") # splits the command into multiple parts for each " and "  that means command like "what time is it and assistant shutdown" would become ["what time is it", "assistant shutdown"]
        for command in commands:
            response(command) # now for each command in the list it is calling the response function
    
    def response(voice):
        if 'saat kaç' in voice: #saat kaç = what time is it
            speak(datetime.now().strftime('%H:%M:%S'))
        elif 'Asistan kapan' in voice: #asistan kapan = assistant shutdown
            speak("görüşürüz") #görüşürüz = bye
            exit()  
        # now instead of multiple ifs you can use elif 
    
    # now inside the infinite while loop call split_command function instead of the response function
    while True:
        voice = record()
        print(voice)
        split_command(voice)
    

    I have added a comment next to all of the new code that i've added. I'm happy to help if you are still confused.