speechtotext.model.assemblyAIWrapper.AssemblyAIAPIWrapper

class AssemblyAIAPIWrapper(model_version)[source]

Bases: ModelWrapper

Wrapper for assemblyAi API. ASSEMBLY_AI_API_KEY needs to be in the ‘.env’ file in current directory.

Wrapper for assemblyAi model.

Parameters:

model_version (assemblyAiAPIVersion) – Model version of assemblyAi STT API to use.

Methods

benchmark_n_samples

Benchmark n samples with model.

benchmark_sample

Benchmark sample with model.

benchmark_samples

Benchmark samples with model.

convert_sample

Convert sample to correct format.

get_model

Get model.

get_transcript_of_file

Attributes

API_URL

Connection url for the API.

LANGUAGE_CODE

Code for the language to transcribe.

PATH_OF_TEMP_CONVERTED_AUDIO_FILE

path to temp file that will be created to convert the audio files to an accepted audio format.

POLLING_ENDPOINT

Polling endpoint url.

TIME_SLEEP

Time to sleep after each polling.

TRANSCRIPT_ENDPOINT

Transcribe endpoint url.

UPLOAD_ENDPOINT

Upload endpoint url.

API_URL: str = 'https://api.assemblyai.com/v2/upload'

Connection url for the API.

Type:

str

LANGUAGE_CODE: str = 'nl'

Code for the language to transcribe.

See supported languages for assemblyAi

Type:

str

PATH_OF_TEMP_CONVERTED_AUDIO_FILE: str = 'converted_audio_file.wav'

path to temp file that will be created to convert the audio files to an accepted audio format.

Type:

PATH_OF_TEMP_CONVERTED_AUDIO_FILE

POLLING_ENDPOINT: str = 'https://api.assemblyai.com/v2/transcript/'

Polling endpoint url.

Type:

str

TIME_SLEEP: int = 3

Time to sleep after each polling.

Type:

int

TRANSCRIPT_ENDPOINT: str = 'https://api.assemblyai.com/v2/transcript'

Transcribe endpoint url.

Type:

str

UPLOAD_ENDPOINT: str = 'https://api.assemblyai.com/v2/upload'

Upload endpoint url.

Type:

str

_append_error(samples, audio_id, error)

Append error to model_errors.

Parameters:
  • samples (SampleDataset) – Dataset of audio.

  • id (str) – Id of failed sample.

  • error (str) – Error message.

_benchmark_sample_with_time(dataset, audio_id, with_cleaning=True)

Benchmark sample for model with timer.

Parameters:
  • dataset (Dataset) – Dataset of audio.

  • id (str) – Id of audio file.

  • with_cleaning (bool, optional) – Set True to clean transcripts. Defaults to True.

Returns:

Metrics of the transcript.

Return type:

Metrics

_clean_output(paragraphs)[source]

Transcript list to 1 string.

Parameters:

paragraphs (list) – Transcript list.

Returns:

Transcript.

Return type:

str

_get_paragraphs(polling_endpoint, header)[source]

Get results from polling endpoint.

Parameters:
  • polling_endpoint (str) – Polling endpoint.

  • header (dict) – Http header.

Returns:

Transcript of audio file in list.

Return type:

list

_make_polling_endpoint(transcript_response)[source]

Make polling endoint.

Parameters:

transcript_response (dict) – Transcript response.

Returns:

Polling endpoint.

Return type:

str

_read_file_with_chunck_size(audio_file_name, chunk_size=5242880)[source]

Read data from file.

Parameters:
  • audio_file_name (str) – Path to audio file.

  • chunk_size (int, optional) – Size of chunk. Defaults to 5242880.

Yields:

generator – Audio file.

_request_transcript(upload_url, header)[source]

Request transcript.

Parameters:
  • upload_url (dict) – Url for request.

  • header (dict) – Http header.

Returns:

Response of request.

Return type:

dict

_upload_file(audio_file_name, header)[source]

Upload file.

Parameters:
  • audio_file_name (str) – Path to audio file.

  • header (dict) – Http header.

Returns:

Url for request.

Return type:

dict

_wait_for_completion(polling_endpoint, header)[source]

Wait for the translation to be done.

Parameters:
  • polling_endpoint (str) – Polling endpoint.

  • header (dict) – Http header.

benchmark_n_samples(dataset, number_of_samples, with_cleaning=True)

Benchmark n samples with model.

Parameters:
  • dataset (Dataset) – Dataset of audio.

  • number_of_samples (int) – Number of random samples to benchmerk.

  • with_cleaning (bool, optional) – Set True to clean transcripts. Defaults to True.

Returns:

List of metrics for each sample.

Return type:

list

benchmark_sample(dataset, audio_id, with_cleaning=True)

Benchmark sample with model.

Parameters:
  • dataset (Dataset) – Dataset of audio.

  • id (str) – Id of audio file.

  • with_cleaning (bool, optional) – Set True to clean transcripts. Defaults to True.

Returns:

Metrics of the transcript.

Return type:

Metrics

benchmark_samples(samples, with_cleaning=True)

Benchmark samples with model.

Parameters:
  • dataset (Dataset) – Dataset of audio.

  • number_of_samples (int) – Number of random samples to benchmerk.

  • with_cleaning (bool, optional) – Set True to clean transcripts. Defaults to True.

Returns:

List of metrics for each sample.

Return type:

list

convert_sample(path_to_sample)

Convert sample to correct format.

Parameters:
  • path_to_sample (str) – Path to sample.

  • override (bool, optional) – Override original file. Defaults to False.

Returns:

Path to converted sample.

Return type:

str

get_model()[source]

Get model.