sinapsis_core 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. sinapsis_core/__init__.py +0 -0
  2. sinapsis_core/agent/__init__.py +5 -0
  3. sinapsis_core/agent/agent.py +362 -0
  4. sinapsis_core/agent/agent_helpers.py +112 -0
  5. sinapsis_core/agent/base_models.py +361 -0
  6. sinapsis_core/agent/builder.py +112 -0
  7. sinapsis_core/agent/profiler_agent.py +117 -0
  8. sinapsis_core/cli/__init__.py +0 -0
  9. sinapsis_core/cli/arg_parser.py +29 -0
  10. sinapsis_core/cli/display_template_info.py +202 -0
  11. sinapsis_core/cli/run_agent_from_config.py +40 -0
  12. sinapsis_core/context_managers/__init__.py +0 -0
  13. sinapsis_core/context_managers/env_var_override_context_manager.py +39 -0
  14. sinapsis_core/context_managers/working_dir_context_manager.py +57 -0
  15. sinapsis_core/data_containers/__init__.py +0 -0
  16. sinapsis_core/data_containers/annotations.py +152 -0
  17. sinapsis_core/data_containers/data_packet.py +364 -0
  18. sinapsis_core/data_containers/model_types/__init__.py +38 -0
  19. sinapsis_core/data_containers/model_types/_asr_base_models.py +39 -0
  20. sinapsis_core/data_containers/model_types/llm_types.py +189 -0
  21. sinapsis_core/exceptions/agent_exceptions.py +2 -0
  22. sinapsis_core/hooks/__init__.py +15 -0
  23. sinapsis_core/hooks/pre_init_hook_registry.py +225 -0
  24. sinapsis_core/template_base/__init__.py +10 -0
  25. sinapsis_core/template_base/base_models.py +59 -0
  26. sinapsis_core/template_base/copy_data_container.py +46 -0
  27. sinapsis_core/template_base/dynamic_template.py +169 -0
  28. sinapsis_core/template_base/dynamic_template_factory.py +454 -0
  29. sinapsis_core/template_base/dynamic_template_helpers/__init__.py +0 -0
  30. sinapsis_core/template_base/dynamic_template_helpers/builder_methods.py +92 -0
  31. sinapsis_core/template_base/dynamic_template_helpers/signature_parser.py +625 -0
  32. sinapsis_core/template_base/empty_data_container.py +23 -0
  33. sinapsis_core/template_base/input_template.py +8 -0
  34. sinapsis_core/template_base/merge_data_flow.py +33 -0
  35. sinapsis_core/template_base/multi_execute_template.py +65 -0
  36. sinapsis_core/template_base/output_template.py +8 -0
  37. sinapsis_core/template_base/split_data_flow.py +50 -0
  38. sinapsis_core/template_base/template.py +292 -0
  39. sinapsis_core/template_base/transfer_data_container.py +13 -0
  40. sinapsis_core/utils/__init__.py +0 -0
  41. sinapsis_core/utils/base_model_updater.py +148 -0
  42. sinapsis_core/utils/doc_string_generator.py +20 -0
  43. sinapsis_core/utils/env_var_keys.py +249 -0
  44. sinapsis_core/utils/env_var_utils.py +6 -0
  45. sinapsis_core/utils/logging_utils.py +53 -0
  46. sinapsis_core-1.0.0.dist-info/METADATA +239 -0
  47. sinapsis_core-1.0.0.dist-info/RECORD +50 -0
  48. sinapsis_core-1.0.0.dist-info/WHEEL +4 -0
  49. sinapsis_core-1.0.0.dist-info/licenses/LICENSE-APACHE +202 -0
  50. sinapsis_core-1.0.0.dist-info/licenses/LICENSE-MIT +21 -0
