Code Samples

Use the code samples below to quickly get started developing with the SDK.

attention

These examples require the Rev AI Python SDK.

Submit a local file for transcription

The following example demonstrates how to submit a local audio file for transcription.

To use this example, replace the <FILEPATH> placeholder with the path to the file you wish to transcribe and the <REVAI_ACCESS_TOKEN> placeholder with your Rev AI account's access token.

Copy
Copied
from rev_ai import apiclient

token = "<REVAI_ACCESS_TOKEN>"
filePath = "<FILEPATH>"

# create your client
client = apiclient.RevAiAPIClient(token)

# send a local file
job = client.submit_job_local_file(filePath)

# check job status
job_details = client.get_job_details(job.id)

# retrieve transcript as text
transcript_text = client.get_transcript_text(job.id)

# retrieve transcript as JSON
transcript_json = client.get_transcript_json(job.id)

# retrieve transcript as a Python object
transcript_object = client.get_transcript_object(job.id)

Submit a remote file for transcription

The following example demonstrates how to submit a remote audio file for transcription.

To use this example, replace the <URL> placeholder with the public URL to the file you wish to transcribe and the <REVAI_ACCESS_TOKEN> placeholder with your Rev AI account's access token.

Copy
Copied
from rev_ai import apiclient

token = "<REVAI_ACCESS_TOKEN>"
source_url = "<URL>"

# create your client
client = apiclient.RevAiAPIClient(token)

# submit a job with a link to the source file
job = client.submit_job_url(source_config=CustomerUrlData(url=source_url))

# check job status
job_details = client.get_job_details(job.id)

# retrieve transcript as text
transcript_text = client.get_transcript_text(job.id)

# retrieve transcript as json
transcript_json = client.get_transcript_json(job.id)

# retrieve transcript as a python object
transcript_object = client.get_transcript_object(job.id)

Stream and transcribe an audio file

The following example can be used to configure your streaming client, send a stream of audio as a generator, and obtain the transcript as the audio is processed.

To use this example, replace the <FILEPATH> placeholder with the path to the file you wish to transcribe and replace the <REVAI_ACCESS_TOKEN> placeholder with your Rev AI access token.

Copy
Copied
from rev_ai.models import MediaConfig
from rev_ai.streamingclient import RevAiStreamingClient
import io


"""
Name of file to be transcribed
"""
filename = "<FILEPATH>"

"""
String of your access token
"""
access_token = "<REVAI_ACCESS_TOKEN>"

"""
Media configuration of audio file.
This includes the content type, layout, rate, format, and # of channels
"""
config = MediaConfig("audio/x-raw", "interleaved", 16000, "S16LE", 1)

"""
Create client with your access token and media configuration
"""
streamclient = RevAiStreamingClient(access_token, config)

"""
Open file and read data into array.
Practically, stream data would be divided into chunks
"""
with io.open(filename, 'rb') as stream:
    MEDIA_GENERATOR = [stream.read()]

"""
Starts the streaming connection and creates a thread to send bytes from the
MEDIA_GENERATOR. response_generator is a generator yielding responses from
the server
"""
response_generator = streamclient.start(MEDIA_GENERATOR)

"""
Iterates through the responses from the server when obtained
"""
for response in response_generator:
    print(response)

"""
Ends the connection early. Not needed as the server will close the connection
upon receiving an "EOS" message.
"""
streamclient.end()

Stream and transcribe microphone audio

The following example can be used to configure your streaming client, send audio as a stream from your microphone input, and obtain the transcript as it is processed.

To use this example, replace the <REVAI_ACCESS_TOKEN> placeholder with your Rev AI access token.

Copy
Copied
import pyaudio
from rev_ai.models import MediaConfig
from rev_ai.streamingclient import RevAiStreamingClient
from six.moves import queue

"""
Insert your access token here
"""
access_token = "<REVAI_ACCESS_TOKEN>"

