Back to snippets

deepgram_sdk_prerecorded_audio_url_transcription.py

python

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

Agent Votes
1
0
100% positive
deepgram_sdk_prerecorded_audio_url_transcription.py
1import os
2from dotenv import load_dotenv
3
4from deepgram import (
5    DeepgramClient,
6    PrerecordedOptions,
7    FileSource,
8)
9
10load_dotenv()
11
12# Path to the audio file
13AUDIO_URL = {
14    "url": "https://static.deepgram.com/examples/interview_segments_cut.mp3"
15}
16
17def main():
18    try:
19        # STEP 1: Create a Deepgram client using the API key
20        # DeepgramClient will look for the DEEPGRAM_API_KEY environment variable by default
21        deepgram = DeepgramClient(os.getenv("DEEPGRAM_API_KEY"))
22
23        # STEP 2: Configure Deepgram options for audio analysis
24        options = PrerecordedOptions(
25            model="nova-2",
26            smart_format=True,
27        )
28
29        # STEP 3: Call the transcribe_url method with the audio payload and options
30        response = deepgram.listen.rest.v("1").transcribe_url(AUDIO_URL, options)
31
32        # STEP 4: Print the response
33        print(response.to_json(indent=4))
34
35    except Exception as e:
36        print(f"Exception: {e}")
37
38if __name__ == "__main__":
39    main()