lollms-client 1.4.5__py3-none-any.whl → 1.4.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of lollms-client might be problematic. Click here for more details.

lollms_client/__init__.py CHANGED
@@ -8,7 +8,7 @@ from lollms_client.lollms_utilities import PromptReshaper # Keep general utiliti
8
8
  from lollms_client.lollms_mcp_binding import LollmsMCPBinding, LollmsMCPBindingManager
9
9
  from lollms_client.lollms_llm_binding import LollmsLLMBindingManager
10
10
 
11
- __version__ = "1.4.5" # Updated version
11
+ __version__ = "1.4.7" # Updated version
12
12
 
13
13
  # Optionally, you could define __all__ if you want to be explicit about exports
14
14
  __all__ = [
@@ -1,3 +1,6 @@
1
+ #lollms_client/lollms_discussion.py
2
+ #author : ParisNeo
3
+
1
4
  import base64
2
5
  import json
3
6
  import re
@@ -1000,6 +1003,13 @@ class LollmsDiscussion:
1000
1003
  self._rebuild_message_index() # Ensure index is fresh
1001
1004
  return [LollmsMessage(self, msg_obj) for msg_obj in self._message_index.values()]
1002
1005
 
1006
+ def setMemory(self, memory:str):
1007
+ """sets memory content
1008
+
1009
+ Args:
1010
+ memory (str): _description_
1011
+ """
1012
+ self.memory = memory
1003
1013
 
1004
1014
  def get_full_data_zone(self):
1005
1015
  """Assembles all data zones into a single, formatted string for the prompt."""
@@ -2585,4 +2595,4 @@ class LollmsDiscussion:
2585
2595
  if db_manager:
2586
2596
  new_discussion.commit()
2587
2597
 
2588
- return new_discussion
2598
+ return new_discussion
@@ -84,7 +84,10 @@ class LollmsPersonality:
84
84
  """
85
85
  Creates a filesystem-safe, unique ID based on the author and name.
86
86
  """
87
- safe_author = "".join(c if c.isalnum() else '_' for c in self.author)
87
+ if self.author:
88
+ safe_author = "".join(c if c.isalnum() else '_' for c in self.author)
89
+ else:
90
+ safe_author = "".join(c if c.isalnum() else '_' for c in "ParisNeo")
88
91
  safe_name = "".join(c if c.isalnum() else '_' for c in self.name)
89
92
  return f"{safe_author}_{safe_name}"
90
93
 
@@ -187,4 +190,4 @@ class LollmsPersonality:
187
190
  "has_data_source": self.data_source is not None,
188
191
  "data_files": [str(p) for p in self.data_files],
189
192
  "has_script": self.script is not None
190
- }
193
+ }
@@ -0,0 +1,125 @@
1
+ import os
2
+ import requests
3
+ import time
4
+ from pathlib import Path
5
+ from typing import Optional, List, Dict, Any
6
+
7
+ from lollms_client.lollms_ttm_binding import LollmsTTMBinding
8
+ from ascii_colors import trace_exception, ASCIIColors
9
+ import pipmaster as pm
10
+
11
+ # Ensure required packages are installed
12
+ pm.ensure_packages(["requests"])
13
+
14
+ BindingName = "BeatovenAITTMBinding"
15
+
16
+ class BeatovenAITTMBinding(LollmsTTMBinding):
17
+ """A Text-to-Music binding for the Beatoven.ai API."""
18
+
19
+ def __init__(self, **kwargs):
20
+ super().__init__(binding_name=BindingName, **kwargs)
21
+ self.api_key = self.settings.get("api_key") or os.environ.get("BEATOVEN_API_KEY")
22
+ if not self.api_key:
23
+ raise ValueError("Beatoven.ai API key is required. Please set it in config or as BEATOVEN_API_KEY env var.")
24
+ self.base_url = "https://api.beatoven.ai/api/v1"
25
+ self.headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
26
+
27
+ def list_models(self, **kwargs) -> List[str]:
28
+ # Beatoven.ai does not expose different models via the API.
29
+ # Customization is done via genre, mood, and tempo.
30
+ return ["default"]
31
+
32
+ def _poll_for_completion(self, task_id: str) -> Dict[str, Any]:
33
+ """Polls the tasks endpoint until the composition is complete."""
34
+ poll_url = f"{self.base_url}/tasks/{task_id}"
35
+ while True:
36
+ try:
37
+ response = requests.get(poll_url, headers=self.headers)
38
+ response.raise_for_status()
39
+ data = response.json()
40
+ status = data.get("status")
41
+
42
+ if status == "success":
43
+ ASCIIColors.green("Composition task successful.")
44
+ return data
45
+ elif status == "failed":
46
+ error_info = data.get("error", "Unknown error.")
47
+ raise Exception(f"Beatoven.ai task failed: {error_info}")
48
+ else:
49
+ ASCIIColors.info(f"Task status is '{status}'. Waiting...")
50
+ time.sleep(5)
51
+ except requests.exceptions.HTTPError as e:
52
+ raise Exception(f"Failed to poll task status: {e.response.text}")
53
+
54
+ def generate_music(self, prompt: str, **kwargs) -> bytes:
55
+ """
56
+ Generates music by creating a track, waiting for composition, and downloading the result.
57
+ """
58
+ # Step 1: Create a track
59
+ create_track_url = f"{self.base_url}/tracks"
60
+ payload = {
61
+ "title": prompt[:100], # Use prompt as title, truncated
62
+ "duration_in_seconds": kwargs.get("duration", 30),
63
+ "genre": kwargs.get("genre", "Cinematic"),
64
+ "tempo": kwargs.get("tempo", "medium"),
65
+ "prompt": prompt
66
+ }
67
+
68
+ try:
69
+ ASCIIColors.info("Submitting music track request to Beatoven.ai...")
70
+ create_response = requests.post(create_track_url, json=payload, headers=self.headers)
71
+ create_response.raise_for_status()
72
+ task_id = create_response.json().get("task_id")
73
+ ASCIIColors.info(f"Track creation submitted. Task ID: {task_id}")
74
+
75
+ # Step 2: Poll for task completion
76
+ task_result = self._poll_for_completion(task_id)
77
+ track_id = task_result.get("track_id")
78
+ if not track_id:
79
+ raise Exception("Task completed but did not return a track_id.")
80
+
81
+ # Step 3: Get track details to find the audio URL
82
+ track_url = f"{self.base_url}/tracks/{track_id}"
83
+ track_response = requests.get(track_url, headers=self.headers)
84
+ track_response.raise_for_status()
85
+
86
+ audio_url = track_response.json().get("renders", {}).get("wav")
87
+ if not audio_url:
88
+ raise Exception("Could not find WAV render URL in the completed track details.")
89
+
90
+ # Step 4: Download the audio file
91
+ ASCIIColors.info(f"Downloading generated audio from {audio_url}")
92
+ audio_response = requests.get(audio_url)
93
+ audio_response.raise_for_status()
94
+
95
+ return audio_response.content
96
+
97
+ except requests.exceptions.HTTPError as e:
98
+ error_details = e.response.json()
99
+ raise Exception(f"Beatoven.ai API HTTP Error: {error_details}") from e
100
+ except Exception as e:
101
+ trace_exception(e)
102
+ raise
103
+
104
+ if __name__ == '__main__':
105
+ ASCIIColors.magenta("--- Beatoven.ai TTM Binding Test ---")
106
+ if "BEATOVEN_API_KEY" not in os.environ:
107
+ ASCIIColors.error("BEATOVEN_API_KEY environment variable not set. Cannot run test.")
108
+ exit(1)
109
+
110
+ try:
111
+ binding = BeatovenAITTMBinding()
112
+
113
+ ASCIIColors.cyan("\n--- Test: Music Generation ---")
114
+ prompt = "A mysterious and suspenseful cinematic track with soft piano and eerie strings, building tension."
115
+ music_bytes = binding.generate_music(prompt, duration=45, genre="Cinematic", tempo="slow")
116
+
117
+ assert len(music_bytes) > 1000, "Generated music bytes are too small."
118
+ output_path = Path(__file__).parent / "tmp_beatoven_music.wav"
119
+ with open(output_path, "wb") as f:
120
+ f.write(music_bytes)
121
+ ASCIIColors.green(f"Music generation OK. Audio saved to {output_path}")
122
+
123
+ except Exception as e:
124
+ trace_exception(e)
125
+ ASCIIColors.error(f"Beatoven.ai TTM binding test failed: {e}")
@@ -0,0 +1,112 @@
1
+ import os
2
+ import requests
3
+ import base64
4
+ from pathlib import Path
5
+ from typing import Optional, List, Dict, Any
6
+
7
+ from lollms_client.lollms_ttm_binding import LollmsTTMBinding
8
+ from ascii_colors import trace_exception, ASCIIColors
9
+ import pipmaster as pm
10
+
11
+ # Ensure required packages are installed
12
+ pm.ensure_packages(["requests"])
13
+
14
+ BindingName = "StabilityAITTMBinding"
15
+
16
+ # Models available via the Stability AI Audio API
17
+ # Sourced from: https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v1~1generation~1stable-audio-2.0~1text-to-audio/post
18
+ STABILITY_AI_MODELS = [
19
+ {"model_name": "stable-audio-2.0", "display_name": "Stable Audio 2.0", "description": "High-quality, full-track music generation up to 3 minutes."},
20
+ {"model_name": "stable-audio-1.0", "display_name": "Stable Audio 1.0", "description": "Original model, best for short clips and sound effects."},
21
+ ]
22
+
23
+ class StabilityAITTMBinding(LollmsTTMBinding):
24
+ """A Text-to-Music binding for Stability AI's Stable Audio API."""
25
+
26
+ def __init__(self, **kwargs):
27
+ super().__init__(binding_name=BindingName, **kwargs)
28
+ self.api_key = self.settings.get("api_key") or os.environ.get("STABILITY_API_KEY")
29
+ if not self.api_key:
30
+ raise ValueError("Stability AI API key is required. Please set it in the configuration or as STABILITY_API_KEY environment variable.")
31
+ self.model_name = self.settings.get("model_name", "stable-audio-2.0")
32
+
33
+ def list_models(self, **kwargs) -> List[Dict[str, str]]:
34
+ return STABILITY_AI_MODELS
35
+
36
+ def generate_music(self, prompt: str, **kwargs) -> bytes:
37
+ """
38
+ Generates music using the Stable Audio API.
39
+
40
+ Args:
41
+ prompt (str): The text prompt describing the desired music.
42
+ duration (int): The duration of the audio in seconds. Defaults to 29.
43
+ **kwargs: Additional parameters for the API.
44
+
45
+ Returns:
46
+ bytes: The generated audio data in WAV format.
47
+ """
48
+ url = f"https://api.stability.ai/v1/generation/{self.model_name}/text-to-audio"
49
+ headers = {
50
+ "Authorization": f"Bearer {self.api_key}",
51
+ "Accept": "audio/wav",
52
+ }
53
+
54
+ # Get duration, with a default of 29 seconds as it's a common value
55
+ duration = kwargs.get("duration", 29)
56
+
57
+ payload = {
58
+ "text_prompts[0][text]": prompt,
59
+ "text_prompts[0][weight]": 1.0,
60
+ "seed": kwargs.get("seed", 0), # 0 for random in API
61
+ "steps": kwargs.get("steps", 100),
62
+ "cfg_scale": kwargs.get("cfg_scale", 7.0),
63
+ }
64
+
65
+ # Handle different parameter names for duration
66
+ if self.model_name == "stable-audio-2.0":
67
+ payload["duration"] = duration
68
+ else: # stable-audio-1.0
69
+ payload["sample_length"] = duration * 44100 # v1 uses sample length
70
+
71
+ try:
72
+ ASCIIColors.info(f"Requesting music from Stability AI ({self.model_name})...")
73
+ response = requests.post(url, headers=headers, data=payload)
74
+ response.raise_for_status()
75
+
76
+ ASCIIColors.green("Successfully generated music from Stability AI.")
77
+ return response.content
78
+ except requests.exceptions.HTTPError as e:
79
+ try:
80
+ error_details = e.response.json()
81
+ error_message = error_details.get("message", e.response.text)
82
+ except:
83
+ error_message = e.response.text
84
+ ASCIIColors.error(f"HTTP Error from Stability AI: {e.response.status_code} - {error_message}")
85
+ raise Exception(f"Stability AI API Error: {error_message}") from e
86
+ except Exception as e:
87
+ trace_exception(e)
88
+ raise Exception(f"An unexpected error occurred: {e}")
89
+
90
+ if __name__ == '__main__':
91
+ ASCIIColors.magenta("--- Stability AI TTM Binding Test ---")
92
+ if "STABILITY_API_KEY" not in os.environ:
93
+ ASCIIColors.error("STABILITY_API_KEY environment variable not set. Cannot run test.")
94
+ exit(1)
95
+
96
+ try:
97
+ # Test with default settings
98
+ binding = StabilityAITTMBinding()
99
+
100
+ ASCIIColors.cyan("\n--- Test: Music Generation ---")
101
+ prompt = "80s synthwave, retro futuristic, driving beat, cinematic"
102
+ music_bytes = binding.generate_music(prompt, duration=10)
103
+
104
+ assert len(music_bytes) > 1000, "Generated music bytes are too small."
105
+ output_path = Path(__file__).parent / "tmp_stability_music.wav"
106
+ with open(output_path, "wb") as f:
107
+ f.write(music_bytes)
108
+ ASCIIColors.green(f"Music generation OK. Audio saved to {output_path}")
109
+
110
+ except Exception as e:
111
+ trace_exception(e)
112
+ ASCIIColors.error(f"Stability AI TTM binding test failed: {e}")
@@ -0,0 +1,114 @@
1
+ import os
2
+ import requests
3
+ import time
4
+ from pathlib import Path
5
+ from typing import Optional, List, Dict, Any
6
+
7
+ from lollms_client.lollms_ttm_binding import LollmsTTMBinding
8
+ from ascii_colors import trace_exception, ASCIIColors
9
+ import pipmaster as pm
10
+
11
+ # Ensure required packages are installed
12
+ pm.ensure_packages(["requests"])
13
+
14
+ BindingName = "ReplicateTTMBinding"
15
+
16
+ # Popular music models available on Replicate
17
+ # Sourced from: https://replicate.com/collections/text-to-music
18
+ REPLICATE_MODELS = [
19
+ {"model_name": "meta/musicgen:b05b1dff1d8c6ac63d42422dd565e23b63869bf2d51acda751e04b5dd304535d", "display_name": "Meta - MusicGen", "description": "State-of-the-art controllable text-to-music model from Meta."},
20
+ {"model_name": "suno-ai/bark:b76242b40d67c76ab6742e987628a2a9ac019e11d56ab96c4e91ce03b79b2787", "display_name": "Suno - Bark", "description": "Text-to-audio model capable of music, voice, and sound effects."},
21
+ {"model_name": "joehoover/musicgen-melody:7a76a8258b23fae65c5a24debbe88414f9bed22c2422a63465731103f6990803", "display_name": "MusicGen Melody", "description": "MusicGen fine-tuned for generating melodies."},
22
+ ]
23
+
24
+ class ReplicateTTMBinding(LollmsTTMBinding):
25
+ """A Text-to-Music binding for models hosted on Replicate."""
26
+
27
+ def __init__(self, **kwargs):
28
+ super().__init__(binding_name=BindingName, **kwargs)
29
+ self.api_key = self.settings.get("api_key") or os.environ.get("REPLICATE_API_TOKEN")
30
+ if not self.api_key:
31
+ raise ValueError("Replicate API token is required. Please set it in config or as REPLICATE_API_TOKEN env var.")
32
+ self.model_version = self.settings.get("model_name", "meta/musicgen:b05b1dff1d8c6ac63d42422dd565e23b63869bf2d51acda751e04b5dd304535d")
33
+ self.base_url = "https://api.replicate.com/v1"
34
+ self.headers = {"Authorization": f"Token {self.api_key}", "Content-Type": "application/json"}
35
+
36
+ def list_models(self, **kwargs) -> List[Dict[str, str]]:
37
+ return REPLICATE_MODELS
38
+
39
+ def generate_music(self, prompt: str, **kwargs) -> bytes:
40
+ """
41
+ Generates music via Replicate by starting a prediction and polling for the result.
42
+ """
43
+ model_id, version_id = self.model_version.split(":")
44
+
45
+ payload = {
46
+ "version": version_id,
47
+ "input": {
48
+ "prompt": prompt,
49
+ "duration": kwargs.get("duration", 8),
50
+ "temperature": kwargs.get("temperature", 1.0),
51
+ "top_p": kwargs.get("top_p", 0.9),
52
+ # Add other model-specific parameters here
53
+ }
54
+ }
55
+
56
+ try:
57
+ # 1. Start the prediction
58
+ ASCIIColors.info(f"Submitting music generation job to Replicate ({model_id})...")
59
+ start_response = requests.post(f"{self.base_url}/predictions", json=payload, headers=self.headers)
60
+ start_response.raise_for_status()
61
+ job_data = start_response.json()
62
+ get_url = job_data["urls"]["get"]
63
+ ASCIIColors.info(f"Job submitted. Polling for results at: {get_url}")
64
+
65
+ # 2. Poll for the result
66
+ while True:
67
+ poll_response = requests.get(get_url, headers=self.headers)
68
+ poll_response.raise_for_status()
69
+ poll_data = poll_response.json()
70
+ status = poll_data["status"]
71
+
72
+ if status == "succeeded":
73
+ ASCIIColors.green("Generation successful!")
74
+ output_url = poll_data["output"]
75
+ # Download the resulting audio file
76
+ audio_response = requests.get(output_url)
77
+ audio_response.raise_for_status()
78
+ return audio_response.content
79
+ elif status in ["starting", "processing"]:
80
+ ASCIIColors.info(f"Job status: {status}. Waiting...")
81
+ time.sleep(3)
82
+ else: # failed, canceled
83
+ error_log = poll_data.get("logs", "No logs available.")
84
+ raise Exception(f"Replicate job failed with status '{status}'. Log: {error_log}")
85
+
86
+ except requests.exceptions.HTTPError as e:
87
+ error_details = e.response.json().get("detail", e.response.text)
88
+ raise Exception(f"Replicate API HTTP Error: {error_details}") from e
89
+ except Exception as e:
90
+ trace_exception(e)
91
+ raise
92
+
93
+ if __name__ == '__main__':
94
+ ASCIIColors.magenta("--- Replicate TTM Binding Test ---")
95
+ if "REPLICATE_API_TOKEN" not in os.environ:
96
+ ASCIIColors.error("REPLICATE_API_TOKEN environment variable not set. Cannot run test.")
97
+ exit(1)
98
+
99
+ try:
100
+ binding = ReplicateTTMBinding()
101
+
102
+ ASCIIColors.cyan("\n--- Test: Music Generation with MusicGen ---")
103
+ prompt = "An epic cinematic orchestral piece, with soaring strings and dramatic percussion, fit for a movie trailer"
104
+ music_bytes = binding.generate_music(prompt, duration=10)
105
+
106
+ assert len(music_bytes) > 1000, "Generated music bytes are too small."
107
+ output_path = Path(__file__).parent / "tmp_replicate_music.wav"
108
+ with open(output_path, "wb") as f:
109
+ f.write(music_bytes)
110
+ ASCIIColors.green(f"Music generation OK. Audio saved to {output_path}")
111
+
112
+ except Exception as e:
113
+ trace_exception(e)
114
+ ASCIIColors.error(f"Replicate TTM binding test failed: {e}")
@@ -0,0 +1,93 @@
1
+ import os
2
+ import requests
3
+ from pathlib import Path
4
+ from typing import Optional, List, Dict, Any
5
+
6
+ from lollms_client.lollms_ttm_binding import LollmsTTMBinding
7
+ from ascii_colors import trace_exception, ASCIIColors
8
+ import pipmaster as pm
9
+
10
+ # Ensure required packages are installed
11
+ pm.ensure_packages(["requests"])
12
+
13
+ BindingName = "TopMediaiTTMBinding"
14
+
15
+ class TopMediaiTTMBinding(LollmsTTMBinding):
16
+ """A Text-to-Music binding for the TopMediai API."""
17
+
18
+ def __init__(self, **kwargs):
19
+ super().__init__(binding_name=BindingName, **kwargs)
20
+ self.api_key = self.settings.get("api_key") or os.environ.get("TOPMEDIAI_API_KEY")
21
+ if not self.api_key:
22
+ raise ValueError("TopMediai API key is required. Please set it in config or as TOPMEDIAI_API_KEY env var.")
23
+ self.base_url = "https://api.topmediai.com/v1"
24
+ self.headers = {"x-api-key": self.api_key, "Content-Type": "application/json"}
25
+
26
+ def list_models(self, **kwargs) -> List[str]:
27
+ # The API does not provide a list of selectable models.
28
+ # It's a single, prompt-based system.
29
+ return ["default"]
30
+
31
+ def generate_music(self, prompt: str, **kwargs) -> bytes:
32
+ """
33
+ Generates music using the TopMediai synchronous API.
34
+ """
35
+ url = f"{self.base_url}/music"
36
+ duration = kwargs.get("duration", 30)
37
+
38
+ payload = {
39
+ "text": prompt,
40
+ "duration": f"{duration}", # API expects duration as a string
41
+ }
42
+
43
+ try:
44
+ ASCIIColors.info("Requesting music from TopMediai...")
45
+ response = requests.post(url, json=payload, headers=self.headers)
46
+ response.raise_for_status()
47
+ data = response.json()
48
+
49
+ if data.get("code") != 0:
50
+ raise Exception(f"TopMediai API returned an error: {data.get('message', 'Unknown error')}")
51
+
52
+ audio_url = data.get("data", {}).get("music_url")
53
+ if not audio_url:
54
+ raise Exception("API response did not contain a music URL.")
55
+
56
+ ASCIIColors.info(f"Downloading generated audio from {audio_url}")
57
+ audio_response = requests.get(audio_url)
58
+ audio_response.raise_for_status()
59
+
60
+ return audio_response.content
61
+
62
+ except requests.exceptions.HTTPError as e:
63
+ try:
64
+ error_details = e.response.json()
65
+ raise Exception(f"TopMediai API HTTP Error: {error_details}") from e
66
+ except:
67
+ raise Exception(f"TopMediai API HTTP Error: {e.response.text}") from e
68
+ except Exception as e:
69
+ trace_exception(e)
70
+ raise
71
+
72
+ if __name__ == '__main__':
73
+ ASCIIColors.magenta("--- TopMediai TTM Binding Test ---")
74
+ if "TOPMEDIAI_API_KEY" not in os.environ:
75
+ ASCIIColors.error("TOPMEDIAI_API_KEY environment variable not set. Cannot run test.")
76
+ exit(1)
77
+
78
+ try:
79
+ binding = TopMediaiTTMBinding()
80
+
81
+ ASCIIColors.cyan("\n--- Test: Music Generation ---")
82
+ prompt = "lo-fi hip hop beat, chill, relaxing, perfect for studying"
83
+ music_bytes = binding.generate_music(prompt, duration=30)
84
+
85
+ assert len(music_bytes) > 1000, "Generated music bytes are too small."
86
+ output_path = Path(__file__).parent / "tmp_topmediai_music.mp3"
87
+ with open(output_path, "wb") as f:
88
+ f.write(music_bytes)
89
+ ASCIIColors.green(f"Music generation OK. Audio saved to {output_path}")
90
+
91
+ except Exception as e:
92
+ trace_exception(e)
93
+ ASCIIColors.error(f"TopMediai TTM binding test failed: {e}")
@@ -1,9 +1,210 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lollms_client
3
- Version: 1.4.5
3
+ Version: 1.4.7
4
4
  Summary: A client library for LoLLMs generate endpoint
