¡Hola habr! Mi nombre es Gleb Pryakhin, tengo 14 años, escribí un asistente de voz en Python 3 y lo compilé en exe.
( Enlace a la versión compilada )
Te pido que pruebes al asistente y le hagas preguntas,
si tiene preguntas o desea escribir para apoyar: en los comentarios debajo de la publicación, o por correo electrónico. correo glebilic@gmail.com.
¡Empecemos con el código! Primero, importemos los módulos:
import pyttsx3
import os
import time
import datetime
import speech_recognition as sr
import random
import webbrowser
import sounddevice as sd
Para agregar voces, descargue e instale 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()
¿Y si hay un error? O, ¿qué hará el bot si no encuentra una respuesta? Eso es lo que. Por cierto, el asistente entiende que no ejecutó ningún comando durante el ciclo si la 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()
Queda solo a "cero", no hay esto, sino una variable para que no haya operaciones repetidas:
an = ""
if cikl == 2:
cikl = 0
cont = ""
Ese es todo mi código, si lo usa, incluya un enlace a esta publicación en el código o interfaz.
Estoy esperando tus consejos, ideas y críticas en los comentarios debajo de la publicación y en el correo: glebilic@gmail.com.
¡Hasta!