-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(genai): genai live audio #13520
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
Open
Guiners
wants to merge
7
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/genai_live_audio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6523796
adding live_txt_with_audio with test
9b400a3
adding live_audio_with_txt with test
973f133
adding live_audio_with_txt with test
4858df1
adding live_transcribe_with_audio with test
359384b
Update genai/live/test_live_examples.py
Guiners 705a4bc
Merge remote-tracking branch 'upstream/main' into sample/genai_live_a…
68ae455
codereview fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
|
||
# Test file: https://storage.googleapis.com/generativeai-downloads/data/16000.wav | ||
# Install helpers for converting files: pip install librosa soundfile | ||
|
||
import asyncio | ||
|
||
|
||
async def generate_content() -> list[str]: | ||
# [START googlegenaisdk_live_audio_with_txt] | ||
import numpy as np | ||
from IPython.display import Audio, Markdown, display | ||
from google import genai | ||
from google.genai.types import ( | ||
Content, | ||
LiveConnectConfig, | ||
Modality, | ||
Part, | ||
SpeechConfig, | ||
VoiceConfig, | ||
PrebuiltVoiceConfig, | ||
) | ||
|
||
client = genai.Client() | ||
voice_name = "Aoede" | ||
model = "gemini-2.0-flash-live-preview-04-09" | ||
|
||
config = LiveConnectConfig( | ||
response_modalities=[Modality.AUDIO], | ||
speech_config=SpeechConfig( | ||
voice_config=VoiceConfig( | ||
prebuilt_voice_config=PrebuiltVoiceConfig( | ||
voice_name=voice_name, | ||
) | ||
), | ||
), | ||
) | ||
|
||
async with client.aio.live.connect( | ||
model=model, | ||
config=config, | ||
) as session: | ||
text_input = "Hello? Gemini are you there?" | ||
print("> ", text_input, "\n") | ||
|
||
await session.send_client_content( | ||
turns=Content(role="user", parts=[Part(text=text_input)]) | ||
) | ||
|
||
audio_data = [] | ||
async for message in session.receive(): | ||
if ( | ||
message.server_content.model_turn | ||
and message.server_content.model_turn.parts | ||
): | ||
for part in message.server_content.model_turn.parts: | ||
if part.inline_data: | ||
audio_data.append( | ||
np.frombuffer(part.inline_data.data, dtype=np.int16) | ||
) | ||
|
||
if audio_data: | ||
print("Received audio answer: ") | ||
display(Audio(np.concatenate(audio_data), rate=24000, autoplay=True)) | ||
|
||
# Example output: | ||
# > Hello? Gemini are you there? | ||
# Received audio answer: | ||
# <IPython.lib.display.Audio object> | ||
# [END googlegenaisdk_live_audio_with_txt] | ||
return [] | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(generate_content()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
|
||
# Test file: https://storage.googleapis.com/generativeai-downloads/data/16000.wav | ||
# Install helpers for converting files: pip install librosa soundfile | ||
|
||
import asyncio | ||
|
||
|
||
async def generate_content() -> list[str]: | ||
# [START googlegenaisdk_live_txt_with_audio] | ||
import io | ||
import requests | ||
from google import genai | ||
from google.genai.types import Modality, LiveConnectConfig, Blob | ||
import soundfile as sf | ||
import librosa | ||
|
||
client = genai.Client() | ||
model = "gemini-2.0-flash-live-preview-04-09" | ||
config = LiveConnectConfig(response_modalities=[Modality.TEXT]) | ||
|
||
async with client.aio.live.connect(model=model, config=config) as session: | ||
audio_url = ( | ||
"https://storage.googleapis.com/generativeai-downloads/data/16000.wav" | ||
) | ||
response = requests.get(audio_url) | ||
response.raise_for_status() | ||
buffer = io.BytesIO(response.content) | ||
y, sr = librosa.load(buffer, sr=16000) | ||
sf.write(buffer, y, sr, format="RAW", subtype="PCM_16") | ||
buffer.seek(0) | ||
audio_bytes = buffer.read() | ||
|
||
# If you've pre-converted to sample.pcm using ffmpeg, use this instead: | ||
# audio_bytes = Path("sample.pcm").read_bytes() | ||
|
||
print("> Answer to this audio url", audio_url, "\n") | ||
|
||
await session.send_realtime_input( | ||
media=Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000") | ||
) | ||
|
||
response = [] | ||
|
||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async for message in session.receive(): | ||
if message.text is not None: | ||
response.append(message.text) | ||
|
||
print("".join(response)) | ||
# Example output: | ||
# > Answer to this audio url https://storage.googleapis.com/generativeai-downloads/data/16000.wav | ||
# Yes, I can hear you. How can I help you today? | ||
# [END googlegenaisdk_live_txt_with_audio] | ||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(generate_content()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.