Google Cloud Speech to Text API not working on Python

44 views Asked by At

I've tried multiple things but the code just ain't working. I don't even know why. Can anyone understand what's happening here? Many thanks, for real.

import io
from google.oauth2 import service_account
from google.cloud import speech

client_file = 'key.json'
credentials = service_account.Credentials.from_service_account_file(client_file)
client = speech.SpeechClient(credentials=credentials)

audio_file = 'record.flac'
with io.open(audio_file, 'rb') as f:
    content = f.read()
    audio = speech.RecognitionAudio(content=content)

config = speech.RecognitionConfig(
    encoding =speech.RecognitionConfig.AudioEncoding.FLAC,
    sample_rate_hertz=48000,
    language_code='es_ES'
)

response = client.recognize(config=config,audio=audio)
for result in response.results:
    print(f"Transcript: {result.alternatives[0].transcript}")

With this code I get the following Visual Studio Code error:

  File "C:\Python312\Lib\site-packages\google\api_core\grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 audio_channel_count `1` in RecognitionConfig must either be unspecified or match the value in the FLAC header `2`.

I'm a newbie programmer by the way, so most likely the error is due to something very stupid, but thanks again if someone manages to find what's happening :)

0

There are 0 answers