Create interface.py
Browse files- interface.py +63 -0
interface.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
import pyttsx3
|
| 4 |
+
import pickle
|
| 5 |
+
from model import MaestroAssistant
|
| 6 |
+
|
| 7 |
+
class VoiceAssistant:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.recognizer = sr.Recognizer()
|
| 10 |
+
self.engine = pyttsx3.init()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
with open('maestro_model.pkl', 'rb') as f:
|
| 15 |
+
self.assistant = pickle.load(f)
|
| 16 |
+
except:
|
| 17 |
+
self.assistant = MaestroAssistant()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
voices = self.engine.getProperty('voices')
|
| 21 |
+
try:
|
| 22 |
+
self.engine.setProperty('voice', voices[0].id)
|
| 23 |
+
except:
|
| 24 |
+
pass
|
| 25 |
+
|
| 26 |
+
self.engine.setProperty('rate', 150)
|
| 27 |
+
|
| 28 |
+
def listen(self):
|
| 29 |
+
|
| 30 |
+
with sr.Microphone() as source:
|
| 31 |
+
print("Слушаю...")
|
| 32 |
+
self.recognizer.adjust_for_ambient_noise(source)
|
| 33 |
+
audio = self.recognizer.listen(source)
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
text = self.recognizer.recognize_google(audio, language='ru-RU').lower()
|
| 37 |
+
print(f"Распознано: {text}")
|
| 38 |
+
return text
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Ошибка распознавания: {e}")
|
| 41 |
+
return ""
|
| 42 |
+
|
| 43 |
+
def speak(self, text):
|
| 44 |
+
|
| 45 |
+
print(f"Маэстро: {text}")
|
| 46 |
+
self.engine.say(text)
|
| 47 |
+
self.engine.runAndWait()
|
| 48 |
+
|
| 49 |
+
def run(self):
|
| 50 |
+
|
| 51 |
+
print("Ассистент Маэстро активирован! Говорите 'Эй Маэстро' для начала.")
|
| 52 |
+
while True:
|
| 53 |
+
text = self.listen()
|
| 54 |
+
if text.startswith(self.assistant.wake_word):
|
| 55 |
+
response = self.assistant.handle_command(text)
|
| 56 |
+
if response:
|
| 57 |
+
self.speak(response)
|
| 58 |
+
else:
|
| 59 |
+
self.speak("Не понял команду")
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
assistant = VoiceAssistant()
|
| 63 |
+
assistant.run()
|