Skip to content

Commit

Permalink
Enable Google TTS with application default credentials (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinnet authored Sep 8, 2024
1 parent d3dc608 commit 24f0d51
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-bikes-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-plugins-google": minor
---

Enable use of Google STT with Application Default Credentials.
2 changes: 1 addition & 1 deletion livekit-plugins/livekit-plugins-google/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pip install livekit-plugins-google

## Pre-requisites

For credentials, you'll need a Google Cloud account and obtain the correct credentials. Credentials can be passed directly or set as [GOOGLE_APPLICATION_CREDENTIALS](https://cloud.google.com/docs/authentication/application-default-credentials) environment variable.
For credentials, you'll need a Google Cloud account and obtain the correct credentials. Credentials can be passed directly or via Application Default Credentials as specified in [How Application Default Credentials works](https://cloud.google.com/docs/authentication/application-default-credentials).
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from livekit import agents, rtc
from livekit.agents import stt, utils

from google.auth import default as gauth_default
from google.auth.exceptions import DefaultCredentialsError
from google.cloud.speech_v2 import SpeechAsyncClient
from google.cloud.speech_v2.types import cloud_speech

Expand Down Expand Up @@ -61,8 +63,8 @@ def __init__(
Create a new instance of Google STT.
Credentials must be provided, either by using the ``credentials_info`` dict, or reading
from the file specified in ``credentials_file`` or the ``GOOGLE_APPLICATION_CREDENTIALS``
environmental variable.
from the file specified in ``credentials_file`` or via Application Default Credentials as
described in https://cloud.google.com/docs/authentication/application-default-credentials
"""
super().__init__(
capabilities=stt.STTCapabilities(streaming=True, interim_results=True)
Expand All @@ -73,10 +75,13 @@ def __init__(
self._credentials_file = credentials_file

if credentials_file is None and credentials_info is None:
creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
if not creds:
try:
gauth_default()
except DefaultCredentialsError:
raise ValueError(
"GOOGLE_APPLICATION_CREDENTIALS must be set if no credentials is provided"
"Application default credentials must be available "
"when using Google STT without explicitly passing "
"credentials through credentials_info or credentials_file."
)

if isinstance(languages, str):
Expand Down Expand Up @@ -112,7 +117,11 @@ def _recognizer(self) -> str:
# recognizers may improve latency https://cloud.google.com/speech-to-text/v2/docs/recognizers#understand_recognizers

# TODO(theomonnom): find a better way to access the project_id
project_id = self._ensure_client().transport._credentials.project_id # type: ignore
try:
project_id = self._ensure_client().transport._credentials.project_id # type: ignore
except AttributeError:
from google.auth import default as ga_default
_, project_id = ga_default()
return f"projects/{project_id}/locations/global/recognizers/_"

def _sanitize_options(self, *, language: str | None = None) -> STTOptions:
Expand Down
1 change: 1 addition & 0 deletions livekit-plugins/livekit-plugins-google/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
packages=setuptools.find_namespace_packages(include=["livekit.*"]),
python_requires=">=3.9.0",
install_requires=[
"google-auth >= 2, < 3",
"google-cloud-speech >= 2, < 3",
"google-cloud-texttospeech >= 2, < 3",
"livekit-agents>=0.8.0.dev0",
Expand Down

0 comments on commit 24f0d51

Please sign in to comment.