sinapsis 0.2.10__tar.gz → 0.2.12__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.10 → sinapsis-0.2.12}/PKG-INFO +87 -7
- {sinapsis-0.2.10 → sinapsis-0.2.12}/README.md +86 -6
- {sinapsis-0.2.10 → sinapsis-0.2.12}/pyproject.toml +1 -1
- sinapsis-0.2.12/src/sinapsis/webapp/chatbot_base.py +284 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/webapp/tts_base.py +19 -22
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis.egg-info/PKG-INFO +87 -7
- {sinapsis-0.2.10 → sinapsis-0.2.12}/uv.lock +47 -31
- sinapsis-0.2.10/src/sinapsis/webapp/chatbot_base.py +0 -301
- {sinapsis-0.2.10 → sinapsis-0.2.12}/.dockerignore +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/.gitignore +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/.pre-commit-config.yaml +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/.python-version +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/LICENSE +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/README-internal.md +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/docker/Dockerfile +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/docker/compose.yaml +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/setup.cfg +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/__init__.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/bin/sinapsis_cli.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/configs/hello_world_agent.yml +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/templates/__init__.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/templates/display_hello_world.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/templates/hello_world.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/webapp/__init__.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/webapp/agent_gradio_helper.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis/webapp/agent_webapp_utils.py +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis.egg-info/SOURCES.txt +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis.egg-info/dependency_links.txt +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis.egg-info/entry_points.txt +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/src/sinapsis.egg-info/requires.txt +0 -0
- {sinapsis-0.2.10 → sinapsis-0.2.12}/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.12
|
|
4
4
|
Summary: Modular and Universal AI platform
|
|
5
5
|
Author-email: SinapsisAI <dev@sinapsis.tech>
|
|
6
6
|
Project-URL: Homepage, https://sinapsis.tech
|
|
@@ -241,18 +241,56 @@ Tutorials for different projects within sinapsis are available at [sinapsis tuto
|
|
|
241
241
|
|
|
242
242
|
The sinapsis framework contains a list of public packages with **Templates** covering a wide range of tasks,
|
|
243
243
|
including computer vision, time series, nlp, llm'. These packages include:
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
<details>
|
|
261
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis anomaly detection</span></strong></summary>
|
|
262
|
+
|
|
263
|
+
* [sinapsis-anomaly-detection](https://github.com/Sinapsis-ai/sinapsis-anomaly-detection)
|
|
264
|
+
</details>
|
|
265
|
+
|
|
266
|
+
<details>
|
|
267
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis chatbots</span></strong></summary>
|
|
268
|
+
|
|
269
|
+
* [sinapsis-chatbots](https://github.com/Sinapsis-ai/sinapsis-chatbots)
|
|
270
|
+
</details>
|
|
271
|
+
|
|
244
272
|
<details>
|
|
245
273
|
<summary><strong><span style="font-size: 1.4em;">sinapsis data tools</span></strong></summary>
|
|
246
274
|
|
|
275
|
+
* [sinapsis-data-tools](https://github.com/Sinapsis-ai/sinapsis-data-tools)
|
|
276
|
+
</details>
|
|
277
|
+
|
|
278
|
+
<details>
|
|
279
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis framework converter</span></strong></summary>
|
|
247
280
|
|
|
248
|
-
* [sinapsis-
|
|
281
|
+
* [sinapsis-framework-converter](https://github.com/Sinapsis-ai/sinapsis-framework-comverter)
|
|
282
|
+
</details>
|
|
249
283
|
|
|
250
|
-
|
|
284
|
+
<details>
|
|
285
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis huggingface</span></strong></summary>
|
|
251
286
|
|
|
252
|
-
* [sinapsis-
|
|
287
|
+
* [sinapsis-huggingface](https://github.com/Sinapsis-ai/sinapsis-huggingface)
|
|
288
|
+
</details>
|
|
253
289
|
|
|
254
|
-
|
|
290
|
+
<details>
|
|
291
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis image transforms</span></strong></summary>
|
|
255
292
|
|
|
293
|
+
* [sinapsis-image-transforms](https://github.com/Sinapsis-ai/sinapsis-image-transforms)
|
|
256
294
|
</details>
|
|
257
295
|
|
|
258
296
|
<details>
|
|
@@ -260,12 +298,54 @@ including computer vision, time series, nlp, llm'. These packages include:
|
|
|
260
298
|
|
|
261
299
|
* [sinapsis-langchain-readers](https://github.com/Sinapsis-ai/sinapsis-langchain)
|
|
262
300
|
</details>
|
|
301
|
+
|
|
263
302
|
<details>
|
|
264
|
-
<summary><strong><span style="font-size: 1.4em;">sinapsis
|
|
303
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis ocr</span></strong></summary>
|
|
265
304
|
|
|
266
|
-
* [sinapsis-
|
|
305
|
+
* [sinapsis-ocr](https://github.com/Sinapsis-ai/sinapsis-ocr)
|
|
306
|
+
</details>
|
|
307
|
+
|
|
308
|
+
<details>
|
|
309
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis object detection</span></strong></summary>
|
|
310
|
+
|
|
311
|
+
* [sinapsis-object-detection](https://github.com/Sinapsis-ai/sinapsis-object-detection)
|
|
312
|
+
</details>
|
|
313
|
+
|
|
314
|
+
<details>
|
|
315
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis openai</span></strong></summary>
|
|
316
|
+
|
|
317
|
+
* [sinapsis-openai](https://github.com/Sinapsis-ai/sinapsis-openai)
|
|
318
|
+
</details>
|
|
319
|
+
|
|
320
|
+
<details>
|
|
321
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis retina face trt</span></strong></summary>
|
|
322
|
+
|
|
323
|
+
* [sinapsis-retina-face-trt](https://github.com/Sinapsis-ai/sinapsis-retina-face-trt)
|
|
324
|
+
</details>
|
|
325
|
+
|
|
326
|
+
<details>
|
|
327
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis speech</span></strong></summary>
|
|
328
|
+
|
|
329
|
+
* [sinapsis-speech](https://github.com/Sinapsis-ai/sinapsis-speech)
|
|
330
|
+
</details>
|
|
331
|
+
|
|
332
|
+
<details>
|
|
333
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis time series</span></strong></summary>
|
|
334
|
+
|
|
335
|
+
* [sinapsis-time-series](https://github.com/Sinapsis-ai/sinapsis-time-series)
|
|
336
|
+
</details>
|
|
337
|
+
|
|
338
|
+
<details>
|
|
339
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis trackers</span></strong></summary>
|
|
340
|
+
|
|
341
|
+
* [sinapsis-trackers](https://github.com/Sinapsis-ai/sinapsis-trackers)
|
|
267
342
|
</details>
|
|
268
343
|
|
|
344
|
+
<h2 id="license">🔍 License</h2>
|
|
345
|
+
|
|
346
|
+
This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
|
|
347
|
+
|
|
348
|
+
For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
|
|
269
349
|
|
|
270
350
|
<h2 id="license">🔍 License</h2>
|
|
271
351
|
|
|
@@ -222,18 +222,56 @@ Tutorials for different projects within sinapsis are available at [sinapsis tuto
|
|
|
222
222
|
|
|
223
223
|
The sinapsis framework contains a list of public packages with **Templates** covering a wide range of tasks,
|
|
224
224
|
including computer vision, time series, nlp, llm'. These packages include:
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
<details>
|
|
242
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis anomaly detection</span></strong></summary>
|
|
243
|
+
|
|
244
|
+
* [sinapsis-anomaly-detection](https://github.com/Sinapsis-ai/sinapsis-anomaly-detection)
|
|
245
|
+
</details>
|
|
246
|
+
|
|
247
|
+
<details>
|
|
248
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis chatbots</span></strong></summary>
|
|
249
|
+
|
|
250
|
+
* [sinapsis-chatbots](https://github.com/Sinapsis-ai/sinapsis-chatbots)
|
|
251
|
+
</details>
|
|
252
|
+
|
|
225
253
|
<details>
|
|
226
254
|
<summary><strong><span style="font-size: 1.4em;">sinapsis data tools</span></strong></summary>
|
|
227
255
|
|
|
256
|
+
* [sinapsis-data-tools](https://github.com/Sinapsis-ai/sinapsis-data-tools)
|
|
257
|
+
</details>
|
|
258
|
+
|
|
259
|
+
<details>
|
|
260
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis framework converter</span></strong></summary>
|
|
228
261
|
|
|
229
|
-
* [sinapsis-
|
|
262
|
+
* [sinapsis-framework-converter](https://github.com/Sinapsis-ai/sinapsis-framework-comverter)
|
|
263
|
+
</details>
|
|
230
264
|
|
|
231
|
-
|
|
265
|
+
<details>
|
|
266
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis huggingface</span></strong></summary>
|
|
232
267
|
|
|
233
|
-
* [sinapsis-
|
|
268
|
+
* [sinapsis-huggingface](https://github.com/Sinapsis-ai/sinapsis-huggingface)
|
|
269
|
+
</details>
|
|
234
270
|
|
|
235
|
-
|
|
271
|
+
<details>
|
|
272
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis image transforms</span></strong></summary>
|
|
236
273
|
|
|
274
|
+
* [sinapsis-image-transforms](https://github.com/Sinapsis-ai/sinapsis-image-transforms)
|
|
237
275
|
</details>
|
|
238
276
|
|
|
239
277
|
<details>
|
|
@@ -241,12 +279,54 @@ including computer vision, time series, nlp, llm'. These packages include:
|
|
|
241
279
|
|
|
242
280
|
* [sinapsis-langchain-readers](https://github.com/Sinapsis-ai/sinapsis-langchain)
|
|
243
281
|
</details>
|
|
282
|
+
|
|
244
283
|
<details>
|
|
245
|
-
<summary><strong><span style="font-size: 1.4em;">sinapsis
|
|
284
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis ocr</span></strong></summary>
|
|
246
285
|
|
|
247
|
-
* [sinapsis-
|
|
286
|
+
* [sinapsis-ocr](https://github.com/Sinapsis-ai/sinapsis-ocr)
|
|
287
|
+
</details>
|
|
288
|
+
|
|
289
|
+
<details>
|
|
290
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis object detection</span></strong></summary>
|
|
291
|
+
|
|
292
|
+
* [sinapsis-object-detection](https://github.com/Sinapsis-ai/sinapsis-object-detection)
|
|
293
|
+
</details>
|
|
294
|
+
|
|
295
|
+
<details>
|
|
296
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis openai</span></strong></summary>
|
|
297
|
+
|
|
298
|
+
* [sinapsis-openai](https://github.com/Sinapsis-ai/sinapsis-openai)
|
|
299
|
+
</details>
|
|
300
|
+
|
|
301
|
+
<details>
|
|
302
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis retina face trt</span></strong></summary>
|
|
303
|
+
|
|
304
|
+
* [sinapsis-retina-face-trt](https://github.com/Sinapsis-ai/sinapsis-retina-face-trt)
|
|
305
|
+
</details>
|
|
306
|
+
|
|
307
|
+
<details>
|
|
308
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis speech</span></strong></summary>
|
|
309
|
+
|
|
310
|
+
* [sinapsis-speech](https://github.com/Sinapsis-ai/sinapsis-speech)
|
|
311
|
+
</details>
|
|
312
|
+
|
|
313
|
+
<details>
|
|
314
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis time series</span></strong></summary>
|
|
315
|
+
|
|
316
|
+
* [sinapsis-time-series](https://github.com/Sinapsis-ai/sinapsis-time-series)
|
|
317
|
+
</details>
|
|
318
|
+
|
|
319
|
+
<details>
|
|
320
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis trackers</span></strong></summary>
|
|
321
|
+
|
|
322
|
+
* [sinapsis-trackers](https://github.com/Sinapsis-ai/sinapsis-trackers)
|
|
248
323
|
</details>
|
|
249
324
|
|
|
325
|
+
<h2 id="license">🔍 License</h2>
|
|
326
|
+
|
|
327
|
+
This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
|
|
328
|
+
|
|
329
|
+
For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
|
|
250
330
|
|
|
251
331
|
<h2 id="license">🔍 License</h2>
|
|
252
332
|
|
|
@@ -0,0 +1,284 @@
|
|
|
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
|
+
|
|
89
|
+
def _setup_working_directory(self) -> None:
|
|
90
|
+
"""Creates a temporary directory for storing uploaded media files."""
|
|
91
|
+
self.gradio_temp_dir = get_upload_folder()
|
|
92
|
+
os.makedirs(self.gradio_temp_dir, exist_ok=True)
|
|
93
|
+
|
|
94
|
+
@staticmethod
|
|
95
|
+
def _format_history(history: list[dict[str, Any]], max_turns: int = 5) -> dict[str, Any]:
|
|
96
|
+
"""Formats the chat history into a simplified dictionary with limited turns per role.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
history (list[dict[str, Any]]): Full chat history with role and content.
|
|
100
|
+
max_turns (int, optional): Number of recent messages to keep per role. Defaults to 5.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
dict[str, Any]: A dictionary in the form {"user": [...], "assistant": [...]}.
|
|
104
|
+
"""
|
|
105
|
+
user_msgs = [entry[ChatKeys.content] for entry in history if entry[ChatKeys.role] == ChatKeys.user][-max_turns:]
|
|
106
|
+
assistant_msgs = [entry[ChatKeys.content] for entry in history if entry[ChatKeys.role] == ChatKeys.assistant][
|
|
107
|
+
-max_turns:
|
|
108
|
+
]
|
|
109
|
+
return {
|
|
110
|
+
ChatKeys.user: user_msgs,
|
|
111
|
+
ChatKeys.assistant: assistant_msgs,
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
def generate_packet(
|
|
115
|
+
self, container: DataContainer, message: dict[str, Any], history: list[dict[str, Any]], conv_id: str
|
|
116
|
+
) -> DataContainer:
|
|
117
|
+
"""Creates a `DataContainer` object from user message and chat history.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
container (container): incoming container for the query
|
|
121
|
+
message (dict[str, Any]): Dictionary with keys like 'text' and 'files'.
|
|
122
|
+
history (list[dict[str, Any]]): List of previous chat messages.
|
|
123
|
+
conv_id (str): Conversation id value
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
DataContainer: A structured container of the parsed input.
|
|
127
|
+
"""
|
|
128
|
+
formatted_history = self._format_history(history)
|
|
129
|
+
container.texts.append(
|
|
130
|
+
TextPacket(content=message.get(ChatKeys.text), source=conv_id, generic_data={"history": formatted_history})
|
|
131
|
+
)
|
|
132
|
+
for file_path in message.get(ChatKeys.files, []):
|
|
133
|
+
if file_path.endswith(".wav"):
|
|
134
|
+
container.generic_data[ChatKeys.audio_path] = file_path
|
|
135
|
+
else:
|
|
136
|
+
img_bgr = cv2.imread(file_path)
|
|
137
|
+
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|
|
138
|
+
filename = os.path.basename(file_path)
|
|
139
|
+
container.images.append(ImagePacket(content=img_rgb, color_space=1, source=filename))
|
|
140
|
+
return container
|
|
141
|
+
|
|
142
|
+
def agent_execution(self, container: DataContainer) -> dict[str, Any]:
|
|
143
|
+
"""Executes the Sinapsis agent and processes the result into chatbot output.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
container (DataContainer): Structured input for the agent.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
dict[str, Any]: Dictionary with keys such as 'text' and 'files' to display in the UI.
|
|
150
|
+
"""
|
|
151
|
+
default_response = {ChatKeys.text: "Could not process request, please try again", ChatKeys.files: []}
|
|
152
|
+
result_container = self.agent(container)
|
|
153
|
+
response = {}
|
|
154
|
+
|
|
155
|
+
if result_container.texts:
|
|
156
|
+
response[ChatKeys.text] = result_container.texts[-1].content
|
|
157
|
+
|
|
158
|
+
if result_container.images:
|
|
159
|
+
image_packet = container.images[-1]
|
|
160
|
+
img_array = np.uint8(image_packet.content)
|
|
161
|
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
|
162
|
+
image_path = os.path.join(self.gradio_temp_dir, image_packet.source)
|
|
163
|
+
cv2.imwrite(image_path, img_array)
|
|
164
|
+
response[ChatKeys.files] = [image_path]
|
|
165
|
+
|
|
166
|
+
return response if response else default_response
|
|
167
|
+
|
|
168
|
+
@staticmethod
|
|
169
|
+
def _set_conversation_id(conv_id: str) -> str:
|
|
170
|
+
"""
|
|
171
|
+
Ensure a valid conversation ID is set. If no ID is provided, a new one is
|
|
172
|
+
generated.
|
|
173
|
+
Args:
|
|
174
|
+
conv_id (str): The provided conversation ID.
|
|
175
|
+
Returns:
|
|
176
|
+
str: The valid conversation ID (either provided or generated).
|
|
177
|
+
"""
|
|
178
|
+
if not conv_id:
|
|
179
|
+
conv_id = str(uuid.uuid4())
|
|
180
|
+
return conv_id
|
|
181
|
+
|
|
182
|
+
def stop_agent(self) -> dict:
|
|
183
|
+
"""
|
|
184
|
+
Stop the chatbot's agent and save the chat history to a file.
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
Returns:
|
|
188
|
+
tuple: A tuple with None and Gradio updates for interactivity.
|
|
189
|
+
"""
|
|
190
|
+
|
|
191
|
+
self.agent = None
|
|
192
|
+
return gr.update(interactive=False)
|
|
193
|
+
|
|
194
|
+
def _clear_history(self) -> None:
|
|
195
|
+
"""
|
|
196
|
+
Clears the chat history and saves it to a file named "chat.txt".
|
|
197
|
+
|
|
198
|
+
This method writes the current chat history to a file in JSON format,
|
|
199
|
+
then clears the history.
|
|
200
|
+
"""
|
|
201
|
+
|
|
202
|
+
try:
|
|
203
|
+
os.remove(self.file_name)
|
|
204
|
+
except FileNotFoundError:
|
|
205
|
+
sinapsis_logger.warning("Chat history file does not exist")
|
|
206
|
+
|
|
207
|
+
def process_msg(self, message: dict, history: list[dict], conv_id: str, container: DataContainer) -> dict:
|
|
208
|
+
"""
|
|
209
|
+
Process a user message and generate a chatbot response.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
message (str): The user's input message.
|
|
213
|
+
history: list[dict]: list of messages sent through chatbot
|
|
214
|
+
conv_id (str): The conversation ID for the current session.
|
|
215
|
+
container (DataContainer): Incoming data container for the query
|
|
216
|
+
|
|
217
|
+
Returns:
|
|
218
|
+
tuple: The updated chat history and UI components for the chatbot interface.
|
|
219
|
+
"""
|
|
220
|
+
container = container or DataContainer()
|
|
221
|
+
conv_id = self._set_conversation_id(conv_id)
|
|
222
|
+
container = self.generate_packet(container, message, history, conv_id)
|
|
223
|
+
response = self.agent_execution(container)
|
|
224
|
+
return response
|
|
225
|
+
|
|
226
|
+
def add_app_components(self, conv_id) -> gr.ChatInterface:
|
|
227
|
+
"""Builds and returns the main Gradio chat interface.
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
gr.ChatInterface: Configured chat interface with multimodal input.
|
|
231
|
+
"""
|
|
232
|
+
chatbot = gr.Chatbot(
|
|
233
|
+
type="messages",
|
|
234
|
+
height=600,
|
|
235
|
+
show_label=False,
|
|
236
|
+
avatar_images=(None, SINAPSIS_AVATAR),
|
|
237
|
+
show_copy_all_button=True,
|
|
238
|
+
)
|
|
239
|
+
textbox = gr.MultimodalTextbox(
|
|
240
|
+
file_count="multiple",
|
|
241
|
+
file_types=[".png", ".jpg", ".wav"],
|
|
242
|
+
sources=["upload", "microphone"],
|
|
243
|
+
placeholder="Message Chatbot",
|
|
244
|
+
)
|
|
245
|
+
container = gr.State(value=DataContainer())
|
|
246
|
+
interface = gr.ChatInterface(
|
|
247
|
+
fn=self.process_msg,
|
|
248
|
+
title=None,
|
|
249
|
+
multimodal=True,
|
|
250
|
+
chatbot=chatbot,
|
|
251
|
+
fill_height=True,
|
|
252
|
+
css=css_header,
|
|
253
|
+
type="messages",
|
|
254
|
+
examples=self.examples,
|
|
255
|
+
example_icons=[SINAPSIS_AVATAR] * len(self.examples) if self.examples else None,
|
|
256
|
+
textbox=textbox,
|
|
257
|
+
save_history=True,
|
|
258
|
+
additional_inputs=[conv_id, container],
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
stop_agent = gr.Button("Stop chatbot")
|
|
262
|
+
stop_agent.click(self.stop_agent, outputs=[textbox])
|
|
263
|
+
|
|
264
|
+
return interface
|
|
265
|
+
|
|
266
|
+
def app_interface(self) -> gr.Blocks:
|
|
267
|
+
"""Builds the full Gradio UI layout for the chatbot application.
|
|
268
|
+
|
|
269
|
+
Returns:
|
|
270
|
+
gr.Blocks: Gradio Blocks layout for the complete application.
|
|
271
|
+
"""
|
|
272
|
+
with gr.Blocks(css=css_header(), title=self.config.app_title) as chatbot_interface:
|
|
273
|
+
add_logo_and_title(self.config.app_title)
|
|
274
|
+
conv_id = gr.State(value=str(uuid.uuid4()))
|
|
275
|
+
self.add_app_components(conv_id)
|
|
276
|
+
|
|
277
|
+
return chatbot_interface
|
|
278
|
+
|
|
279
|
+
def launch(self, **kwargs: dict[str, Any]) -> None:
|
|
280
|
+
"""Launches the Gradio app, optionally with authentication and custom options."""
|
|
281
|
+
interface = self.app_interface()
|
|
282
|
+
interface.launch(
|
|
283
|
+
**kwargs,
|
|
284
|
+
)
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
|
-
from typing import cast
|
|
3
2
|
|
|
4
3
|
import gradio as gr
|
|
5
4
|
from sinapsis_core.agent import Agent
|
|
@@ -38,6 +37,7 @@ class BaseTTSApp:
|
|
|
38
37
|
self.framework = framework
|
|
39
38
|
self.task = task
|
|
40
39
|
self.generic_key_or_base_path = generic_key_or_base_path
|
|
40
|
+
self.agent, self.initialized_state = self.init_agent()
|
|
41
41
|
|
|
42
42
|
def init_agent(self) -> tuple[Agent, bool]:
|
|
43
43
|
"""
|
|
@@ -61,16 +61,12 @@ class BaseTTSApp:
|
|
|
61
61
|
str | None: The path to the generated audio file, or None if the output is invalid.
|
|
62
62
|
"""
|
|
63
63
|
|
|
64
|
-
def text_to_speech(
|
|
65
|
-
self, initialized: bool, agent: Agent | None, text_to_convert: str
|
|
66
|
-
) -> tuple[gr.Audio | None, str | None]:
|
|
64
|
+
def text_to_speech(self, text_to_convert: str) -> tuple[gr.Audio | None, str | None]:
|
|
67
65
|
"""
|
|
68
66
|
Converts text to speech by passing the text through the agent and postprocessing steps.
|
|
69
67
|
If models has not been initialized, only a text hint is returned.
|
|
70
68
|
|
|
71
69
|
Args:
|
|
72
|
-
initialized (bool): Whether the agent has been initialized.
|
|
73
|
-
agent (object): The agent object generated using `generic_agent_builder`.
|
|
74
70
|
text_to_convert (str): The text to be converted to speech.
|
|
75
71
|
|
|
76
72
|
Returns:
|
|
@@ -79,10 +75,9 @@ class BaseTTSApp:
|
|
|
79
75
|
Raises:
|
|
80
76
|
gr.Error: If the speech generation fails.
|
|
81
77
|
"""
|
|
82
|
-
if
|
|
83
|
-
agent = cast(Agent, agent)
|
|
78
|
+
if self.initialized_state:
|
|
84
79
|
container = DataContainer(texts=[TextPacket(content=text_to_convert)])
|
|
85
|
-
output_container = agent(container)
|
|
80
|
+
output_container = self.agent(container)
|
|
86
81
|
audio_path = self._postprocess_output(output_container, self.generic_key_or_base_path)
|
|
87
82
|
if audio_path:
|
|
88
83
|
return gr.Audio(audio_path, visible=True), None
|
|
@@ -92,15 +87,14 @@ class BaseTTSApp:
|
|
|
92
87
|
@staticmethod
|
|
93
88
|
def update_status_msg() -> str:
|
|
94
89
|
"""Updates status_text to indicate that the model is ready."""
|
|
95
|
-
return
|
|
90
|
+
return gr.Markdown(
|
|
91
|
+
"#### Model ready. Click on the box the text you wish to convert to speech and submit to generate!"
|
|
92
|
+
)
|
|
96
93
|
|
|
97
|
-
def inner_tts_functionality(self
|
|
94
|
+
def inner_tts_functionality(self) -> None:
|
|
98
95
|
"""
|
|
99
96
|
Defines the inner functionality of the TTS app interface using Gradio components.
|
|
100
97
|
"""
|
|
101
|
-
agent_state = gr.State()
|
|
102
|
-
initialized_state = gr.State(False)
|
|
103
|
-
interface.load(self.init_agent, outputs=[agent_state, initialized_state])
|
|
104
98
|
|
|
105
99
|
text_to_convert = gr.Textbox(
|
|
106
100
|
submit_btn=True,
|
|
@@ -109,26 +103,29 @@ class BaseTTSApp:
|
|
|
109
103
|
+ "a flexible template system that ensures modularity and compatibility between models and tools widely "
|
|
110
104
|
+ "used by the community.",
|
|
111
105
|
)
|
|
112
|
-
status_msg =
|
|
113
|
-
initialized_state.change(self.update_status_msg, outputs=[status_msg])
|
|
106
|
+
status_msg = self.update_status_msg()
|
|
114
107
|
|
|
115
108
|
audio_generated = gr.Audio(label="Audio generated:", visible=False)
|
|
116
109
|
|
|
117
110
|
text_to_convert.submit(
|
|
118
111
|
self.text_to_speech,
|
|
119
|
-
inputs=[
|
|
112
|
+
inputs=[text_to_convert],
|
|
120
113
|
outputs=[audio_generated, status_msg],
|
|
121
114
|
)
|
|
122
115
|
|
|
123
|
-
def
|
|
124
|
-
"""
|
|
125
|
-
Invokes the Gradio interface and displays the TTS functionality.
|
|
126
|
-
"""
|
|
116
|
+
def app_interface(self):
|
|
127
117
|
with gr.Blocks(css=css_header()) as tts_interface:
|
|
128
118
|
add_logo_and_title(f"Sinapsis {self.framework} {self.task} demo")
|
|
129
|
-
self.inner_tts_functionality(
|
|
119
|
+
self.inner_tts_functionality()
|
|
130
120
|
return tts_interface
|
|
131
121
|
|
|
122
|
+
def launch(self, **kwargs) -> None:
|
|
123
|
+
"""Launches the Gradio app, optionally with authentication and custom options."""
|
|
124
|
+
interface = self.app_interface()
|
|
125
|
+
interface.launch(
|
|
126
|
+
**kwargs,
|
|
127
|
+
)
|
|
128
|
+
|
|
132
129
|
|
|
133
130
|
class TTSAppAudioFromPacket(BaseTTSApp):
|
|
134
131
|
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sinapsis
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.12
|
|
4
4
|
Summary: Modular and Universal AI platform
|
|
5
5
|
Author-email: SinapsisAI <dev@sinapsis.tech>
|
|
6
6
|
Project-URL: Homepage, https://sinapsis.tech
|
|
@@ -241,18 +241,56 @@ Tutorials for different projects within sinapsis are available at [sinapsis tuto
|
|
|
241
241
|
|
|
242
242
|
The sinapsis framework contains a list of public packages with **Templates** covering a wide range of tasks,
|
|
243
243
|
including computer vision, time series, nlp, llm'. These packages include:
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
<details>
|
|
261
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis anomaly detection</span></strong></summary>
|
|
262
|
+
|
|
263
|
+
* [sinapsis-anomaly-detection](https://github.com/Sinapsis-ai/sinapsis-anomaly-detection)
|
|
264
|
+
</details>
|
|
265
|
+
|
|
266
|
+
<details>
|
|
267
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis chatbots</span></strong></summary>
|
|
268
|
+
|
|
269
|
+
* [sinapsis-chatbots](https://github.com/Sinapsis-ai/sinapsis-chatbots)
|
|
270
|
+
</details>
|
|
271
|
+
|
|
244
272
|
<details>
|
|
245
273
|
<summary><strong><span style="font-size: 1.4em;">sinapsis data tools</span></strong></summary>
|
|
246
274
|
|
|
275
|
+
* [sinapsis-data-tools](https://github.com/Sinapsis-ai/sinapsis-data-tools)
|
|
276
|
+
</details>
|
|
277
|
+
|
|
278
|
+
<details>
|
|
279
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis framework converter</span></strong></summary>
|
|
247
280
|
|
|
248
|
-
* [sinapsis-
|
|
281
|
+
* [sinapsis-framework-converter](https://github.com/Sinapsis-ai/sinapsis-framework-comverter)
|
|
282
|
+
</details>
|
|
249
283
|
|
|
250
|
-
|
|
284
|
+
<details>
|
|
285
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis huggingface</span></strong></summary>
|
|
251
286
|
|
|
252
|
-
* [sinapsis-
|
|
287
|
+
* [sinapsis-huggingface](https://github.com/Sinapsis-ai/sinapsis-huggingface)
|
|
288
|
+
</details>
|
|
253
289
|
|
|
254
|
-
|
|
290
|
+
<details>
|
|
291
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis image transforms</span></strong></summary>
|
|
255
292
|
|
|
293
|
+
* [sinapsis-image-transforms](https://github.com/Sinapsis-ai/sinapsis-image-transforms)
|
|
256
294
|
</details>
|
|
257
295
|
|
|
258
296
|
<details>
|
|
@@ -260,12 +298,54 @@ including computer vision, time series, nlp, llm'. These packages include:
|
|
|
260
298
|
|
|
261
299
|
* [sinapsis-langchain-readers](https://github.com/Sinapsis-ai/sinapsis-langchain)
|
|
262
300
|
</details>
|
|
301
|
+
|
|
263
302
|
<details>
|
|
264
|
-
<summary><strong><span style="font-size: 1.4em;">sinapsis
|
|
303
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis ocr</span></strong></summary>
|
|
265
304
|
|
|
266
|
-
* [sinapsis-
|
|
305
|
+
* [sinapsis-ocr](https://github.com/Sinapsis-ai/sinapsis-ocr)
|
|
306
|
+
</details>
|
|
307
|
+
|
|
308
|
+
<details>
|
|
309
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis object detection</span></strong></summary>
|
|
310
|
+
|
|
311
|
+
* [sinapsis-object-detection](https://github.com/Sinapsis-ai/sinapsis-object-detection)
|
|
312
|
+
</details>
|
|
313
|
+
|
|
314
|
+
<details>
|
|
315
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis openai</span></strong></summary>
|
|
316
|
+
|
|
317
|
+
* [sinapsis-openai](https://github.com/Sinapsis-ai/sinapsis-openai)
|
|
318
|
+
</details>
|
|
319
|
+
|
|
320
|
+
<details>
|
|
321
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis retina face trt</span></strong></summary>
|
|
322
|
+
|
|
323
|
+
* [sinapsis-retina-face-trt](https://github.com/Sinapsis-ai/sinapsis-retina-face-trt)
|
|
324
|
+
</details>
|
|
325
|
+
|
|
326
|
+
<details>
|
|
327
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis speech</span></strong></summary>
|
|
328
|
+
|
|
329
|
+
* [sinapsis-speech](https://github.com/Sinapsis-ai/sinapsis-speech)
|
|
330
|
+
</details>
|
|
331
|
+
|
|
332
|
+
<details>
|
|
333
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis time series</span></strong></summary>
|
|
334
|
+
|
|
335
|
+
* [sinapsis-time-series](https://github.com/Sinapsis-ai/sinapsis-time-series)
|
|
336
|
+
</details>
|
|
337
|
+
|
|
338
|
+
<details>
|
|
339
|
+
<summary><strong><span style="font-size: 1.4em;">sinapsis trackers</span></strong></summary>
|
|
340
|
+
|
|
341
|
+
* [sinapsis-trackers](https://github.com/Sinapsis-ai/sinapsis-trackers)
|
|
267
342
|
</details>
|
|
268
343
|
|
|
344
|
+
<h2 id="license">🔍 License</h2>
|
|
345
|
+
|
|
346
|
+
This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
|
|
347
|
+
|
|
348
|
+
For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
|
|
269
349
|
|
|
270
350
|
<h2 id="license">🔍 License</h2>
|
|
271
351
|
|
|
@@ -323,6 +323,21 @@ wheels = [
|
|
|
323
323
|
{ url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515 },
|
|
324
324
|
]
|
|
325
325
|
|
|
326
|
+
[[package]]
|
|
327
|
+
name = "hf-xet"
|
|
328
|
+
version = "1.1.0"
|
|
329
|
+
source = { registry = "https://pypi.org/simple" }
|
|
330
|
+
sdist = { url = "https://files.pythonhosted.org/packages/aa/2c/70009910fcbd204bde75842b60c1e47fe72edb0e978954cb8001735885c7/hf_xet-1.1.0.tar.gz", hash = "sha256:a7c2a4c2b6eee9ce0a1a367a82b60d95ba634420ef1c250addad7aa4af419cf4", size = 263996 }
|
|
331
|
+
wheels = [
|
|
332
|
+
{ url = "https://files.pythonhosted.org/packages/dc/fd/0db331297e331f0f02005fd7ea666439bf15efd74f0dd62af02a43236a1b/hf_xet-1.1.0-cp37-abi3-macosx_10_12_x86_64.whl", hash = "sha256:0322c42551e275fcb7949c083a54a81b2898e50787c9aa74284fcb8d2c58c12c", size = 5069444 },
|
|
333
|
+
{ url = "https://files.pythonhosted.org/packages/b9/7d/4d7ae44219d3744ad55669cb90ef3d4ed9f5f8a4729fa635a6499491cb78/hf_xet-1.1.0-cp37-abi3-macosx_11_0_arm64.whl", hash = "sha256:667153a0304ac2debf2af95a8ff7687186f885b493f4cd16344869af270cd110", size = 4881465 },
|
|
334
|
+
{ url = "https://files.pythonhosted.org/packages/83/9a/d40d2a57b132d609d8a4ccc29e59ed69749021610616749cabcda2532158/hf_xet-1.1.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:995eeffb119636ea617b96c7d7bf3c3f5ea8727fa57974574e25d700b8532d48", size = 53584225 },
|
|
335
|
+
{ url = "https://files.pythonhosted.org/packages/2e/01/d94553f91d85746e0862f24d239da88d10f5ce252b028565744e982432f4/hf_xet-1.1.0-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:3aee847da362393331f515c4010d0aaa1c2669acfcca1f4b28946d6949cc0086", size = 52043680 },
|
|
336
|
+
{ url = "https://files.pythonhosted.org/packages/29/89/1f31853bf378f0ceb3363c07fd8a12af9b904b1f8c21e65eb5c19397bc98/hf_xet-1.1.0-cp37-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:68c5813a6074aa36e12ef5983230e3b03148cce61e0fcdd294096493795565b4", size = 53072672 },
|
|
337
|
+
{ url = "https://files.pythonhosted.org/packages/b5/9f/5ecb92b18a4b2135a72a95dc08bcbeda9176f46642c745ee052420d2aea8/hf_xet-1.1.0-cp37-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:4ee9222bf9274b1c198b88a929de0b5a49349c4962d89c5b3b2f0f7f47d9761c", size = 53521053 },
|
|
338
|
+
{ url = "https://files.pythonhosted.org/packages/53/d6/cb32842cbf1cf5a154b41fa918a2fd86003af9bca227a2397cd7f312a8a6/hf_xet-1.1.0-cp37-abi3-win_amd64.whl", hash = "sha256:73153eab9abf3d6973b21e94a67ccba5d595c3e12feb8c0bf50be02964e7f126", size = 4204376 },
|
|
339
|
+
]
|
|
340
|
+
|
|
326
341
|
[[package]]
|
|
327
342
|
name = "httpcore"
|
|
328
343
|
version = "1.0.9"
|
|
@@ -353,20 +368,21 @@ wheels = [
|
|
|
353
368
|
|
|
354
369
|
[[package]]
|
|
355
370
|
name = "huggingface-hub"
|
|
356
|
-
version = "0.
|
|
371
|
+
version = "0.31.1"
|
|
357
372
|
source = { registry = "https://pypi.org/simple" }
|
|
358
373
|
dependencies = [
|
|
359
374
|
{ name = "filelock" },
|
|
360
375
|
{ name = "fsspec" },
|
|
376
|
+
{ name = "hf-xet", marker = "platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'arm64' or platform_machine == 'x86_64'" },
|
|
361
377
|
{ name = "packaging" },
|
|
362
378
|
{ name = "pyyaml" },
|
|
363
379
|
{ name = "requests" },
|
|
364
380
|
{ name = "tqdm" },
|
|
365
381
|
{ name = "typing-extensions" },
|
|
366
382
|
]
|
|
367
|
-
sdist = { url = "https://files.pythonhosted.org/packages/
|
|
383
|
+
sdist = { url = "https://files.pythonhosted.org/packages/25/eb/9268c1205d19388659d5dc664f012177b752c0eef194a9159acc7227780f/huggingface_hub-0.31.1.tar.gz", hash = "sha256:492bb5f545337aa9e2f59b75ef4c5f535a371e8958a6ce90af056387e67f1180", size = 403036 }
|
|
368
384
|
wheels = [
|
|
369
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
385
|
+
{ url = "https://files.pythonhosted.org/packages/3a/bf/6002da17ec1c7a47bedeb216812929665927c70b6e7500b3c7bf36f01bdd/huggingface_hub-0.31.1-py3-none-any.whl", hash = "sha256:43f73124819b48b42d140cbc0d7a2e6bd15b2853b1b9d728d4d55ad1750cac5b", size = 484265 },
|
|
370
386
|
]
|
|
371
387
|
|
|
372
388
|
[[package]]
|
|
@@ -764,11 +780,11 @@ wheels = [
|
|
|
764
780
|
|
|
765
781
|
[[package]]
|
|
766
782
|
name = "platformdirs"
|
|
767
|
-
version = "4.3.
|
|
783
|
+
version = "4.3.8"
|
|
768
784
|
source = { registry = "https://pypi.org/simple" }
|
|
769
|
-
sdist = { url = "https://files.pythonhosted.org/packages/
|
|
785
|
+
sdist = { url = "https://files.pythonhosted.org/packages/fe/8b/3c73abc9c759ecd3f1f7ceff6685840859e8070c4d947c93fae71f6a0bf2/platformdirs-4.3.8.tar.gz", hash = "sha256:3d512d96e16bcb959a814c9f348431070822a6496326a4be0911c40b5a74c2bc", size = 21362 }
|
|
770
786
|
wheels = [
|
|
771
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
787
|
+
{ url = "https://files.pythonhosted.org/packages/fe/39/979e8e21520d4e47a0bbe349e2713c0aac6f3d853d0e5b34d76206c439aa/platformdirs-4.3.8-py3-none-any.whl", hash = "sha256:ff7059bb7eb1179e2685604f4aaf157cfd9535242bd23742eadc3c13542139b4", size = 18567 },
|
|
772
788
|
]
|
|
773
789
|
|
|
774
790
|
[[package]]
|
|
@@ -1012,27 +1028,27 @@ wheels = [
|
|
|
1012
1028
|
|
|
1013
1029
|
[[package]]
|
|
1014
1030
|
name = "ruff"
|
|
1015
|
-
version = "0.11.
|
|
1016
|
-
source = { registry = "https://pypi.org/simple" }
|
|
1017
|
-
sdist = { url = "https://files.pythonhosted.org/packages/
|
|
1018
|
-
wheels = [
|
|
1019
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1020
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1021
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1022
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1023
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1024
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1025
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1026
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1027
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1028
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1029
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1030
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1031
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1032
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1033
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1034
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1035
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1031
|
+
version = "0.11.9"
|
|
1032
|
+
source = { registry = "https://pypi.org/simple" }
|
|
1033
|
+
sdist = { url = "https://files.pythonhosted.org/packages/f5/e7/e55dda1c92cdcf34b677ebef17486669800de01e887b7831a1b8fdf5cb08/ruff-0.11.9.tar.gz", hash = "sha256:ebd58d4f67a00afb3a30bf7d383e52d0e036e6195143c6db7019604a05335517", size = 4132134 }
|
|
1034
|
+
wheels = [
|
|
1035
|
+
{ url = "https://files.pythonhosted.org/packages/fb/71/75dfb7194fe6502708e547941d41162574d1f579c4676a8eb645bf1a6842/ruff-0.11.9-py3-none-linux_armv6l.whl", hash = "sha256:a31a1d143a5e6f499d1fb480f8e1e780b4dfdd580f86e05e87b835d22c5c6f8c", size = 10335453 },
|
|
1036
|
+
{ url = "https://files.pythonhosted.org/packages/74/fc/ad80c869b1732f53c4232bbf341f33c5075b2c0fb3e488983eb55964076a/ruff-0.11.9-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:66bc18ca783b97186a1f3100e91e492615767ae0a3be584e1266aa9051990722", size = 11072566 },
|
|
1037
|
+
{ url = "https://files.pythonhosted.org/packages/87/0d/0ccececef8a0671dae155cbf7a1f90ea2dd1dba61405da60228bbe731d35/ruff-0.11.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:bd576cd06962825de8aece49f28707662ada6a1ff2db848d1348e12c580acbf1", size = 10435020 },
|
|
1038
|
+
{ url = "https://files.pythonhosted.org/packages/52/01/e249e1da6ad722278094e183cbf22379a9bbe5f21a3e46cef24ccab76e22/ruff-0.11.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5b1d18b4be8182cc6fddf859ce432cc9631556e9f371ada52f3eaefc10d878de", size = 10593935 },
|
|
1039
|
+
{ url = "https://files.pythonhosted.org/packages/ed/9a/40cf91f61e3003fe7bd43f1761882740e954506c5a0f9097b1cff861f04c/ruff-0.11.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:0f3f46f759ac623e94824b1e5a687a0df5cd7f5b00718ff9c24f0a894a683be7", size = 10172971 },
|
|
1040
|
+
{ url = "https://files.pythonhosted.org/packages/61/12/d395203de1e8717d7a2071b5a340422726d4736f44daf2290aad1085075f/ruff-0.11.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f34847eea11932d97b521450cf3e1d17863cfa5a94f21a056b93fb86f3f3dba2", size = 11748631 },
|
|
1041
|
+
{ url = "https://files.pythonhosted.org/packages/66/d6/ef4d5eba77677eab511644c37c55a3bb8dcac1cdeb331123fe342c9a16c9/ruff-0.11.9-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:f33b15e00435773df97cddcd263578aa83af996b913721d86f47f4e0ee0ff271", size = 12409236 },
|
|
1042
|
+
{ url = "https://files.pythonhosted.org/packages/c5/8f/5a2c5fc6124dd925a5faf90e1089ee9036462118b619068e5b65f8ea03df/ruff-0.11.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7b27613a683b086f2aca8996f63cb3dd7bc49e6eccf590563221f7b43ded3f65", size = 11881436 },
|
|
1043
|
+
{ url = "https://files.pythonhosted.org/packages/39/d1/9683f469ae0b99b95ef99a56cfe8c8373c14eba26bd5c622150959ce9f64/ruff-0.11.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:9e0d88756e63e8302e630cee3ce2ffb77859797cc84a830a24473939e6da3ca6", size = 13982759 },
|
|
1044
|
+
{ url = "https://files.pythonhosted.org/packages/4e/0b/c53a664f06e0faab596397867c6320c3816df479e888fe3af63bc3f89699/ruff-0.11.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:537c82c9829d7811e3aa680205f94c81a2958a122ac391c0eb60336ace741a70", size = 11541985 },
|
|
1045
|
+
{ url = "https://files.pythonhosted.org/packages/23/a0/156c4d7e685f6526a636a60986ee4a3c09c8c4e2a49b9a08c9913f46c139/ruff-0.11.9-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:440ac6a7029f3dee7d46ab7de6f54b19e34c2b090bb4f2480d0a2d635228f381", size = 10465775 },
|
|
1046
|
+
{ url = "https://files.pythonhosted.org/packages/43/d5/88b9a6534d9d4952c355e38eabc343df812f168a2c811dbce7d681aeb404/ruff-0.11.9-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:71c539bac63d0788a30227ed4d43b81353c89437d355fdc52e0cda4ce5651787", size = 10170957 },
|
|
1047
|
+
{ url = "https://files.pythonhosted.org/packages/f0/b8/2bd533bdaf469dc84b45815ab806784d561fab104d993a54e1852596d581/ruff-0.11.9-py3-none-musllinux_1_2_i686.whl", hash = "sha256:c67117bc82457e4501473c5f5217d49d9222a360794bfb63968e09e70f340abd", size = 11143307 },
|
|
1048
|
+
{ url = "https://files.pythonhosted.org/packages/2f/d9/43cfba291788459b9bfd4e09a0479aa94d05ab5021d381a502d61a807ec1/ruff-0.11.9-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:e4b78454f97aa454586e8a5557facb40d683e74246c97372af3c2d76901d697b", size = 11603026 },
|
|
1049
|
+
{ url = "https://files.pythonhosted.org/packages/22/e6/7ed70048e89b01d728ccc950557a17ecf8df4127b08a56944b9d0bae61bc/ruff-0.11.9-py3-none-win32.whl", hash = "sha256:7fe1bc950e7d7b42caaee2a8a3bc27410547cc032c9558ee2e0f6d3b209e845a", size = 10548627 },
|
|
1050
|
+
{ url = "https://files.pythonhosted.org/packages/90/36/1da5d566271682ed10f436f732e5f75f926c17255c9c75cefb77d4bf8f10/ruff-0.11.9-py3-none-win_amd64.whl", hash = "sha256:52edaa4a6d70f8180343a5b7f030c7edd36ad180c9f4d224959c2d689962d964", size = 11634340 },
|
|
1051
|
+
{ url = "https://files.pythonhosted.org/packages/40/f7/70aad26e5877c8f7ee5b161c4c9fa0100e63fc4c944dc6d97b9c7e871417/ruff-0.11.9-py3-none-win_arm64.whl", hash = "sha256:bcf42689c22f2e240f496d0c183ef2c6f7b35e809f12c1db58f75d9aa8d630ca", size = 10741080 },
|
|
1036
1052
|
]
|
|
1037
1053
|
|
|
1038
1054
|
[[package]]
|
|
@@ -1067,7 +1083,7 @@ wheels = [
|
|
|
1067
1083
|
|
|
1068
1084
|
[[package]]
|
|
1069
1085
|
name = "sinapsis"
|
|
1070
|
-
version = "0.2.
|
|
1086
|
+
version = "0.2.11"
|
|
1071
1087
|
source = { editable = "." }
|
|
1072
1088
|
dependencies = [
|
|
1073
1089
|
{ name = "sinapsis-core" },
|
|
@@ -1232,16 +1248,16 @@ wheels = [
|
|
|
1232
1248
|
|
|
1233
1249
|
[[package]]
|
|
1234
1250
|
name = "virtualenv"
|
|
1235
|
-
version = "20.
|
|
1251
|
+
version = "20.31.2"
|
|
1236
1252
|
source = { registry = "https://pypi.org/simple" }
|
|
1237
1253
|
dependencies = [
|
|
1238
1254
|
{ name = "distlib" },
|
|
1239
1255
|
{ name = "filelock" },
|
|
1240
1256
|
{ name = "platformdirs" },
|
|
1241
1257
|
]
|
|
1242
|
-
sdist = { url = "https://files.pythonhosted.org/packages/
|
|
1258
|
+
sdist = { url = "https://files.pythonhosted.org/packages/56/2c/444f465fb2c65f40c3a104fd0c495184c4f2336d65baf398e3c75d72ea94/virtualenv-20.31.2.tar.gz", hash = "sha256:e10c0a9d02835e592521be48b332b6caee6887f332c111aa79a09b9e79efc2af", size = 6076316 }
|
|
1243
1259
|
wheels = [
|
|
1244
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
1260
|
+
{ url = "https://files.pythonhosted.org/packages/f3/40/b1c265d4b2b62b58576588510fc4d1fe60a86319c8de99fd8e9fec617d2c/virtualenv-20.31.2-py3-none-any.whl", hash = "sha256:36efd0d9650ee985f0cad72065001e66d49a6f24eb44d98980f630686243cf11", size = 6057982 },
|
|
1245
1261
|
]
|
|
1246
1262
|
|
|
1247
1263
|
[[package]]
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
import json
|
|
3
|
-
import os.path
|
|
4
|
-
import uuid
|
|
5
|
-
|
|
6
|
-
import gradio as gr
|
|
7
|
-
import numpy as np
|
|
8
|
-
from PIL import Image
|
|
9
|
-
from pydantic.dataclasses import dataclass
|
|
10
|
-
from sinapsis_core.cli.run_agent_from_config import generic_agent_builder
|
|
11
|
-
from sinapsis_core.data_containers.data_packet import DataContainer, TextPacket
|
|
12
|
-
from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
|
|
13
|
-
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
14
|
-
|
|
15
|
-
from sinapsis.webapp.agent_gradio_helper import add_logo_and_title, css_header
|
|
16
|
-
|
|
17
|
-
SINAPSIS_AVATAR = "https://github.com/Sinapsis-AI/brand-resources/blob/main/sinapsis_logo/4x/fav_icon.png?raw=true"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
@dataclass
|
|
21
|
-
class ChatKeys:
|
|
22
|
-
"""
|
|
23
|
-
Defines key names used for referencing various chat-related data types.
|
|
24
|
-
|
|
25
|
-
This class serves as a centralized place to manage key names for different types of data
|
|
26
|
-
that may be used in chat interactions. These keys are typically used to map data in structured formats.
|
|
27
|
-
"""
|
|
28
|
-
|
|
29
|
-
text: str = "text"
|
|
30
|
-
image: str = "image"
|
|
31
|
-
files: str = "files"
|
|
32
|
-
file_path: str = "path"
|
|
33
|
-
audio_path: str = "audio_path"
|
|
34
|
-
role: str = "role"
|
|
35
|
-
content: str = "content"
|
|
36
|
-
user: str = "user"
|
|
37
|
-
assistant: str = "assistant"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
class BaseChatbot:
|
|
41
|
-
"""
|
|
42
|
-
A base chatbot class designed to work with various LLM frameworks, such as LLaMA.
|
|
43
|
-
This class provides the functionality to interact with users through text, audio,
|
|
44
|
-
and file inputs, maintain chat history, and integrate with Gradio for a web
|
|
45
|
-
interface. The class is intended to serve as a foundation
|
|
46
|
-
that can be adapted to different LLM frameworks by modifying the agent
|
|
47
|
-
initialization and response handling methods.
|
|
48
|
-
"""
|
|
49
|
-
|
|
50
|
-
def __init__(self, config_file: str, app_title: str = "Sinapsis LLaMA Chatbot") -> None:
|
|
51
|
-
"""
|
|
52
|
-
Initialize the chatbot with given configuration, framework, and task.
|
|
53
|
-
|
|
54
|
-
Args:
|
|
55
|
-
config_file (str): Path to the configuration file.
|
|
56
|
-
app_title (str): Title of the app
|
|
57
|
-
"""
|
|
58
|
-
self.config_file = config_file
|
|
59
|
-
self.file_name = f"{SINAPSIS_CACHE_DIR}/chatbot/chat.txt"
|
|
60
|
-
os.makedirs(os.path.dirname(self.file_name), exist_ok=True)
|
|
61
|
-
self.app_title = app_title
|
|
62
|
-
self.chat_history: list = self.load_chat()
|
|
63
|
-
self.agent = generic_agent_builder(self.config_file)
|
|
64
|
-
|
|
65
|
-
def load_chat(self) -> list:
|
|
66
|
-
"""If the file self.file_name exists, it loads
|
|
67
|
-
previous chat history. Otherwise, it creates a new file and
|
|
68
|
-
sets the chat history as an empty list
|
|
69
|
-
|
|
70
|
-
Returns:
|
|
71
|
-
the list of messages or empty list for messages
|
|
72
|
-
"""
|
|
73
|
-
if os.path.exists(self.file_name):
|
|
74
|
-
with open(self.file_name, "r", encoding="utf-8") as chat:
|
|
75
|
-
try:
|
|
76
|
-
history = json.load(chat)
|
|
77
|
-
except json.JSONDecodeError:
|
|
78
|
-
history = []
|
|
79
|
-
else:
|
|
80
|
-
history = []
|
|
81
|
-
with open(self.file_name, "w") as file:
|
|
82
|
-
json.dump(history, file)
|
|
83
|
-
return history
|
|
84
|
-
|
|
85
|
-
def generate_packet(self, message, conv_id: str) -> DataContainer:
|
|
86
|
-
"""
|
|
87
|
-
Args:
|
|
88
|
-
message (str): The user's input message, either as a audio file or text
|
|
89
|
-
conv_id (str): The conversation ID to associate with the message.
|
|
90
|
-
"""
|
|
91
|
-
container = DataContainer()
|
|
92
|
-
if isinstance(message, dict):
|
|
93
|
-
text_msg = message.get(ChatKeys.text, False)
|
|
94
|
-
image_msg = message.get(ChatKeys.files, False)
|
|
95
|
-
if text_msg:
|
|
96
|
-
text_msg = TextPacket(content=text_msg, id=conv_id)
|
|
97
|
-
container.texts.append(text_msg)
|
|
98
|
-
if image_msg:
|
|
99
|
-
full_image_path = image_msg[0].split("/gradio/")
|
|
100
|
-
data_dir = f"{full_image_path[0]}/gradio/"
|
|
101
|
-
image_name = full_image_path[1]
|
|
102
|
-
|
|
103
|
-
self.agent.update_template_attribute("ImageReader", "data_dir", data_dir)
|
|
104
|
-
self.agent.update_template_attribute("ImageReader", "pattern", image_name)
|
|
105
|
-
self.agent.reset_state("ImageReader")
|
|
106
|
-
|
|
107
|
-
if isinstance(message, str):
|
|
108
|
-
if not message.endswith("wav"):
|
|
109
|
-
text_msg = TextPacket(content=message, id=conv_id)
|
|
110
|
-
container.texts.append(text_msg)
|
|
111
|
-
elif message.endswith("wav"):
|
|
112
|
-
container.generic_data[ChatKeys.audio_path] = message
|
|
113
|
-
return container
|
|
114
|
-
|
|
115
|
-
def agent_execution(self, container: DataContainer) -> dict:
|
|
116
|
-
"""
|
|
117
|
-
Makes a call to the agent to retrieve a response from the templates in the agent.
|
|
118
|
-
|
|
119
|
-
Args:
|
|
120
|
-
container (DataContainer): container with the packets to be processed by the
|
|
121
|
-
templates in the agent.
|
|
122
|
-
|
|
123
|
-
Returns:
|
|
124
|
-
dict : The response to be added to the chatbot window
|
|
125
|
-
|
|
126
|
-
"""
|
|
127
|
-
response = self.agent(container)
|
|
128
|
-
response_dict = {
|
|
129
|
-
ChatKeys.text: "Could not process request, please try again",
|
|
130
|
-
ChatKeys.files: [],
|
|
131
|
-
}
|
|
132
|
-
if response.texts:
|
|
133
|
-
response_dict = response.texts[-1].content
|
|
134
|
-
|
|
135
|
-
if container.generic_data.get(ChatKeys.audio_path):
|
|
136
|
-
container.generic_data[ChatKeys.audio_path] = False
|
|
137
|
-
if response.images:
|
|
138
|
-
image_as_packet = response.images[-1]
|
|
139
|
-
image = Image.fromarray(np.uint8(image_as_packet.content))
|
|
140
|
-
image_path = f"{SINAPSIS_CACHE_DIR}{image_as_packet.source}"
|
|
141
|
-
os.makedirs(os.path.dirname(image_path), exist_ok=True)
|
|
142
|
-
|
|
143
|
-
image.save(image_path)
|
|
144
|
-
response_dict = {ChatKeys.files: [image_path]}
|
|
145
|
-
|
|
146
|
-
return response_dict
|
|
147
|
-
|
|
148
|
-
@staticmethod
|
|
149
|
-
def _set_conversation_id(conv_id: str) -> str:
|
|
150
|
-
"""
|
|
151
|
-
Ensure a valid conversation ID is set. If no ID is provided, a new one is
|
|
152
|
-
generated.
|
|
153
|
-
Args:
|
|
154
|
-
conv_id (str): The provided conversation ID.
|
|
155
|
-
Returns:
|
|
156
|
-
str: The valid conversation ID (either provided or generated).
|
|
157
|
-
"""
|
|
158
|
-
if not conv_id:
|
|
159
|
-
conv_id = str(uuid.uuid4())
|
|
160
|
-
return conv_id
|
|
161
|
-
|
|
162
|
-
def add_message(
|
|
163
|
-
self, message: dict | str, role: str
|
|
164
|
-
) -> tuple[gr.components.MultimodalTextbox, gr.components.Audio]:
|
|
165
|
-
"""
|
|
166
|
-
The method adds a user or assistant message to the chat history.
|
|
167
|
-
|
|
168
|
-
Args:
|
|
169
|
-
message (dict | str): The input message, either as text or a dictionary.
|
|
170
|
-
role (str): The role of the message sender, e.g., "user" or "assistant".
|
|
171
|
-
|
|
172
|
-
Returns:
|
|
173
|
-
tuple: A tuple with updated chat history, a Textbox, and an Audio component.
|
|
174
|
-
"""
|
|
175
|
-
if isinstance(message, dict):
|
|
176
|
-
for x in message[ChatKeys.files]:
|
|
177
|
-
self.chat_history.append({ChatKeys.role: role, ChatKeys.content: {ChatKeys.file_path: x}})
|
|
178
|
-
if message.get(ChatKeys.text, False):
|
|
179
|
-
self.chat_history.append({ChatKeys.role: role, ChatKeys.content: message[ChatKeys.text]})
|
|
180
|
-
elif isinstance(message, str) and not message.endswith("wav"):
|
|
181
|
-
self.chat_history.append({ChatKeys.role: role, ChatKeys.content: message})
|
|
182
|
-
else:
|
|
183
|
-
self.chat_history.append({ChatKeys.role: role, ChatKeys.content: {ChatKeys.file_path: message}})
|
|
184
|
-
return (
|
|
185
|
-
gr.MultimodalTextbox(),
|
|
186
|
-
gr.Audio(),
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
def stop_agent(self) -> tuple[dict, dict]:
|
|
190
|
-
"""
|
|
191
|
-
Stop the chatbot's agent and save the chat history to a file.
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
Returns:
|
|
195
|
-
tuple: A tuple with None and Gradio updates for interactivity.
|
|
196
|
-
"""
|
|
197
|
-
with open(self.file_name, "w+", encoding="utf-8") as chat:
|
|
198
|
-
json.dump(self.chat_history, chat, indent=4)
|
|
199
|
-
chat.close()
|
|
200
|
-
self.agent = None
|
|
201
|
-
return gr.update(interactive=False), gr.update(interactive=False)
|
|
202
|
-
|
|
203
|
-
def _clear_history(self) -> None:
|
|
204
|
-
"""
|
|
205
|
-
Clears the chat history and saves it to a file named "chat.txt".
|
|
206
|
-
|
|
207
|
-
This method writes the current chat history to a file in JSON format,
|
|
208
|
-
then clears the history.
|
|
209
|
-
"""
|
|
210
|
-
self.chat_history = []
|
|
211
|
-
try:
|
|
212
|
-
os.remove(self.file_name)
|
|
213
|
-
except FileNotFoundError:
|
|
214
|
-
sinapsis_logger.warning("Chat history file does not exist")
|
|
215
|
-
|
|
216
|
-
def process_msg(self, message: str, conv_id: str) -> tuple[list, gr.MultimodalTextbox, gr.Audio, str] | list:
|
|
217
|
-
"""
|
|
218
|
-
Process a user message and generate a chatbot response.
|
|
219
|
-
|
|
220
|
-
Args:
|
|
221
|
-
message (str): The user's input message.
|
|
222
|
-
conv_id (str): The conversation ID for the current session.
|
|
223
|
-
|
|
224
|
-
Returns:
|
|
225
|
-
tuple: The updated chat history and UI components for the chatbot interface.
|
|
226
|
-
"""
|
|
227
|
-
|
|
228
|
-
conv_id = self._set_conversation_id(conv_id)
|
|
229
|
-
_ = self.add_message(message, ChatKeys.user)
|
|
230
|
-
container = self.generate_packet(message, conv_id)
|
|
231
|
-
response = self.agent_execution(container)
|
|
232
|
-
retrieved_chat = self.add_message(response, ChatKeys.assistant)
|
|
233
|
-
return self.chat_history, retrieved_chat[0], retrieved_chat[1], conv_id
|
|
234
|
-
|
|
235
|
-
def add_app_components(self) -> tuple[gr.Chatbot, gr.MultimodalTextbox, gr.Audio]:
|
|
236
|
-
"""
|
|
237
|
-
Add interactive components (buttons, inputs, and chatbot UI) to the interface.
|
|
238
|
-
|
|
239
|
-
Returns:
|
|
240
|
-
tuple: A tuple containing the components for the chatbot interface.
|
|
241
|
-
"""
|
|
242
|
-
chatbot = gr.Chatbot(
|
|
243
|
-
self.chat_history,
|
|
244
|
-
type="messages",
|
|
245
|
-
height=800,
|
|
246
|
-
show_label=False,
|
|
247
|
-
avatar_images=(None, SINAPSIS_AVATAR),
|
|
248
|
-
show_copy_all_button=True,
|
|
249
|
-
)
|
|
250
|
-
chat_input = gr.MultimodalTextbox(
|
|
251
|
-
interactive=True, placeholder="Enter a message", show_label=False, file_types=[".png", ".jpg"]
|
|
252
|
-
)
|
|
253
|
-
|
|
254
|
-
audio_input = gr.Audio(
|
|
255
|
-
sources=["microphone"],
|
|
256
|
-
type="filepath",
|
|
257
|
-
label="Record Audio",
|
|
258
|
-
interactive=True,
|
|
259
|
-
)
|
|
260
|
-
|
|
261
|
-
return chatbot, chat_input, audio_input
|
|
262
|
-
|
|
263
|
-
def app_interface(self) -> None:
|
|
264
|
-
"""
|
|
265
|
-
Define the core functionality of the chatbot, including initialization,
|
|
266
|
-
message handling, and user interaction.
|
|
267
|
-
"""
|
|
268
|
-
|
|
269
|
-
conversation_id = gr.State()
|
|
270
|
-
(
|
|
271
|
-
chatbot,
|
|
272
|
-
chat_input,
|
|
273
|
-
audio_input,
|
|
274
|
-
) = self.add_app_components()
|
|
275
|
-
|
|
276
|
-
clear_btn = gr.ClearButton([audio_input, chat_input, chatbot, conversation_id])
|
|
277
|
-
clear_btn.click(self._clear_history, inputs=[], outputs=[])
|
|
278
|
-
|
|
279
|
-
audio_input.stop_recording(
|
|
280
|
-
self.process_msg,
|
|
281
|
-
inputs=[audio_input, conversation_id],
|
|
282
|
-
outputs=[chatbot, chat_input, audio_input, conversation_id],
|
|
283
|
-
)
|
|
284
|
-
chat_input.submit(
|
|
285
|
-
self.process_msg,
|
|
286
|
-
inputs=[chat_input, conversation_id],
|
|
287
|
-
outputs=[chatbot, chat_input, audio_input, conversation_id],
|
|
288
|
-
)
|
|
289
|
-
|
|
290
|
-
finish_chatbot = gr.Button(value="Finish chatbot", elem_id="finish-button", variant="primary")
|
|
291
|
-
finish_chatbot.click(
|
|
292
|
-
self.stop_agent,
|
|
293
|
-
inputs=[],
|
|
294
|
-
outputs=[chat_input, audio_input],
|
|
295
|
-
)
|
|
296
|
-
|
|
297
|
-
def __call__(self) -> gr.Blocks:
|
|
298
|
-
with gr.Blocks(css=css_header()) as chatbot_interface:
|
|
299
|
-
add_logo_and_title(self.app_title)
|
|
300
|
-
self.app_interface()
|
|
301
|
-
return chatbot_interface
|
|
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
|