Whisper API breaks on AWS Lambda

Hey there, I am having issues getting the whisper API to run when deploying to AWS Lambda since I keep getting

openai.Bad Request Error: Error code: 400 - {error: {message: Invalid file format. Supported formats: [flac, m4a, mp3, mp4, mpeg, mpga, oga, ogg, wav, webm], type: invalid_request_error, param: None, code: None}}

Running this locally on my mac works without issue, even when using the same python:3.9 Docker image that I use when deploying this thing on Lambda.

def transcribeRoute():
    audio_file = request.files.get('audio', None)

    if audio_file:
        print(audio_file) # <FileStorage: 'blob' ('audio/webm;codecs=opus')>

        buffer = BytesIO(audio_file.read())
        buffer.name = "test.webm"

        transcript = client.audio.transcriptions.create(model="whisper-1", file=buffer).text

        print(f"Transcribed audio: {transcript}")
        return {"transcription": transcript}, 200

    print("No audio file found")
    return {"transcription": "error"}, 400

Any help is greatly appreciated!

I am running something like this on Lambda. Problem I had from day one was in the OAI SDK, so just use the API without it using requests directly.

In Lambda, you just use /tmp as your local directory.


Hmm, that didn’t seem to help, both when saving it to /temp and just using BytesIO. I am thinking that maybe it has something to do with dependencies. I have installed both opus-tools, libopus0, and ffmpeg but that didn’t do nothing either. I am gonna try to convert it to a different format with ffmpeg firs, maybe that is gonna work

1 Like

I pretty much only use wave files. But I hear it can be finicky on exact formats. So convert it to a know “good” format, then troubleshoot from there. :+1:

1 Like

After much trying and researching the problem was a mix of 2 issues:
a) In order for the Whisper API to work, the buffer with the audio-bytes has to have a name (which happens automatically when you write and read it to the file, just make sure you have the right extension).
b) The AWS API-Gateway doesn’t support binary data in requests by default, and you have to manually allow it. F.e. if you are using Serverless Framework for deployment add this:

      - '*/*'

Lifesaver, thank you! This did the trick.

  # Create a buffer with the file content and set the file name
    buffer = BytesIO(file_content)
    buffer.name = file_name