Skip to main content

Call Summarization

In this tutorial, you'll learn how to:

Summarizing a local file

The speech-to-text API takes an audio file and a model as input. For ease of use, it is compatible with OpenAI's API. In this tutorial, we will use the OpenAI Python SDK. To do this, pass your Telnyx API Key and the Telnyx base URL to the client.

Note

Make sure you have set the TELNYX_API_KEY environment variable

import os
from openai import OpenAI

client = OpenAI(
api_key=os.environ.get("TELNYX_API_KEY"),
base_url="https://api.telnyx.com/v2/ai",
)
def get_transcription(file_path):
with open(file_path, "rb") as audio_file:
transcription = client.audio.transcriptions.create(
model="distil-whisper/distil-large-v2",
file=audio_file
)
return transcription

transcription = get_transcription("example.mp3")
print(transcription.text)

The chat completions API takes an array of messages and a model as input. Here we are using the same client and transcription from the previous example.

def get_summary(transcription):
summary = client.chat.completions.create(
messages=[{
"role": "system",
"content": "Summarize this"
}, {
"role": "user",
"content": transcription.text
}],
model="meta-llama/Meta-Llama-3.1-8B-Instruct"
)
return(summary.choices[0].message.content)

print(get_summary(transcription))

Summarizing a call recording

These APIs are general-purpose and combine nicely with other Telnyx products you use to store your recordings.

For instance, if you store call recordings with Telnyx you could fetch them using our recordings API and transcribe and summarize them. In this example, we reuse the get_summary function defined above.

import io
import os
import requests
from openai import OpenAI

TELNYX_API_KEY = os.environ.get("TELNYX_API_KEY")

recording_response = requests.get(
"https://api.telnyx.com/v2/recordings",
headers={"Authorization": f"Bearer {TELNYX_API_KEY}"}
).json()

# Grab the mp3 download URL for your latest recording
recording_url = recording_response["data"][0]["download_urls"]["mp3"]
media_response = requests.get(recording_url)
recording_file = io.BytesIO(media_response.content)
recording_file.name = "recording.mp3"

client = OpenAI(
api_key=TELNYX_API_KEY,
base_url="https://api.telnyx.com/v2/ai",
)
transcription = client.audio.transcriptions.create(
model="distil-whisper/distil-large-v2",
file=recording_file
)
print(transcription.text)
print(get_summary(transcription))

Summarizing audio in Telnyx Storage

Or, you could store your audio files in Telnyx Storage

import io
import os
import boto3
from openai import OpenAI

TELNYX_API_KEY = os.environ.get("TELNYX_API_KEY")

b3_session = boto3.Session(aws_access_key_id=TELNYX_API_KEY)
s3 = b3_session.client("s3", endpoint_url="https://us-central-1.telnyxcloudstorage.com")

# change this to your bucket and object
bucket_name = "call-recordings"
file_name = "call_recording.mp3"

media_response = s3.get_object(Bucket=bucket_name, Key=file_name)
recording_file = io.BytesIO(media_response["Body"].read())
recording_file.name = file_name

client = OpenAI(
api_key=TELNYX_API_KEY,
base_url="https://api.telnyx.com/v2/ai",
)
transcription = client.audio.transcriptions.create(
model="distil-whisper/distil-large-v2",
file=recording_file
)
print(transcription.text)
print(get_summary(transcription))