summaryrefslogtreecommitdiff
path: root/__main__.py
blob: 6692d7ec0ea3f88ce9385cfdc1968c2f92efc92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import yaml

from pathlib import Path

from modules.config import Config
from .modules.config import load_config
from .modules.hassil.recognize import recognize
from .modules.hassil.util import merge_dict
from .modules.hassil.intents import Intents, TextSlotList
from modules.responses.responses import Responses

from .modules.input_handlers.stdin_input import StdinInput
from .modules.input_handlers.pipewire_record import PipeWireRecord

from .modules.intents import *


def main():
    config = load_config()
    input_dict = {"intents": {}}

    intent_yaml_path = Path(config.intents_dir)
    intent_yaml_file_paths = intent_yaml_path.glob("*.yaml")
    for yaml_file_path in intent_yaml_file_paths:
        with open(yaml_file_path, "r", encoding="utf-8") as yaml_file:
            merge_dict(input_dict, yaml.safe_load(yaml_file))

    intents = Intents.from_dict(input_dict)

    response_yaml_path = Path(config.responses_dir)
    response_yaml_file_paths = response_yaml_path.glob("*.yaml")
    for yaml_file_path in response_yaml_file_paths:
        with open(yaml_file_path, "r", encoding="utf-8") as yaml_file:
            merge_dict(input_dict, yaml.safe_load(yaml_file))

    responses = Responses.from_dict(input_dict)
    
    processes = []
    for file in os.listdir(config.applications_dir):
        if file == "[":
            continue

        full_path = os.path.join(config.applications_dir, file)
        if os.path.isfile(full_path) or os.path.islink(full_path):
            processes.append(file)

    slot_lists = {
        "process": TextSlotList.from_strings(processes)
    }

    input_handler = PipeWireRecord() if config.input_handler == Config.INPUT_PW else StdinInput()

    try:
        # TODO select input type from config
        for input_text in input_handler.get_input():
            result = recognize(input_text, intents, slot_lists=slot_lists)
            if result is not None:
                result_dict = {
                    "intent": result.intent.name,
                    **{e.name: e.value for e in result.entities_list},
                }
                print(result_dict)
                handler = getattr(globals()[result_dict["domain"]], result_dict["intent"])
                handler(result_dict, config)
            else:
                print("<no match>")
    finally:
        input_handler.cleanup()

if __name__ == '__main__':
    main()