# Transcribe an audio file

Source: [Transcribe an audio file](<https://docs.gptunnel.ru/en/transcription/quickstart/>)

<a id="_top"></a>
Use the [sample WAV file](<https://docs.gptunnel.ru/examples/blue-sky.wav>). The speaker says “The sky is blue today.” You need an [API key](<https://docs.gptunnel.ru/en/#get-an-api-key>) with access to `deepgram-whisper-tiny`. Set the `GPTUNNEL_API_KEY` environment variable to your key. The script requires Python and the `requests` package.

To use your own recording, set `GPTUNNEL_AUDIO_URL` to its public URL. Update the expected transcript in the script to match your recording.

<a id="create-a-task-and-get-the-result"></a>
## Create a task and get the result

On the first run, the script [creates a task](<https://docs.gptunnel.ru/en/transcription/create/>) and prints `TASK_ID`. It then uses this ID to [poll for the result](<https://docs.gptunnel.ru/en/transcription/result/>). If the task already exists, set the `TASK_ID` environment variable to its ID. The script will resume polling that task.

```python
import os
import re
import time

import requests

url = 'https://gptunnel.ru/v1/audio/transcriptions'
headers = {'Authorization': os.environ['GPTUNNEL_API_KEY']}

task_id = os.environ.get('TASK_ID')
if not task_id:
    response = requests.post(
        url,
        headers=headers,
        json={
            'audio_url': os.environ.get('GPTUNNEL_AUDIO_URL') or 'https://docs.gptunnel.ru/examples/blue-sky.wav',
            'model': 'deepgram-whisper-tiny',
            'language': 'en',
        },
        timeout=30,
    )
    response.raise_for_status()
    task_id = response.json()['id']
    print(f'TASK_ID={task_id}', flush=True)

deadline = time.monotonic() + 30 * 60
while time.monotonic() < deadline:
    response = requests.get(url, headers=headers, params={'task_id': task_id}, timeout=15)
    response.raise_for_status()
    task = response.json()
    if task['id'] != task_id:
        raise RuntimeError('API returned a different task ID')
    if task['status'] == 'failed':
        raise RuntimeError(f"{task.get('error_title')}: {task.get('error_description')}")
    if task['status'] == 'done':
        channels = task['result']['results']['channels']
        text = ' '.join(
            alternatives[0].get('transcript', '')
            for channel in channels
            if (alternatives := channel.get('alternatives'))
        )
        print(text)
        if ' '.join(re.findall(r'[a-z]+', text.lower())) != 'the sky is blue today':
            raise RuntimeError('Unexpected transcript for the sample audio')
        break
    time.sleep(10)
else:
    raise TimeoutError(f'Check the existing task with TASK_ID={task_id}')
```

The script checks the task every ten seconds for up to 30 minutes. If it times out, set `TASK_ID` to the ID it printed and run it again. **Running the script again without `TASK_ID` creates a new paid task.** Clear the variable only when you want a new transcription.

<a id="upload-a-file-directly"></a>
## Upload a file directly

For a file on your computer, use the [synchronous multipart request with `whisper-1`](<https://docs.gptunnel.ru/en/transcription/openai-compat/>). You can test it with the same WAV file.

For other audio tasks, [choose a voice for text to speech](<https://docs.gptunnel.ru/en/creative-lab/voices/>) or [generate music with Suno](<https://docs.gptunnel.ru/en/creative-lab/suno/>).
