Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6021db52aa | |||
| a2375e6daf | |||
| 8504126d4a | |||
| 4dc4ffa58c | |||
| 26b38811cc | |||
| 1d63461e35 | |||
| 5f1e2e67a4 | |||
| c23b1d42bf | |||
| b2866d073c | |||
| a889c68e40 |
@@ -72,9 +72,3 @@ weather:
|
||||
- возможен дождь сегодня?
|
||||
- прогноз погоды на сегодня
|
||||
- погода
|
||||
home_assistant_execute:
|
||||
- включи телевизор
|
||||
- выключи телевизор
|
||||
- начни уборку
|
||||
home_assistant_get:
|
||||
- тест
|
||||
+2
-7
@@ -1,10 +1,5 @@
|
||||
VA_ALIAS = ("джарвис",)
|
||||
VA_TBR = ("скажи", "покажи", "ответь", "произнеси", "расскажи", "сколько", "слушай")
|
||||
VA_ALIAS = ('джарвис',)
|
||||
VA_TBR = ('скажи', 'покажи', 'ответь', 'произнеси', 'расскажи', 'сколько', 'слушай')
|
||||
MODEL_NAME = "vosk-model-small-ru-0.22" # vosk-model-ru-0.42
|
||||
MICROPHONE_INDEX = -1
|
||||
PICOVOICE_TOKEN = "4xbwaZwZmSHeTiowFl5Rgqsc8CR4FKGV8YueJUlR4Zt2e1kB64IDcA=="
|
||||
|
||||
|
||||
# home assistant
|
||||
HOME_ASSISTANT_URL = "http://192.168.0.112:9999/api"
|
||||
HOME_ASSISTANT_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI5NjczNDZjYjc2YzI0YWQzODdhMmUwMmM2MjViZGVjZCIsImlhdCI6MTcxNDQ3MzkzNywiZXhwIjoyMDI5ODMzOTM3fQ.TATpIMXivJOioCtUI8PKg6gyTQYMG6bur6enm6NxjtY"
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
пылесос:
|
||||
- entity_id:vacuum.roborock_vacuum_m1s
|
||||
- state:находится в
|
||||
- attributes.battery_level:а его уровень зарядки
|
||||
@@ -1,67 +0,0 @@
|
||||
import requests
|
||||
import yaml
|
||||
from fuzzywuzzy import process
|
||||
|
||||
from data import config
|
||||
|
||||
|
||||
class HomeAssistant:
|
||||
def __init__(self):
|
||||
self.url = "http://192.168.0.112:9999/api"
|
||||
self.token = config.HOME_ASSISTANT_TOKEN
|
||||
self.HA_CMD_LIST = yaml.safe_load(open('data/home_assistant_entities.yaml', encoding='utf8'))
|
||||
|
||||
def get_info(self, state):
|
||||
response = requests.get(
|
||||
url=f"{self.url}/states",
|
||||
headers={
|
||||
"Authorization": "Bearer " + self.token
|
||||
}
|
||||
)
|
||||
for entity in response.json():
|
||||
if entity["entity_id"] == state:
|
||||
return entity
|
||||
return response
|
||||
|
||||
def send_process(self, command="выключи телевизор"):
|
||||
response = requests.post(
|
||||
url=f"{self.url}/services/conversation/process",
|
||||
json={"text": command},
|
||||
headers={
|
||||
"Authorization": "Bearer " + self.token,
|
||||
"content-type": "application/json"
|
||||
},
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return True
|
||||
return False
|
||||
|
||||
def voice_to_name(self, voice: str) -> str:
|
||||
words = voice.lower().split()
|
||||
best_match = None
|
||||
highest_score = 0
|
||||
for word in words:
|
||||
result, score = process.extractOne(word, self.HA_CMD_LIST.keys())
|
||||
if score > highest_score:
|
||||
highest_score = score
|
||||
best_match = result
|
||||
return best_match
|
||||
|
||||
def validate_info(self, name: str):
|
||||
answer = name
|
||||
entity_config = self.HA_CMD_LIST.get(name)
|
||||
if entity_config:
|
||||
# Создание словаря, разделяя каждый элемент конфигурации на ключ и значение
|
||||
entity_details = {item.split(':')[0]: item.split(':')[1] for item in entity_config}
|
||||
entity_id = entity_details.pop("entity_id", None)
|
||||
if entity_id:
|
||||
responses = self.get_info(entity_id)
|
||||
for attribute_path, label in entity_details.items():
|
||||
response = responses
|
||||
try:
|
||||
for attribute in attribute_path.split("."):
|
||||
response = response[attribute]
|
||||
answer += f" {label} {response}"
|
||||
except KeyError:
|
||||
continue
|
||||
return answer
|
||||
+1
-4
@@ -11,7 +11,6 @@ from fuzzywuzzy import fuzz
|
||||
from pvrecorder import PvRecorder
|
||||
|
||||
from data import config
|
||||
from modules import HomeAssistant
|
||||
from utils import download_models, execute_cmd, play
|
||||
|
||||
|
||||
@@ -21,7 +20,6 @@ class Jarvis:
|
||||
self.recorder = None
|
||||
self.CDIR = os.getcwd()
|
||||
self.VA_CMD_LIST = yaml.safe_load(open('data/commands.yaml', encoding='utf8'))
|
||||
self.home_assistant = HomeAssistant.HomeAssistant()
|
||||
self.porcupine = pvporcupine.create(
|
||||
access_key=config.PICOVOICE_TOKEN,
|
||||
keywords=['jarvis'],
|
||||
@@ -73,7 +71,6 @@ class Jarvis:
|
||||
if vrt > rc['percent']:
|
||||
rc['cmd'] = c
|
||||
rc['percent'] = vrt
|
||||
rc['recognized_phrase'] = x
|
||||
if len(rc['cmd'].strip()) <= 0:
|
||||
return False
|
||||
elif rc['percent'] < 70 or rc['cmd'] not in self.VA_CMD_LIST.keys():
|
||||
@@ -81,7 +78,7 @@ class Jarvis:
|
||||
time.sleep(1)
|
||||
return False
|
||||
else:
|
||||
execute_cmd.execute_cmd(self, rc['cmd'], rc['recognized_phrase'], voice)
|
||||
execute_cmd.execute_cmd(self, rc['cmd'])
|
||||
return True
|
||||
|
||||
def play(self, phrase, wait_done=True):
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
def execute_cmd(self, cmd: str, recognized_phrase: str, voice: str):
|
||||
def execute_cmd(self, cmd: str):
|
||||
if cmd == 'thanks':
|
||||
self.play("thanks")
|
||||
elif cmd == 'stupid':
|
||||
@@ -7,9 +7,3 @@ def execute_cmd(self, cmd: str, recognized_phrase: str, voice: str):
|
||||
self.play("off", True)
|
||||
self.porcupine.delete()
|
||||
exit(0)
|
||||
elif cmd == 'home_assistant_execute':
|
||||
self.home_assistant.send_process(recognized_phrase)
|
||||
elif cmd == 'home_assistant_get':
|
||||
entity_name = self.home_assistant.voice_to_name(voice)
|
||||
entity_info = self.home_assistant.validate_info(entity_name)
|
||||
print(entity_info)
|
||||
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
import time
|
||||
|
||||
import sounddevice as sd
|
||||
import torch
|
||||
|
||||
language = 'ru'
|
||||
model_id = 'ru_v3'
|
||||
sample_rate = 48000 # 48000
|
||||
speaker = 'aidar' # aidar, baya, kseniya, xenia, random
|
||||
put_accent = True
|
||||
put_yo = True
|
||||
device = torch.device('cpu') # cpu или gpu
|
||||
text = "Хауди Хо, друзья!!!"
|
||||
|
||||
model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models',
|
||||
model='silero_tts',
|
||||
language=language,
|
||||
speaker=model_id)
|
||||
model.to(device)
|
||||
|
||||
|
||||
def va_speak(what: str):
|
||||
audio = model.apply_tts(text=what + "..",
|
||||
speaker=speaker,
|
||||
sample_rate=sample_rate,
|
||||
put_accent=put_accent,
|
||||
put_yo=put_yo)
|
||||
|
||||
sd.play(audio, sample_rate * 1.05)
|
||||
time.sleep((len(audio) / sample_rate) + 0.5)
|
||||
sd.stop()
|
||||
Reference in New Issue
Block a user