sinapsis 0.2.13__tar.gz → 0.2.15__tar.gz

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 (31) hide show
  1. {sinapsis-0.2.13 → sinapsis-0.2.15}/PKG-INFO +10 -2
  2. {sinapsis-0.2.13 → sinapsis-0.2.15}/README.md +9 -1
  3. {sinapsis-0.2.13 → sinapsis-0.2.15}/docker/Dockerfile +2 -2
  4. {sinapsis-0.2.13 → sinapsis-0.2.15}/pyproject.toml +1 -1
  5. sinapsis-0.2.15/src/sinapsis/webapp/chatbot_base.py +356 -0
  6. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis.egg-info/PKG-INFO +10 -2
  7. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis.egg-info/SOURCES.txt +0 -1
  8. sinapsis-0.2.13/src/sinapsis/webapp/chatbot_base.py +0 -284
  9. sinapsis-0.2.13/uv.lock +0 -1332
  10. {sinapsis-0.2.13 → sinapsis-0.2.15}/.dockerignore +0 -0
  11. {sinapsis-0.2.13 → sinapsis-0.2.15}/.gitignore +0 -0
  12. {sinapsis-0.2.13 → sinapsis-0.2.15}/.pre-commit-config.yaml +0 -0
  13. {sinapsis-0.2.13 → sinapsis-0.2.15}/.python-version +0 -0
  14. {sinapsis-0.2.13 → sinapsis-0.2.15}/LICENSE +0 -0
  15. {sinapsis-0.2.13 → sinapsis-0.2.15}/README-internal.md +0 -0
  16. {sinapsis-0.2.13 → sinapsis-0.2.15}/docker/compose.yaml +0 -0
  17. {sinapsis-0.2.13 → sinapsis-0.2.15}/setup.cfg +0 -0
  18. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/__init__.py +0 -0
  19. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/bin/sinapsis_cli.py +0 -0
  20. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/configs/hello_world_agent.yml +0 -0
  21. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/templates/__init__.py +0 -0
  22. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/templates/display_hello_world.py +0 -0
  23. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/templates/hello_world.py +0 -0
  24. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/webapp/__init__.py +0 -0
  25. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/webapp/agent_gradio_helper.py +0 -0
  26. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/webapp/agent_webapp_utils.py +0 -0
  27. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis/webapp/tts_base.py +0 -0
  28. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis.egg-info/dependency_links.txt +0 -0
  29. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis.egg-info/entry_points.txt +0 -0
  30. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis.egg-info/requires.txt +0 -0
  31. {sinapsis-0.2.13 → sinapsis-0.2.15}/src/sinapsis.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sinapsis
3
- Version: 0.2.13
3
+ Version: 0.2.15
4
4
  Summary: Modular and Universal AI platform
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  Project-URL: Homepage, https://sinapsis.tech
@@ -218,7 +218,7 @@ you need to make sure the sinapsis base images are built in your system. To buil
218
218
  ```bash
219
219
  git clone git@github.com:Sinapsis-ai/sinapsis.git
220
220
  cd sinapsis
221
- ```
221
+ ```
222
222
 
223
223
  2. **Build the image**
224
224
  ```bash
@@ -347,3 +347,11 @@ This project is licensed under the AGPLv3 license, which encourages open collabo
347
347
 
348
348
  For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
349
349
 
350
+ <h2 id="license">🔍 License</h2>
351
+
352
+ This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
353
+
354
+ For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
355
+
356
+
357
+
@@ -199,7 +199,7 @@ you need to make sure the sinapsis base images are built in your system. To buil
199
199
  ```bash
200
200
  git clone git@github.com:Sinapsis-ai/sinapsis.git
201
201
  cd sinapsis
202
- ```
202
+ ```
203
203
 
204
204
  2. **Build the image**
205
205
  ```bash
@@ -328,3 +328,11 @@ This project is licensed under the AGPLv3 license, which encourages open collabo
328
328
 
329
329
  For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
330
330
 
