Whisper OpenAI error in Streamlit: InvalidRequestError

Assume that, the API Key correctly used here. For privacy issues, replaced the API Key with paste your code here. I’m trying this below code, in Streamlit. My motive is to read the audio file and show the transcript on the app. While doing that, I got below error.

import streamlit as st
import os
import openai

os.environ['OPENAI_API_KEY'] = 'paste your code here'
openai.api_key = os.environ['OPENAI_API_KEY']

st.title('OpenAIs Whisper')

# Upload audio file
audio_file = st.file_uploader("Upload audio file", type=['wav', 'mp3', 'ogg'])

# Load audio file
if audio_file is not None:
    audio_bytes = audio_file.read()
    st.audio(audio_bytes, format='audio/ogg')

# Process audio file in Whisper OpenAI API
if st.button('Process audio file'):
    # print the audio file format
    transcript = openai.Audio.transcribe("whisper-1", audio_file)

The above code returns the error as:

Traceback (most recent call last):
  File "/home/harima/Desktop/whisper ai test/env/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/home/harima/Desktop/whisper ai test/test_whisper.py", line 25, in <module>
    transcript = openai.Audio.transcribe("whisper-1", audio_file)
  File "/home/harima/Desktop/whisper ai test/env/lib/python3.10/site-packages/openai/api_resources/audio.py", line 65, in transcribe
    response, _, api_key = requestor.request("post", url, files=files, params=data)
  File "/home/harima/Desktop/whisper ai test/env/lib/python3.10/site-packages/openai/api_requestor.py", line 230, in request
    resp, got_stream = self._interpret_response(result, stream)
  File "/home/harima/Desktop/whisper ai test/env/lib/python3.10/site-packages/openai/api_requestor.py", line 624, in _interpret_response
  File "/home/harima/Desktop/whisper ai test/env/lib/python3.10/site-packages/openai/api_requestor.py", line 687, in _interpret_response_line
    raise self.handle_error_response(
openai.error.InvalidRequestError: Invalid file format. Supported formats: ['m4a', 'mp3', 'webm', 'mp4', 'mpga', 'wav', 'mpeg']

Hi @iamsaiyan

You just have to make sure that the audio file format is supported:

Currently you’re sending in ogg format which isn’t in the list of supported formats.

Hi @sps

The above code, is format for the displaying the audio format in the Streamlit app. It doesn’t have anything to do with Whisper AI, I guess.