google.api_core.exceptions.InvalidArgument: 400 RecognitionAudio not set

Viewed 827

I am having a problem with the Google Cloud Speech API, every time I run the script the error

six.raise_from (exceptions.from_grpc_error (exc), exc) occurs
 File "<string>", line 3, in raise_from
 google.api_core.exceptions.InvalidArgument: 400 RecognitionAudio not set.

he doesn't seem to recognize RecognitionAudio for some reason, I already checked the API documentation but I couldn't solve the problem I am not understanding the reason for the error, I will leave my code here in case anyone knows and can help me, thanks

import telebot
import requests
from pydub import AudioSegment

import os
import io

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./chatbot.json"

token = "1233361335"
bot = telebot.TeleBot(token)
downloadAudio = "https://api.telegram.org/file/bot{token}/".format(token = token)

@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.reply_to(message, "welcome")

@bot.message_handler(content_types=['voice'])
def handlerAudio(message):

    #get audio from telegram 
    messageVoice = message.voice

    #get download link 
    audioPath = bot.get_file(messageVoice.file_id).file_path
    audioLink = downloadAudio+audioPath

    #download file
    audioFile = requests.get(audioLink)
    audioName = "audio.ogg"

    #save locally
    open(audioName, 'wb').write(audioFile.content)

    #convert format to .WAV
    AudioSegment.from_file(audioName).export("audio.wav", format="wav")
    sound = AudioSegment.from_wav("audio.wav")
    sound = sound.set_channels(1) #convert mono
    sound.export("audio.wav", format="wav")

client = speech.SpeechClient()

with io.open("audio.wav", 'rb') as audio_file:
       content = audio_file.read()

audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=48000,
    language_code='pt-BR')

response = client.recognize(config, audio)

for result in response.results:
  print(u'Transcript: {}'.format(result.alternatives[0].transcript))
  #bot.reply_to(message, result.alternatives[0].transcript)

bot.polling()
0 Answers
Related