Content moderation on audio recordsdata with Python

With a rising share of human communication taking place on-line, making certain that this communication is content-appropriate for a given platform is vital to sustaining the integrity and security of on-line areas. Content moderation is important in these efforts, serving to to detect and handle inappropriate or delicate materials in media recordsdata.
In this tutorial, we’ll learn the way you should utilize Python and state-of-the-art AI fashions to routinely carry out content material moderation on audio recordsdata at scale with just some strains of code.
We’ll use this instance file:
Below is an excerpt from the output, displaying a bit of the file that discusses the delicate matter of well being points, together with a severity rating for the content material and a confidence estimate within the prediction. Additionally, the timestamps for the related part are displayed.:
So what's it on this haze that makes it dangerous? And I'm assuming it's dangerous. It is. It is. The ranges outdoors proper now in Baltimore are thought of unhealthy. And most of that is because of what's known as particulate matter, that are tiny particles, microscopic, smaller than the width of your hair, that may get into your lungs and affect your respiratory system, your cardiovascular system, and even your neurological, your mind. What makes this notably dangerous?
Timestamp: 56.3s - 85.4s
Label: health_issues - Confidence: 94% - Severity: 88%
Step 1: Set up your surroundings
Before we begin coding, we’ll want to make sure your surroundings is correctly configured. First, guarantee Python is put in in your pc. You can obtain and set up Python from the official Python web site if it is not already put in.
Next, set up the assemblyai Python bundle, which permits us to submit recordsdata to AssemblyAI for fast content material moderation. Install the bundle with pip by working the next command in your terminal or command immediate:
pip set up assemblyai
After putting in the assemblyai bundle, you will must set your API key as an surroundings variable. Your AssemblyAI API key’s a singular identifier that enables you entry to AssemblyAI’s AI fashions. You can get an API key totally free right here, or copy it out of your dashboard if you have already got one.
Once you have copied your API key, set it as an surroundings variable – for Mac and Linux customers, use the terminal to run:
export ASSEMBLYAI_API_KEY=YOUR_KEY_HERE
For Windows customers, use the Command Prompt to execute:
set ASSEMBLYAI_API_KEY=YOUR_KEY_HERE
Step 2: Transcribe the file with content material moderation
Now that your surroundings is ready up, the following step is to transcribe your audio file and apply content material moderation, permitting you to detect probably delicate or inappropriate content material throughout the file.
First, create a file known as fundamental.py, import the assemblyai bundle, and specify the placement of the audio file you want to use. This location could be both an area file path or a publicly-accessible obtain URL. If you do not need to use your individual file, you’ll be able to hold the default instance specified under:
import assemblyai as aai
audio_url = "https://github.com/AssemblyAI-Examples/audio-examples/uncooked/fundamental/20230607_me_canadian_wildfires.mp3"
Before we transcribe the audio file, we have to specify the configuration for the transcription. Create an aai.TranscriptionConfig object and allow content material moderation through content_safety=True. This setting instructs AssemblyAI to research the audio for any content material that could be thought of delicate in the course of the transcription. You can try the AssemblyAI docs to see different obtainable fashions you’ll be able to allow by way of the TranscriptionConfig. Add the next line to fundamental.py:
config = aai.TranscriptionConfig(content_safety=True)
Next, move this config into an aai.Transcriber object, after which move the audio file into the Transcriber‘s transcribe methodology. This submits the audio file for transcription in line with the settings outlined within the TranscriptionConfig. Add the next strains to fundamental.py:
transcriber = aai.Transcriber(config=config)
transcript = transcriber.transcribe(audio_url)
The ensuing transcript is an aai.Transcript object which incorporates, amongst different info, the details about any probably delicate segments within the file. Let’s check out what’s returned now.
Step 3: Print the end result
After transcribing the audio file and analyzing it for delicate content material, we are able to print the Content Moderation outcomes to see what info is returned. You can then embrace some logic in your software to routinely deal with delicate content material in line with your content material insurance policies.
All of the content material moderation info for the transcript is discovered within the transcript.content_safety object. The outcomes attribute of this object incorporates a listing of objects, one for every part within the audio file that the Content Moderation mannequin flagged as delicate content material.
Below we iterate by way of every component on this listing and print off the textual content for the corresponding part within the file, in addition to the timestamps for the start and finish of the part. Then, we print info for all the content material moderation labels assigned to the part. Each label specifies a distinct kind of delicate content material that was detected within the given part, together with a confidence rating and a severity ranking for that kind of content material in that individual part. Add the next strains to fundamental.py:
# Get the components of the transcript which had been flagged as delicate.
for lead to transcript.content_safety.outcomes:
print(end result.textual content)
print(f"Timestamp: {end result.timestamp.begin/1000:.1f}s - {end result.timestamp.finish/1000:.1f}s")
# Get class, confidence, and severity.
for label in end result.labels:
print(f"Label: {label.label} - Confidence: {label.confidence*100:.0f}% - Severity: {label.severity*100:.0f}%") # content material security class
print()
Here is without doubt one of the gadgets that might be output once we run the script:
Smoke from a whole lot of wildfires in Canada is triggering air high quality alerts all through the US. Skylines from Maine to Maryland to Minnesota are grey and smoggy. And in some locations, the air high quality warnings embrace the warning to remain inside. We wished to higher perceive what's taking place right here and why. So he known as Peter DiCarlo, an affiliate professor within the division of Environmental Health and Engineering at Johns Hopkins University. Good morning. Professor. Good morning.
Timestamp: 0.2s - 28.8s
Label: disasters - Confidence: 81% - Severity: 39%
We can see that this part was recognized, with 81% accuracy, to the touch on the delicate matter of disasters with a 39% severity.
You can use these outcomes to determine sections of audio which can be thought of delicate in line with some inside criterion. In the code block under, we have added the criterion that the multiplicative product of the severity and confidence of a delicate part should meet a sure threshold to be reported, with the intent of reporting solely sections which can be a minimum of one among fairly extreme or fairly assured.
THRESHOLD = 0.7
# Get the components of the transcript which had been flagged as delicate.
for lead to transcript.content_safety.outcomes:
if not any([label.confidence*label.severity > THRESHOLD for label in result.labels]):
proceed
print(end result.textual content)
print(f" Timestamps: {end result.timestamp.begin/1000:0.1f}s - {end result.timestamp.finish/1000:0.1f}s")
# Get class, confidence, and severity.
for label in end result.labels:
if label.confidence*label.severity > THRESHOLD:
print(f" Label: {label.label} (Confidence: {label.confidence:.02f}, Severity: {label.severity:.02f})") # content material security class
print()
Here is the total output of this code block:
So what's it on this haze that makes it dangerous? And I'm assuming it's dangerous. It is. It is. The ranges outdoors proper now in Baltimore are thought of unhealthy. And most of that is because of what's known as particulate matter, that are tiny particles, microscopic, smaller than the width of your hair, that may get into your lungs and affect your respiratory system, your cardiovascular system, and even your neurological, your mind. What makes this notably dangerous?
Timestamps: 56.3s - 85.4s
Label: health_issues (Confidence: 0.94, Severity: 0.88)
Summarizing general findings
Furthermore, you’ll be able to summarize the general findings of the Content Moderation mannequin to get a broader view of the audio content material’s nature. Add the next strains to fundamental.py:
# Get the boldness of the most typical labels in relation to your entire audio file.
for label, confidence in transcript.content_safety.abstract.gadgets():
print(f"{confidence * 100:.2f}% assured that the audio incorporates {label}")
print()
When you run the script, you will notice this output, which signifies that the Content Moderation is very assured that this audio file as an entire considerations disasters and well being points.
98.88% assured that the audio incorporates disasters
90.83% assured that the audio incorporates health_issues
Additionally, you may get a finer-grained breakdown of those points by accessing a severity rating abstract. This breakdown equally considers the audio file as an entire, however this time giving a chance throughout low, medium, and excessive severities for every of the labels that describe the file as an entire. In different phrases, for every delicate matter that applies to the audio file as an entire, this breakdown provides a distribution throughout 3 discrete severity ranges for every delicate matter.
# Get the general severity of the most typical labels in relation to your entire audio file.
for label, severity_confidence in transcript.content_safety.severity_score_summary.gadgets():
print(f"{severity_confidence.low * 100:.2f}% assured that the audio incorporates low-severity {label}")
print(f"{severity_confidence.medium * 100:.2f}% assured that the audio incorporates medium-severity {label}")
print(f"{severity_confidence.excessive * 100:.2f}% assured that the audio incorporates high-severity {label}")
Here is the output:
53.14% assured that the audio incorporates low-severity disasters
46.86% assured that the audio incorporates medium-severity disasters
0.00% assured that the audio incorporates high-severity disasters
20.70% assured that the audio incorporates low-severity health_issues
46.23% assured that the audio incorporates medium-severity health_issues
33.07% assured that the audio incorporates high-severity health_issu
We can see that, for every distinct label that applies to your entire file, the possibilities for low, medium, and excessive sum to 100%.
Run python fundamental.py within the terminal wherein you set your AssemblyAI API key as an surroundings variable to see all of those outputs printed to the console.
Final phrases
In this tutorial, you discovered the right way to carry out a Content Moderation evaluation of an audio file utilizing AI. With the outcomes printed and analyzed, you can also make knowledgeable choices to make sure your audio content material aligns along with your group’s security and content material requirements.
If you need to study extra about the right way to analyze audio and video recordsdata with AI, try extra of our weblog, like this text on filtering profanity from audio recordsdata with Python. Alternatively, be at liberty to take a look at our YouTube channel for academic movies on AI and AI-adjacent tasks, like this video on the right way to routinely extract telephone name insights utilizing LLMs and Python:
HI-FI News
through News, Tutorials, AI Research https://ift.tt/lysp0j7
May 27, 2024 at 05:38PM
-
Product on sale
Audiophile Vinyl Records Cleaning BundleOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Product on sale
Easy Start Vinyl Records Cleaning KitOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Vinyl Records Cleaner Easy Groove Concentrate€34.95 incl. VAT -
Easy Groove Super Set€199.00 incl. VAT -
Easy Groove Enzycaster – vinyl records prewash cleaner€34.95 incl. VAT -
Easy Groove Extreme MK. II- vinyl records prewash cleaner€45.00 incl. VAT -
Vinyl Records Cleaner – Easy Groove Virgin Concentrate€35.00 incl. VAT -
Easy Groove Spray&Wipe vinyl records cleaner€34.95 incl. VAT








