From af232005112d0014d01b6735b6ed4e3ce3dadd02 Mon Sep 17 00:00:00 2001 From: Shaiyon Hariri Date: Thu, 3 Apr 2025 16:42:58 -0400 Subject: [PATCH] Use default google creds as fallback when not provided in llm_vertex,stt, and tts --- src/pipecat/services/google/llm_vertex.py | 9 +++++++++ src/pipecat/services/google/stt.py | 14 +++++++++++++- src/pipecat/services/google/tts.py | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/pipecat/services/google/llm_vertex.py b/src/pipecat/services/google/llm_vertex.py index 1a23fe7d5..e2a1267f3 100644 --- a/src/pipecat/services/google/llm_vertex.py +++ b/src/pipecat/services/google/llm_vertex.py @@ -17,6 +17,8 @@ from loguru import logger from pipecat.services.openai.llm import OpenAILLMService try: + from google.auth import default + from google.auth.exceptions import GoogleAuthError from google.auth.transport.requests import Request from google.oauth2 import service_account @@ -98,6 +100,13 @@ class GoogleVertexLLMService(OpenAILLMService): creds = service_account.Credentials.from_service_account_file( credentials_path, scopes=["https://www.googleapis.com/auth/cloud-platform"] ) + else: + try: + creds, project_id = default( + scopes=["https://www.googleapis.com/auth/cloud-platform"] + ) + except GoogleAuthError: + pass if not creds: raise ValueError("No valid credentials provided.") diff --git a/src/pipecat/services/google/stt.py b/src/pipecat/services/google/stt.py index 8c79bb89a..cf0385857 100644 --- a/src/pipecat/services/google/stt.py +++ b/src/pipecat/services/google/stt.py @@ -32,6 +32,8 @@ from pipecat.utils.time import time_now_iso8601 try: from google.api_core.client_options import ClientOptions + from google.auth import default + from google.auth.exceptions import GoogleAuthError from google.cloud import speech_v2 from google.cloud.speech_v2.types import cloud_speech from google.oauth2 import service_account @@ -451,6 +453,7 @@ class GoogleSTTService(STTService): client_options = ClientOptions(api_endpoint=f"{self._location}-speech.googleapis.com") # Extract project ID and create client + creds: Optional[service_account.Credentials] = None if credentials: json_account_info = json.loads(credentials) self._project_id = json_account_info.get("project_id") @@ -461,7 +464,16 @@ class GoogleSTTService(STTService): self._project_id = json_account_info.get("project_id") creds = service_account.Credentials.from_service_account_file(credentials_path) else: - raise ValueError("Either credentials or credentials_path must be provided") + try: + creds, project_id = default( + scopes=["https://www.googleapis.com/auth/cloud-platform"] + ) + self._project_id = project_id + except GoogleAuthError: + pass + + if not creds: + raise ValueError("No valid credentials provided.") if not self._project_id: raise ValueError("Project ID not found in credentials") diff --git a/src/pipecat/services/google/tts.py b/src/pipecat/services/google/tts.py index ef9023a8c..acbe80de5 100644 --- a/src/pipecat/services/google/tts.py +++ b/src/pipecat/services/google/tts.py @@ -27,6 +27,8 @@ from pipecat.services.tts_service import TTSService from pipecat.transcriptions.language import Language try: + from google.auth import default + from google.auth.exceptions import GoogleAuthError from google.cloud import texttospeech_v1 from google.oauth2 import service_account @@ -251,6 +253,16 @@ class GoogleTTSService(TTSService): elif credentials_path: # Use service account JSON file if provided creds = service_account.Credentials.from_service_account_file(credentials_path) + else: + try: + creds, project_id = default( + scopes=["https://www.googleapis.com/auth/cloud-platform"] + ) + except GoogleAuthError: + pass + + if not creds: + raise ValueError("No valid credentials provided.") return texttospeech_v1.TextToSpeechAsyncClient(credentials=creds)