Skip to content
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

feat(google-cloud-speech): Delegate to google-auth #811

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ Google Cloud Speech Library for Python (for Google Cloud Speech-to-Text API user
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The library `google-cloud-speech <https://pypi.org/project/google-cloud-speech/>`__ is **required if and only if you want to use Google Cloud Speech-to-Text API** (``recognizer_instance.recognize_google_cloud``).

You can install it with :command:`python3 -m pip install SpeechRecognition[google-cloud]`.
(ref: `official installation instructions <https://cloud.google.com/speech-to-text/docs/transcribe-client-libraries#install_the_client_library>`__)
(ref: `official installation instructions <https://cloud.google.com/speech-to-text/docs/transcribe-client-libraries#client-libraries-install-python>`__)

**Prerequisite**: Create local authentication credentials for your Google account

* `Before you begin (Transcribe speech to text by using client libraries) <https://cloud.google.com/speech-to-text/docs/transcribe-client-libraries#before-you-begin>`__
* Detail: `User credentials (Set up ADC for a local development environment) <https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment#local-user-cred>`__

Currently only `V1 <https://cloud.google.com/speech-to-text/docs/quickstart>`__ is supported. (`V2 <https://cloud.google.com/speech-to-text/v2/docs/quickstart>`__ is not supported)

Expand Down
3 changes: 0 additions & 3 deletions speech_recognition/recognizers/google_cloud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
from urllib.error import URLError

from speech_recognition.audio import AudioData
Expand Down Expand Up @@ -42,8 +41,6 @@ def recognize(
assert isinstance(
audio_data, AudioData
), "``audio_data`` must be audio data"
if credentials_json_path is None:
assert os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") is not None
assert isinstance(language, str), "``language`` must be a string"
assert preferred_phrases is None or all(
isinstance(preferred_phrases, (type(""), type("")))
Expand Down
18 changes: 3 additions & 15 deletions tests/recognizers/test_google_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@


@patch("google.cloud.speech.SpeechClient")
def test_transcribe_with_google_cloud_speech(SpeechClient, monkeypatch):
monkeypatch.setenv(
"GOOGLE_APPLICATION_CREDENTIALS", "path/to/credentials.json"
)

def test_transcribe_with_google_cloud_speech(SpeechClient):
client = SpeechClient.return_value
# ref: https://cloud.google.com/speech-to-text/docs/transcribe-gcloud?hl=ja#make_an_audio_transcription_request
client.recognize.return_value = RecognizeResponse(
Expand Down Expand Up @@ -84,11 +80,7 @@ def test_transcribe_with_specified_credentials(SpeechClient):


@patch("google.cloud.speech.SpeechClient")
def test_transcribe_show_all(SpeechClient, monkeypatch):
monkeypatch.setenv(
"GOOGLE_APPLICATION_CREDENTIALS", "path/to/credentials.json"
)

def test_transcribe_show_all(SpeechClient):
client = SpeechClient.return_value
client.recognize.return_value = RecognizeResponse(
results=[
Expand Down Expand Up @@ -151,11 +143,7 @@ def test_transcribe_show_all(SpeechClient, monkeypatch):


@patch("google.cloud.speech.SpeechClient")
def test_transcribe_with_specified_api_parameters(SpeechClient, monkeypatch):
monkeypatch.setenv(
"GOOGLE_APPLICATION_CREDENTIALS", "path/to/credentials.json"
)

def test_transcribe_with_specified_api_parameters(SpeechClient):
client = SpeechClient.return_value
client.recognize.return_value = RecognizeResponse(
results=[
Expand Down
Loading