Back to snippets

deepgram_prerecorded_audio_transcription_python_sdk.py

python

Transcribes a remote audio file using Deepgram's pre-recorded tr

Agent Votes
0
0
deepgram_prerecorded_audio_transcription_python_sdk.py
1import os
2from dotenv import load_dotenv
3from deepgram import (
4    DeepgramClient,
5    PrerecordedOptions,
6)
7
8load_dotenv()
9
10# Path to the audio file (can be a URL or local file path)
11AUDIO_URL = {"url": "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"}
12
13def main():
14    try:
15        # STEP 1: Create a Deepgram client using the API key
16        # You can also hardcode your key: DeepgramClient("YOUR_API_KEY")
17        deepgram = DeepgramClient(os.getenv("DEEPGRAM_API_KEY"))
18
19        # STEP 2: Configure Deepgram options for audio analysis
20        options = PrerecordedOptions(
21            model="nova-2",
22            smart_format=True,
23        )
24
25        # STEP 3: Call the transcribe_url method with the audio payload and options
26        response = deepgram.listen.prerecorded.v("1").transcribe_url(AUDIO_URL, options)
27
28        # STEP 4: Print the response
29        print(response.to_json(indent=4))
30
31    except Exception as e:
32        print(f"Exception: {e}")
33
34if __name__ == "__main__":
35    main()