Voice assistant in Python (Vitaliy alfa 2.0)

Hello habr! My name is Gleb Pryakhin, I am 14 years old, I wrote a voice assistant in python 3 and compiled it into exe.





( Link to compiled version )





( Link to source )





I ask you to test the assistant and ask him questions,





if you have questions or want to write to support: in the comments under the post, or by email. mail glebilic@gmail.com.






Let's get started with the code! First, let's import the modules:





import pyttsx3
import os
import time
import datetime
import speech_recognition as sr
import random
import webbrowser
import sounddevice as sd
      
      



To add voices, download and install RHvoice.





- tts, . num123 = , tts1 = .





f = open("sp.txt", "r")
num123 = f.read(1000)
num123 = int(num123)
f.close

f = open("tts.txt", "r")
tts1 = int(f.read(1))
f.close()
# 
tts = pyttsx3.init()
speak_engine = pyttsx3.init()

voices = speak_engine.getProperty('voices')
speak_engine.setProperty('voice', voices[tts1].id)
      
      



:





os.system('cls' if os.name == 'nt' else 'clear')
print(" 2.0 ALFA \nBy  \n2021 \n-...")
      
      



name , ! , ! :





f = open('name.txt', 'r')
if f.read(1) == "":
    os.system('cls' if os.name == 'nt' else 'clear')
    tts.say(" ,  ,  ,         ,  .  .   ?")
    tts.runAndWait()


    r = sr.Recognizer()
    with sr.Microphone(device_index = 1) as source:
        print(' ')
        r.adjust_for_ambient_noise(source, duration=0.5) #  
        print('...')
        audio = r.listen(source)
        print(' ')
    try:
        query = r.recognize_google(audio, language = 'ru-RU')
        name = query.lower()
        print(f' : {query.lower()}')
            
    except:
        print('-')

    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say(" .")
    tts.runAndWait()
    r = sr.Recognizer()
    with sr.Microphone(device_index = 1) as source:
        print(' ')
        r.adjust_for_ambient_noise(source, duration=0.5) #  
        print('...')
        audio = r.listen(source)
        print(' ')
    try:
        query = r.recognize_google(audio, language = 'ru-RU')
        ver = query.lower()
        print(f' : {query.lower()}')
            
    except:
        print('-')
    if "" in ver or "" in ver:
        f = open('name.txt', 'w')
        f.write(name.title())
        f.close()
        tts.say("!   !")
        tts.runAndWait()
    
    else:
        tts.say("    ")
        tts.runAndWait()
        name = input(" : ")
        tts.say("!   !")
        tts.runAndWait()
        f = open('name.txt', 'w')
        f.write(name.title())
        f.close()
      
      



, - , " ", :





if num123 == 0:
        tts.say("      :      :")
        tts.runAndWait()
        tts.say("  ")
        tts.runAndWait()
      
      



:





#      
f = open("sp.txt", "w")
num123 = num123 + 1
num123 = str(num123)
num123 = f.write(num123)
f.close
#       
f = open('name.txt', 'r')
name = f.read(10)
r = random.randint(1,10)
      
      



