-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(cambai): Add camb.ai realtime speech-to-speech translation #7305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1fdd09a
e8533a7
adb8d58
073411c
9367d32
1900739
b863858
edecc99
f206007
055d8d3
231198f
f42bce2
5a0347f
7c2f5df
b453947
8a62b08
1e42f35
abfeb9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """Live speech-to-speech translation with Camb.ai realtime. | ||
|
|
||
| Every participant who publishes audio gets a translated track published back into the | ||
| room, named ``translated-<target language>``, plus the source transcript and the | ||
| translated text on the console. One websocket per speaker does the whole job: no STT, | ||
| no LLM, no TTS. | ||
|
|
||
| Run it with a Camb.ai key and LiveKit credentials in the environment: | ||
|
|
||
| export CAMB_API_KEY=... | ||
| export LIVEKIT_URL=... LIVEKIT_API_KEY=... LIVEKIT_API_SECRET=... | ||
| python camb_realtime_translator.py dev | ||
|
|
||
| The realtime endpoint speaks 24 kHz mono PCM16; room audio is resampled for you. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| from livekit import rtc | ||
| from livekit.agents import AgentServer, AutoSubscribe, JobContext, cli | ||
| from livekit.plugins import cambai | ||
|
|
||
| load_dotenv() | ||
|
|
||
| logger = logging.getLogger("camb-translator") | ||
|
|
||
| SOURCE_LANGUAGE = "en-US" | ||
| TARGET_LANGUAGE = "fr-FR" | ||
|
|
||
| # "fast" begins speaking sooner; "slow" covers a longer language list. Both translate | ||
| # every complete utterance, so a live translator wants the lower latency. | ||
| MODE = "fast" | ||
|
|
||
| SAMPLE_RATE = 24000 | ||
| NUM_CHANNELS = 1 | ||
|
|
||
| # The event loop only holds a weak reference to a task, so keep them alive here. | ||
| _tasks: set[asyncio.Task[None]] = set() | ||
|
|
||
|
|
||
| def _spawn(coro: asyncio.coroutines) -> None: | ||
| task = asyncio.create_task(coro) | ||
| _tasks.add(task) | ||
| task.add_done_callback(_tasks.discard) | ||
|
|
||
|
|
||
| async def translate_track(ctx: JobContext, track: rtc.Track, identity: str) -> None: | ||
| """Translate one participant's audio and publish the result as its own track.""" | ||
| model = cambai.experimental.realtime.RealtimeModel( | ||
| source_language=SOURCE_LANGUAGE, | ||
| target_language=TARGET_LANGUAGE, | ||
| mode=MODE, | ||
| ) | ||
| session = model.session() | ||
|
|
||
| source = rtc.AudioSource(SAMPLE_RATE, NUM_CHANNELS) | ||
| publication = await ctx.room.local_participant.publish_track( | ||
| rtc.LocalAudioTrack.create_audio_track(f"translated-{TARGET_LANGUAGE}", source), | ||
| rtc.TrackPublishOptions(source=rtc.TrackSource.SOURCE_MICROPHONE), | ||
| ) | ||
| logger.info("translating %s into %s on %s", identity, TARGET_LANGUAGE, publication.sid) | ||
|
|
||
| @session.on("input_audio_transcription_completed") | ||
| def _on_transcript(ev: object) -> None: | ||
| logger.info("%s said: %s", identity, ev.transcript) # type: ignore[attr-defined] | ||
|
Comment on lines
+68
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Showing the translated text alongside the source is the point of the example, and examples/other/translation/multi-user-translator.py:203 in this same directory logs the translated transcript already |
||
|
|
||
| @session.on("error") | ||
| def _on_error(ev: object) -> None: | ||
| logger.error("translation failed for %s: %s", identity, ev.error) # type: ignore[attr-defined] | ||
|
|
||
| @session.on("generation_created") | ||
| def _on_generation(ev: object) -> None: | ||
| async def show_text(message: object) -> None: | ||
| text = "" | ||
| async for chunk in message.text_stream: # type: ignore[attr-defined] | ||
| text += chunk | ||
| if text: | ||
| logger.info("%s translated: %s", identity, text) | ||
|
Comment on lines
+82
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is an example proof printing what the speaker said is what this example is demonstrating, examples/other/transcription/multi-user-transcriber.py:40 logs {participant_identity} -> {user_transcript} identically. |
||
|
|
||
| async def play_audio(message: object) -> None: | ||
| async for frame in message.audio_stream: # type: ignore[attr-defined] | ||
| await source.capture_frame(frame) | ||
|
|
||
| async def deliver() -> None: | ||
| async for msg in ev.message_stream: # type: ignore[attr-defined] | ||
| await asyncio.gather(show_text(msg), play_audio(msg)) | ||
|
|
||
| _spawn(deliver()) | ||
|
|
||
| try: | ||
| async for event in rtc.AudioStream(track): | ||
| session.push_audio(event.frame) | ||
| finally: | ||
| await session.aclose() | ||
| await model.aclose() | ||
|
|
||
|
|
||
| server = AgentServer() | ||
|
|
||
|
|
||
| @server.rtc_session() | ||
| async def entrypoint(ctx: JobContext) -> None: | ||
| await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) | ||
|
|
||
| @ctx.room.on("track_subscribed") | ||
| def _on_track( | ||
| track: rtc.Track, | ||
| publication: rtc.RemoteTrackPublication, | ||
| participant: rtc.RemoteParticipant, | ||
| ) -> None: | ||
| if track.kind == rtc.TrackKind.KIND_AUDIO: | ||
| _spawn(translate_track(ctx, track, participant.identity)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli.run_app(server) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Copyright 2023 LiveKit, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from . import realtime | ||
|
|
||
| __all__ = ["realtime"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Copyright 2023 LiveKit, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from .realtime_model import RealtimeModel, RealtimeSession | ||
|
|
||
| __all__ = ["RealtimeModel", "RealtimeSession"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟨 Participant identity enters unredacted logs
Starting a translation logs the remote participant identity in the message body. Standard redaction cannot protect this PII-sensitive value.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the example's startup line showing which participant is being translated and on which track. examples/other/transcription/multi-user-transcriber.py:67 logs starting session for {participant.identity} the same way