livekit-plugins-bithuman 1.0.14__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,168 @@
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
+ # Pyre type checker
150
+ .pyre/
151
+
152
+ # pytype static type analyzer
153
+ .pytype/
154
+
155
+ # Cython debug symbols
156
+ cython_debug/
157
+
158
+ # PyCharm
159
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
160
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
161
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
162
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
163
+ .idea/
164
+
165
+ node_modules
166
+
167
+ credentials.json
168
+ pyrightconfig.json
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: livekit-plugins-bithuman
3
+ Version: 1.0.14
4
+ Summary: Agent Framework plugin for services from BitHuman Avatar Rendering
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-email: LiveKit <support@livekit.io>
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>=1.0.14
22
+ Description-Content-Type: text/markdown
23
+
24
+ # LiveKit Plugins BitHuman Avatar Runtime
25
+
26
+ Agent Framework Plugin for avatars with [bitHuman](https://www.bithuman.ai/)'s local runtime SDK.
@@ -0,0 +1,3 @@
1
+ # LiveKit Plugins BitHuman Avatar Runtime
2
+
3
+ Agent Framework Plugin for avatars with [bitHuman](https://www.bithuman.ai/)'s local runtime SDK.
@@ -0,0 +1,35 @@
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
+
16
+ from .avatar import AvatarSession, BitHumanException
17
+ from .version import __version__
18
+
19
+ __all__ = [
20
+ "BitHumanException",
21
+ "AvatarSession",
22
+ "__version__",
23
+ ]
24
+
25
+ from livekit.agents import Plugin
26
+
27
+ from .log import logger
28
+
29
+
30
+ class BitHumanPlugin(Plugin):
31
+ def __init__(self) -> None:
32
+ super().__init__(__name__, __version__, __package__, logger)
33
+
34
+
35
+ Plugin.register_plugin(BitHumanPlugin())
@@ -0,0 +1,155 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import sys
5
+ from collections.abc import AsyncGenerator, AsyncIterator
6
+
7
+ import cv2
8
+ import numpy as np
9
+ from loguru import logger as _logger
10
+
11
+ from bithuman import AsyncBithuman
12
+ from livekit import rtc
13
+ from livekit.agents import NOT_GIVEN, AgentSession, NotGivenOr, utils
14
+ from livekit.agents.voice.avatar import (
15
+ AudioSegmentEnd,
16
+ AvatarOptions,
17
+ AvatarRunner,
18
+ QueueAudioOutput,
19
+ VideoGenerator,
20
+ )
21
+
22
+ from .log import logger
23
+
24
+ _logger.remove()
25
+ _logger.add(sys.stdout, level="INFO")
26
+
27
+
28
+ class BitHumanException(Exception):
29
+ """Exception for BitHuman errors"""
30
+
31
+
32
+ class AvatarSession:
33
+ """A Beyond Presence avatar session"""
34
+
35
+ def __init__(
36
+ self,
37
+ *,
38
+ model_path: NotGivenOr[str | None] = NOT_GIVEN,
39
+ api_url: NotGivenOr[str] = NOT_GIVEN,
40
+ api_secret: NotGivenOr[str] = NOT_GIVEN,
41
+ api_token: NotGivenOr[str] = NOT_GIVEN,
42
+ ) -> None:
43
+ self._api_url = api_url or os.getenv("BITHUMAN_API_URL")
44
+ self._api_secret = api_secret or os.getenv("BITHUMAN_API_SECRET")
45
+ self._api_token = api_token or os.getenv("BITHUMAN_API_TOKEN")
46
+ self._model_path = model_path or os.getenv("BITHUMAN_MODEL_PATH")
47
+
48
+ if self._api_secret is None and self._api_token is None:
49
+ raise BitHumanException("BITHUMAN_API_SECRET or BITHUMAN_API_TOKEN must be set")
50
+ if self._model_path is None:
51
+ raise BitHumanException("BITHUMAN_MODEL_PATH must be set")
52
+
53
+ self._avatar_runner: AvatarRunner | None = None
54
+
55
+ async def start(self, agent_session: AgentSession, room: rtc.Room) -> None:
56
+ kwargs = {
57
+ "model_path": self._model_path,
58
+ }
59
+ if self._api_secret:
60
+ kwargs["api_secret"] = self._api_secret
61
+ if self._api_token:
62
+ kwargs["token"] = self._api_token
63
+ if self._api_url:
64
+ kwargs["api_url"] = self._api_url
65
+
66
+ runtime = await AsyncBithuman.create(**kwargs)
67
+ await runtime.start()
68
+ video_generator = BithumanGenerator(runtime)
69
+
70
+ output_width, output_height = video_generator.video_resolution
71
+ avatar_options = AvatarOptions(
72
+ video_width=output_width,
73
+ video_height=output_height,
74
+ video_fps=video_generator.video_fps,
75
+ audio_sample_rate=video_generator.audio_sample_rate,
76
+ audio_channels=1,
77
+ )
78
+
79
+ audio_buffer = QueueAudioOutput(sample_rate=runtime.settings.INPUT_SAMPLE_RATE)
80
+ # create avatar runner
81
+ self._avatar_runner = AvatarRunner(
82
+ room=room,
83
+ video_gen=video_generator,
84
+ audio_recv=audio_buffer,
85
+ options=avatar_options,
86
+ )
87
+ await self._avatar_runner.start()
88
+
89
+ agent_session.output.audio = audio_buffer
90
+
91
+
92
+ class BithumanGenerator(VideoGenerator):
93
+ def __init__(self, runtime: AsyncBithuman):
94
+ self._runtime = runtime
95
+
96
+ @property
97
+ def video_resolution(self) -> tuple[int, int]:
98
+ frame = self._runtime.get_first_frame()
99
+ if frame is None:
100
+ raise ValueError("Failed to read frame")
101
+ return frame.shape[1], frame.shape[0]
102
+
103
+ @property
104
+ def video_fps(self) -> int:
105
+ return self._runtime.settings.FPS
106
+
107
+ @property
108
+ def audio_sample_rate(self) -> int:
109
+ return self._runtime.settings.INPUT_SAMPLE_RATE
110
+
111
+ @utils.log_exceptions(logger=logger)
112
+ async def push_audio(self, frame: rtc.AudioFrame | AudioSegmentEnd) -> None:
113
+ if isinstance(frame, AudioSegmentEnd):
114
+ await self._runtime.flush()
115
+ return
116
+ await self._runtime.push_audio(bytes(frame.data), frame.sample_rate, last_chunk=False)
117
+
118
+ def clear_buffer(self) -> None:
119
+ self._runtime.interrupt()
120
+
121
+ def __aiter__(self) -> AsyncIterator[rtc.VideoFrame | rtc.AudioFrame | AudioSegmentEnd]:
122
+ return self._stream_impl()
123
+
124
+ async def _stream_impl(
125
+ self,
126
+ ) -> AsyncGenerator[rtc.VideoFrame | rtc.AudioFrame | AudioSegmentEnd, None]:
127
+ def create_video_frame(image: np.ndarray) -> rtc.VideoFrame:
128
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
129
+ return rtc.VideoFrame(
130
+ width=image.shape[1],
131
+ height=image.shape[0],
132
+ type=rtc.VideoBufferType.RGBA,
133
+ data=image.tobytes(),
134
+ )
135
+
136
+ async for frame in self._runtime.run():
137
+ if frame.bgr_image is not None:
138
+ video_frame = create_video_frame(frame.bgr_image)
139
+ yield video_frame
140
+
141
+ audio_chunk = frame.audio_chunk
142
+ if audio_chunk is not None:
143
+ audio_frame = rtc.AudioFrame(
144
+ data=audio_chunk.bytes,
145
+ sample_rate=audio_chunk.sample_rate,
146
+ num_channels=1,
147
+ samples_per_channel=len(audio_chunk.array),
148
+ )
149
+ yield audio_frame
150
+
151
+ if frame.end_of_speech:
152
+ yield AudioSegmentEnd()
153
+
154
+ async def stop(self) -> None:
155
+ await self._runtime.stop()
@@ -0,0 +1,3 @@
1
+ import logging
2
+
3
+ logger = logging.getLogger("livekit.plugins.bithuman")
@@ -0,0 +1,15 @@
1
+ # Copyright 2025 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.0.14"
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "livekit-plugins-bithuman"
7
+ dynamic = ["version"]
8
+ description = "Agent Framework plugin for services from BitHuman Avatar Rendering"
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ requires-python = ">=3.9.0"
12
+ authors = [{ name = "LiveKit", email = "support@livekit.io" }]
13
+ keywords = ["webrtc", "realtime", "audio", "video", "livekit"]
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>=1.0.14"]
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/bithuman/version.py"
34
+
35
+ [tool.hatch.build.targets.wheel]
36
+ packages = ["livekit"]
37
+
38
+ [tool.hatch.build.targets.sdist]
39
+ include = ["/livekit"]