sinapsis 0.2.2__tar.gz → 0.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (21) hide show
  1. {sinapsis-0.2.2 → sinapsis-0.2.3}/PKG-INFO +2 -2
  2. {sinapsis-0.2.2 → sinapsis-0.2.3}/pyproject.toml +2 -2
  3. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/webapp/chatbot_base.py +52 -25
  4. sinapsis-0.2.3/src/sinapsis/webapp/tts_base.py +183 -0
  5. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis.egg-info/PKG-INFO +2 -2
  6. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis.egg-info/SOURCES.txt +2 -1
  7. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis.egg-info/requires.txt +1 -1
  8. {sinapsis-0.2.2 → sinapsis-0.2.3}/LICENSE +0 -0
  9. {sinapsis-0.2.2 → sinapsis-0.2.3}/README.md +0 -0
  10. {sinapsis-0.2.2 → sinapsis-0.2.3}/setup.cfg +0 -0
  11. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/__init__.py +0 -0
  12. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/bin/sinapsis_cli.py +0 -0
  13. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/templates/__init__.py +0 -0
  14. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/templates/display_hello_world.py +0 -0
  15. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/templates/hello_world.py +0 -0
  16. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/webapp/__init__.py +0 -0
  17. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/webapp/agent_gradio_helper.py +0 -0
  18. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis/webapp/agent_webapp_utils.py +0 -0
  19. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis.egg-info/dependency_links.txt +0 -0
  20. {sinapsis-0.2.2 → sinapsis-0.2.3}/src/sinapsis.egg-info/entry_points.txt +0 -0
  21. {sinapsis-0.2.2 → sinapsis-0.2.3}/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.2
3
+ Version: 0.2.3
4
4
  Summary: Modular and Universal AI platform
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  License: GNU AFFERO GENERAL PUBLIC LICENSE
@@ -675,7 +675,7 @@ Requires-Dist: sinapsis-core>=0.1.0
675
675
  Provides-Extra: all
676
676
  Requires-Dist: sinapsis[dev]; extra == "all"
677
677
  Provides-Extra: webapp
678
- Requires-Dist: gradio>=5.23.3; extra == "webapp"
678
+ Requires-Dist: gradio<=5.23; extra == "webapp"
679
679
 
680
680
  <h1 align="center">
681
681
  <br>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sinapsis"
3
- version = "0.2.2"
3
+ version = "0.2.3"
4
4
  description = "Modular and Universal AI platform"
