Сделал своего голосового ассистента, все работало исправно до перехода на Windows(Я сидел на Linux). При произнесении имени ассистента он показывает что услышал User said: имя, но на команду никак не реагирует, ошибки так же нет. Вот сам код:
[code python]from ast import While
import json
import datetime as dt
import pyaudio
from vosk import Model, KaldiRecognizer
import pyttsx3
import os
import pygame
import time
from fuzzywuzzy import fuzz
import platform
import webbrowser
import subprocess
import sys
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLineEdit, QMainWindow, QPushButton,
QSizePolicy, QStatusBar, QWidget)

class Ui_LoginWindow(object):
with open('commands.json', 'r') as file:
commands_data = json.load(file)
commands = commands_data.get('commands', [])
def setupUi(self, LoginWindow):
if not LoginWindow.objectName():
LoginWindow.setObjectName(u"LoginWindow")
LoginWindow.resize(480, 157)
LoginWindow.setMinimumSize(QSize(480, 157))
LoginWindow.setMaximumSize(QSize(480, 157))
icon = QIcon()
icon.addFile(u"1679059321_bogatyr-club-p-fioletovii-fon-kiberpank-foni-vkontakte-80.png", QSize(), QIcon.Normal, QIcon.Off)
LoginWindow.setWindowIcon(icon)
LoginWindow.setStyleSheet(u"background-color: rgb(1, 0, 31);")
self.centralwidget = QWidget(LoginWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.LoginEnter = QPushButton(self.centralwidget)
self.LoginEnter.setObjectName(u"LoginEnter")
self.LoginEnter.setGeometry(QRect(290, 60, 161, 51))
self.LoginEnter.setStyleSheet(u"background-color: rgb(56, 56, 56);")
self.NoAccount = QPushButton(self.centralwidget)
self.NoAccount.setObjectName(u"NoAccount")
self.NoAccount.setGeometry(QRect(230, 0, 251, 34))
self.NoAccount.setStyleSheet(u"background-color: rgb(56, 56, 56);")
self.LoginLogin = QLineEdit(self.centralwidget)
self.LoginLogin.setObjectName(u"LoginLogin")
self.LoginLogin.setGeometry(QRect(20, 40, 221, 41))
self.LoginLogin.setStyleSheet(u"background-color: rgb(56, 56, 56);")
self.LoginPassword = QLineEdit(self.centralwidget)
self.LoginPassword.setObjectName(u"LoginPassword")
self.LoginPassword.setGeometry(QRect(20, 90, 221, 41))
self.LoginPassword.setStyleSheet(u"background-color: rgb(56, 56, 56);")
self.LoginPassword.setEchoMode(QLineEdit.Password)
LoginWindow.setCentralWidget(self.centralwidget)
self.statusbar = QStatusBar(LoginWindow)
self.statusbar.setObjectName(u"statusbar")
LoginWindow.setStatusBar(self.statusbar)

self.retranslateUi(LoginWindow)
self.LoginEnter.clicked.connect(self.login)
QMetaObject.connectSlotsByName(LoginWindow)

def login(self):
is_gosha_active = False
last_command_time = None
username = self.LoginLogin.text()
password = self.LoginPassword.text()

try:
with open('accounts.json') as accounts_file:
accounts = json.load(accounts_file)
except FileNotFoundError:
print("Файл 'accounts.json' не найден.")
return
except json.JSONDecodeError:
print("Ошибка декодирования JSON в файле 'accounts.json'.")
return

for account in accounts:
if account["username"] == username and account["password"] == password:
print("Вход выполнен!")
user_role = account['role']
if user_role == 'admin':
model = Model("vosk")
rec = KaldiRecognizer(model, 16000)
engine = pyttsx3.init()
p = pyaudio.PyAudio()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)
user = os.getlogin()
now = dt.datetime.now()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
stream.start_stream()

with open('commands.json', 'r') as file:
commands_data = json.load(file)
commands = commands_data.get('commands', [])

def close_current_window():
system = platform.system()
if system == "Windows":
os.system("taskkill /f /im explorer.exe")
elif system == "Linux":
os.system("xdotool key Alt+F4")
elif system == "Darwin":
os.system('osascript -e "tell application \\"System Events\\" to keystroke \\"w\\" using {command down}"')

def minimize_current_window():
system = platform.system()
if system == "Windows":
os.system("powershell -command $wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('% ')")
elif system == "Linux":
os.system("xdotool getactivewindow windowminimize")
elif system == "Darwin":
os.system('osascript -e "tell application \\"System Events\\" to keystroke \\"m\\" using {command down}"')

def listen():
while True:
data = stream.read(4000, exception_on_overflow=False)
if (rec.AcceptWaveform(data)) and (len(data) > 0):
answer = json.loads(rec.Result())
if answer['text']:
user_text = answer['text']
print("User said:", user_text)
yield user_text

def find_most_similar_command(text):
max_similarity = -1
closest_command = None
for command_data in commands:
activation_phrase = command_data.get('activation_phrase', '')
similarity = fuzz.token_set_ratio(text.lower(), activation_phrase.lower())
if similarity > max_similarity:
max_similarity = similarity
closest_command = command_data

return closest_command, max_similarity