r, elif` cont, :





cont = ""
if r == 1:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say(" ?")
    tts.runAndWait()
elif r == 2:
    tts.say("")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say("  ?")
    tts.runAndWait()
elif r == 3:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say(" ?")
    tts.runAndWait()
elif r == 4:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say("  ?")
    tts.runAndWait()
    cont = ("")

elif r == 5:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say(" ?")
    tts.runAndWait()
    cont = ""
elif r == 6:
    tts.say("")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say(" ?")
    tts.runAndWait()
    cont = ""
elif r == 7:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say("  ?")
    tts.runAndWait()
elif r == 8:
    tts.say("")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say("   ?")
    tts.runAndWait()
    cont = ""
elif r == 9:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say("  ?")
    tts.runAndWait()
elif r == 10:
    tts.say(" ")
    tts.runAndWait()
    tts.say(name)
    tts.runAndWait()
    tts.say(" ?")
    tts.runAndWait()
      
      



:





while True:
    cikl = cikl + 1
    ca = 0
    ra = random.randint(1,10)
    an = ""
    #
    r = sr.Recognizer()
    with sr.Microphone(device_index = 1) as source:
        print(' ')
        r.adjust_for_ambient_noise(source, duration=0.5) #  
        print('...')
        audio = r.listen(source)
        print(' ')
    try:
        query = r.recognize_google(audio, language = 'ru-RU')
        an = query.lower()
        print(f' : {query.lower()}')
            
    except:
        print('-')

      
      



"", cont? "" " ?", "" " ?", , .





 #
    if "" in an and len(an) == 2 or "" in an or "-  " in an:
        ca = 1
        if cont == "":
            f = open('email.txt', 'r')
            if f.read(1) == "":
                tts.say("  ,      !     .")
                tts.runAndWait()
                a = 1
                while True:
                    v = input("    .")
                    f = open('email.txt', 'w')
                    if "https" in v:
                        web = v
                        f.write(web)
                        f.close()
                        break

            tts.say(" ")
            f = open('email.txt', 'r')
            web = f.read(97)
            f.close()
            tts.runAndWait()
            webbrowser.open(web)
        
        elif cont == "":
            tts.say(",  ")
            tts.runAndWait()
            webbrowser.open('https://www.youtube.com/')

        elif cont == "":
            tts.say("     ")
            tts.runAndWait()
            webbrowser.open('https://okko.tv/')

        elif cont == "":
            tts.say(" !")
            tts.runAndWait()
            webbrowser.open('https://www.youtube.com/watch?v=E3rH3KdVWcc')
        
        elif cont == "":
            tts.say(",   ")
            tts.runAndWait()
            webbrowser.open('https://www.youtube.com/channel/UCy0uukwm4dOSFCGyfp8g2sw')
        else:
            tts.say("  .")
            tts.runAndWait()
      
      



. 1 :





:





elif " " in an or "" in an:
        ca = 1
        tts.say(" ")
        tts.runAndWait()
        webbrowser.open("https:/vk.com")
      
      







, :





elif "  " in an:
        ca = 1
        tts.say("   ")
        tts.runAndWait()
        tts.say(an[an.find("")+3:])
        tts.runAndWait()
        sear = an[an.find("")+3:]
        webbrowser.open("https://www.google.com/search?q=" + sear)
      
      



, , :





elif "" in an:
        ca = 1
        f = open('email.txt', 'r')
        if f.read(1) == "":
            tts.say("  ,      !     .")
            tts.runAndWait()
            a = 1
            while True:
                v = input("    .")
                f = open('email.txt', 'w')
                if "https" in v:
                    web = v
                    f.write(web)
                    f.close()
                    break
      
      



:





:





elif "   " in an:
        ca = 1
        os.system('       ')
        tts.say("  ")
        tts.runAndWait()
        tts.say(name) #   .
        tts.runAndWait()
      
      



:





  elif " " in an or " " in an:
        ca = 1
        tts.say("")
        tts.runAndWait()
        tts.say(name)
        tts.runAndWait()
        tts.say("  .   .")
        tts.runAndWait()
        for i in range(10):
            os.system('cls' if os.name == 'nt' else 'clear')
            print(" ", i, ".")
            time.sleep(1)
        print("...")
        time.sleep(1)
        os.system('shutdown -s')
      
      



. , :





#    
    elif "" in an or "" in an:
        ca = 1
        f = open("z1.txt", "a")
        if an[len(""):] == "":
            tts.say("    !    ,        .")
            tts.runAndWait()
            print("!     !")
        else:
            tts.say(" ,      ,   ?")
            tts.runAndWait()
            an45 = an[len(""):] + ","
            f.write(an45)
            f.close()
    elif "" in an:
        ca = 1
        f = open("z1.txt", "r")
        if f.read(1) == "":
            tts.say("     .    ,        .")
            tts.runAndWait()
        else:
            st = f.read()
            print(st)
            tts.say("     :")
            tts.runAndWait()
            tts.say(st)
            tts.runAndWait()
            tts.say("   ,    .")
            tts.runAndWait()
            f.close()
    elif "  " in an or "  " in an:
        ca = 1
        print(" ?")
        tts.say("    ?  .")
        tts.runAndWait()
        #
        r = sr.Recognizer()
        with sr.Microphone(device_index = 1) as source:
            print(' ')
            r.adjust_for_ambient_noise(source, duration=0.5) #  
            print('...')
            audio = r.listen(source)
            print(' ')
        try:
            query = r.recognize_google(audio, language = 'ru-RU')
            an = query.lower()
            print(f' : {query.lower()}')
            
        except:
            print('-')
        if an == "" or "" in an or "" in an:
            ca = 1
            print("...")
            f = open("z1.txt", "w")
            f.write("")
            tts.say("  .")
            tts.runAndWait()
        else:
            print("...")
            tts.say("  ,   .     ...")
            tts.runAndWait()
        f.close()
      
      



:





elif "" in an:
				ca = 1
        tts1 = input("  :")
        f = open("tts.txt", "w")
        f.write(tts1)
        f.close()
 elif "" in an or "" in an:
        ca = 1
        tts.say(",  .     ")
        tts.runAndWait()
        an4925479864 = input("[]  enter: ")
        tts.say("-,  ?.")
        tts.runAndWait()
      
      



. , , " " " ".





elif "  " in an:
        ca = 1
        tts.say("  ,   ?")
        tts.runAndWait()
        f = open('dialogset.txt', 'w')
        f.write("1")
        f.close()
    elif "  " in an:
        ca = 1
        tts.say("  .")
        tts.runAndWait()
        f = open('dialogset.txt', 'w')
        f.write("0")
        f.close()
    f = open('dialogset.txt', 'r')
    an4897987 = f.read(1)
    f.close()

    

    if an4897987 == "1":
        rsm = random.randint(1,3)
        if "" in an or "" in an:
            ca = 1
            if rsm == 1:
                tts.say(",    ?.")
                tts.runAndWait()
            elif rsm == 2:
                tts.say(" .")
                tts.runAndWait()
            elif rsm == 3:
                tts.say(".")
                tts.runAndWait()
         #   `
        elif "1" in an or "2" in an:
            ca = 1
            if rsm == 1:
                tts.say("1 ")
                tts.runAndWait()
            elif rsm == 2:
                tts.say("2 .")
                tts.runAndWait()
            elif rsm == 3:
                tts.say("3 ")
                tts.runAndWait()
        
        elif "" in an and "" in an:
            ca = 1
            if rsm == 1:
                tts.say(" - ,    ,   ,    -.")
                tts.runAndWait()
            elif rsm == 2:
                tts.say("  !     .   .  ?")
                tts.runAndWait()
                cont = ""
                cikl = 0
            elif rsm == 3:
                tts.say("   !   ?")
                tts.runAndWait()   

        elif "" in an or "" in an:
            ca = 1
            if rsm == 1:
                tts.say("   ,  ?")
                tts.runAndWait()
            elif rsm == 2:
                tts.say("   ,     .")
                tts.runAndWait()
            elif rsm == 3:
                tts.say("   .")
                tts.runAndWait()
      
      



And if there is a mistake? Or, what will the bot do if it doesn't find an answer? That's what. By the way, the assistant understands that he did not execute any commands during the cycle if the variable ca = 0.





if an == "":
            print("")
            ca = 1
    if ca == 0:
        print("")

        if ra == 1 or ra ==2:
            tts.say(" ...")
            tts.runAndWait()
        elif ra == 3 or ra == 4:
            tts.say("  .")
            tts.runAndWait()
        elif ra == 5 or ra == 6:
            tts.say("    .")
            tts.runAndWait()
        elif ra == 7 or ra == 8:
            tts.say("      ,    ")
            tts.runAndWait()
        elif ra == 9 or ra == 10:
            tts.say("     .")
            tts.runAndWait()
      
      







It remains only to "zero", there is not this, but a variable so that there are no repeated operations:





    an = ""
    if cikl == 2:
        cikl = 0
        cont = ""
      
      




That's all my code, if you use it, please include a link to this post in the code or interface.





I am waiting for your advice, ideas and criticism in the comments below the post and in the mail: glebilic@gmail.com.





Until!








All Articles