BLONDIEBYTES: If you've ever tried
to redact a file, typically, it looks something
like this, where you loop through the file looking
for the exact words you want to redact.
But what if you had a theme of words you wanted to redact?
For example, say you wanted to moderate
a piece of text for harmful words and topics
that may be sensitive.
Or better yet, you wanted to give the author the chance
to rewrite the text so it's less harmful.
That's what we're going to do today with Google AI.
[COMPUTER BEEPING]
So hello, world.
I'm BlondieBytes.
And I make videos on my channel about Java, Python,
and software development.
We're going to see the real capabilities of Google AI
and how we can use them in Python.
With that out of the way, let's make an app
that not only has the ability to moderate text
but also gives the author an opportunity
to rewrite their text so it's compliant with the guidelines.
The first step is to get our authentication credentials
from Google's AI Studio.
We'll be authenticating with an API key.
So let's create an API key.
[MUSIC PLAYING]
And it's generated.
Be sure to save this API key somewhere safe because we're
going to use it later on in our application.
With that done, let's set up our development environment.
We'll go into the terminal into our desktop.
We'll create a folder called Moderate Text Projects.
And we'll CD into it.
Now, in order to run our application,
we'll need to download some dependencies.
We can do that using pip.
These will allow us to use Gemini in our application.
We'll also set up our API key as an environment variable
to use in our scripts later.
I'll clear this up, and we'll move on to the Python script.
To write our Python code, I'll be using Sublime,
but you can use any plain-text editor.
Let's save this as app.py.
To start off, we'll add some imports.
The first import is the OS module.
And this will allow us to retrieve our API
key from the environment.
The second is for genai, and this
will allow us to connect to Google's Gemini AI.
To get our API key, we'll create a new variable
and use the OS module.
We'll use getenv and then the name of our environment
variable, so that is GOOGLE_API_KEY.
All right, let's write some code.
First, we'll configure the genai module to use our API key.
This will authenticate all of our requests.
Then we'll retrieve the model we want to generate content from.
We'll be using Gemini Pro.
So we'll access the genai module and get
our model called Gemini Pro.
Now for the core part of the program--
determining if the text needs to be moderated
and providing options for a rewrite.
Let's create our prompt.
Here's our prompt.
So we asked Gemini to determine if the text needs
to be moderated, and if so, provide options for rewriting
the text.
If the text does not need to be modified,
then we should not be providing suggestions.
And so we tell Gemini, don't provide suggestions.
And for that output, we want it to be
easy to print to the console because that
is how we're going to be interacting
with this application.
So we asked Gemini to make it something easy to read.
There's actually a place in Google AI Studio
where we can practice creating prompts
and see what Gemini will output.
Initially, my plan for this video
was generating text with Google AI
and then moderating that text with Google AI.
But the problem with doing that is AI
refuses to come up with some good insults.
And this is actually a good thing.
One of the reasons that LLMs are so useful
is that they're creative tools that can address
many different language tasks.
However, sometimes they do generate
output you don't expect.
It can be offensive, it can be insensitive,
or factually incorrect.
Google has built-in content filtering, as well as
adjustable safety settings, across four dimensions of harm.
There are safety risks when it comes to using LLMs,
so it's important to understand them and perform
appropriate safety testing for your use case.
On Google's "DeepMind" blog, they talk about how they use
automated testing in a classifier to detect various
harmful behaviors with test cases.
It covers offensive language, data leakage,
contact-information generation, distributed bias,
and conversational harms.
It's a pretty interesting post if you
want to dive deeper into this.
But for now, we'll go back to AI Studio,
and I'll show you some of those safety settings.
By default, many of them block some common themes,
but you can make it so it blocks nothing.
Again, there are some things that Google
is required to block, even if you have it set to none.
And for our use case, generating things that need to be moderated
fall into that category.
You can learn more about the safety attributes and settings
with this documentation.
I'll link it down below.
Let's head back to the code.
Now, with our prompt, the text to moderate is parameterized,
but we can add it dynamically when we send it to Gemini.
Let's create a variable for this called Text to Moderate.
Then we'll call generate_content on the model
to send this to Gemini.
And we'll print the output.
Let's run it.
So we'll go to our terminal.
And this is where our app.py file lives.
We'll go app.py.
And it gives us some suggestions on how
we could improve that text.
Let's take one and put it in our text to moderate.
We'll run it again.
Our rewrite passed our moderation check.
All right, that's it for this video.
With 20 lines of code, we were able to create a script
that moderates texts and provides
the opportunity for the author to rewrite it.
I hope you enjoyed using Google's new model with me.
And I'll see you next time.
Thank you for watching, and happy coding.
[MUSIC PLAYING]