livekit-plugins-spitch 1.4.0rc1__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.
@@ -0,0 +1,179 @@
1
+ **/.vscode
2
+ **/.DS_Store
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+ cover/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ .pybuilder/
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ # For a library or package, you might want to ignore these files since the code is
90
+ # intended to run in multiple environments; otherwise, check them in:
91
+ # .python-version
92
+
93
+ # pipenv
94
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
96
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
97
+ # install all needed dependencies.
98
+ #Pipfile.lock
99
+
100
+ # poetry
101
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
105
+ #poetry.lock
106
+
107
+ # pdm
108
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
109
+ #pdm.lock
110
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
111
+ # in version control.
112
+ # https://pdm.fming.dev/#use-with-ide
113
+ .pdm.toml
114
+
115
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
116
+ __pypackages__/
117
+
118
+ # Celery stuff
119
+ celerybeat-schedule
120
+ celerybeat.pid
121
+
122
+ # SageMath parsed files
123
+ *.sage.py
124
+
125
+ # Environments
126
+ .env
127
+ .venv
128
+ env/
129
+ venv/
130
+ ENV/
131
+ env.bak/
132
+ venv.bak/
133
+
134
+ # Spyder project settings
135
+ .spyderproject
136
+ .spyproject
137
+
138
+ # Rope project settings
139
+ .ropeproject
140
+
141
+ # mkdocs documentation
142
+ /site
143
+
144
+ # mypy
145
+ .mypy_cache/
146
+ .dmypy.json
147
+ dmypy.json
148
+
149
+ # trunk
150
+ .trunk/
151
+
152
+ # Pyre type checker
153
+ .pyre/
154
+
155
+ # pytype static type analyzer
156
+ .pytype/
157
+
158
+ # Cython debug symbols
159
+ cython_debug/
160
+
161
+ # PyCharm
162
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
163
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
164
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
165
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
166
+ .idea/
167
+
168
+ node_modules
169
+
170
+ credentials.json
171
+ pyrightconfig.json
172
+ docs/
173
+
174
+ # Database files
175
+ *.db
176
+
177
+
178
+ # Examples for development
179
+ examples/dev/*
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: livekit-plugins-spitch
3
+ Version: 1.4.0rc1
4
+ Summary: spitch plugin template for LiveKit Agents
5
+ Project-URL: Documentation, https://docs.livekit.io
6
+ Project-URL: Website, https://livekit.io/
7
+ Project-URL: Source, https://github.com/livekit/agents
8
+ Author: LiveKit
9
+ License-Expression: Apache-2.0
10
+ Keywords: ai,audio,livekit,realtime,video,voice,webrtc
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Topic :: Multimedia :: Sound/Audio
18
+ Classifier: Topic :: Multimedia :: Video
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Requires-Python: >=3.9.0
21
+ Requires-Dist: livekit-agents[codecs]>=1.4.0.rc1
22
+ Requires-Dist: spitch
23
+ Description-Content-Type: text/markdown
24
+
25
+ # Spitch plugin for LiveKit Agents
26
+
27
+ Support for [Spitch](https://spitch.app/)'s African-language voice AI services in LiveKit Agents.
28
+
29
+ More information is available in the docs for the [STT](https://docs.livekit.io/agents/integrations/stt/spitch/) and [TTS](https://docs.livekit.io/agents/integrations/tts/spitch/) integrations.
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install livekit-plugins-spitch
35
+ ```
36
+
37
+ ## Pre-requisites
38
+
39
+ You'll need an API key from Spitch. It can be set as an environment variable: `SPITCH_API_KEY`
@@ -0,0 +1,15 @@
1
+ # Spitch plugin for LiveKit Agents
2
+
3
+ Support for [Spitch](https://spitch.app/)'s African-language voice AI services in LiveKit Agents.
4
+
5
+ More information is available in the docs for the [STT](https://docs.livekit.io/agents/integrations/stt/spitch/) and [TTS](https://docs.livekit.io/agents/integrations/tts/spitch/) integrations.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install livekit-plugins-spitch
11
+ ```
12
+
13
+ ## Pre-requisites
14
+
15
+ You'll need an API key from Spitch. It can be set as an environment variable: `SPITCH_API_KEY`
@@ -0,0 +1,41 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Spitch plugin for LiveKit Agents"""
16
+
17
+ from .stt import STT
18
+ from .tts import TTS
19
+ from .version import __version__
20
+
21
+ __all__ = ["STT", "TTS", "__version__"]
22
+
23
+ from livekit.agents import Plugin
24
+
25
+ from .log import logger
26
+
27
+
28
+ class SpitchPlugin(Plugin):
29
+ def __init__(self):
30
+ super().__init__(__name__, __version__, __package__, logger)
31
+
32
+
33
+ Plugin.register_plugin(SpitchPlugin())
34
+
35
+ _module = dir()
36
+ NOT_IN_ALL = [m for m in _module if m not in __all__]
37
+
38
+ __pdoc__ = {}
39
+
40
+ for n in NOT_IN_ALL:
41
+ __pdoc__[n] = False
@@ -0,0 +1,3 @@
1
+ import logging
2
+
3
+ logger = logging.getLogger("livekit.plugins.spitch")
@@ -0,0 +1,107 @@
1
+ from __future__ import annotations
2
+
3
+ import dataclasses
4
+ from dataclasses import dataclass
5
+
6
+ import httpx
7
+
8
+ import spitch
9
+ from livekit import rtc
10
+ from livekit.agents import (
11
+ NOT_GIVEN,
12
+ APIConnectionError,
13
+ APIConnectOptions,
14
+ APIStatusError,
15
+ APITimeoutError,
16
+ NotGivenOr,
17
+ )
18
+ from livekit.agents.stt import stt
19
+ from livekit.agents.utils import AudioBuffer
20
+ from livekit.agents.voice.io import TimedString
21
+ from spitch import AsyncSpitch
22
+
23
+
24
+ @dataclass
25
+ class _STTOptions:
26
+ language: str
27
+
28
+
29
+ class STT(stt.STT):
30
+ def __init__(self, *, language: str = "en") -> None:
31
+ super().__init__(
32
+ capabilities=stt.STTCapabilities(
33
+ streaming=False,
34
+ interim_results=False,
35
+ # word timestamps don't seem to work despite the docs saying they do
36
+ aligned_transcript=False,
37
+ )
38
+ )
39
+
40
+ self._opts = _STTOptions(language=language)
41
+ self._client = AsyncSpitch()
42
+
43
+ @property
44
+ def model(self) -> str:
45
+ return "unknown"
46
+
47
+ @property
48
+ def provider(self) -> str:
49
+ return "Spitch"
50
+
51
+ def update_options(self, language: str) -> None:
52
+ self._opts.language = language or self._opts.language
53
+
54
+ def _sanitize_options(self, *, language: str | None = None) -> _STTOptions:
55
+ config = dataclasses.replace(self._opts)
56
+ config.language = language or config.language
57
+ return config
58
+
59
+ async def _recognize_impl(
60
+ self,
61
+ buffer: AudioBuffer,
62
+ *,
63
+ language: NotGivenOr[str] = NOT_GIVEN,
64
+ conn_options: APIConnectOptions,
65
+ ) -> stt.SpeechEvent:
66
+ try:
67
+ config = self._sanitize_options(language=language or None)
68
+ data = rtc.combine_audio_frames(buffer).to_wav_bytes()
69
+ model = "mansa_v1" if config.language == "en" else "legacy"
70
+ resp = await self._client.speech.transcribe(
71
+ language=config.language, # type: ignore
72
+ content=data,
73
+ timeout=httpx.Timeout(30, connect=conn_options.timeout),
74
+ timestamp="word" if "mansa" in model else None,
75
+ )
76
+
77
+ return stt.SpeechEvent(
78
+ type=stt.SpeechEventType.FINAL_TRANSCRIPT,
79
+ alternatives=[
80
+ stt.SpeechData(
81
+ text=resp.text or "",
82
+ language=config.language or "",
83
+ start_time=resp.segments[0].start
84
+ if resp.segments and resp.segments[0]
85
+ else 0,
86
+ end_time=resp.segments[-1].end
87
+ if resp.segments and resp.segments[-1]
88
+ else 0,
89
+ words=[
90
+ TimedString(
91
+ text=segment.text,
92
+ start_time=segment.start,
93
+ end_time=segment.end,
94
+ )
95
+ for segment in resp.segments
96
+ ]
97
+ if resp.segments
98
+ else None,
99
+ ),
100
+ ],
101
+ )
102
+ except spitch.APITimeoutError as e:
103
+ raise APITimeoutError() from e
104
+ except spitch.APIStatusError as e:
105
+ raise APIStatusError(e.message, status_code=e.status_code, body=e.body) from e
106
+ except Exception as e:
107
+ raise APIConnectionError() from e
@@ -0,0 +1,107 @@
1
+ from __future__ import annotations
2
+
3
+ import uuid
4
+ from dataclasses import dataclass
5
+
6
+ import httpx
7
+
8
+ import spitch
9
+ from livekit.agents import (
10
+ DEFAULT_API_CONNECT_OPTIONS,
11
+ APIConnectionError,
12
+ APIConnectOptions,
13
+ APIStatusError,
14
+ APITimeoutError,
15
+ tts,
16
+ )
17
+ from spitch import AsyncSpitch
18
+
19
+ SAMPLE_RATE = 24_000
20
+ NUM_CHANNELS = 1
21
+ MIME_TYPE = "audio/mpeg"
22
+
23
+
24
+ @dataclass
25
+ class _TTSOptions:
26
+ language: str
27
+ voice: str
28
+
29
+
30
+ class TTS(tts.TTS):
31
+ def __init__(self, *, language: str = "en", voice: str = "lina"):
32
+ super().__init__(
33
+ capabilities=tts.TTSCapabilities(streaming=False), sample_rate=24_000, num_channels=1
34
+ )
35
+
36
+ self._opts = _TTSOptions(language=language, voice=voice)
37
+ self._client = AsyncSpitch()
38
+
39
+ @property
40
+ def model(self) -> str:
41
+ return "unknown"
42
+
43
+ @property
44
+ def provider(self) -> str:
45
+ return "Spitch"
46
+
47
+ def synthesize(
48
+ self,
49
+ text: str,
50
+ *,
51
+ conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
52
+ ) -> ChunkedStream:
53
+ return ChunkedStream(
54
+ tts=self,
55
+ input_text=text,
56
+ conn_options=conn_options,
57
+ opts=self._opts,
58
+ client=self._client,
59
+ )
60
+
61
+
62
+ class ChunkedStream(tts.ChunkedStream):
63
+ def __init__(
64
+ self,
65
+ *,
66
+ tts: TTS,
67
+ input_text: str,
68
+ conn_options: APIConnectOptions,
69
+ opts: _TTSOptions,
70
+ client: AsyncSpitch,
71
+ ) -> None:
72
+ super().__init__(tts=tts, input_text=input_text, conn_options=conn_options)
73
+ self._client = client
74
+ self._opts = opts
75
+
76
+ async def _run(self, output_emitter: tts.AudioEmitter) -> None:
77
+ spitch_stream = self._client.speech.with_streaming_response.generate(
78
+ text=self.input_text,
79
+ language=self._opts.language, # type: ignore
80
+ voice=self._opts.voice, # type: ignore
81
+ format="mp3",
82
+ timeout=httpx.Timeout(30, connect=self._conn_options.timeout),
83
+ )
84
+
85
+ request_id = str(uuid.uuid4().hex)[:12]
86
+ try:
87
+ async with spitch_stream as stream:
88
+ output_emitter.initialize(
89
+ request_id=request_id,
90
+ sample_rate=SAMPLE_RATE,
91
+ num_channels=NUM_CHANNELS,
92
+ mime_type=MIME_TYPE,
93
+ )
94
+
95
+ async for data in stream.iter_bytes():
96
+ output_emitter.push(data)
97
+
98
+ output_emitter.flush()
99
+
100
+ except spitch.APITimeoutError:
101
+ raise APITimeoutError() from None
102
+ except spitch.APIStatusError as e:
103
+ raise APIStatusError(
104
+ e.message, status_code=e.status_code, request_id=request_id, body=e.body
105
+ ) from None
106
+ except Exception as e:
107
+ raise APIConnectionError() from e
@@ -0,0 +1,15 @@
1
+ # Copyright 2023 LiveKit, Inc.
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ __version__ = "1.4.0.rc1"
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "livekit-plugins-spitch"
7
+ dynamic = ["version"]
8
+ description = "spitch plugin template for LiveKit Agents"
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ requires-python = ">=3.9.0"
12
+ authors = [{ name = "LiveKit" }]
13
+ keywords = ["voice", "ai", "realtime", "audio", "video", "livekit", "webrtc"]
14
+ classifiers = [
15
+ "Intended Audience :: Developers",
16
+ "License :: OSI Approved :: Apache Software License",
17
+ "Topic :: Multimedia :: Sound/Audio",
18
+ "Topic :: Multimedia :: Video",
19
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3 :: Only",
24
+ ]
25
+ dependencies = ["livekit-agents[codecs]>=1.4.0.rc1", "spitch"]
26
+
27
+ [project.urls]
28
+ Documentation = "https://docs.livekit.io"
29
+ Website = "https://livekit.io/"
30
+ Source = "https://github.com/livekit/agents"
31
+
32
+ [tool.hatch.version]
33
+ path = "livekit/plugins/spitch/version.py"
34
+
35
+ [tool.hatch.build.targets.wheel]
36
+ packages = ["livekit"]
37
+
38
+ [tool.hatch.build.targets.sdist]
39
+ include = ["/livekit"]