sinapsis 0.1.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.

Potentially problematic release.


This version of sinapsis might be problematic. Click here for more details.

sinapsis/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ # -*- coding: utf-8 -*-
2
+ # sinapsis package
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+ from sinapsis_core.cli.arg_parser import args_parser
3
+ from sinapsis_core.cli.display_template_info import display_template_info
4
+ from sinapsis_core.cli.run_agent_from_config import run_agent_from_config
5
+
6
+ import sinapsis.templates as templates
7
+
8
+
9
+ def welcome_msg():
10
+ print("Welcome to Sinapsis CLI. Please run 'sinapsis -h' to obtain information about available commands.") # noqa: T201
11
+
12
+
13
+ def main():
14
+ args = args_parser()
15
+ match args.action:
16
+ case "run":
17
+ run_agent_from_config(args.agent_config, args.enable_profiler)
18
+ case "info":
19
+ display_template_info(args, templates)
20
+ case _:
21
+ welcome_msg()
22
+
23
+
24
+ if __name__ == "__main__":
25
+ main()
@@ -0,0 +1,22 @@
1
+ agent:
2
+ name: hello_world_agent
3
+ attributes:
4
+ templates_to_profile: "HelloWorld"
5
+ description: "Hello world agent example"
6
+
7
+ templates:
8
+ - template_name: InputTemplate # template name can be anything but must be unique
9
+ class_name: InputTemplate
10
+ attributes: {}
11
+
12
+ - template_name: HelloWorld
13
+ class_name: HelloWorld
14
+ template_input: InputTemplate
15
+ attributes:
16
+ display_text: "Hello, this is my first template!"
17
+
18
+ - template_name: DisplayHelloWorld
19
+ class_name: DisplayHelloWorld
20
+ template_input: HelloWorld
21
+ attributes: {} # we did not define any attributes in our template
22
+
@@ -0,0 +1,100 @@
1
+ # -*- coding: utf-8 -*-
2
+ import importlib
3
+ import sys
4
+ from importlib import metadata
5
+ from types import ModuleType
6
+ from typing import Callable
7
+
8
+ from sinapsis_core.utils.env_var_keys import SinapsisEnvVarDefaults
9
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
10
+
11
+ _root_lib_path = "sinapsis.templates"
12
+ _core_lib_path = "sinapsis_core.template_base"
13
+ _template_lookup = {
14
+ "CopyDataContainer": f"{_core_lib_path}.copy_data_container",
15
+ "DisplayHelloWorld": f"{_root_lib_path}.display_hello_world",
16
+ "HelloWorld": f"{_root_lib_path}.hello_world",
17
+ "InputTemplate": f"{_core_lib_path}.input_template",
18
+ "MergeDataFlow": f"{_core_lib_path}.merge_data_flow",
19
+ "OutputTemplate": f"{_core_lib_path}.output_template",
20
+ "SplitDataFlow": f"{_core_lib_path}.split_data_flow",
21
+ "TransferDataContainer": f"{_core_lib_path}.transfer_data_container",
22
+ }
23
+
24
+
25
+ def _import_template_package(package_name: str) -> dict:
26
+ available_templates = {}
27
+ try:
28
+ template_module = importlib.import_module(package_name)
29
+ template_names = getattr(template_module, "__all__")
30
+ for name in template_names:
31
+ available_templates[name] = package_name
32
+ except (ModuleNotFoundError, ImportError, AttributeError) as err:
33
+ sinapsis_logger.debug(f"Skipping package: {package_name}, does it have a `templates` directory?\n\terr: {err}")
34
+ return available_templates
35
+
36
+
37
+ def _read_from_additional_paths(template_name: str):
38
+ def lazy_import(module_name, file_path):
39
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
40
+ loader = importlib.util.LazyLoader(spec.loader)
41
+ spec.loader = loader
42
+ module = importlib.util.module_from_spec(spec)
43
+ sys.modules[module_name] = module
44
+ loader.exec_module(module)
45
+ return module
46
+
47
+ additional_paths = SinapsisEnvVarDefaults.ADDITIONAL_TEMPLATE_PATHS.value
48
+ if not additional_paths or not isinstance(additional_paths, list):
49
+ return None
50
+
51
+ template_module_name = "templates"
52
+ for idx, add_path in enumerate(additional_paths):
53
+ m_name = f"{template_module_name}_{idx}"
54
+ add_path_init = f"{add_path}__init__.py"
55
+ t_module = lazy_import(m_name, add_path_init)
56
+
57
+ if hasattr(t_module, template_name):
58
+ return_template = getattr(t_module, template_name)
59
+ return return_template
60
+
61
+ return None
62
+
63
+
64
+ def smart_search() -> dict:
65
+ excluded_dists = ["sinapsis_core"]
66
+ available_sinapsis_templates: dict = {}
67
+
68
+ for available_dist in metadata.distributions():
69
+ dist_name = available_dist._normalized_name # type:ignore[attr-defined]
70
+ if dist_name.startswith("sinapsis_") and dist_name not in excluded_dists:
71
+ sinapsis_logger.debug(f"Adding package {available_dist.name}=={available_dist.version}")
72
+ available_sinapsis_templates |= _import_template_package(f"{dist_name}.templates")
73
+
74
+ return available_sinapsis_templates
75
+
76
+
77
+ _template_lookup |= smart_search()
78
+
79
+
80
+ def __getattr__(name: str) -> Callable | ModuleType:
81
+ if name in _template_lookup:
82
+ module = importlib.import_module(_template_lookup[name])
83
+ return getattr(module, name)
84
+ else:
85
+ try:
86
+ module = _read_from_additional_paths(name)
87
+ if module is not None:
88
+ return module
89
+ except FileNotFoundError as err:
90
+ sinapsis_logger.error(err)
91
+
92
+ raise AttributeError(
93
+ f"Template `{name}` not found. Please ensure it is in {_root_lib_path}.\nIf you have"
94
+ f"implemented a sinapsis package, please ensure it is installed,\n\t e.g., using uv|pdm|poetry|pip. "
95
+ f"\notherwise, please set the `ADDITIONAL_TEMPLATE_PATHS` environment variable to make your"
96
+ f"templates discoverable, e.g. \n\texport ADDITIONAL_TEMPLATE_PATHS='[\"path/to/my_package/templates/\"]'"
97
+ )
98
+
99
+
100
+ __all__ = list(_template_lookup.keys())
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Example Display Hello World Sinapsis template."""
3
+
4
+ from sinapsis_core.data_containers.data_packet import DataContainer
5
+ from sinapsis_core.template_base import Template
6
+
7
+
8
+ class DisplayHelloWorld(Template):
9
+ """
10
+ This template simply logs all the text packets received in a data container.
11
+ It is meant to be used for illustration purposes and in combination with
12
+ HelloWorld template.
13
+ """
14
+
15
+ def execute(self, container: DataContainer) -> DataContainer:
16
+ for text_packet in container.texts:
17
+ print(f"\n{text_packet.content}\n") # noqa: T201
18
+
19
+ return container
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Example Hello World Sinapsis template."""
3
+
4
+ from sinapsis_core.data_containers.data_packet import DataContainer, TextPacket
5
+ from sinapsis_core.template_base import Template, TemplateAttributes, TemplateAttributeType
6
+
7
+
8
+ class HelloWorld(Template):
9
+ """
10
+ This template simply adds a text packet to our data container. The data container
11
+ is `sent` to any subsequent templates in our Agent.
12
+ """
13
+
14
+ class AttributesBaseModel(TemplateAttributes): # type:ignore
15
+ """
16
+ AttributesBaseModel is used to allow users to set attributes for a given template.
17
+
18
+ All attribute classes are pydantic BaseModels so they get validated upon
19
+ initialization. All pydantic BaseModel features are supported.
20
+
21
+ In this example we will use two attributes to illustrate how these work.
22
+ """
23
+
24
+ display_text: str = "Hello World"
25
+
26
+ def __init__(self, attributes: TemplateAttributeType) -> None:
27
+ """
28
+ In this example we don't really need the init as we're not doing anything in it,
29
+ but we will leave it here for illustration purposes
30
+
31
+ Args:
32
+ attributes (TemplateAttributeType): attributes to set as per the AttributesBaseModel
33
+ BaseModel above.
34
+ """
35
+ super().__init__(attributes)
36
+
37
+ def execute(self, container: DataContainer) -> DataContainer:
38
+ """
39
+ Append a text packet to the data container with some user defined text.
40
+
41
+ Args:
42
+ container (DataContainer): Input data container. If this is our first Template
43
+ in the Agent then it's always empty.
44
+
45
+ Returns:
46
+ container (DataContainer): The modified data container. A text packet is appended to the 'texts' field.
47
+ """
48
+ text_packet = TextPacket(content=self.attributes.display_text)
49
+ # optionally add additional metadata that may be useful in downstream tasks
50
+ text_packet.source = self.instance_name
51
+
52
+ # finally append it to the data container
53
+ container.texts.append(text_packet)
54
+
55
+ return container
File without changes
@@ -0,0 +1,127 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ from typing import Any
4
+
5
+ import gradio as gr
6
+ from sinapsis_core.cli.run_agent_from_config import generic_agent_builder
7
+ from sinapsis_core.utils.env_var_keys import AGENT_CONFIG_PATH
8
+
9
+ from sinapsis.webapp.agent_webapp_utils import infer_image
10
+
11
+ SINAPSIS_WEBAPP_ROOT_FOLDER = os.getenv("SINAPSIS_WEBAPP_ROOT_FOLDER", "")
12
+ SINAPSIS_WEBAPP_PATH = os.path.dirname(os.path.abspath(__file__))
13
+
14
+
15
+ def lambda_init_agent(infer_func: Any, config_file: str) -> Any:
16
+ """
17
+ Method to start the agent and run the agent on a single image
18
+ Args:
19
+ infer_func (Any): method to run inference
20
+ config_file (str): Configuration of the agent
21
+ Returns:
22
+ the final data container after running the agent
23
+ """
24
+ config_path = AGENT_CONFIG_PATH or os.path.join(
25
+ SINAPSIS_WEBAPP_ROOT_FOLDER,
26
+ config_file,
27
+ )
28
+ agent = generic_agent_builder(config_path)
29
+ return lambda image: infer_func(agent, image)
30
+
31
+
32
+ def init_image_inference(
33
+ config_path: str,
34
+ title: str | None = None,
35
+ stream: bool = False,
36
+ image_input: gr.Image | None = gr.Image,
37
+ app_message: str | None = None,
38
+ ):
39
+ """
40
+ Method to perform inference on the input from gradio
41
+ Args:
42
+ config_path (str): path to config file for the agent
43
+ title (str) : title of the gradio app
44
+ stream (bool) : Whether to consume a single image (False) or a video (True)
45
+ image_input (gr.Image | None): Whether the app takes an input image or not
46
+ app_message (str | None): The message that shows up in the interface when starting it.
47
+ """
48
+ input_sources = ["webcam"]
49
+ if not stream:
50
+ input_sources.append("upload")
51
+ fn = lambda_init_agent(infer_image, config_path)
52
+ if image_input:
53
+ image_input = [gr.Image(sources=input_sources, streaming=stream)]
54
+
55
+ live_interface = gr.Interface(
56
+ fn,
57
+ inputs=image_input,
58
+ outputs=gr.Image(type="pil"),
59
+ live=True,
60
+ title=title,
61
+ flagging_mode="never",
62
+ article=app_message,
63
+ )
64
+ return live_interface
65
+
66
+
67
+ def css_header() -> str:
68
+ """Adds a css header for sinapsis logo and title of the app"""
69
+ return """#sinapsis-logo {
70
+ background-color: transparent;
71
+ border: none;
72
+ border-color: transparent;
73
+ }
74
+ #title {
75
+ font-size: 100px;
76
+ font-weight: bold;
77
+ color: #333333;
78
+ text-align: center;
79
+ margin-bottom: 10px;
80
+ }
81
+ .primary {
82
+ background-color: #004aad !important;
83
+ opacity: 0.8 !important;
84
+ color: white !important;
85
+ border: 2px solid #004aad !important;
86
+ }
87
+ .secondary {
88
+ background-color: #38b6ff !important;
89
+ opacity: 0.8 !important;
90
+ color: white !important;
91
+ border: 2px solid #38b6ff !important;
92
+ }
93
+ #finish-chatbot-btn {
94
+ background-color: blue !important;
95
+ color: white !important;
96
+ border: 2px solid black !important;
97
+ }
98
+ .color-primary.svelte-s8feoe.svelte-s8feoe {
99
+ fill: #004aad;
100
+ stroke: #004aad;
101
+ color: #004aad;
102
+ }
103
+ """
104
+
105
+
106
+ css = css_header()
107
+
108
+
109
+ def add_logo_and_title(page_title: str | None = None) -> None:
110
+ """Method to create the header for the gradio apps
111
+ Args:
112
+ page_title (str): title of the page in gradio
113
+ """
114
+ logo_path = "https://github.com/Sinapsis-AI/brand-resources/blob/main/sinapsis_logo/4x/logo.png?raw=true"
115
+ with gr.Row():
116
+ gr.Image(
117
+ logo_path,
118
+ elem_id="sinapsis-logo",
119
+ width=800,
120
+ height=50,
121
+ show_label=False,
122
+ show_download_button=False,
123
+ show_fullscreen_button=False,
124
+ )
125
+
126
+ gr.Markdown(f"# {page_title}", elem_id="title")
127
+ gr.Markdown("")
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+ from typing import Any
3
+
4
+ from sinapsis_core.agent import Agent
5
+ from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket
6
+
7
+
8
+ def infer_image(agent: Agent, image: Any) -> Any:
9
+ """Method used in apps that require an image input
10
+ Args:
11
+ agent (Agent): Agent instance
12
+ image (Any): input from the gradio/streamlit app
13
+ Returns:
14
+ the final image content after agent execution
15
+ """
16
+
17
+ container = DataContainer()
18
+ container.images = [
19
+ ImagePacket(
20
+ content=image,
21
+ source="live_stream",
22
+ )
23
+ ]
24
+ result_container = agent(container)
25
+ return result_container.images[-1].content