Ir para o conteúdo

AssemblyAI

AssemblyAIAudioTranscriber

Bases: BaseModel

Transcriber using AssemblyAI's API.

api_key class-attribute instance-attribute

api_key: str | None = None

API key for AssemblyAI.

model class-attribute instance-attribute

model: Literal['best', 'nano'] = 'best'

Model to use for transcription.

language class-attribute instance-attribute

language: LanguageAlpha2 | None = None

Language of the transcription.

custom_spelling class-attribute instance-attribute

custom_spelling: dict[str, str | Sequence[str]] | None = (
    None
)

Custom spelling dictionary for the transcription.

transcribe

transcribe(audio_asset: AudioAsset) -> Transcription

Transcribe audio using AssemblyAI's API.

Parameters:

Name Type Description Default
audio_asset AudioAsset
required

Returns:

Type Description
Transcription

The transcription.

Source code in src/mosaico/audio_transcribers/assemblyai.py
def transcribe(self, audio_asset: AudioAsset) -> Transcription:
    """
    Transcribe audio using AssemblyAI's API.

    :param audio_asset:
    :return: The transcription.
    """
    try:
        import assemblyai as aai
    except ImportError:
        raise ImportError("AssemblyAI package is required for AssemblyAIAudioTranscriber.")

    config = aai.TranscriptionConfig(
        speech_model=aai.SpeechModel(self.model),
        language_code=str(self.language) if self.language else None,
        language_detection=self.language is None,
        custom_spelling=self.custom_spelling,
        punctuate=True,
        format_text=True,
    )

    transcriber = aai.Transcriber(client=self._client, config=config)

    with audio_asset.to_bytes_io() as audio_file:
        response = transcriber.transcribe(audio_file)

    transcription = response.wait_for_completion()

    if transcription.words is None:
        raise ValueError("No word timestamps found in transcription.")

    return Transcription(
        words=[
            TranscriptionWord(start_time=word.start / 1000, end_time=word.end / 1000, text=word.text)
            for word in transcription.words
        ]
    )