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

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

Open
twers1 opened this issue Jun 29, 2023 · 0 comments

Comments

@twers1
Copy link

twers1 commented Jun 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant