import speech_recognition as sr
import pyttsx3
from typing import Optional

engine = pyttsx3.init()
engine.setProperty('rate', 150)   # немного медленнее, читаемо

def speak(text: str) -> None:
    engine.say(text)
    engine.runAndWait()

def recognize() -> str:
    r = sr.Recognizer()
    try:
        with sr.Microphone() as source:
            print("Listening…")
            audio = r.listen(source, timeout=5)
            return r.recognize_google(audio)
    except (sr.WaitTimeoutError, OSError):
        # fallback – ввод с клавиатуры
        return input("Введите команду вручную: ")
