skip to Main Content

I want to create async func for transcribing telegram voices (.ogg files), but when i try send request to OpenAI API i recive error:

Error code: 400 – {‘error’: {‘message’: "Unrecognized file format.
Supported formats: [‘flac’, ‘m4a’, ‘mp3’, ‘mp4’, ‘mpeg’, ‘mpga’,
‘oga’, ‘ogg’, ‘wav’, ‘webm’]", ‘type’: ‘invalid_request_error’,
‘param’: None, ‘code’: None}}

Here is code of my functions:

sync def transcribe_voice_message(file_path):
    """OpenAI Speech To Text request"""
    client = AsyncOpenAI(api_key=openai_api_key)
    try:
        async with aiofiles.open(file_path, 'rb') as f:
            file_content = await f.read()
            transcription = await client.audio.transcriptions.create(
            model="whisper-1", file=file_content
            )
    except Exception as e:
        logger.error(
            f"Open API error: {e}"
        )
    return transcription.text

I wil be aprecciate for your help

I tried different ways to solve problem, change type files to .mp3, chande object types to IO[bytes] or bytes, but always same

2

Answers


  1. Chosen as BEST ANSWER

    Solved by this message: https://stackoverflow.com/a/77616053/12834417

    In order for the Whisper API to work, the buffer with the audio-bytes has to have a name

    New code:

    async def transcribe_voice_message(file_path):
        """OpenAI Speech To Text request"""
        client = AsyncOpenAI(api_key=openai_api_key)
        try:
            async with aiofiles.open(file_path, 'rb') as f:
                file_content = await f.read()
                buffer = io.BytesIO(file_content)
                buffer.name = os.path.basename(file_path)
    
                transcription = await client.audio.transcriptions.create(
                model="whisper-1", file=buffer
                )
        except Exception as e:
            logger.error(
                f"Open API error: {e}"
            )
        return transcription.text
    

  2. There are two codecs that encode in .ogg format: opus and vorbis . Most probably OpenAI support only one of this codecs, not both of them. I would suggest to check your audio file format by running ffprobe on it:

    ffprobe your_file.ogg
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search