5
5
  authors = [
6
6
  {name = "SinapsisAI", email = "dev@sinapsis.tech"},
@@ -27,7 +27,7 @@ all = [
27
27
  "sinapsis[dev]",
28
28
  ]
29
29
  webapp = [
30
- "gradio>=5.23.3",
30
+ "gradio<=5.23",
31
31
  ]
32
32
 
33
33
 
@@ -4,6 +4,8 @@ import os.path
4
4
  import uuid
5
5
 
6
6
  import gradio as gr
7
+ import numpy as np
8
+ from PIL import Image
7
9
  from pydantic.dataclasses import dataclass
8
10
  from sinapsis_core.cli.run_agent_from_config import generic_agent_builder
9
11
  from sinapsis_core.data_containers.data_packet import DataContainer, TextPacket
@@ -25,6 +27,7 @@ class ChatKeys:
25
27
  """
26
28
 
27
29
  text: str = "text"
30
+ image: str = "image"
28
31
  files: str = "files"
29
32
  file_path: str = "path"
30
33
  audio_path: str = "audio_path"
@@ -79,19 +82,34 @@ class BaseChatbot:
79
82
  json.dump(history, file)
80
83
  return history
81
84
 
82
- @staticmethod
83
- def generate_packet(message, conv_id: str) -> DataContainer:
85
+ def generate_packet(self, message, conv_id: str) -> DataContainer:
84
86
  """
85
87
  Args:
86
88
  message (str): The user's input message, either as a audio file or text
87
89
  conv_id (str): The conversation ID to associate with the message.
88
90
  """
89
91
  container = DataContainer()
90
- if not message.endswith("wav"):
91
- text_msg = TextPacket(content=message, id=conv_id)
92
- container.texts.append(text_msg)
93
- else:
94
- container.generic_data[ChatKeys.audio_path] = message
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=message, 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
95
113
  return container
96
114
 
97
115
  def agent_execution(self, container: DataContainer) -> dict:
@@ -111,14 +129,20 @@ class BaseChatbot:
111
129
  ChatKeys.text: "Could not process request, please try again",
112
130
  ChatKeys.files: [],
113
131
  }
114
-
115
132
  if response.texts:
116
- response_dict = {
117
- ChatKeys.text: response.texts[-1].content,
118
- ChatKeys.files: [],
119
- }
133
+ response_dict = response.texts[-1].content
134
+
120
135
  if container.generic_data.get(ChatKeys.audio_path):
121
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
+
122
146
  return response_dict
123
147
 
124
148
  @staticmethod
@@ -135,7 +159,9 @@ class BaseChatbot:
135
159
  conv_id = str(uuid.uuid4())
136
160
  return conv_id
137
161
 
138
- def add_message(self, message: dict | str, role: str) -> tuple[gr.components.Textbox, gr.components.Audio]:
162
+ def add_message(
163
+ self, message: dict | str, role: str
164
+ ) -> tuple[gr.components.MultimodalTextbox, gr.components.Audio]:
139
165
  """
140
166
  The method adds a user or assistant message to the chat history.
141
167
 
@@ -146,19 +172,18 @@ class BaseChatbot:
146
172
  Returns:
147
173
  tuple: A tuple with updated chat history, a Textbox, and an Audio component.
148
174
  """
149
-
150
175
  if isinstance(message, dict):
151
176
  for x in message[ChatKeys.files]:
152
177
  self.chat_history.append({ChatKeys.role: role, ChatKeys.content: {ChatKeys.file_path: x}})
153
- if message[ChatKeys.text]:
178
+ if message.get(ChatKeys.text, False):
154
179
  self.chat_history.append({ChatKeys.role: role, ChatKeys.content: message[ChatKeys.text]})
155
180
  elif isinstance(message, str) and not message.endswith("wav"):
156
181
  self.chat_history.append({ChatKeys.role: role, ChatKeys.content: message})
157
182
  else:
158
183
  self.chat_history.append({ChatKeys.role: role, ChatKeys.content: {ChatKeys.file_path: message}})
159
184
  return (
160
- gr.Textbox(value=None),
161
- gr.Audio(value=None),
185
+ gr.MultimodalTextbox(),
186
+ gr.Audio(),
162
187
  )
163
188
 
164
189
  def stop_agent(self) -> tuple[dict, dict]:
@@ -188,7 +213,7 @@ class BaseChatbot:
188
213
  except FileNotFoundError:
189
214
  sinapsis_logger.warning("Chat history file does not exist")
190
215
 
191
- def process_msg(self, message: str, conv_id: str) -> tuple[list, gr.Textbox, gr.Audio, str] | list:
216
+ def process_msg(self, message: str, conv_id: str) -> tuple[list, gr.MultimodalTextbox, gr.Audio, str] | list:
192
217
  """
193
218
  Process a user message and generate a chatbot response.
194
219
 
@@ -204,11 +229,10 @@ class BaseChatbot:
204
229
  _ = self.add_message(message, ChatKeys.user)
205
230
  container = self.generate_packet(message, conv_id)
206
231
  response = self.agent_execution(container)
207
-
208
232
  retrieved_chat = self.add_message(response, ChatKeys.assistant)
209
233
  return self.chat_history, retrieved_chat[0], retrieved_chat[1], conv_id
210
234
 
211
- def add_app_components(self) -> tuple[gr.Chatbot, gr.Textbox, gr.Audio]:
235
+ def add_app_components(self) -> tuple[gr.Chatbot, gr.MultimodalTextbox, gr.Audio]:
212
236
  """
213
237
  Add interactive components (buttons, inputs, and chatbot UI) to the interface.
214
238
 
@@ -216,12 +240,15 @@ class BaseChatbot:
216
240
  tuple: A tuple containing the components for the chatbot interface.
217
241
  """
218
242
  chatbot = gr.Chatbot(
219
- self.chat_history, type="messages", height=600, show_label=False, avatar_images=(None, SINAPSIS_AVATAR)
220
- )
221
- chat_input = gr.Textbox(
222
- interactive=True,
223
- placeholder="Enter a message",
243
+ self.chat_history,
244
+ type="messages",
245
+ height=800,
224
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"]
225
252
  )
226
253
 
227
254
  audio_input = gr.Audio(
@@ -0,0 +1,183 @@
1
+ # -*- coding: utf-8 -*-
2
+ from typing import cast
3
+
4
+ import gradio as gr
5
+ from sinapsis_core.agent import Agent
6
+ from sinapsis_core.cli.run_agent_from_config import generic_agent_builder
7
+ from sinapsis_core.data_containers.data_packet import DataContainer, TextPacket
8
+
9
+ from sinapsis.webapp.agent_gradio_helper import add_logo_and_title
10
+
11
+
12
+ class BaseTTSApp:
13
+ """
14
+ A base class for building text-to-speech (TTS) applications using various configurations.
15
+ """
16
+
17
+ def __init__(
18
+ self,
19
+ config_file: str,
20
+ generic_key: str | None = None,
21
+ framework: str = "Elevenlabs",
22
+ task: str = "Text-to-Speech",
23
+ ) -> None:
24
+ """
25
+ Initializes the TTS (Text-to-Speech) application with a given configuration file, framework, and task settings.
26
+
27
+ Args:
28
+ config_file (str): Path to the configuration file for the TTS application, which contains
29
+ necessary settings and parameters.
30
+ generic_key (str | None): Optional key to access the container's generic data from.
31
+ framework (str, optional): The name of the framework to be used in the user interface title.
32
+ Defaults to Elevenlabs.
33
+ task (str, optional): The task to be performed by the application (default is "Text-to-Speech").
34
+
35
+ """
36
+ self.config_file = config_file
37
+ self.framework = framework
38
+ self.task = task
39
+ self.generic_key = generic_key
40
+
41
+ def init_agent(self) -> tuple[Agent, bool]:
42
+ """
43
+ Initializes the agent using the configuration file.
44
+
45
+ Returns:
46
+ tuple: Updates agent_state and initialized_state.
47
+ """
48
+ return generic_agent_builder(self.config_file), True
49
+
50
+ def _postprocess_output(self, container: DataContainer, generic_key: str | None) -> str | None:
51
+ """
52
+ Abstract method for postprocessing the agent's output to extract the audio path.
53
+
54
+ Args:
55
+ container (DataContainer): The container object returned by the agent after processing.
56
+ generic_key (str): An optional key used for accessing specific data or configurations
57
+ related to the TTS agent. If not provided, the key defaults to None.
58
+
59
+ Returns:
60
+ str | None: The path to the generated audio file, or None if the output is invalid.
61
+ """
62
+
63
+ def text_to_speech(
64
+ self, initialized: bool, agent: Agent | None, text_to_convert: str
65
+ ) -> tuple[gr.Audio | None, str | None]:
66
+ """
67
+ Converts text to speech by passing the text through the agent and postprocessing steps.
68
+ If models has not been initialized, only a text hint is returned.
69
+
70
+ Args:
71
+ initialized (bool): Whether the agent has been initialized.
72
+ agent (object): The agent object generated using `generic_agent_builder`.
73
+ text_to_convert (str): The text to be converted to speech.
74
+
75
+ Returns:
76
+ tuple: Updates for audio_generated and status_msg.
77
+
78
+ Raises:
79
+ gr.Error: If the speech generation fails.
80
+ """
81
+ if initialized:
82
+ agent = cast(Agent, agent)
83
+ container = DataContainer(texts=[TextPacket(content=text_to_convert)])
84
+ output_container = agent(container)
85
+ audio_path = self._postprocess_output(output_container, self.generic_key)
86
+ if audio_path:
87
+ return gr.Audio(audio_path, visible=True), None
88
+ raise gr.Error("Unable to generate speech")
89
+ return None, "#### Model not ready! Please wait..."
90
+
91
+ @staticmethod
92
+ def update_status_msg() -> str:
93
+ """Updates status_text to indicate that the model is ready."""
94
+ return "#### Model ready. Click on the box the text you wish to convert to speech and submit to generate!"
95
+
96
+ def inner_tts_functionality(self, interface: gr.Interface) -> None:
97
+ """
98
+ Defines the inner functionality of the TTS app interface using Gradio components.
99
+ """
100
+ agent_state = gr.State()
101
+ initialized_state = gr.State(False)
102
+ interface.load(self.init_agent, outputs=[agent_state, initialized_state])
103
+
104
+ title: str = f"# Sinapsis {self.framework} {self.task} demo"
105
+ gr.Markdown(title)
106
+ text_to_convert = gr.Textbox(
107
+ submit_btn=True,
108
+ label="Text to convert:",
109
+ value="The Sinapsis platform aims to democratize artificial intelligence by offering "
110
+ + "a flexible template system that ensures modularity and compatibility between models and tools widely "
111
+ + "used by the community.",
112
+ )
113
+ status_msg = gr.Markdown("#### Initializing model...")
114
+ initialized_state.change(self.update_status_msg, outputs=[status_msg])
115
+
116
+ audio_generated = gr.Audio(label="Audio generated:", visible=False)
117
+
118
+ text_to_convert.submit(
119
+ self.text_to_speech,
120
+ inputs=[initialized_state, agent_state, text_to_convert],
121
+ outputs=[audio_generated, status_msg],
122
+ )
123
+
124
+ def __call__(self) -> gr.Blocks:
125
+ """
126
+ Invokes the Gradio interface and displays the TTS functionality.
127
+ """
128
+ with gr.Blocks() as tts_interface:
129
+ add_logo_and_title("Sinapsis text-to-speech")
130
+ self.inner_tts_functionality(tts_interface)
131
+ return tts_interface
132
+
133
+
134
+ class TTSAppAudioFromPacket(BaseTTSApp):
135
+ BASE_AUDIO_PATH: str
136
+ """
137
+ A subclass of BaseTTSApp for implementing text-to-speech functionality using F5TTS.
138
+
139
+ This class overrides the abstract methods from BaseTTSApp to provide specific preprocessing
140
+ and postprocessing steps for the F5TTS application.
141
+ """
142
+
143
+ def _postprocess_output(self, container: DataContainer, generic_key: str | None) -> str | None:
144
+ """
145
+ Postprocesses the output of the TTS agent to extract the audio file path.
146
+ """
147
+ _ = generic_key
148
+
149
+ audio = container.audios[-1]
150
+ audio_path = f"{audio.source}-{audio.id.split('-')[0]}"
151
+ audio_path = f"{self.BASE_AUDIO_PATH}/{audio_path}.wav"
152
+ return audio_path
153
+
154
+
155
+ class TTSAppAudioFromGeneric(BaseTTSApp):
156
+ """
157
+ A subclass of BaseTTSApp for implementing text-to-speech functionality using ElevenLabs.
158
+
159
+ This class overrides the abstract methods from BaseTTSApp to provide specific preprocessing
160
+ and postprocessing steps for the ElevenLabs TTS application.
161
+ """
162
+
163
+ def _postprocess_output(self, container: DataContainer, generic_key: str | None) -> str | None:
164
+ """
165
+ Post-processes the output of the ElevenLabs TTS agent to extract the audio file path.
166
+
167
+ This method checks the `generic_data` of the provided container to find the audio file path
168
+ associated with the given `generic_key`. If the key is found and the audio path exists,
169
+ it is returned. Otherwise, `None` is returned.
170
+
171
+ Args:
172
+ container (DataContainer): The container returned by the TTS agent containing the output
173
+ data, including the audio file path stored under the `generic_key`.
174
+ generic_key (str | None): An optional key used to retrieve the audio path from the `generic_data`
175
+ of the container.
176
+
177
+ Returns:
178
+ str | None: The path to the generated audio file if the key exists and contains the audio path;
179
+ otherwise, None if no valid path is found.
180
+ """
181
+ _ = self
182
+ audio_path = container.generic_data.get(generic_key, None)
183
+ return audio_path[0] if audio_path else audio_path
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sinapsis
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Modular and Universal AI platform
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  License: GNU AFFERO GENERAL PUBLIC LICENSE
@@ -675,7 +675,7 @@ Requires-Dist: sinapsis-core>=0.1.0
675
675
  Provides-Extra: all
676
676
  Requires-Dist: sinapsis[dev]; extra == "all"
677
677
  Provides-Extra: webapp
678
- Requires-Dist: gradio>=5.23.3; extra == "webapp"
678
+ Requires-Dist: gradio<=5.23; extra == "webapp"
679
679
 
680
680
  <h1 align="center">
681
681
  <br>
@@ -15,4 +15,5 @@ src/sinapsis/templates/hello_world.py
15
15
  src/sinapsis/webapp/__init__.py
16
16
  src/sinapsis/webapp/agent_gradio_helper.py
17
17
  src/sinapsis/webapp/agent_webapp_utils.py
18
- src/sinapsis/webapp/chatbot_base.py
18
+ src/sinapsis/webapp/chatbot_base.py
19
+ src/sinapsis/webapp/tts_base.py
@@ -4,4 +4,4 @@ sinapsis-core>=0.1.0
4
4
  sinapsis[dev]
5
5
 
6
6
  [webapp]
7
- gradio>=5.23.3
7
+ gradio<=5.23
File without changes
File without changes
File without changes