Transcreva áudio com filtro de obscenidades

Este exemplo demonstra como transcrever áudio com o filtro de linguagem imprópria ativado.

Exemplo de código

Python

Para saber como instalar e usar a biblioteca cliente do Speech-to-Text, consulte o artigo Bibliotecas cliente do Speech-to-Text. Para mais informações, consulte a documentação de referência da API Python Speech-to-Text.

Para se autenticar no Speech-to-Text, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

import os

from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def transcribe_profanity_filter_v2(
    audio_file: str,
) -> cloud_speech.RecognizeResponse:
    """Transcribe an audio file with profanity filtering enabled.
    Args:
        audio_file (str): Path to the local audio file to be transcribed.
            Example: "resources/audio.wav"
    Returns:
        cloud_speech.RecognizeResponse: The response containing the transcription results.
    """
    # Instantiates a client
    client = SpeechClient()

    # Reads a file as bytes
    with open(audio_file, "rb") as f:
        audio_content = f.read()

    config = cloud_speech.RecognitionConfig(
        auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
        language_codes=["en-US"],
        model="long",
        features=cloud_speech.RecognitionFeatures(
            profanity_filter=True,  # Enable profanity filtering
        ),
    )

    request = cloud_speech.RecognizeRequest(
        recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
        config=config,
        content=audio_content,
    )

    # Transcribes the audio into text
    response = client.recognize(request=request)

    for result in response.results:
        print(f"Transcript: {result.alternatives[0].transcript}")

    return response

O que se segue?

Para pesquisar e filtrar exemplos de código para outros Google Cloud produtos, consulte o Google Cloud navegador de exemplos.