5
5
  Author-email: ParisNeo <parisneoai@gmail.com>
6
- License: Apache Software License
6
+ License: Apache License
7
+ Version 2.0, January 2004
8
+ http://www.apache.org/licenses/
9
+
10
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
11
+
12
+ 1. Definitions.
13
+
14
+ "License" shall mean the terms and conditions for use, reproduction,
15
+ and distribution as defined by Sections 1 through 9 of this document.
16
+
17
+ "Licensor" shall mean the copyright owner or entity authorized by
18
+ the copyright owner that is granting the License.
19
+
20
+ "Legal Entity" shall mean the union of the acting entity and all
21
+ other entities that control, are controlled by, or are under common
22
+ control with that entity. For the purposes of this definition,
23
+ "control" means (i) the power, direct or indirect, to cause the
24
+ direction or management of such entity, whether by contract or
25
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
26
+ outstanding shares, or (iii) beneficial ownership of such entity.
27
+
28
+ "You" (or "Your") shall mean an individual or Legal Entity
29
+ exercising permissions granted by this License.
30
+
31
+ "Source" form shall mean the preferred form for making modifications,
32
+ including but not limited to software source code, documentation
33
+ source, and configuration files.
34
+
35
+ "Object" form shall mean any form resulting from mechanical
36
+ transformation or translation of a Source form, including but
37
+ not limited to compiled object code, generated documentation,
38
+ and conversions to other media types.
39
+
40
+ "Work" shall mean the work of authorship, whether in Source or
41
+ Object form, made available under the License, as indicated by a
42
+ copyright notice that is included in or attached to the work
43
+ (an example is provided in the Appendix below).
44
+
45
+ "Derivative Works" shall mean any work, whether in Source or Object
46
+ form, that is based on (or derived from) the Work and for which the
47
+ editorial revisions, annotations, elaborations, or other modifications
48
+ represent, as a whole, an original work of authorship. For the purposes
49
+ of this License, Derivative Works shall not include works that remain
50
+ separable from, or merely link (or bind by name) to the interfaces of,
51
+ the Work and Derivative Works thereof.
52
+
53
+ "Contribution" shall mean any work of authorship, including
54
+ the original version of the Work and any modifications or additions
55
+ to that Work or Derivative Works thereof, that is intentionally
56
+ submitted to Licensor for inclusion in the Work by the copyright owner
57
+ or by an individual or Legal Entity authorized to submit on behalf of
58
+ the copyright owner. For the purposes of this definition, "submitted"
59
+ means any form of electronic, verbal, or written communication sent
60
+ to the Licensor or its representatives, including but not limited to
61
+ communication on electronic mailing lists, source code control systems,
62
+ and issue tracking systems that are managed by, or on behalf of, the
63
+ Licensor for the purpose of discussing and improving the Work, but
64
+ excluding communication that is conspicuously marked or otherwise
65
+ designated in writing by the copyright owner as "Not a Contribution."
66
+
67
+ "Contributor" shall mean Licensor and any individual or Legal Entity
68
+ on behalf of whom a Contribution has been received by Licensor and
69
+ subsequently incorporated within the Work.
70
+
71
+ 2. Grant of Copyright License. Subject to the terms and conditions of
72
+ this License, each Contributor hereby grants to You a perpetual,
73
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
74
+ copyright license to reproduce, prepare Derivative Works of,
75
+ publicly display, publicly perform, sublicense, and distribute the
76
+ Work and such Derivative Works in Source or Object form.
77
+
78
+ 3. Grant of Patent License. Subject to the terms and conditions of
79
+ this License, each Contributor hereby grants to You a perpetual,
80
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
81
+ (except as stated in this section) patent license to make, have made,
82
+ use, offer to sell, sell, import, and otherwise transfer the Work,
83
+ where such license applies only to those patent claims licensable
84
+ by such Contributor that are necessarily infringed by their
85
+ Contribution(s) alone or by combination of their Contribution(s)
86
+ with the Work to which such Contribution(s) was submitted. If You
87
+ institute patent litigation against any entity (including a
88
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
89
+ or a Contribution incorporated within the Work constitutes direct
90
+ or contributory patent infringement, then any patent licenses
91
+ granted to You under this License for that Work shall terminate
92
+ as of the date such litigation is filed.
93
+
94
+ 4. Redistribution. You may reproduce and distribute copies of the
95
+ Work or Derivative Works thereof in any medium, with or without
96
+ modifications, and in Source or Object form, provided that You
97
+ meet the following conditions:
98
+
99
+ (a) You must give any other recipients of the Work or
100
+ Derivative Works a copy of this License; and
101
+
102
+ (b) You must cause any modified files to carry prominent notices
103
+ stating that You changed the files; and
104
+
105
+ (c) You must retain, in the Source form of any Derivative Works
106
+ that You distribute, all copyright, patent, trademark, and
107
+ attribution notices from the Source form of the Work,
108
+ excluding those notices that do not pertain to any part of
109
+ the Derivative Works; and
110
+
111
+ (d) If the Work includes a "NOTICE" text file as part of its
112
+ distribution, then any Derivative Works that You distribute must
113
+ include a readable copy of the attribution notices contained
114
+ within such NOTICE file, excluding those notices that do not
115
+ pertain to any part of the Derivative Works, in at least one
116
+ of the following places: within a NOTICE text file distributed
117
+ as part of the Derivative Works; within the Source form or
118
+ documentation, if provided along with the Derivative Works; or,
119
+ within a display generated by the Derivative Works, if and
120
+ wherever such third-party notices normally appear. The contents
121
+ of the NOTICE file are for informational purposes only and
122
+ do not modify the License. You may add Your own attribution
123
+ notices within Derivative Works that You distribute, alongside
124
+ or as an addendum to the NOTICE text from the Work, provided
125
+ that such additional attribution notices cannot be construed
126
+ as modifying the License.
127
+
128
+ You may add Your own copyright statement to Your modifications and
129
+ may provide additional or different license terms and conditions
130
+ for use, reproduction, or distribution of Your modifications, or
131
+ for any such Derivative Works as a whole, provided Your use,
132
+ reproduction, and distribution of the Work otherwise complies with
133
+ the conditions stated in this License.
134
+
135
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
136
+ any Contribution intentionally submitted for inclusion in the Work
137
+ by You to the Licensor shall be under the terms and conditions of
138
+ this License, without any additional terms or conditions.
139
+ Notwithstanding the above, nothing herein shall supersede or modify
140
+ the terms of any separate license agreement you may have executed
141
+ with Licensor regarding such Contributions.
142
+
143
+ 6. Trademarks. This License does not grant permission to use the trade
144
+ names, trademarks, service marks, or product names of the Licensor,
145
+ except as required for reasonable and customary use in describing the
146
+ origin of the Work and reproducing the content of the NOTICE file.
147
+
148
+ 7. Disclaimer of Warranty. Unless required by applicable law or
149
+ agreed to in writing, Licensor provides the Work (and each
150
+ Contributor provides its Contributions) on an "AS IS" BASIS,
151
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
152
+ implied, including, without limitation, any warranties or conditions
153
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
154
+ PARTICULAR PURPOSE. You are solely responsible for determining the
155
+ appropriateness of using or redistributing the Work and assume any
156
+ risks associated with Your exercise of permissions under this License.
157
+
158
+ 8. Limitation of Liability. In no event and under no legal theory,
159
+ whether in tort (including negligence), contract, or otherwise,
160
+ unless required by applicable law (such as deliberate and grossly
161
+ negligent acts) or agreed to in writing, shall any Contributor be
162
+ liable to You for damages, including any direct, indirect, special,
163
+ incidental, or consequential damages of any character arising as a
164
+ result of this License or out of the use or inability to use the
165
+ Work (including but not limited to damages for loss of goodwill,
166
+ work stoppage, computer failure or malfunction, or any and all
167
+ other commercial damages or losses), even if such Contributor
168
+ has been advised of the possibility of such damages.
169
+
170
+ 9. Accepting Warranty or Additional Liability. While redistributing
171
+ the Work or Derivative Works thereof, You may choose to offer,
172
+ and charge a fee for, acceptance of support, warranty, indemnity,
173
+ or other liability obligations and/or rights consistent with this
174
+ License. However, in accepting such obligations, You may act only
175
+ on Your own behalf and on Your sole responsibility, not on behalf
176
+ of any other Contributor, and only if You agree to indemnify,
177
+ defend, and hold each Contributor harmless for any liability
178
+ incurred by, or claims asserted against, such Contributor by reason
179
+ of your accepting any such warranty or additional liability.
180
+
181
+ END OF TERMS AND CONDITIONS
182
+
183
+ APPENDIX: How to apply the Apache License to your work.
184
+
185
+ To apply the Apache License to your work, attach the following
186
+ boilerplate notice, with the fields enclosed by brackets "[]"
187
+ replaced with your own identifying information. (Don't include
188
+ the brackets!) The text should be enclosed in the appropriate
189
+ comment syntax for the file format. We also recommend that a
190
+ file or class name and description of purpose be included on the
191
+ same "printed page" as the copyright notice for easier
192
+ identification within third-party archives.
193
+
194
+ Copyright [yyyy] [name of copyright owner]
195
+
196
+ Licensed under the Apache License, Version 2.0 (the "License");
197
+ you may not use this file except in compliance with the License.
198
+ You may obtain a copy of the License at
199
+
200
+ http://www.apache.org/licenses/LICENSE-2.0
201
+
202
+ Unless required by applicable law or agreed to in writing, software
203
+ distributed under the License is distributed on an "AS IS" BASIS,
204
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
205
+ See the License for the specific language governing permissions and
206
+ limitations under the License.
207
+
7
208
  Project-URL: Homepage, https://github.com/ParisNeo/lollms_client
8
209
  Classifier: Programming Language :: Python :: 3
9
210
  Classifier: Programming Language :: Python :: 3.8
@@ -18,6 +219,7 @@ Classifier: Intended Audience :: Science/Research
18
219
  Requires-Python: >=3.7
19
220
  Description-Content-Type: text/markdown
20
221
  License-File: LICENSE
222
+ Requires-Dist: httpx
21
223
  Requires-Dist: requests
22
224
  Requires-Dist: ascii-colors
23
225
  Requires-Dist: pipmaster
@@ -1,13 +1,13 @@
1
- lollms_client/__init__.py,sha256=1ATuXFW5bb2NSxUu0nl6c3tXgX30-GS0pYWv6hkA8Vg,1146
1
+ lollms_client/__init__.py,sha256=HTzTbgGMZL9TCSuVkx9Psfjo0IUgURC4dW7nZeGItHk,1146
2
2
  lollms_client/lollms_agentic.py,sha256=pQiMEuB_XkG29-SW6u4KTaMFPr6eKqacInggcCuCW3k,13914
3
3
  lollms_client/lollms_config.py,sha256=goEseDwDxYJf3WkYJ4IrLXwg3Tfw73CXV2Avg45M_hE,21876
4
4
  lollms_client/lollms_core.py,sha256=aCEoxmEF6ZmkBgJgZd74lKkM4A3PVVyt2IwMvLfScWw,315053
5
- lollms_client/lollms_discussion.py,sha256=jWw1lSq0Oz_X5pnkECf1XwdDP2Lf84im00VpwuvsXXk,123041
5
+ lollms_client/lollms_discussion.py,sha256=W4VYFcpPwQX0Qot1NlK6THhFJFRtBVVaSE6j70tkq-Q,123273
6
6
  lollms_client/lollms_js_analyzer.py,sha256=01zUvuO2F_lnUe_0NLxe1MF5aHE1hO8RZi48mNPv-aw,8361
7
7
  lollms_client/lollms_llm_binding.py,sha256=Dj1PI2bQBYv_JgPxCIaIC7DMUvWdFJGwXFdsP5hdGBg,25014
8
8
  lollms_client/lollms_mcp_binding.py,sha256=psb27A23VFWDfZsR2WUbQXQxiZDW5yfOak6ZtbMfszI,10222
9
9
  lollms_client/lollms_mcp_security.py,sha256=FhVTDhSBjksGEZnopVnjFmEF5dv7D8bBTqoaj4BiF0E,3562
10
- lollms_client/lollms_personality.py,sha256=O-9nqZhazcITOkxjT24ENTxTmIoZLgqIsQ9WtWs0Id0,8719
10
+ lollms_client/lollms_personality.py,sha256=kGuFwmgA9QDLcQlLQ9sKeceMujdEo0Aw28fN5H8MpjI,8847
11
11
  lollms_client/lollms_python_analyzer.py,sha256=7gf1fdYgXCOkPUkBAPNmr6S-66hMH4_KonOMsADASxc,10246
12
12
  lollms_client/lollms_stt_binding.py,sha256=jAUhLouEhh2hmm1bK76ianfw_6B59EHfY3FmLv6DU-g,5111
13
13
  lollms_client/lollms_tti_binding.py,sha256=B38nzBCSPV9jVRZa-x8W7l9nJEW0RyS1MMJoueb8kt0,8519
@@ -60,8 +60,11 @@ lollms_client/tti_bindings/openai/__init__.py,sha256=YWJolJSQfIzTJvrLQVe8rQewP7r
60
60
  lollms_client/tti_bindings/stability_ai/__init__.py,sha256=GJxE0joQ0UZbheozysbN96AvQq60pjY2UjnSLFmRh4g,8025
61
61
  lollms_client/ttm_bindings/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
62
62
  lollms_client/ttm_bindings/audiocraft/__init__.py,sha256=a0k6wTrHth6GaVOiNnVboeFY3oKVvCQPbQlqO38XEyc,14328
63
- lollms_client/ttm_bindings/bark/__init__.py,sha256=Pr3ou2a-7hNYDqbkxrAbghZpO5HvGUhz7e-7VGXIHHA,18976
63
+ lollms_client/ttm_bindings/beatoven_ai/__init__.py,sha256=cQDpk0x0LfLZFaFs51_QQru_XkS-0nKRdVsrT36gdYw,5589
64
64
  lollms_client/ttm_bindings/lollms/__init__.py,sha256=DU3WLmJaWNM1NAMtJsnaFo4Y9wlfc675M8aUiaLnojA,3143
65
+ lollms_client/ttm_bindings/replicate/__init__.py,sha256=OG-xDsElyt14T3nWIQPBdV_SH1RL25xYCphb7hKWz1Y,4924
66
+ lollms_client/ttm_bindings/stability_ai/__init__.py,sha256=djI0dKaJziVmz8AUDfqEXAyzBws-9jl1joeqcPWy1xo,5574
67
+ lollms_client/ttm_bindings/topmediai/__init__.py,sha256=KvY8rpP5Cx6e08u28-vHy7cEtwrxS0a6v4fiNfGWw4k,3706
65
68
  lollms_client/tts_bindings/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
66
69
  lollms_client/tts_bindings/bark/__init__.py,sha256=Fqjz5r1aYwggbEfBSHyHB26V8OmbfUlxqH9a9i6HzfQ,4634
67
70
  lollms_client/tts_bindings/bark/server/install_bark.py,sha256=y9VhplwOqPwCVx_ex2MzkkBKMaUtzwPiUKg93_1LJzo,2221
@@ -76,8 +79,8 @@ lollms_client/tts_bindings/xtts/server/main.py,sha256=T-Kn5NM-u1FJMygeV8rOoZKlqn
76
79
  lollms_client/tts_bindings/xtts/server/setup_voices.py,sha256=UdHaPa5aNcw8dR-aRGkZr2OfSFFejH79lXgfwT0P3ss,1964
77
80
  lollms_client/ttv_bindings/__init__.py,sha256=UZ8o2izQOJLQgtZ1D1cXoNST7rzqW22rL2Vufc7ddRc,3141
78
81
  lollms_client/ttv_bindings/lollms/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
79
- lollms_client-1.4.5.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
80
- lollms_client-1.4.5.dist-info/METADATA,sha256=IJc5k53zOajOIIvuq-dhLQQ67bHpLwzNyTqxlQZNCz8,58689
81
- lollms_client-1.4.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
82
- lollms_client-1.4.5.dist-info/top_level.txt,sha256=Bk_kz-ri6Arwsk7YG-T5VsRorV66uVhcHGvb_g2WqgE,14
83
- lollms_client-1.4.5.dist-info/RECORD,,
82
+ lollms_client-1.4.7.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
83
+ lollms_client-1.4.7.dist-info/METADATA,sha256=sxuL8J9XKNUP_D_DN2zMZoLTpyaQxsJ6ukWJXiZVa2Q,71854
84
+ lollms_client-1.4.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
85
+ lollms_client-1.4.7.dist-info/top_level.txt,sha256=Bk_kz-ri6Arwsk7YG-T5VsRorV66uVhcHGvb_g2WqgE,14
86
+ lollms_client-1.4.7.dist-info/RECORD,,
@@ -1,339 +0,0 @@
1
- # lollms_client/ttm_bindings/bark/__init__.py
2
- import io
3
- import os
4
- from pathlib import Path
5
- from typing import Optional, List, Union, Dict, Any
6
-
7
- from ascii_colors import trace_exception, ASCIIColors
8
-
9
- # --- Package Management and Conditional Imports ---
10
- _bark_deps_installed_with_correct_torch = False
11
- _bark_installation_error = ""
12
- try:
13
- import pipmaster as pm
14
- import platform
15
-
16
- preferred_torch_device_for_install = "cpu"
17
- if platform.system() == "Linux" or platform.system() == "Windows":
18
- preferred_torch_device_for_install = "cuda"
19
- elif platform.system() == "Darwin":
20
- preferred_torch_device_for_install = "mps"
21
-
22
- torch_pkgs = ["torch"]
23
- bark_core_pkgs = ["transformers", "accelerate", "sentencepiece"]
24
- other_deps = ["scipy", "numpy"]
25
-
26
- torch_index_url = None
27
- if preferred_torch_device_for_install == "cuda":
28
- torch_index_url = "https://download.pytorch.org/whl/cu126"
29
- ASCIIColors.info(f"Attempting to ensure PyTorch with CUDA support (target index: {torch_index_url}) for Bark binding.")
30
- pm.ensure_packages(torch_pkgs, index_url=torch_index_url)
31
- pm.ensure_packages(bark_core_pkgs + other_deps)
32
- else:
33
- ASCIIColors.info("Ensuring PyTorch, Bark dependencies, and others using default PyPI index for Bark binding.")
34
- pm.ensure_packages(torch_pkgs + bark_core_pkgs + other_deps)
35
-
36
- import torch
37
- from transformers import AutoProcessor, BarkModel, GenerationConfig
38
- import scipy.io.wavfile
39
- import numpy as np
40
-
41
- _bark_deps_installed_with_correct_torch = True
42
- except Exception as e:
43
- _bark_installation_error = str(e)
44
- AutoProcessor, BarkModel, GenerationConfig, torch, scipy, np = None, None, None, None, None, None
45
- # --- End Package Management ---
46
-
47
- from lollms_client.lollms_ttm_binding import LollmsTTMBinding
48
-
49
- BindingName = "BarkTTMBinding"
50
-
51
- DEFAULT_BARK_MODELS = [
52
- "suno/bark",
53
- "suno/bark-small",
54
- ]
55
-
56
- BARK_VOICE_PRESETS_EXAMPLES = [
57
- "v2/en_speaker_0", "v2/en_speaker_1", "v2/en_speaker_2", "v2/en_speaker_3",
58
- "v2/en_speaker_4", "v2/en_speaker_5", "v2/en_speaker_6", "v2/en_speaker_7",
59
- "v2/en_speaker_8", "v2/en_speaker_9",
60
- "v2/de_speaker_0", "v2/es_speaker_0", "v2/fr_speaker_0",
61
- ]
62
-
63
-
64
- class BarkTTMBinding(LollmsTTMBinding):
65
- def __init__(self,
66
- model_name: str = "suno/bark-small",
67
- device: Optional[str] = None,
68
- default_voice_preset: Optional[str] = "v2/en_speaker_6",
69
- enable_better_transformer: bool = True,
70
- **kwargs):
71
-
72
- super().__init__(binding_name="bark")
73
-
74
- if not _bark_deps_installed_with_correct_torch:
75
- raise ImportError(f"Bark TTM binding dependencies not met. Error: {_bark_installation_error}")
76
-
77
- self.device = device
78
- if self.device is None:
79
- if torch.cuda.is_available(): self.device = "cuda"; ASCIIColors.info("CUDA device detected by PyTorch for Bark.")
80
- elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(): self.device = "mps"; ASCIIColors.info("MPS device detected for Bark.")
81
- else: self.device = "cpu"; ASCIIColors.info("No GPU (CUDA/MPS) by PyTorch, using CPU for Bark.")
82
- elif self.device == "cuda" and not torch.cuda.is_available(): self.device = "cpu"; ASCIIColors.warning("CUDA req, not avail. CPU for Bark.")
83
- elif self.device == "mps" and not (hasattr(torch.backends, 'mps') and torch.backends.mps.is_available()): self.device = "cpu"; ASCIIColors.warning("MPS req, not avail. CPU for Bark.")
84
-
85
- ASCIIColors.info(f"BarkTTMBinding: Using device '{self.device}'.")
86
-
87
- self.loaded_model_name = None
88
- self.model: Optional[BarkModel] = None
89
- self.processor: Optional[AutoProcessor] = None
90
- self.default_voice_preset = default_voice_preset
91
- self.enable_better_transformer = enable_better_transformer
92
-
93
- self.default_generation_params = {}
94
- temp_gen_config = GenerationConfig()
95
- for key, value in kwargs.items():
96
- if hasattr(temp_gen_config, key):
97
- self.default_generation_params[key] = value
98
-
99
- self._load_bark_model(model_name)
100
-
101
- def _load_bark_model(self, model_name_to_load: str):
102
- if self.model is not None and self.loaded_model_name == model_name_to_load:
103
- ASCIIColors.info(f"Bark model '{model_name_to_load}' already loaded.")
104
- return
105
-
106
- ASCIIColors.info(f"Loading Bark model: '{model_name_to_load}' on device '{self.device}'...")
107
- try:
108
- dtype_for_bark = torch.float16 if self.device == "cuda" else None
109
-
110
- self.processor = AutoProcessor.from_pretrained(model_name_to_load)
111
- self.model = BarkModel.from_pretrained(
112
- model_name_to_load,
113
- torch_dtype=dtype_for_bark,
114
- low_cpu_mem_usage=True if self.device != "cpu" else False
115
- ).to(self.device)
116
-
117
- if self.enable_better_transformer and self.device == "cuda":
118
- try:
119
- self.model = self.model.to_bettertransformer()
120
- ASCIIColors.info("Applied BetterTransformer optimization to Bark model.")
121
- except Exception as e_bt:
122
- ASCIIColors.warning(f"Failed to apply BetterTransformer: {e_bt}. Proceeding without it.")
123
-
124
- if "small" not in model_name_to_load and self.device=="cpu":
125
- ASCIIColors.warning("Using full Bark model on CPU. Generation might be slow.")
126
- elif self.device != "cpu" and "small" not in model_name_to_load:
127
- if hasattr(self.model, "enable_model_cpu_offload"):
128
- try: self.model.enable_model_cpu_offload(); ASCIIColors.info("Enabled model_cpu_offload for Bark.")
129
- except Exception as e: ASCIIColors.warning(f"Could not enable model_cpu_offload: {e}")
130
- elif hasattr(self.model, "enable_cpu_offload"):
131
- try: self.model.enable_cpu_offload(); ASCIIColors.info("Enabled cpu_offload for Bark (older API).")
132
- except Exception as e: ASCIIColors.warning(f"Could not enable cpu_offload (older API): {e}")
133
- else: ASCIIColors.info("CPU offload not explicitly enabled.")
134
-
135
- self.loaded_model_name = model_name_to_load
136
- ASCIIColors.green(f"Bark model '{model_name_to_load}' loaded successfully.")
137
- except Exception as e:
138
- self.model, self.processor, self.loaded_model_name = None, None, None
139
- ASCIIColors.error(f"Failed to load Bark model '{model_name_to_load}': {e}"); trace_exception(e)
140
- raise RuntimeError(f"Failed to load Bark model '{model_name_to_load}'") from e
141
-
142
- def generate_music(self,
143
- prompt: str,
144
- voice_preset: Optional[str] = None,
145
- do_sample: Optional[bool] = None,
146
- temperature: Optional[float] = None,
147
- **kwargs) -> bytes:
148
- if self.model is None or self.processor is None:
149
- raise RuntimeError("Bark model or processor not loaded.")
150
-
151
- effective_voice_preset = voice_preset if voice_preset is not None else self.default_voice_preset
152
-
153
- ASCIIColors.info(f"Generating SFX/audio with Bark: '{prompt[:60]}...' (Preset: {effective_voice_preset})")
154
- try:
155
- # The processor correctly returns 'input_ids' and 'attention_mask'
156
- inputs = self.processor(
157
- text=[prompt], # Processor expects a list of texts
158
- voice_preset=effective_voice_preset,
159
- return_tensors="pt",
160
- # Explicitly ask for padding if tokenizer supports it,
161
- # though Bark's processor might handle this internally.
162
- # padding=True, # Let processor decide best padding strategy
163
- # truncation=True # Ensure inputs fit model context
164
- )
165
- inputs = {k: v.to(self.device) for k, v in inputs.items()}
166
-
167
- # Ensure attention_mask is present
168
- if 'attention_mask' not in inputs:
169
- ASCIIColors.warning("Processor did not return attention_mask. Creating a default one (all ones). This might lead to suboptimal results if padding was intended.")
170
- inputs['attention_mask'] = torch.ones_like(inputs['input_ids'])
171
-
172
-
173
- if hasattr(self.model, 'generation_config') and self.model.generation_config is not None:
174
- gen_config = GenerationConfig.from_dict(self.model.generation_config.to_dict())
175
- else:
176
- gen_config = GenerationConfig()
177
-
178
- for key, value in self.default_generation_params.items():
179
- if hasattr(gen_config, key): setattr(gen_config, key, value)
180
-
181
- if do_sample is not None: gen_config.do_sample = do_sample
182
-
183
- if temperature is not None:
184
- if 'semantic_temperature' not in kwargs and hasattr(gen_config, 'semantic_temperature'): gen_config.semantic_temperature = temperature
185
- if 'coarse_temperature' not in kwargs and hasattr(gen_config, 'coarse_temperature'): gen_config.coarse_temperature = temperature
186
- if 'fine_temperature' not in kwargs and hasattr(gen_config, 'fine_temperature'): gen_config.fine_temperature = temperature
187
-
188
- for key, value in kwargs.items():
189
- if hasattr(gen_config, key): setattr(gen_config, key, value)
190
-
191
- # Critical: Set pad_token_id in GenerationConfig.
192
- # Bark uses specific token IDs for its different codebooks.
193
- # The processor's tokenizer should have the correct pad_token_id if it's used for text inputs.
194
- # For Bark, the semantic vocabulary has its own pad_token_id, often same as EOS.
195
- # Let's try to get it from the model's semantic config or text config.
196
- pad_token_id_to_set = None
197
- if hasattr(self.model.config, 'semantic_config') and hasattr(self.model.config.semantic_config, 'pad_token_id'):
198
- pad_token_id_to_set = self.model.config.semantic_config.pad_token_id
199
- elif hasattr(self.model.config, 'text_config') and hasattr(self.model.config.text_config, 'pad_token_id'):
200
- pad_token_id_to_set = self.model.config.text_config.pad_token_id
201
- elif hasattr(self.processor, 'tokenizer') and self.processor.tokenizer and self.processor.tokenizer.pad_token_id is not None:
202
- pad_token_id_to_set = self.processor.tokenizer.pad_token_id
203
-
204
- if pad_token_id_to_set is not None:
205
- gen_config.pad_token_id = pad_token_id_to_set
206
- # Also set EOS token if it's distinct and meaningful for generation stopping
207
- if hasattr(gen_config, 'eos_token_id') and gen_config.eos_token_id is None:
208
- eos_id = None
209
- if hasattr(self.model.config, 'semantic_config') and hasattr(self.model.config.semantic_config, 'eos_token_id'):
210
- eos_id = self.model.config.semantic_config.eos_token_id
211
- if eos_id is not None:
212
- gen_config.eos_token_id = eos_id
213
-
214
- else:
215
- # This state is problematic for Bark if pad_token_id is truly needed and distinct from EOS
216
- ASCIIColors.warning("Could not determine a specific pad_token_id from Bark's config for GenerationConfig. This might lead to issues.")
217
- # If eos_token_id is also not set, generation might not stop correctly.
218
- # Defaulting pad_token_id to eos_token_id if eos_token_id exists.
219
- if gen_config.eos_token_id is not None:
220
- gen_config.pad_token_id = gen_config.eos_token_id
221
- ASCIIColors.info(f"Setting pad_token_id to eos_token_id ({gen_config.eos_token_id}) as a fallback.")
222
- else:
223
- # This is a last resort and might not be correct for Bark specifically
224
- gen_config.pad_token_id = 0
225
- ASCIIColors.warning("pad_token_id defaulted to 0 as a last resort.")
226
-
227
-
228
- ASCIIColors.debug(f"Bark final generation_config: {gen_config.to_json_string()}")
229
-
230
- with torch.no_grad():
231
- output = self.model.generate(
232
- input_ids=inputs['input_ids'], # Explicitly pass input_ids
233
- attention_mask=inputs.get('attention_mask'), # Pass attention_mask if available
234
- generation_config=gen_config
235
- )
236
-
237
- if isinstance(output, torch.Tensor): speech_output_tensor = output
238
- elif isinstance(output, dict) and "audio_features" in output: speech_output_tensor = output["audio_features"]
239
- elif isinstance(output, dict) and "waveform" in output: speech_output_tensor = output["waveform"] # Bark might return this key
240
- else: raise TypeError(f"Unexpected output type from BarkModel.generate: {type(output)}. Content: {output}")
241
-
242
- audio_array_np = speech_output_tensor.cpu().numpy().squeeze()
243
- if audio_array_np.ndim == 0 or audio_array_np.size == 0:
244
- raise RuntimeError("Bark model returned empty audio data.")
245
-
246
- audio_int16 = (audio_array_np * 32767).astype(np.int16)
247
-
248
- buffer = io.BytesIO()
249
- sample_rate_to_use = int(self.model.generation_config.sample_rate if hasattr(self.model.generation_config, 'sample_rate') and self.model.generation_config.sample_rate else 24_000)
250
- scipy.io.wavfile.write(buffer, rate=sample_rate_to_use, data=audio_int16)
251
- audio_bytes = buffer.getvalue()
252
- buffer.close()
253
-
254
- ASCIIColors.green("Bark audio generation successful.")
255
- return audio_bytes
256
- except Exception as e:
257
- ASCIIColors.error(f"Bark audio generation failed: {e}"); trace_exception(e)
258
- if "out of memory" in str(e).lower() and self.device == "cuda":
259
- ASCIIColors.yellow("CUDA out of memory. Consider using suno/bark-small or ensure GPU has sufficient VRAM.")
260
- raise RuntimeError(f"Bark audio generation error: {e}") from e
261
-
262
- def list_models(self, **kwargs) -> List[str]:
263
- return DEFAULT_BARK_MODELS.copy()
264
-
265
- def list_voice_presets(self) -> List[str]:
266
- return BARK_VOICE_PRESETS_EXAMPLES.copy()
267
-
268
- def __del__(self):
269
- if hasattr(self, 'model') and self.model is not None:
270
- del self.model; self.model = None
271
- if hasattr(self, 'processor') and self.processor is not None:
272
- del self.processor; self.processor = None
273
- if torch and hasattr(torch, 'cuda') and torch.cuda.is_available():
274
- torch.cuda.empty_cache()
275
- loaded_name = getattr(self, 'loaded_model_name', None)
276
- msg = f"BarkTTMBinding for model '{loaded_name}' destroyed." if loaded_name else "BarkTTMBinding destroyed."
277
- ASCIIColors.info(msg)
278
-
279
- # --- Main Test Block ---
280
- if __name__ == '__main__':
281
- if not _bark_deps_installed_with_correct_torch:
282
- print(f"{ASCIIColors.RED}Bark TTM binding dependencies not met. Skipping tests. Error: {_bark_installation_error}{ASCIIColors.RESET}")
283
- exit()
284
-
285
- ASCIIColors.yellow("--- BarkTTMBinding Test ---")
286
- test_model_id = "suno/bark-small"
287
- test_output_dir = Path("./test_bark_sfx_output")
288
- test_output_dir.mkdir(exist_ok=True)
289
- ttm_binding = None
290
-
291
- try:
292
- ASCIIColors.cyan(f"\n--- Initializing BarkTTMBinding (model: '{test_model_id}') ---")
293
- ttm_binding = BarkTTMBinding(model_name=test_model_id)
294
-
295
- ASCIIColors.cyan("\n--- Listing common Bark models ---")
296
- models = ttm_binding.list_models(); print(f"Common Bark models: {models}")
297
- ASCIIColors.cyan("\n--- Listing example Bark voice presets ---")
298
- presets = ttm_binding.list_voice_presets(); print(f"Example presets: {presets[:5]}...")
299
-
300
- sfx_prompts_to_test = [
301
- ("laser_blast", "A short, sharp laser blast sound effect [SFX]"),
302
- ("footsteps_gravel", "Footsteps walking on gravel [footsteps]."),
303
- ("explosion_distant", "A distant explosion [boom] with a slight echo."),
304
- ("interface_click", "A clean, quick digital interface click sound. [click]"),
305
- ("creature_roar_short", "[roar] A short, guttural creature roar."),
306
- ("ambient_wind", "[wind] Gentle wind blowing through trees."),
307
- ("speech_hello", "Hello, this is a test of Bark's speech capabilities."),
308
- ]
309
-
310
- for name, prompt in sfx_prompts_to_test:
311
- ASCIIColors.cyan(f"\n--- Generating SFX/Audio for: '{name}' ---"); print(f"Prompt: {prompt}")
312
- try:
313
- call_kwargs = {}
314
- if "speech" in name:
315
- call_kwargs = {"semantic_temperature": 0.6, "coarse_temperature": 0.8, "fine_temperature": 0.5, "do_sample": True}
316
- elif name == "laser_blast":
317
- call_kwargs = {"semantic_temperature": 0.5, "coarse_temperature": 0.6, "fine_temperature": 0.4, "do_sample": True}
318
- else: # For SFX, sometimes more deterministic sampling helps for consistency
319
- call_kwargs = {"do_sample": True, "semantic_temperature": 0.7, "coarse_temperature": 0.7, "fine_temperature": 0.7}
320
-
321
-
322
- sfx_bytes = ttm_binding.generate_music(prompt, voice_preset=None, **call_kwargs)
323
- if sfx_bytes:
324
- output_filename = f"sfx_{name}_{test_model_id.split('/')[-1]}.wav"
325
- output_path = test_output_dir / output_filename
326
- with open(output_path, "wb") as f: f.write(sfx_bytes)
327
- ASCIIColors.green(f"SFX for '{name}' saved to: {output_path} ({len(sfx_bytes) / 1024:.2f} KB)")
328
- else: ASCIIColors.error(f"SFX generation for '{name}' returned empty bytes.")
329
- except Exception as e_gen: ASCIIColors.error(f"Failed to generate SFX for '{name}': {e_gen}")
330
-
331
- except ImportError as e_imp: ASCIIColors.error(f"Import error: {e_imp}")
332
- except RuntimeError as e_rt: ASCIIColors.error(f"Runtime error: {e_rt}")
333
- except Exception as e: ASCIIColors.error(f"Unexpected error: {e}"); trace_exception(e)
334
- finally:
335
- if ttm_binding: del ttm_binding
336
- ASCIIColors.info(f"Test SFX (if any) are in: {test_output_dir.resolve()}")
337
- print(f"{ASCIIColors.YELLOW}Check the audio files in '{test_output_dir.resolve()}'!{ASCIIColors.RESET}")
338
-
339
- ASCIIColors.yellow("\n--- BarkTTMBinding Test Finished ---")