Uberi / speech_recognition

Speech recognition module for Python, supporting several engines and APIs, online and offline.

Home Page:https://pypi.python.org/pypi/SpeechRecognition/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

in recognize_google raise RequestError("recognition request failed: {}".format(e.reason)) speech_recognition.exceptions.RequestError: recognition request failed: Bad Request

twers1 opened this issue · comments

I have an error in recognize_google
raise RequestError("recognition request failed: {}".format(e.reason))
speech_recognition.exceptions.RequestError: recognition request failed: Bad Request

My code:

`
@dp.message_handler(content_types=types.ContentType.TEXT, text="✍️Хочу текстовое сообщение!")
async def start_get_text_message(message: types.Message):
    if message.text == '✍️Хочу текстовое сообщение!':
        print('Хочу текстовое сообщение!')
        await message.answer('Пришли голосовое сообщение')

        @dp.message_handler(content_types=types.ContentType.VOICE)
        async def start_get_voice_message(message: types.Message):
            if message.voice is None:
                await message.reply("Голосовое сообщение не обнаружено.")
                return

            # Download the audio file sent by the user
            file_info = await bot.get_file(message.voice.file_id)
            audio_file = await bot.download_file(file_info.file_path)

            # Converting speech to text
            audio_text = audio_to_text(audio_file)

            # Reply to the user with the converted text
            await message.reply(audio_text)`
            
            ` 
os.makedirs("temp", exist_ok=True)

# Если не работает предыдущий код, то попробуйте этот
audio_file_path = "temp/audio.ogg"
if not os.path.exists(audio_file_path):
    with open(audio_file_path, "wb") as file:
        pass


# Функция для удаления после распознавания и окончания конвертации
def remove_audio_files():
    if os.path.exists("temp/audio.ogg"):
        os.remove("temp/audio.ogg")
    if os.path.exists("temp/audio.wav"):
        os.remove("temp/audio.wav")


# Функция конвертации голосового сообщения в текст
def audio_to_text(audio_data, language: str = 'ru-RU'):
    audio_data_bytes = audio_data.read()  # Convert `_io.BytesIO` to bytes
    with open("temp/audio.ogg", "wb") as file:
        file.write(audio_data_bytes)

    # Конвертация в WAV
    process = subprocess.run(["ffmpeg", "-i", "temp/audio.ogg", "temp/audio.wav"])
    if process.returncode != 0:
        raise Exception("Something went wrong")

    with sr.AudioFile("temp/audio.wav") as source:
        r = sr.Recognizer()
        audio = r.record(source)
        audio_text = r.recognize_google(audio)
        response = audio_text
        remove_audio_files()
        return response
        # except:
        #     response = "Слова не распознаны. Попробуйте еще раз!💔"`

how to solve this problem? Please advise me if you know