@@ -0,0 +1,202 @@
1
+ # -*- coding: utf-8 -*-
2
+ import argparse
3
+ import difflib
4
+ import inspect
5
+ from types import ModuleType
6
+
7
+ import yaml
8
+ from pydantic import BaseModel
9
+ from pydantic._internal._model_construction import ModelMetaclass
10
+ from pydantic_core import PydanticUndefined
11
+
12
+ from sinapsis_core.template_base.template import Template
13
+
14
+ # ruff: noqa: T201 print
15
+
16
+
17
+ def get_template(template_name: str, templates_module: ModuleType) -> Template | None:
18
+ """
19
+ Try to import a template based on it's class name. If unable to import,
20
+ try and find close matches using `difflib`. Prints a log message indicating
21
+ if there are close matches or if the template name is not valid.
22
+
23
+ Args:
24
+ template_name (str): string name of the template.
25
+ templates_module (ModuleType): Module where the template exists
26
+
27
+ Returns:
28
+ template_cls: Template, if able to import it, otherwise None.
29
+ """
30
+ try:
31
+ template_cls = getattr(templates_module, template_name)
32
+ except (AttributeError, FileNotFoundError):
33
+ not_found_msg = f"\n`{template_name}` is not a valid template. "
34
+ closest_match = difflib.get_close_matches(template_name, templates_module.__all__)
35
+ if closest_match:
36
+ not_found_msg += f"Did you mean one of these? {closest_match}"
37
+ print(not_found_msg)
38
+ return None
39
+ return template_cls
40
+
41
+
42
+ def parse_template_doc_string(template_name: str, template_cls: Template) -> str:
43
+ """
44
+ Read a template docstring and return it as a string. If the template is a dynamic template
45
+ that wraps another module, then return the docstring for the module instead of the base template.
46
+ Args:
47
+ template_name: Template name str only required to append to the message to be displayed.
48
+ template_cls: Template class to read the docstring from.
49
+
50
+ Returns:
51
+ str: String representation of the template with the name of the template included..
52
+
53
+ """
54
+ template_doc_string = f"\n{template_name}: {inspect.getdoc(template_cls) or template_cls.__doc__}"
55
+ return template_doc_string
56
+
57
+
58
+ def make_example_atts_config(att_base_model: type[BaseModel]) -> dict:
59
+ """Generates an example attributes configuration based on the BaseModel
60
+ Args:
61
+ att_base_model (BaseModel): The Base model of the attributes for the template.
62
+ Returns:
63
+ dict : The example dictionary for the attributes
64
+ """
65
+ placeholder_str = "replace_me:"
66
+ example_config = {}
67
+ for att_name, att_value in att_base_model.model_fields.items():
68
+ if att_name == "metadata":
69
+ continue
70
+ field_annotation = att_value.annotation
71
+ default_value = att_value.default
72
+
73
+ if inspect.isclass(att_value.annotation) and isinstance(att_value.annotation, (BaseModel, ModelMetaclass)):
74
+ sub_model_dic = make_example_atts_config(att_value.annotation)
75
+ default_value = sub_model_dic
76
+ if default_value == PydanticUndefined:
77
+ default_value = f"`{placeholder_str}{field_annotation}`"
78
+
79
+ example_config[att_name] = default_value
80
+
81
+ return example_config
82
+
83
+
84
+ def make_example_template_config(template_name: str, templates_module: ModuleType) -> dict:
85
+ """Generates an example configuration dictionary for a given template and agent
86
+ The method retrieves a template class based on the template name and constructs an attribute configuration
87
+ using the AttributesBaseModel and formats it into a dictionary
88
+ Args:
89
+ template_name (str): Name of the template for the example
90
+ templates_module (ModuleType): Module where the template exists.
91
+ Returns:
92
+ dict : The example configuration of the Agent.
93
+ """
94
+ template_cls: Template = get_template(template_name, templates_module)
95
+ atts = make_example_atts_config(template_cls.AttributesBaseModel)
96
+ input_template_name: str = "InputTemplate"
97
+ config_dict = {
98
+ "template_name": template_cls.__name__,
99
+ "class_name": template_cls.__name__,
100
+ "template_input": input_template_name,
101
+ "attributes": atts,
102
+ }
103
+ predefined_agent_config = {
104
+ "agent": {"name": "my_test_agent"},
105
+ "templates": [
106
+ {
107
+ "template_name": input_template_name,
108
+ "class_name": input_template_name,
109
+ "attributes": {},
110
+ },
111
+ config_dict,
112
+ ],
113
+ }
114
+ return predefined_agent_config
115
+
116
+
117
+ def parse_attributes_base_model(att_base_model: type[BaseModel]) -> list[str]:
118
+ """
119
+ Creates a list of strings containing attribute names and attribute values in a
120
+ pydantic base model. It will skip template_info attributes given they are not used.
121
+
122
+ Args:
123
+ att_base_model: In most cases, this is the AttributesBaseModel class of a template. Some
124
+ dynamic templates may have additional base models which are also valid.
125
+
126
+ Returns:
127
+ help_message_att_lines: list of strings to be printed to console.
128
+
129
+ """
130
+ help_message_att_lines: list[str] = []
131
+ for att_name, att_value in att_base_model.model_fields.items():
132
+ if att_name == "metadata":
133
+ continue
134
+ field_annotation = att_value.annotation if att_value.annotation else "None"
135
+
136
+ # if (isinstance(att_value, FieldInfo) and hasattr(att_value, "annotation") and \
137
+ if inspect.isclass(att_value.annotation) and isinstance(att_value.annotation, (BaseModel, ModelMetaclass)):
138
+ sub_lines: list = parse_attributes_base_model(att_value.annotation)
139
+ field_annotation = "".join([f"\n\t{sub_line}" for sub_line in sub_lines])
140
+
141
+ log_msg = f"\t{att_name:30}{field_annotation}"
142
+ if att_value.default != PydanticUndefined:
143
+ log_msg += f": default: {att_value.default}"
144
+
145
+ help_message_att_lines.append(log_msg)
146
+ return help_message_att_lines
147
+
148
+
149
+ def display_single_template_info(template_name: str, templates_module: ModuleType) -> None:
150
+ """
151
+ For a given template name, display it's docstring and information about
152
+ all the attributes that can be set by a user, either through code, a config,
153
+ or the UI. If the template is dynamic, show the hidden attributes as well.
154
+
155
+ Args:
156
+ template_name (str): Case sensitive string representing the template class name.
157
+ templates_module (ModuleType): Module where the template_name resides.
158
+
159
+ """
160
+
161
+ template_cls: Template = get_template(template_name, templates_module)
162
+ if template_cls:
163
+ print(inspect.getfile(template_cls))
164
+ print(parse_template_doc_string(template_name, template_cls))
165
+ print(f"\n{template_name} attributes: ")
166
+ for att_line in parse_attributes_base_model(template_cls.AttributesBaseModel):
167
+ print(att_line)
168
+ print()
169
+
170
+
171
+ def display_all_templates_info(templates_module: ModuleType) -> None:
172
+ """
173
+ Iterate through all templates available and print their docstrings and attributes.
174
+ See display_single_template_info. Catches import error exceptions and logs them to console..
175
+ """
176
+ for template_name in templates_module.__all__:
177
+ try:
178
+ display_single_template_info(template_name, templates_module)
179
+ except (ModuleNotFoundError, ImportError) as err:
180
+ print(f"Unable to import {template_name} due to : {err}")
181
+ print("-" * 70)
182
+
183
+
184
+ def display_template_info(parsed_args: argparse.Namespace, templates_module: ModuleType) -> None:
185
+ """
186
+ Reads the CLI action args for the 'info' command and displays info
187
+ according to the option selected by the user.
188
+
189
+ Args:
190
+ parsed_args (argparse.Namespace): parsed arguments from the CLI with 'info' options.
191
+ templates_module (ModuleType): module where the template exists.
192
+ """
193
+ if parsed_args.template:
194
+ display_single_template_info(parsed_args.template, templates_module)
195
+ elif parsed_args.all:
196
+ display_all_templates_info(templates_module)
197
+ elif parsed_args.all_template_names:
198
+ for template_name in templates_module.__all__:
199
+ print(template_name)
200
+ elif parsed_args.example_template_config:
201
+ example_config = make_example_template_config(parsed_args.example_template_config, templates_module)
202
+ print(yaml.dump(example_config, sort_keys=False))
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from sinapsis_core.agent import Agent
4
+ from sinapsis_core.agent.base_models import AgentDefinitionType, AgentMode
5
+ from sinapsis_core.agent.profiler_agent import AgentProfiler
6
+ from sinapsis_core.utils.env_var_keys import MP_START_METHOD
7
+ from sinapsis_core.utils.env_var_utils import set_multiprocessing_start_method
8
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
9
+
10
+ # need to validate that this is still needed in newer torch versions
11
+ set_multiprocessing_start_method(MP_START_METHOD)
12
+
13
+
14
+ def generic_agent_builder(build_from: AgentDefinitionType, enable_profiler: bool = False) -> Agent:
15
+ """Returns the specific type of Agent.
16
+ Args:
17
+ build_from (AgentDefinitionType): Type of configuration for the Agent
18
+ enable_profiler (bool): boolean to enable profiling
19
+ """
20
+ if enable_profiler:
21
+ return AgentProfiler(build_from)
22
+ return Agent(build_from)
23
+
24
+
25
+ def run_agent_from_config(config_file_path: str, enable_profiler: bool = False) -> None:
26
+ """Method to run the agent on an emtpy container and with the AGENT_MODE set
27
+ Args:
28
+ config_file_path (str): path to config file
29
+ enable_profiler (bool): boolean to enable profiling
30
+ """
31
+ agent = generic_agent_builder(config_file_path, enable_profiler)
32
+
33
+ agent_mode = agent.agent_attributes.agent_mode
34
+
35
+ if agent_mode == AgentMode.GENERATOR:
36
+ for result in agent(container=None):
37
+ sinapsis_logger.debug(f"result: {result}")
38
+ else:
39
+ result = agent(container=None)
40
+ sinapsis_logger.debug(f"result: {result}")
File without changes
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+
4
+
5
+ class AgentEnvVarContextManager:
6
+ """Context manager for temporarily overriding environment variables.
7
+
8
+ Manages the temporary overriding of environment variables specified in the agent's attributes.
9
+ Upon exiting the context, the original environment variable values are restored.
10
+ """
11
+
12
+ def __init__(self, agent_attributes):
13
+ self.original_env = {}
14
+ self.agent_attributes = agent_attributes
15
+
16
+ def __enter__(self):
17
+ self._backup_env()
18
+ self._set_env_overrides()
19
+
20
+ def __exit__(self, exc_type, exc_val, exc_tb):
21
+ self._cleanup_env()
22
+
23
+ def _set_env_overrides(self) -> None:
24
+ """Set the new environment variable values."""
25
+ for key, value in self.agent_attributes.env_var_overrides.items():
26
+ os.environ[key] = value
27
+
28
+ def _backup_env(self) -> None:
29
+ """Backup environment variables that need to be overridden."""
30
+ for key in self.agent_attributes.env_var_overrides:
31
+ self.original_env[key] = os.getenv(key)
32
+
33
+ def _cleanup_env(self) -> None:
34
+ """Restore original environment variables."""
35
+ for key, original_value in self.original_env.items():
36
+ if original_value is None:
37
+ os.environ.pop(key, None)
38
+ else:
39
+ os.environ[key] = original_value
@@ -0,0 +1,57 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ import threading
4
+ from pathlib import Path
5
+
6
+ from sinapsis_core.context_managers.env_var_override_context_manager import (
7
+ AgentEnvVarContextManager,
8
+ )
9
+ from sinapsis_core.utils.env_var_keys import USER_PERSISTENT_DATA_ROOT_DIR
10
+
11
+ thread_local = threading.local()
12
+ GLOBAL_LOCK = threading.Lock()
13
+
14
+
15
+ class WorkingDirContextManager(AgentEnvVarContextManager):
16
+ """
17
+ Warning: This context manager will block during template execution and
18
+ if other templates are running in other threads, they will be blocked.
19
+ """
20
+
21
+ def __init__(self, agent_attributes):
22
+ super().__init__(agent_attributes)
23
+ self.original_cwd = os.getcwd()
24
+ self.user_working_dir = self._get_user_working_dir()
25
+ self.user_working_dir.mkdir(exist_ok=True, parents=True)
26
+
27
+ def __enter__(self):
28
+ GLOBAL_LOCK.acquire()
29
+ # with GLOBAL_LOCK:
30
+ super().__enter__()
31
+ thread_local.cwd = os.getcwd()
32
+ self._change_working_dir()
33
+ return self
34
+
35
+ def __exit__(self, exc_type, exc_val, exc_tb):
36
+ # with GLOBAL_LOCK:
37
+ super().__exit__(exc_type, exc_val, exc_tb)
38
+ self._restore_working_dir()
39
+ GLOBAL_LOCK.release()
40
+
41
+ def _get_user_working_dir(self) -> Path:
42
+ """Get the user-specific working directory."""
43
+ return Path(USER_PERSISTENT_DATA_ROOT_DIR) / self.agent_attributes.user_id / self.agent_attributes.agent_id
44
+
45
+ def _change_working_dir(self) -> None:
46
+ """Change the current working directory to the user-specific directory."""
47
+ # self.user_working_dir.mkdir(exist_ok=True, parents=True)
48
+ os.chdir(str(self.user_working_dir))
49
+
50
+ def _restore_working_dir(self) -> None:
51
+ """Restore the original working directory."""
52
+ # os.chdir(self.original_cwd)
53
+ os.chdir(thread_local.cwd)
54
+
55
+ @property
56
+ def index(self):
57
+ return threading.get_ident()
File without changes
@@ -0,0 +1,152 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from typing import Any
4
+
5
+ from pydantic.dataclasses import dataclass
6
+
7
+ from sinapsis_core.data_containers.model_types import DiarizedTranscript, SpeakerTurn
8
+
9
+
10
+ class Config:
11
+ """config class for different types."""
12
+
13
+ arbitrary_types_allowed = True
14
+
15
+
16
+ @dataclass
17
+ class BoundingBox:
18
+ """
19
+ Dataclass to store image Bounding box data in [x1, y1, w, h] format
20
+ Arguments:
21
+ x (float): x-coordinate of the bbox
22
+ y (float): y-coordinate of the bbox
23
+ w (float): width of the bbox
24
+ h (float): height of the bbox
25
+ """
26
+
27
+ x: float
28
+ y: float
29
+ w: float
30
+ h: float
31
+
32
+
33
+ @dataclass
34
+ class OrientedBoundingBox:
35
+ """
36
+ Oriented Bounding Box in [x1, y1, x2, y2, x3, y3, x4, y4] format
37
+ """
38
+
39
+ x1: float
40
+ y1: float
41
+ x2: float
42
+ y2: float
43
+ x3: float
44
+ y3: float
45
+ x4: float
46
+ y4: float
47
+
48
+
49
+ @dataclass
50
+ class KeyPoint:
51
+ """
52
+ Dataclass to store image KeyPoint data in [x, y, score, visibility, label] format
53
+ Attributes:
54
+ x (float): x-coordinate of the keypoint.
55
+ y (float): y-coordinate of the keypoint.
56
+ score (float | None): confidence score of the keypoint.
57
+ visibility (int | None): visibility of the keypoint.
58
+ label (str | None): label of the keypoint.
59
+ """
60
+
61
+ x: float
62
+ y: float
63
+ score: float | None = None
64
+ visibility: int | None = None
65
+ label: str | None = None
66
+
67
+
68
+ @dataclass(config=Config)
69
+ class Segmentation:
70
+ """
71
+ Dataclass to store image segmentation data
72
+
73
+ Attributes:
74
+ mask (Any | None): full image binary mask
75
+ polygon (List[List[float]] | None): polygon points stored in
76
+ [ [ [x1, y1], [x2, y2], ... ], [ [x1, y1], ... ], ... ] format
77
+ rle (Dict[str, List[Any] | str] | None): run-length encoding of binary mask
78
+ """
79
+
80
+ mask: Any | None = None
81
+ polygon: list[list[float]] | None = None
82
+ rle: dict[str, list[Any] | str] | None = None
83
+
84
+
85
+ @dataclass
86
+ class BaseAnnotations:
87
+ """
88
+ Dataclass for annotations that can be used in different
89
+ types of data.
90
+ Args:
91
+ label (int | str | None): label of the annotation,
92
+ label_str (str | None): string representation of the label,
93
+ confidence_score (float | None): score of the annotation,
94
+ extra_labels (dict[str, float | int]): additional labels for the annotation,
95
+ is_ground_truth (bool): flag to determine if the annotation is a ground truth.
96
+ extras (Any | None): Other information about the annotation.
97
+
98
+ """
99
+
100
+ label: int | str | None = None
101
+ label_str: str | None = None
102
+ confidence_score: float | None = None
103
+ extra_labels: dict[str, float | int] | None = None
104
+ is_ground_truth: bool = False
105
+ extras: Any | None = None
106
+
107
+
108
+ @dataclass
109
+ class TextAnnotations(BaseAnnotations):
110
+ """Additional annotations for texts
111
+ Args:
112
+ language (str | None): language in which the text is written.
113
+ """
114
+
115
+ language: str | None = None
116
+
117
+
118
+ @dataclass
119
+ class ImageAnnotations(BaseAnnotations):
120
+ """
121
+ Class for image annotations used in coco sets.
122
+ Args:
123
+ bbox (BoundingBox | None): Bounding box of the annotation
124
+ oriented_bbox (OrientedBoundingBox | None): Oriented bbox of the image
125
+ segmentation (Segmentation | None): Segmentation mask of the image
126
+ is_crowd (int | None): how many annotations the image has
127
+ area (float | None): area of the image.
128
+ keypoints (list[KeyPoint] | None): list of keypoints in the image.
129
+ n_keypoints (int | None): Number of keypoints in the image.
130
+ It should correspond to len(keypoints).
131
+ embedding (list[float] | None): embedding vector of the image.
132
+ text (str | None): additional text associated with image.
133
+
134
+ """
135
+
136
+ bbox: BoundingBox | None = None
137
+ oriented_bbox: OrientedBoundingBox | None = None
138
+ segmentation: Segmentation | None = None
139
+
140
+ is_crowd: int | None = None
141
+ area: float | None = None
142
+ keypoints: list[KeyPoint] | None = None
143
+ n_keypoints: int | None = None
144
+ embedding: Any | list[float] | None = None
145
+ text: str | None = None
146
+
147
+
148
+ @dataclass
149
+ class AudioAnnotations:
150
+ raw_text: str | None = None
151
+ raw_diarization: SpeakerTurn | None = None
152
+ diarized_transcript: DiarizedTranscript | None = None