הפעלת סינון של שפה גסה

בדף הזה מוסבר איך להשתמש ב-Cloud Speech-to-Text כדי לזהות אוטומטית שפה גסה בנתוני האודיו ולצנזר אותה בתמליל.

כדי להפעיל את מסנן השפה הגסה, מגדירים את profanityFilter=true ב-RecognitionFeatures. אם האפשרות הזו מופעלת, Cloud Speech-to-Text ינסה לזהות מילים גסות ויחזיר בתמליל רק את האות הראשונה ואחריה כוכביות (לדוגמה, f***). אם השדה הזה מוגדר לערך false או לא מוגדר, Cloud Speech-to-Text לא ינסה לסנן מילים גסות.

בדוגמה הבאה אפשר לראות איך להפעיל את מסנן השפה הגסה כדי שיזהה אודיו שמאוחסן בקטגוריה של Cloud Storage.

Python

מידע על התקנה ושימוש בספריית הלקוח של Cloud STT מופיע במאמר ספריות הלקוח של Cloud STT. מידע נוסף מופיע במאמרי העזרה של Cloud STT Python API.

כדי לבצע אימות ב-Cloud STT, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud import speech
from google.cloud.speech import RecognizeResponse


def sync_recognize_with_profanity_filter_gcs(audio_uri: str) -> RecognizeResponse:
    """Recognizes speech from an audio file in Cloud Storage and filters out profane language.
    Args:
        audio_uri (str): The Cloud Storage URI of the input audio, e.g., gs://[BUCKET]/[FILE]
    Returns:
        cloud_speech.RecognizeResponse: The full response object which includes the transcription results.
    """
    # Define the audio source
    audio = {"uri": audio_uri}

    client = speech.SpeechClient()
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.FLAC,  # Audio format
        sample_rate_hertz=16000,
        language_code="en-US",
        # Enable profanity filter
        profanity_filter=True,
    )

    response = client.recognize(config=config, audio=audio)

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

    return response.results