livekit-plugins-spitch 1.1.0__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.
- livekit/plugins/spitch/__init__.py +41 -0
- livekit/plugins/spitch/log.py +3 -0
- livekit/plugins/spitch/py.typed +0 -0
- livekit/plugins/spitch/stt.py +70 -0
- livekit/plugins/spitch/tts.py +98 -0
- livekit/plugins/spitch/version.py +15 -0
- livekit_plugins_spitch-1.1.0.dist-info/METADATA +37 -0
- livekit_plugins_spitch-1.1.0.dist-info/RECORD +9 -0
- livekit_plugins_spitch-1.1.0.dist-info/WHEEL +4 -0
@@ -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
|
File without changes
|
@@ -0,0 +1,70 @@
|
|
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 spitch import AsyncSpitch
|
21
|
+
|
22
|
+
|
23
|
+
@dataclass
|
24
|
+
class _STTOptions:
|
25
|
+
language: str
|
26
|
+
|
27
|
+
|
28
|
+
class STT(stt.STT):
|
29
|
+
def __init__(self, *, language: str = "en") -> None:
|
30
|
+
super().__init__(capabilities=stt.STTCapabilities(streaming=False, interim_results=False))
|
31
|
+
|
32
|
+
self._opts = _STTOptions(language=language)
|
33
|
+
self._client = AsyncSpitch()
|
34
|
+
|
35
|
+
def update_options(self, language: str):
|
36
|
+
self._opts.language = language or self._opts.language
|
37
|
+
|
38
|
+
def _sanitize_options(self, *, language: str | None = None) -> _STTOptions:
|
39
|
+
config = dataclasses.replace(self._opts)
|
40
|
+
config.language = language or config.language
|
41
|
+
return config
|
42
|
+
|
43
|
+
async def _recognize_impl(
|
44
|
+
self,
|
45
|
+
buffer: AudioBuffer,
|
46
|
+
*,
|
47
|
+
language: NotGivenOr[str] = NOT_GIVEN,
|
48
|
+
conn_options: APIConnectOptions,
|
49
|
+
) -> stt.SpeechEvent:
|
50
|
+
try:
|
51
|
+
config = self._sanitize_options(language=language or None)
|
52
|
+
data = rtc.combine_audio_frames(buffer).to_wav_bytes()
|
53
|
+
resp = await self._client.speech.transcribe(
|
54
|
+
language=config.language, # type: ignore
|
55
|
+
content=data,
|
56
|
+
timeout=httpx.Timeout(30, connect=conn_options.timeout),
|
57
|
+
)
|
58
|
+
|
59
|
+
return stt.SpeechEvent(
|
60
|
+
type=stt.SpeechEventType.FINAL_TRANSCRIPT,
|
61
|
+
alternatives=[
|
62
|
+
stt.SpeechData(text=resp.text or "", language=config.language or ""),
|
63
|
+
],
|
64
|
+
)
|
65
|
+
except spitch.APITimeoutError as e:
|
66
|
+
raise APITimeoutError() from e
|
67
|
+
except spitch.APIStatusError as e:
|
68
|
+
raise APIStatusError(e.message, status_code=e.status_code, body=e.body) from e
|
69
|
+
except Exception as e:
|
70
|
+
raise APIConnectionError() from e
|
@@ -0,0 +1,98 @@
|
|
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/wav"
|
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
|
+
def synthesize(
|
40
|
+
self,
|
41
|
+
text: str,
|
42
|
+
*,
|
43
|
+
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
|
44
|
+
) -> ChunkedStream:
|
45
|
+
return ChunkedStream(
|
46
|
+
tts=self,
|
47
|
+
input_text=text,
|
48
|
+
conn_options=conn_options,
|
49
|
+
opts=self._opts,
|
50
|
+
client=self._client,
|
51
|
+
)
|
52
|
+
|
53
|
+
|
54
|
+
class ChunkedStream(tts.ChunkedStream):
|
55
|
+
def __init__(
|
56
|
+
self,
|
57
|
+
*,
|
58
|
+
tts: TTS,
|
59
|
+
input_text: str,
|
60
|
+
conn_options: APIConnectOptions,
|
61
|
+
opts: _TTSOptions,
|
62
|
+
client: AsyncSpitch,
|
63
|
+
) -> None:
|
64
|
+
super().__init__(tts=tts, input_text=input_text, conn_options=conn_options)
|
65
|
+
self._client = client
|
66
|
+
self._opts = opts
|
67
|
+
|
68
|
+
async def _run(self, output_emitter: tts.AudioEmitter) -> None:
|
69
|
+
spitch_stream = self._client.speech.with_streaming_response.generate(
|
70
|
+
text=self.input_text,
|
71
|
+
language=self._opts.language, # type: ignore
|
72
|
+
voice=self._opts.voice, # type: ignore
|
73
|
+
timeout=httpx.Timeout(30, connect=self._conn_options.timeout),
|
74
|
+
)
|
75
|
+
|
76
|
+
request_id = str(uuid.uuid4().hex)[:12]
|
77
|
+
try:
|
78
|
+
async with spitch_stream as stream:
|
79
|
+
output_emitter.initialize(
|
80
|
+
request_id=request_id,
|
81
|
+
sample_rate=SAMPLE_RATE,
|
82
|
+
num_channels=NUM_CHANNELS,
|
83
|
+
mime_type=MIME_TYPE,
|
84
|
+
)
|
85
|
+
|
86
|
+
async for data in stream.iter_bytes():
|
87
|
+
output_emitter.push(data)
|
88
|
+
|
89
|
+
output_emitter.flush()
|
90
|
+
|
91
|
+
except spitch.APITimeoutError:
|
92
|
+
raise APITimeoutError() from None
|
93
|
+
except spitch.APIStatusError as e:
|
94
|
+
raise APIStatusError(
|
95
|
+
e.message, status_code=e.status_code, request_id=request_id, body=e.body
|
96
|
+
) from None
|
97
|
+
except Exception as e:
|
98
|
+
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.1.0"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: livekit-plugins-spitch
|
3
|
+
Version: 1.1.0
|
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: audio,livekit,realtime,video,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.1.0
|
22
|
+
Requires-Dist: spitch
|
23
|
+
Description-Content-Type: text/markdown
|
24
|
+
|
25
|
+
# Spitch plugin for LiveKit Agents
|
26
|
+
|
27
|
+
Support for STT and TTS with [spitch.app](https://spitch.app/).
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
```bash
|
32
|
+
pip install livekit-plugins-spitch
|
33
|
+
```
|
34
|
+
|
35
|
+
## Pre-requisites
|
36
|
+
|
37
|
+
You'll need an API key from Spitch. It can be set as an environment variable: `SPITCH_API_KEY`
|
@@ -0,0 +1,9 @@
|
|
1
|
+
livekit/plugins/spitch/__init__.py,sha256=QBKbjYlOHAEOd9R7XlkbrLEi0GAMEyHseNOru1x74PU,1090
|
2
|
+
livekit/plugins/spitch/log.py,sha256=2VykVr41ZII-NHzy0Ih2ZHUpjAYB1fWRb_7LE3xMKI8,69
|
3
|
+
livekit/plugins/spitch/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
+
livekit/plugins/spitch/stt.py,sha256=lQsiOx9sjh5csI3R2ER-SqTb610UnW0dmF-OHFLqNeA,2166
|
5
|
+
livekit/plugins/spitch/tts.py,sha256=faaj24PePvy6seODQRdK2biYjxywpCRujTZ8GGUlQeY,2706
|
6
|
+
livekit/plugins/spitch/version.py,sha256=tN2YnJRhmAfZTRu6EYiraAC9zNeLAxR74SHGVQAsb54,599
|
7
|
+
livekit_plugins_spitch-1.1.0.dist-info/METADATA,sha256=Eu1y9baDDijCN27QkqgXG7H_ShyCRA9qUXMVs8w7PSY,1243
|
8
|
+
livekit_plugins_spitch-1.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
9
|
+
livekit_plugins_spitch-1.1.0.dist-info/RECORD,,
|