class MicrophoneStream(object):
    """
    Opens a recording stream as a generator yielding the audio chunks.
    """
    def __init__(self, rate, chunk):
        self._rate = rate
        self._chunk = chunk
        """
        Create a thread-safe buffer of audio data
        """
        self._buff = queue.Queue()
        self.closed = True

    def __enter__(self):
        self._audio_interface = pyaudio.PyAudio()
        self._audio_stream = self._audio_interface.open(
            format=pyaudio.paInt16,
            """
            The API currently only supports 1-channel (mono) audio
            """
            channels=1, rate=self._rate,
            input=True, frames_per_buffer=self._chunk,
            """
            Run the audio stream asynchronously to fill the buffer object.
            This is necessary so that the input device's buffer doesn't
            overflow while the calling thread makes network requests, etc.
            """
            stream_callback=self._fill_buffer,
        )

        self.closed = False

        return self

    def __exit__(self, type, value, traceback):
        self._audio_stream.stop_stream()
        self._audio_stream.close()
        self.closed = True
        """
        Signal the generator to terminate so that the client's
        streaming_recognize method will not block the process termination.
        """
        self._buff.put(None)
        self._audio_interface.terminate()

    def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
        """
        Continuously collect data from the audio stream, into the buffer.
        """
        self._buff.put(in_data)
        return None, pyaudio.paContinue

    def generator(self):
        while not self.closed:
            """
            Use a blocking get() to ensure there's at least one chunk of
            data, and stop iteration if the chunk is None, indicating the
            end of the audio stream.
            """
            chunk = self._buff.get()
            if chunk is None:
                return
            data = [chunk]

            """
            Now consume whatever other data's still buffered.
            """
            while True:
                try:
                    chunk = self._buff.get(block=False)
                    if chunk is None:
                        return
                    data.append(chunk)
                except queue.Empty:
                    break

            yield b''.join(data)


"""
Sampling rate of your microphone and desired chunk size
"""
rate = 44100
chunk = int(rate/10)

"""
Creates a media config with the settings set for a raw microphone input
"""
example_mc = MediaConfig('audio/x-raw', 'interleaved', 44100, 'S16LE', 1)

streamclient = RevAiStreamingClient(access_token, example_mc)

"""
Opens microphone input. The input will stop after a keyboard interrupt.
"""
with MicrophoneStream(rate, chunk) as stream:
    """
    Uses try method to enable users to manually close the stream
    """
    try:
        """
        Starts the server connection and thread sending microphone audio
        """
        response_gen = streamclient.start(stream.generator())

        """
        Iterates through responses and prints them
        """
        for response in response_gen:
            print(response)

    except KeyboardInterrupt:
        """
        Ends the WebSocket connection.
        """
        streamclient.end()
        pass

Create and use a custom vocabulary

The following example can be used to create and submit custom vocabularies independently and directly to the custom vocabularies API, as well as check on their progress.

To use this example, replace the <REVAI_ACCESS_TOKEN> placeholder with your Rev AI access token.

Copy
Copied
from rev_ai import custom_vocabularies_client
from rev_ai.models import CustomVocabulary

token = "<REVAI_ACCESS_TOKEN>"

# create a client
client = custom_vocabularies_client.RevAiCustomVocabulariesClient(token)

# construct a CustomVocabulary object using your desired phrases
custom_vocabulary = CustomVocabulary(["Patrick Henry Winston", "Robert C Berwick", "Noam Chomsky"])

# submit the CustomVocabulary
custom_vocabularies_job = client.submit_custom_vocabularies([custom_vocabulary])

# view the job's progress
job_state = client.get_custom_vocabularies_information(custom_vocabularies_job['id'])

# get list of previously submitted custom vocabularies
custom_vocabularies_jobs = client.get_list_of_custom_vocabularies()

# delete the CustomVocabulary
client.delete_custom_vocabulary(custom_vocabularies_job['id'])