331
+ <h2 id="license">🔍 License</h2>
332
+
333
+ This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
334
+
335
+ For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
336
+
337
+
338
+
@@ -13,8 +13,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
13
13
  && apt clean && rm -rf /var/lib/apt/lists/*
14
14
 
15
15
  # install uv
16
- ADD https://astral.sh/uv/install.sh /uv-installer.sh
17
- RUN sh /uv-installer.sh && rm /uv-installer.sh
16
+ ADD https://astral.sh/uv/0.6.17/install.sh /uv-installer.sh
17
+ RUN sh /sh uv-installer.sh && rm /uv-installer.sh
18
18
  ENV PATH="/root/.local/bin/:$PATH"
19
19
  COPY . /app/
20
20
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sinapsis"
3
- version = "0.2.13"
3
+ version = "0.2.15"
4
4
  description = "Modular and Universal AI platform"
5
5
  authors = [
6
6
  {name = "SinapsisAI", email = "dev@sinapsis.tech"},
@@ -0,0 +1,356 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os.path
3
+ import uuid
4
+ from typing import Any
5
+
6
+ import cv2
7
+ import gradio as gr
8
+ import numpy as np
9
+ from gradio.utils import get_upload_folder
10
+ from pydantic import ConfigDict
11
+ from pydantic.dataclasses import dataclass
12
+ from sinapsis_core.agent.agent import Agent
13
+ from sinapsis_core.cli.run_agent_from_config import generic_agent_builder
14
+ from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket, TextPacket
15
+ from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
16
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
17
+
18
+ from sinapsis.webapp.agent_gradio_helper import add_logo_and_title, css_header
19
+
20
+ LOGIN_TEMPLATE = """
21
+ <div style="text-align: center;">
22
+ <img src="{image}" width="64" style="display: block; margin: 0 auto 10px auto;" />
23
+ <p style="font-size: 16px; font-weight: bold;">{title}</p>
24
+ <p>{message}</p>
25
+ </div>
26
+ """
27
+ SINAPSIS_AVATAR = "https://github.com/Sinapsis-AI/brand-resources/blob/main/sinapsis_logo/4x/fav_icon.png?raw=true"
28
+
29
+
30
+ @dataclass
31
+ class ChatbotConfig:
32
+ """Configuration class for the chatbot application.
33
+
34
+ Attributes:
35
+ app_title (str): The title displayed on the chatbot interface.
36
+ login_message (str): The message shown on the login screen.
37
+ login_image (str): URL of the image displayed on the login screen.
38
+ enable_memories (bool): Flag to enable memory features.
39
+ examples (list[str] | None): Optional list of example inputs to show in the UI.
40
+ users_db_config (dict[str, str] | None): Database configuration for user authentication.
41
+ """
42
+
43
+ app_title: str = "Sinapsis Chatbot"
44
+ login_image: str = SINAPSIS_AVATAR
45
+ # enable_memories: bool = True
46
+ examples: list[str] | None = None
47
+
48
+
49
+ @dataclass
50
+ class ChatKeys:
51
+ """
52
+ Defines key names used for referencing various chat-related data types.
53
+
54
+ This class serves as a centralized place to manage key names for different types of data
55
+ that may be used in chat interactions. These keys are typically used to map data in structured formats.
56
+ """
57
+
58
+ text: str = "text"
59
+ image: str = "image"
60
+ files: str = "files"
61
+ file_path: str = "path"
62
+ audio_path: str = "audio_path"
63
+ role: str = "role"
64
+ content: str = "content"
65
+ user: str = "user"
66
+ assistant: str = "assistant"
67
+
68
+
69
+ @dataclass(config=ConfigDict(arbitrary_types_allowed=True))
70
+ class ChatInterfaceComponents:
71
+ """Container for core UI components used in the chat interface.
72
+
73
+ Attributes:
74
+ chatbot (gr.Chatbot): The chatbot message display area.
75
+ textbox (gr.MultimodalTextbox): Input field for user messages and file uploads.
76
+ session_state (gr.State): State object to track the user's session.
77
+ chat_interface (gr.ChatInterface): Main Gradio chat interface component.
78
+ """
79
+
80
+ chatbot: gr.Chatbot
81
+ textbox: gr.MultimodalTextbox
82
+ session_state: gr.State
83
+ chat_interface: gr.ChatInterface
84
+
85
+
86
+ class BaseChatbot:
87
+ """
88
+ A base chatbot class designed to work with various LLM frameworks, such as LLaMA.
89
+ This class provides the functionality to interact with users through text, audio,
90
+ and file inputs, maintain chat history, and integrate with Gradio for a web
91
+ interface. The class is intended to serve as a foundation
92
+ that can be adapted to different LLM frameworks by modifying the agent
93
+ initialization and response handling methods.
94
+ """
95
+
96
+ def __init__(self, config_file: str, config: ChatbotConfig | dict[str, Any] = ChatbotConfig()) -> None:
97
+ self.config_file = config_file
98
+ self.config = ChatbotConfig(**config) if isinstance(config, dict) else config
99
+ self.chatbot_height = "70vh"
100
+ self.agent = self.initialize_agent(self.config_file)
101
+ self.examples = (
102
+ [[{ChatKeys.text: example}, None] for example in self.config.examples] if self.config.examples else None
103
+ )
104
+ self._setup_working_directory()
105
+
106
+ @staticmethod
107
+ def initialize_agent(config_file: str) -> Agent:
108
+ """Instantiate the chatbot agent from the configuration file."""
109
+ return generic_agent_builder(config_file)
110
+
111
+ def stop_agent(self) -> tuple[dict, dict, dict]:
112
+ """Stops the chatbot agent and disables user input components in the UI.
113
+
114
+ This method performs cleanup of the agent, releases GPU memory, logs the shutdown,
115
+ and returns Gradio UI updates that:
116
+
117
+ - Disable the input textbox and stop button.
118
+ - Enable the restart button.
119
+
120
+ Returns:
121
+ tuple[dict, dict, dict]: Gradio UI updates for textbox, stop button and restart button.
122
+ """
123
+ self.agent.cancel_agent_execution()
124
+ gr.Info("🛑 Chatbot stopped!")
125
+ sinapsis_logger.info("Chatbot stopped")
126
+ return (
127
+ gr.update(interactive=False),
128
+ gr.update(interactive=False),
129
+ gr.update(interactive=True),
130
+ )
131
+
132
+ def _setup_working_directory(self) -> None:
133
+ """Create and ensure the existence of directories for uploads and cached chat histories."""
134
+ self.gradio_temp_dir = get_upload_folder()
135
+ self.chats_dir = os.path.join(SINAPSIS_CACHE_DIR, "chats")
136
+ os.makedirs(self.gradio_temp_dir, exist_ok=True)
137
+ os.makedirs(self.chats_dir, exist_ok=True)
138
+
139
+ def restart_agent(self) -> tuple[dict, dict, dict]:
140
+ """Reinitializes the chatbot agent and enables user input components in the UI.
141
+
142
+ This method rebuilds the agent, logs the startup, and returns Gradio UI updates that:
143
+
144
+ - Enable the input textbox and stop button.
145
+ - Disable the restart button.
146
+
147
+ Returns:
148
+ tuple[dict, dict, dict]: Gradio UI updates for textbox, stop button and restart button.
149
+ """
150
+ self.agent = self.initialize_agent(self.config_file)
151
+ gr.Success("✅ Chatbot ready!")
152
+ sinapsis_logger.info("Chatbot initialized")
153
+ return (
154
+ gr.update(interactive=True),
155
+ gr.update(interactive=True),
156
+ gr.update(interactive=False),
157
+ )
158
+
159
+ @staticmethod
160
+ def generate_packet(message: dict[str, Any], user_id: str, session_id: str | None = None) -> DataContainer:
161
+ """Constructs a DataContainer from user input including text, files, and optional session info.
162
+
163
+ Args:
164
+ message (dict[str, Any]): User input with keys: 'text', 'files'.
165
+ user_id (str): Unique identifier for the user.
166
+ session_id (str | None, optional): Identifier for the session. Defaults to None.
167
+
168
+ Returns:
169
+ DataContainer: Structured container with text, images, or audio ready for agent execution.
170
+ """
171
+ container = DataContainer()
172
+ container.texts.append(TextPacket(content=message.get(ChatKeys.text), id=user_id, source=session_id))
173
+ for file_path in message.get(ChatKeys.files, []):
174
+ if file_path.endswith(".wav"):
175
+ container.generic_data[ChatKeys.audio_path] = file_path
176
+ else:
177
+ img_bgr = cv2.imread(file_path)
178
+ img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
179
+ filename = os.path.basename(file_path)
180
+ container.images.append(ImagePacket(content=img_rgb, color_space=1, source=filename))
181
+ return container
182
+
183
+ def agent_execution(self, container: DataContainer) -> dict[str, Any]:
184
+ """Executes the Sinapsis agent and processes the result into chatbot output.
185
+
186
+ Args:
187
+ container (DataContainer): Structured input for the agent.
188
+
189
+ Returns:
190
+ dict[str, Any]: Dictionary with keys such as 'text' and 'files' to display in the UI.
191
+ """
192
+ default_response = {ChatKeys.text: "Could not process request, please try again", ChatKeys.files: []}
193
+ result_container = self.agent(container)
194
+ response = {}
195
+
196
+ if result_container.texts:
197
+ response[ChatKeys.text] = result_container.texts[-1].content
198
+
199
+ if result_container.images:
200
+ image_packet = container.images[-1]
201
+ img_array = np.uint8(image_packet.content)
202
+ img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
203
+ image_path = os.path.join(self.gradio_temp_dir, image_packet.source)
204
+ cv2.imwrite(image_path, img_array)
205
+ response[ChatKeys.files] = [image_path]
206
+
207
+ return response if response else default_response
208
+
209
+ def generate_user_response(
210
+ self, message: dict[str, Any], user_id: str, session_id: str | None = None
211
+ ) -> dict[str, Any]:
212
+ """Generates a response from the agent for a given user input.
213
+
214
+ Args:
215
+ message (dict[str, Any]): Dictionary with user inputs (text, files, etc.).
216
+ user_id (str): Unique user identifier.
217
+ session_id (str | None, optional): Optional unique session identifier. Defaults to None.
218
+
219
+ Returns:
220
+ dict[str, Any]: Response payload containing 'text' and/or 'files'.
221
+ """
222
+ container = self.generate_packet(message, user_id, session_id)
223
+ return self.agent_execution(container)
224
+
225
+ def handle_user_message(
226
+ self, message: dict[str, Any], history: list[dict], session_state: str, user_id: gr.State | str
227
+ ) -> dict[str, Any]:
228
+ """Handles a user message from the UI and routes it through the agent.
229
+
230
+ Args:
231
+ message (dict[str, Any]): Input data from the user interface.
232
+ history (list[dict]): Unused chat history.
233
+ session_state (str): Session UUID state.
234
+ user_id (gr.State | None): user ID state.
235
+
236
+ Returns:
237
+ dict[str, Any]: Chatbot response formatted for the UI.
238
+ """
239
+ _ = history
240
+ return self.generate_user_response(message, user_id, session_state)
241
+
242
+ def _build_chat_interface(self) -> ChatInterfaceComponents:
243
+ """Constructs the main chat interface with all core components.
244
+
245
+ This includes the chatbot display, multimodal input textbox,
246
+ session state, and the overall `ChatInterface` wrapper.
247
+
248
+ Returns:
249
+ ChatInterfaceComponents: A container holding all relevant UI components.
250
+ """
251
+ chatbot = gr.Chatbot(
252
+ type="messages",
253
+ height=self.chatbot_height,
254
+ show_label=False,
255
+ avatar_images=(None, SINAPSIS_AVATAR),
256
+ show_copy_button=True,
257
+ )
258
+ textbox = gr.MultimodalTextbox(
259
+ file_count="multiple",
260
+ file_types=[".png", ".jpg", ".wav"],
261
+ sources=["upload", "microphone"],
262
+ placeholder="Message Chatbot",
263
+ )
264
+ session_state = gr.State(str(uuid.uuid4()))
265
+ user_id = gr.Textbox("Chatbot user", visible=False)
266
+ chat_interface = gr.ChatInterface(
267
+ fn=self.handle_user_message,
268
+ additional_inputs=[session_state, user_id],
269
+ title=None,
270
+ multimodal=True,
271
+ chatbot=chatbot,
272
+ fill_height=True,
273
+ type="messages",
274
+ examples=self.examples,
275
+ example_icons=[SINAPSIS_AVATAR] * len(self.examples) if self.examples else None,
276
+ textbox=textbox,
277
+ api_name=False,
278
+ )
279
+ chatbot.clear(self.handle_clear_history, inputs=[session_state, user_id])
280
+
281
+ return ChatInterfaceComponents(chatbot, textbox, session_state, chat_interface)
282
+
283
+ def clear_user_conversation(self, user_id: str, session_id: str | None = None) -> None:
284
+ """Clears stored conversation data for the specified user/session.
285
+
286
+ Can be overridden by subclasses to customize additional actions when clearing conversations.
287
+
288
+ Args:
289
+ user_id (str): Unique user identifier.
290
+ session_id (str | None, optional): Optional session ID to scope clearing. Defaults to None.
291
+
292
+ Returns:
293
+ Any: Result of the clear operation (to be defined by subclasses).
294
+ """
295
+
296
+ def handle_clear_history(self, session_state: gr.State, user_id: str) -> None:
297
+ """Triggers the clearing of a user's chat history from the UI button.
298
+
299
+ Args:
300
+ session_state (gr.State): Current session state object.
301
+ user_id (str): Gradio request containing user/session info.
302
+ """
303
+ # user_id = request.username if request.username else request.session_hash
304
+ self.clear_user_conversation(user_id, session_state)
305
+
306
+ def _add_buttons(self, chat_components: ChatInterfaceComponents):
307
+ """Adds 'Stop' and 'Start Chatbot' buttons to control agent lifecycle.
308
+
309
+ Can be overriden by subclasses to add more control buttons in the footer.
310
+
311
+ Args:
312
+ chat_components (ChatInterfaceComponents): Container with components needed
313
+ to enable/disable input based on chatbot state.
314
+ """
315
+ stop_btn = gr.Button("Stop chatbot")
316
+ restart_btn = gr.Button("Start Chatbot", interactive=False)
317
+ stop_btn.click(self.stop_agent, outputs=[chat_components.textbox, stop_btn, restart_btn])
318
+ restart_btn.click(self.restart_agent, outputs=[chat_components.textbox, stop_btn, restart_btn])
319
+
320
+ def _inject_header_components(self):
321
+ """Adds visual elements at the top of the UI (e.g., logo, app title).
322
+
323
+ Can be overridden by subclasses to customize header layout.
324
+ """
325
+ add_logo_and_title(self.config.app_title)
326
+
327
+ def _inject_footer_components(self, chat_components: ChatInterfaceComponents):
328
+ """Adds UI components below the chat interface, such as control buttons.
329
+
330
+ Can be overriden by subclasses to add more elements in the footer.
331
+
332
+ Args:
333
+ chat_components (ChatInterfaceComponents): Group of UI elements built in the chat interface.
334
+ """
335
+ with gr.Row():
336
+ self._add_buttons(chat_components)
337
+
338
+ def app_interface(self) -> gr.Blocks:
339
+ """Builds the full Gradio Blocks layout including chat interface, headers, footers, and extras.
340
+
341
+ Returns:
342
+ gr.Blocks: Composed Gradio interface ready to launch.
343
+ """
344
+ with gr.Blocks(css=css_header(), title=self.config.app_title) as interface:
345
+ self._inject_header_components()
346
+ chat_components = self._build_chat_interface()
347
+ self._inject_footer_components(chat_components)
348
+
349
+ return interface
350
+
351
+ def launch(self, **kwargs: dict[str, Any]) -> None:
352
+ """Launches the Gradio app, optionally with authentication and custom options."""
353
+ interface = self.app_interface()
354
+ interface.launch(
355
+ **kwargs,
356
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sinapsis
3
- Version: 0.2.13
3
+ Version: 0.2.15
4
4
  Summary: Modular and Universal AI platform
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  Project-URL: Homepage, https://sinapsis.tech
@@ -218,7 +218,7 @@ you need to make sure the sinapsis base images are built in your system. To buil
218
218
  ```bash
219
219
  git clone git@github.com:Sinapsis-ai/sinapsis.git
220
220
  cd sinapsis
221
- ```
221
+ ```
222
222
 
223
223
  2. **Build the image**
224
224
  ```bash
@@ -347,3 +347,11 @@ This project is licensed under the AGPLv3 license, which encourages open collabo
347
347
 
348
348
  For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
349
349
 
350
+ <h2 id="license">🔍 License</h2>
351
+
352
+ This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
353
+
354
+ For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
355
+
356
+
357
+
@@ -6,7 +6,6 @@ LICENSE
6
6
  README-internal.md
7
7
  README.md
8
8
  pyproject.toml
9
- uv.lock
10
9
  docker/Dockerfile
11
10
  docker/compose.yaml
12
11
  src/sinapsis/__init__.py