Whisper help ឵឵឵឵឵឵឵឵឵឵឵឵ ឵឵឵឵឵឵ ឵឵឵

Can someone please share python code with whisper api :pray: ឵឵឵឵឵឵឵឵឵ ឵឵឵឵឵឵឵឵឵឵឵឵ ឵឵឵឵឵឵ ឵឵឵឵឵឵឵឵឵឵឵឵ ឵឵឵ ឵឵឵ ឵឵឵

Basic Whisper API Example:

import requests

openai_api_key = 'ADD YOUR KEY HERE'
file_path = '/path/to/file/audio.mp3'  # Add the Path of your audio file 
headers = {
    'Authorization': f'Bearer {openai_api_key}',

files = {
    'file': open(file_path, 'rb'),
    'model': (None, 'whisper-1'),

response = requests.post('https://api.openai.com/v1/audio/transcriptions', headers=headers, files=files)


for more information, checkout the OpenAI documentation

1 Like

Let’s crank up the level of “basic example”…

import os
import requests

# Gets the API key from environment variable
api_key = os.getenv("OPENAI_API_KEY")
headers = {"Authorization": f"Bearer {api_key}"}
url = "https://api.openai.com/v1/audio/transcriptions"

audio_file_name = "audio.mp3"
base_file_name = os.path.splitext(audio_file_name)[0]  # Get the base file name

with open(audio_file_name, "rb") as audio_file:
    parameters = {
        "file": (audio_file_name, audio_file),
        "language": (None, "en"),
        "model": (None, "whisper-1"),
        "prompt": (None, "Here is the radio show."),
        "response_format": (None, "verbose_json"),
        "temperature": (None, "0.1"),
        "timestamp_granularities[]" : (None, "word"),
    response = requests.post(url, headers=headers, files=parameters)

if response.status_code != 200:
    print(f"HTTP error {response.status_code}: {response.text}")
    # Get the transcribed text and timed words from the response
    transcribed_text = response.json()['text']
    words = response.json()['words']
    formatted_words = [
        {k: f"{v:.2f}" if isinstance(v, float) else v for k, v in word.items()}
        for word in words
    # Save text or words to a file
        with open(f"{base_file_name}_transcription.txt", "w") as file:
        print(f"Transcribed text successfully saved to '{base_file_name}_transcription.txt'.")
        with open(f"{base_file_name}_timestamped.txt", "w") as file:
        print(f"Timestamped words successfully saved to '{base_file_name}_timestamped.txt'.")
    except Exception as e:
        print(f"output file error: {e}")


This will use the same API environment variable as the OpenAI library.
It also demonstrates word time stamps, saving two files, one with the plain text, and one with the word list, based on the original file name. Then you get a bit of output shown also.

The requests library is one you’ll have to “pip install”


tried both codes but it returns “no module named requiests”. (openAI library installed)

The two examples use the requests library, not the Python library. As mentioned in the last line I wrote above, you’ll have to install it just like you did openai, with a pip install requests in your OS shell or environment.

This also means that if you want the latest features like word timestamps, you don’t have to wait for the openai library to be updated to let those unknown parameters through.