def execute_command(command):
global is_gosha_active, Speak

closest_command, similarity = find_most_similar_command(command)

with open('commands.json', 'r') as file:
commands_data = json.load(file)
commands = commands_data.get('commands', [])
if similarity >= 80:
response = closest_command['response']
engine.say(response)
engine.runAndWait()
if closest_command.get('action') == "run_mom_code":
time.sleep(1)
subprocess.Popen(["python", "mom.py"])
if closest_command.get('action') == "time":
timeh = now.hour
timem = now.minute
engine.say(timeh)
engine.say('часов')
engine.say(timem)
engine.say("минут")
engine.runAndWait()
if closest_command.get('action') == "open_browser":
webbrowser.open("https://www.google.ru/")
if closest_command.get('action') == "open_youtube":
webbrowser.open("https://www.youtube.com/")
if closest_command.get('action') == "close_window":
close_current_window()
if closest_command.get('action') == "minimize_window":
minimize_current_window()
if closest_command.get('action') == "user":
engine.say(user)
engine.runAndWait()
else:
print("Команда не распознана.")

for text in listen():
current_time = now
with open('commands.json', 'r') as file:
commands_data = json.load(file)
commands = commands_data.get('commands', [])
if last_command_time and (current_time - last_command_time).seconds > 7:
is_gosha_active = False

if not is_gosha_active and commands[0]["activation_phrase"] in text.lower():
last_command_time = now
is_gosha_active = True
print("Активация ассистента.")
if commands:
engine.say(commands[0]["response"])
engine.runAndWait()
elif is_gosha_active:
last_command_time = now
if text.lower() == commands[4]["activation_phrase"]:
print("Деактивация ассистента.")
if len(commands) > 3:
engine.say(commands[4]["response"])
engine.runAndWait()
sys.exit(app.exec())
else:
execute_command(text.lower())

elif user_role == 'tester':
print('Не доступно для тестера')
elif user_role == 'user':
print('Не доступно для пользователя')

def retranslateUi(self, LoginWindow):
LoginWindow.setWindowTitle(QCoreApplication.translate("LoginWindow", u"\u0413\u043e\u043b\u043e\u0441\u043e\u0432\u043e\u0439 \u0410\u0441\u0441\u0438\u0441\u0442\u0435\u043d\u0442 \u0413\u043e\u0448\u0430(\u0412\u0445\u043e\u0434)", None))
self.LoginEnter.setText(QCoreApplication.translate("LoginWindow", u"\u0412\u043e\u0439\u0442\u0438", None))
self.NoAccount.setText(QCoreApplication.translate("LoginWindow", u"\u041d\u0435\u0442 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430? \u0417\u0430\u0440\u0435\u0433\u0435\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f.", None))
self.LoginLogin.setPlaceholderText(QCoreApplication.translate("LoginWindow", u"\u041b\u043e\u0433\u0438\u043d", None))
self.LoginPassword.setPlaceholderText(QCoreApplication.translate("LoginWindow", u"\u041f\u0430\u0440\u043e\u043b\u044c", None))

class LoginWindow(QMainWindow):
def __init__(self):
super(LoginWindow, self).__init__()
self.ui = Ui_LoginWindow()
self.ui.setupUi(self)

if __name__ == "__main__":
app = QApplication(sys.argv)
window = LoginWindow()
window.show()
sys.exit(app.exec())[/code]
Json файлы:
Commands:
{
"commands": [
{
"name": "гоша",
"activation_phrase": "гоша",
"response": "Слушаю"
},
{
"name": "привет",
"activation_phrase": "привет",
"response": "Приветствую вас!"
},
{
"name": "как дела",
"activation_phrase": "как дела",
"response": "У меня всё отлично, спасибо!"
},
{
"name": "время",
"activation_phrase": "время",
"response": "Текущее время:",
"action": "time"
},
{
"name": "пока",
"activation_phrase": "пока",
"response": "До свидания!"
},
{
"name": "пользователь",
"activation_phrase": "пользователь",
"response": "Сейчас в системе:",
"action": "user"
},
{
"name": "открой браузер",
"activation_phrase": "открой браузер",
"response": "Открываю браузер",
"action": "open_browser"
},
{
"name": "открой ютуб",
"activation_phrase": "открой ютуб",
"response": "Открываю Ютуб",
"action": "open_youtube"
},
{
"name": "др",
"activation_phrase": "поздравь мою маму",
"response": "Поздравляю вашу маму с днем рождения",
"action": "run_mom_code"
},
{
"name": "молодец",
"activation_phrase": "ты молодец",
"response": "Спасибо"
},
{
"name": "закрой текущее окно",
"activation_phrase": "закрой текущее окно",
"response": "Закрываю текущее окно",
"action": "close_window"
},
{
"name": "сверни",
"activation_phrase": "сверни текущее окно",
"response": "Сворачиваю текущее окно",
"action": "minimize_window"
},
{
"activation_phrase": "посчитай",
"response": "Хорошо, давай посчитаем.",
"action": "calculate"
}
]
}
Accounts:
[
{"username": "admin", "password": "admin", "role": "admin"},
{"username": "tester", "password": "tester", "role": "tester"},
{"username": "gosha", "password": "gosha", "role": "user"}
]