sinapsis 0.2.14__tar.gz → 0.2.17__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.
- {sinapsis-0.2.14 → sinapsis-0.2.17}/PKG-INFO +2 -10
- {sinapsis-0.2.14 → sinapsis-0.2.17}/README.md +1 -9
- {sinapsis-0.2.14 → sinapsis-0.2.17}/pyproject.toml +1 -1
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/webapp/agent_webapp_utils.py +1 -3
- sinapsis-0.2.17/src/sinapsis/webapp/chatbot_base.py +356 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis.egg-info/PKG-INFO +2 -10
- sinapsis-0.2.14/src/sinapsis/webapp/chatbot_base.py +0 -283
- {sinapsis-0.2.14 → sinapsis-0.2.17}/.dockerignore +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/.gitignore +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/.pre-commit-config.yaml +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/.python-version +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/LICENSE +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/README-internal.md +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/docker/Dockerfile +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/docker/compose.yaml +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/setup.cfg +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/__init__.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/bin/sinapsis_cli.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/configs/hello_world_agent.yml +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/templates/__init__.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/templates/display_hello_world.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/templates/hello_world.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/webapp/__init__.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/webapp/agent_gradio_helper.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis/webapp/tts_base.py +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis.egg-info/SOURCES.txt +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis.egg-info/dependency_links.txt +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis.egg-info/entry_points.txt +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/src/sinapsis.egg-info/requires.txt +0 -0
- {sinapsis-0.2.14 → sinapsis-0.2.17}/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.
|
|
3
|
+
Version: 0.2.17
|
|
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,11 +347,3 @@ 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,11 +328,3 @@ 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
|
-
|
|
@@ -41,9 +41,7 @@ def infer_video(agent: Agent, video: Any) -> Path:
|
|
|
41
41
|
"""
|
|
42
42
|
agent.update_template_attribute("VideoReaderCV2", "video_file_path", video)
|
|
43
43
|
agent.reset_state()
|
|
44
|
-
|
|
45
|
-
agent.agent_attributes.agent_mode = AgentMode.GENERATOR
|
|
46
|
-
for _ in agent(None):
|
|
44
|
+
for _ in agent(None, AgentMode.GENERATOR):
|
|
47
45
|
pass
|
|
48
46
|
agent.topological_sort["VideoWriterCV2"].template_instance.video_writer_is_done()
|
|
49
47
|
sinapsis_logger.debug("Finished processing video")
|
|
@@ -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.
|
|
3
|
+
Version: 0.2.17
|
|
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,11 +347,3 @@ 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
|
-
|
|
@@ -1,283 +0,0 @@
|
|
|
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.dataclasses import dataclass
|
|
11
|
-
from sinapsis_core.cli.run_agent_from_config import generic_agent_builder
|
|
12
|
-
from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket, TextPacket
|
|
13
|
-
from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
|
|
14
|
-
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
15
|
-
|
|
16
|
-
from sinapsis.webapp.agent_gradio_helper import add_logo_and_title, css_header
|
|
17
|
-
|
|
18
|
-
LOGIN_TEMPLATE = """
|
|
19
|
-
<div style="text-align: center;">
|
|
20
|
-
<img src="{image}" width="64" style="display: block; margin: 0 auto 10px auto;" />
|
|
21
|
-
<p style="font-size: 16px; font-weight: bold;">{title}</p>
|
|
22
|
-
<p>{message}</p>
|
|
23
|
-
</div>
|
|
24
|
-
"""
|
|
25
|
-
SINAPSIS_AVATAR = "https://github.com/Sinapsis-AI/brand-resources/blob/main/sinapsis_logo/4x/fav_icon.png?raw=true"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
@dataclass
|
|
29
|
-
class ChatbotConfig:
|
|
30
|
-
"""Configuration class for the chatbot application.
|
|
31
|
-
|
|
32
|
-
Attributes:
|
|
33
|
-
app_title (str): The title displayed on the chatbot interface.
|
|
34
|
-
login_message (str): The message shown on the login screen.
|
|
35
|
-
login_image (str): URL of the image displayed on the login screen.
|
|
36
|
-
enable_memories (bool): Flag to enable memory features.
|
|
37
|
-
examples (list[str] | None): Optional list of example inputs to show in the UI.
|
|
38
|
-
users_db_config (dict[str, str] | None): Database configuration for user authentication.
|
|
39
|
-
"""
|
|
40
|
-
|
|
41
|
-
app_title: str = "Sinapsis Chatbot"
|
|
42
|
-
# login_message: str = "Please log in with your credentials to continue."
|
|
43
|
-
login_image: str = SINAPSIS_AVATAR
|
|
44
|
-
enable_memories: bool = True
|
|
45
|
-
examples: list[str] | None = None
|
|
46
|
-
# users_db_config: dict[str, 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
|
-
class BaseChatbot:
|
|
70
|
-
"""
|
|
71
|
-
A base chatbot class designed to work with various LLM frameworks, such as LLaMA.
|
|
72
|
-
This class provides the functionality to interact with users through text, audio,
|
|
73
|
-
and file inputs, maintain chat history, and integrate with Gradio for a web
|
|
74
|
-
interface. The class is intended to serve as a foundation
|
|
75
|
-
that can be adapted to different LLM frameworks by modifying the agent
|
|
76
|
-
initialization and response handling methods.
|
|
77
|
-
"""
|
|
78
|
-
|
|
79
|
-
def __init__(self, config_file: str, config: ChatbotConfig | dict[str, Any] = ChatbotConfig()) -> None:
|
|
80
|
-
self.config_file = config_file
|
|
81
|
-
self.config = ChatbotConfig(**config) if isinstance(config, dict) else config
|
|
82
|
-
self.agent = generic_agent_builder(self.config_file)
|
|
83
|
-
self.examples = [{ChatKeys.text: example} for example in self.config.examples] if self.config.examples else None
|
|
84
|
-
self.file_name = f"{SINAPSIS_CACHE_DIR}/chatbot/chat.txt"
|
|
85
|
-
os.makedirs(os.path.dirname(self.file_name), exist_ok=True)
|
|
86
|
-
self._setup_working_directory()
|
|
87
|
-
|
|
88
|
-
def _setup_working_directory(self) -> None:
|
|
89
|
-
"""Creates a temporary directory for storing uploaded media files."""
|
|
90
|
-
self.gradio_temp_dir = get_upload_folder()
|
|
91
|
-
os.makedirs(self.gradio_temp_dir, exist_ok=True)
|
|
92
|
-
|
|
93
|
-
@staticmethod
|
|
94
|
-
def _format_history(history: list[dict[str, Any]], max_turns: int = 5) -> dict[str, Any]:
|
|
95
|
-
"""Formats the chat history into a simplified dictionary with limited turns per role.
|
|
96
|
-
|
|
97
|
-
Args:
|
|
98
|
-
history (list[dict[str, Any]]): Full chat history with role and content.
|
|
99
|
-
max_turns (int, optional): Number of recent messages to keep per role. Defaults to 5.
|
|
100
|
-
|
|
101
|
-
Returns:
|
|
102
|
-
dict[str, Any]: A dictionary in the form {"user": [...], "assistant": [...]}.
|
|
103
|
-
"""
|
|
104
|
-
user_msgs = [entry[ChatKeys.content] for entry in history if entry[ChatKeys.role] == ChatKeys.user][-max_turns:]
|
|
105
|
-
assistant_msgs = [entry[ChatKeys.content] for entry in history if entry[ChatKeys.role] == ChatKeys.assistant][
|
|
106
|
-
-max_turns:
|
|
107
|
-
]
|
|
108
|
-
return {
|
|
109
|
-
ChatKeys.user: user_msgs,
|
|
110
|
-
ChatKeys.assistant: assistant_msgs,
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
def generate_packet(
|
|
114
|
-
self, container: DataContainer, message: dict[str, Any], history: list[dict[str, Any]], conv_id: str
|
|
115
|
-
) -> DataContainer:
|
|
116
|
-
"""Creates a `DataContainer` object from user message and chat history.
|
|
117
|
-
|
|
118
|
-
Args:
|
|
119
|
-
container (container): incoming container for the query
|
|
120
|
-
message (dict[str, Any]): Dictionary with keys like 'text' and 'files'.
|
|
121
|
-
history (list[dict[str, Any]]): List of previous chat messages.
|
|
122
|
-
conv_id (str): Conversation id value
|
|
123
|
-
|
|
124
|
-
Returns:
|
|
125
|
-
DataContainer: A structured container of the parsed input.
|
|
126
|
-
"""
|
|
127
|
-
formatted_history = self._format_history(history)
|
|
128
|
-
container.texts.append(
|
|
129
|
-
TextPacket(content=message.get(ChatKeys.text), source=conv_id, generic_data={"history": formatted_history})
|
|
130
|
-
)
|
|
131
|
-
for file_path in message.get(ChatKeys.files, []):
|
|
132
|
-
if file_path.endswith(".wav"):
|
|
133
|
-
container.generic_data[ChatKeys.audio_path] = file_path
|
|
134
|
-
else:
|
|
135
|
-
img_bgr = cv2.imread(file_path)
|
|
136
|
-
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
|
137
|
-
filename = os.path.basename(file_path)
|
|
138
|
-
container.images.append(ImagePacket(content=img_rgb, color_space=1, source=filename))
|
|
139
|
-
return container
|
|
140
|
-
|
|
141
|
-
def agent_execution(self, container: DataContainer) -> dict[str, Any]:
|
|
142
|
-
"""Executes the Sinapsis agent and processes the result into chatbot output.
|
|
143
|
-
|
|
144
|
-
Args:
|
|
145
|
-
container (DataContainer): Structured input for the agent.
|
|
146
|
-
|
|
147
|
-
Returns:
|
|
148
|
-
dict[str, Any]: Dictionary with keys such as 'text' and 'files' to display in the UI.
|
|
149
|
-
"""
|
|
150
|
-
default_response = {ChatKeys.text: "Could not process request, please try again", ChatKeys.files: []}
|
|
151
|
-
result_container = self.agent(container)
|
|
152
|
-
response = {}
|
|
153
|
-
|
|
154
|
-
if result_container.texts:
|
|
155
|
-
response[ChatKeys.text] = result_container.texts[-1].content
|
|
156
|
-
|
|
157
|
-
if result_container.images:
|
|
158
|
-
image_packet = container.images[-1]
|
|
159
|
-
img_array = np.uint8(image_packet.content)
|
|
160
|
-
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
|
161
|
-
image_path = os.path.join(self.gradio_temp_dir, image_packet.source)
|
|
162
|
-
cv2.imwrite(image_path, img_array)
|
|
163
|
-
response[ChatKeys.files] = [image_path]
|
|
164
|
-
|
|
165
|
-
return response if response else default_response
|
|
166
|
-
|
|
167
|
-
@staticmethod
|
|
168
|
-
def _set_conversation_id(conv_id: str) -> str:
|
|
169
|
-
"""
|
|
170
|
-
Ensure a valid conversation ID is set. If no ID is provided, a new one is
|
|
171
|
-
generated.
|
|
172
|
-
Args:
|
|
173
|
-
conv_id (str): The provided conversation ID.
|
|
174
|
-
Returns:
|
|
175
|
-
str: The valid conversation ID (either provided or generated).
|
|
176
|
-
"""
|
|
177
|
-
if not conv_id:
|
|
178
|
-
conv_id = str(uuid.uuid4())
|
|
179
|
-
return conv_id
|
|
180
|
-
|
|
181
|
-
def stop_agent(self) -> dict:
|
|
182
|
-
"""
|
|
183
|
-
Stop the chatbot's agent and save the chat history to a file.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
Returns:
|
|
187
|
-
tuple: A tuple with None and Gradio updates for interactivity.
|
|
188
|
-
"""
|
|
189
|
-
|
|
190
|
-
self.agent = None
|
|
191
|
-
return gr.update(interactive=False)
|
|
192
|
-
|
|
193
|
-
def _clear_history(self) -> None:
|
|
194
|
-
"""
|
|
195
|
-
Clears the chat history and saves it to a file named "chat.txt".
|
|
196
|
-
|
|
197
|
-
This method writes the current chat history to a file in JSON format,
|
|
198
|
-
then clears the history.
|
|
199
|
-
"""
|
|
200
|
-
|
|
201
|
-
try:
|
|
202
|
-
os.remove(self.file_name)
|
|
203
|
-
except FileNotFoundError:
|
|
204
|
-
sinapsis_logger.warning("Chat history file does not exist")
|
|
205
|
-
|
|
206
|
-
def process_msg(self, message: dict, history: list[dict], conv_id: str, container: DataContainer) -> dict:
|
|
207
|
-
"""
|
|
208
|
-
Process a user message and generate a chatbot response.
|
|
209
|
-
|
|
210
|
-
Args:
|
|
211
|
-
message (str): The user's input message.
|
|
212
|
-
history: list[dict]: list of messages sent through chatbot
|
|
213
|
-
conv_id (str): The conversation ID for the current session.
|
|
214
|
-
container (DataContainer): Incoming data container for the query
|
|
215
|
-
|
|
216
|
-
Returns:
|
|
217
|
-
tuple: The updated chat history and UI components for the chatbot interface.
|
|
218
|
-
"""
|
|
219
|
-
container = container or DataContainer()
|
|
220
|
-
conv_id = self._set_conversation_id(conv_id)
|
|
221
|
-
container = self.generate_packet(container, message, history, conv_id)
|
|
222
|
-
response = self.agent_execution(container)
|
|
223
|
-
return response
|
|
224
|
-
|
|
225
|
-
def add_app_components(self, conv_id) -> gr.ChatInterface:
|
|
226
|
-
"""Builds and returns the main Gradio chat interface.
|
|
227
|
-
|
|
228
|
-
Returns:
|
|
229
|
-
gr.ChatInterface: Configured chat interface with multimodal input.
|
|
230
|
-
"""
|
|
231
|
-
chatbot = gr.Chatbot(
|
|
232
|
-
type="messages",
|
|
233
|
-
height=600,
|
|
234
|
-
show_label=False,
|
|
235
|
-
avatar_images=(None, SINAPSIS_AVATAR),
|
|
236
|
-
show_copy_all_button=True,
|
|
237
|
-
)
|
|
238
|
-
textbox = gr.MultimodalTextbox(
|
|
239
|
-
file_count="multiple",
|
|
240
|
-
file_types=[".png", ".jpg", ".wav"],
|
|
241
|
-
sources=["upload", "microphone"],
|
|
242
|
-
placeholder="Message Chatbot",
|
|
243
|
-
)
|
|
244
|
-
container = gr.State(value=DataContainer())
|
|
245
|
-
interface = gr.ChatInterface(
|
|
246
|
-
fn=self.process_msg,
|
|
247
|
-
title=None,
|
|
248
|
-
multimodal=True,
|
|
249
|
-
chatbot=chatbot,
|
|
250
|
-
fill_height=True,
|
|
251
|
-
css=css_header,
|
|
252
|
-
type="messages",
|
|
253
|
-
examples=self.examples,
|
|
254
|
-
example_icons=[SINAPSIS_AVATAR] * len(self.examples) if self.examples else None,
|
|
255
|
-
textbox=textbox,
|
|
256
|
-
save_history=True,
|
|
257
|
-
additional_inputs=[conv_id, container],
|
|
258
|
-
)
|
|
259
|
-
|
|
260
|
-
stop_agent = gr.Button("Stop chatbot")
|
|
261
|
-
stop_agent.click(self.stop_agent, outputs=[textbox])
|
|
262
|
-
|
|
263
|
-
return interface
|
|
264
|
-
|
|
265
|
-
def app_interface(self) -> gr.Blocks:
|
|
266
|
-
"""Builds the full Gradio UI layout for the chatbot application.
|
|
267
|
-
|
|
268
|
-
Returns:
|
|
269
|
-
gr.Blocks: Gradio Blocks layout for the complete application.
|
|
270
|
-
"""
|
|
271
|
-
with gr.Blocks(css=css_header(), title=self.config.app_title) as chatbot_interface:
|
|
272
|
-
add_logo_and_title(self.config.app_title)
|
|
273
|
-
conv_id = gr.State(value=str(uuid.uuid4()))
|
|
274
|
-
self.add_app_components(conv_id)
|
|
275
|
-
|
|
276
|
-
return chatbot_interface
|
|
277
|
-
|
|
278
|
-
def launch(self, **kwargs: dict[str, Any]) -> None:
|
|
279
|
-
"""Launches the Gradio app, optionally with authentication and custom options."""
|
|
280
|
-
interface = self.app_interface()
|
|
281
|
-
interface.launch(
|
|
282
|
-
**kwargs,
|
|
283
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|