---
title: "NSFW protection"
video_id: fnL1J6lm9a0
url: https://www.youtube.com/watch?v=fnL1J6lm9a0
workflow_page: https://workflows-of-ai.com/workflow/nsfw-protection
language: en
captions: generated
---

0:00 hey there Marcus here this video is
0:03 sponsored by freely give freely give pay
0:05 my salary so I can help out the make
0:08 Drupal AI module everything it can be we
0:11 are expert in everything Drupal and AI
0:13 so if you need with help with
0:15 development consultancy and so on get in
0:18 contact with
0:19 us uh today's use case is going to be uh
0:24 fairly easy to set up fairly small but
0:29 basically before chat GPT happened you
0:32 would have been astounded by this uh
0:35 thing I'm going to Showcase now of
0:37 course now it's kind of normal so what
0:40 we're going to do is make sure that you
0:42 can't upload images that are not safe
0:45 for work and not say work means uh
0:47 images that are gory that might be
0:50 extremely offensive for whatever reason
0:52 they might have a a pornography or or
0:55 nudity in
0:56 them uh so any type of image uh that
1:01 would be
1:03 deemed offensive enough that you would
1:05 have a problem if you both saw it on
1:07 your screen while you're at work
1:10 and because of that I have to say it is
1:14 caveat I I try to search for an image
1:16 that uh hits this filter that is still
1:20 pretty unoffensive and what I came up
1:23 with is the C Calvin Klein adds from
1:25 Justin
1:26 Bieber if this is still uh too much for
1:31 you to showcase at your workplace please
1:33 look at this video at home so this is
1:36 just a warning because this image will
1:38 be
1:38 showcase uh to be able to show this
1:42 functionality uh so this is a subm
1:44 module of the druple module called AI
1:47 validations it was written by uh Walter
1:50 Walters if is his username is Frederick
1:53 Walters um he spent most of the time or
1:58 all time more or less to actually make
2:00 this uh module
2:02 happen and it's a uh it's a subm module
2:06 to this module field validations so
2:09 field validation has been around for
2:10 long it can make sure that you can
2:12 validate fields on specific use cases
2:15 like emails and Ian numbers and IP
2:17 addresses so what uh Fredick added was
2:21 so you can use it uh llm to actually you
2:25 know read the text and try to get
2:27 understanding from the text and validate
2:29 uh or not and the same for
2:32 images uh so what I've added that I will
2:35 showcase this time is image
2:38 classification models and we're going to
2:39 use hugging phase for
2:41 this uh so we're going to use um this
2:46 model called uh Falcon AI not safe for
2:50 work image detection and it's a pretty
2:52 small model that in theory you could
2:55 probably run on a CPU you could host on
2:57 a normal server if you set up a python
3:00 server with fast API or something like
3:02 this uh but what's interesting with this
3:05 is that it's it's actually free to use
3:07 hugging face models if you have a
3:09 account uh there's heavy rate limiting
3:12 on those and there's something called
3:14 Cod models so most models are cold which
3:17 means that when someone request them it
3:19 starts them starts the server then which
3:22 can take anything from like 20 seconds
3:24 to 2 minutes which of course is not
3:27 acceptable if you have an end user that
3:29 uploads a image uh and expect it to be
3:33 uploaded and you know continue with
3:36 whatever they're doing in maybe five or
3:38 10
3:39 seconds uh but what's special with this
3:41 it's actually one of the War models so
3:44 it's available for free uh at any time
3:47 now they have heavy rate limiting so if
3:50 you're going to expose this to end users
3:52 and you have I I don't know exactly
3:55 where the limited is if it's 100 plus
3:57 images per day thousand plus images
4:00 whatever it is then you should get a
4:02 dedicated host or host it yourself but
4:06 if this is just for editors or you don't
4:08 have that much of a traffic you can use
4:11 it actually for free
4:14 um
4:16 so with that being said yeah one thing
4:19 to mention as well because the AI
4:21 validations model module is a so subm
4:24 module of the AI module the dependency
4:26 of field validation is not downloaded
4:29 automatically so so you need to download
4:30 field validations uh 3.0
4:34 beta as uh for the AI mod module for to
4:38 get this uh uh to get this uh feature I
4:42 think you have to get the dev version
4:44 currect so I have downloaded those
4:47 modules I have not installed anything so
4:50 we're going to go ahead and install here
4:51 now then so we have the AI validations
4:54 mod module we're going to install that
4:56 will install the AI Core module we're
4:58 going to install the hugging face
5:00 provider but what we're going to add as
5:02 well is this AI API Explorer so we can
5:05 actually try it out before we start uh
5:08 setting up the validation so that's a
5:09 Dev tool where you can try out different
5:11 type of operation types against a
5:14 provider uh so now when we have set this
5:17 up the next step is we have to set up
5:19 hugging face so that's under Ai and
5:21 provider settings hugging face
5:24 configuration and we have to create a
5:26 new key uh with a access key so we're
5:29 just going to call this hugging face key
5:33 and we're going to go here into hugging
5:35 face so if you're logged in it's under
5:36 settings access tokens and you create a
5:39 new
5:41 one uh I just call it not save for work
5:44 token and we just have to allow the
5:46 inference end point so this depends on
5:48 if you have a dedicator or a server list
5:50 and so on I just enable all of them but
5:53 if you have if you know what you're
5:55 doing you can just enable the specific
5:57 one you want we create the to we just
6:00 copy this this will be gone when you see
6:02 this video by the way and we just paste
6:03 it in here now just so you know for with
6:06 the key module this is stored now in
6:08 configuration so when I save this it
6:11 means that if someone exports the
6:12 configuration this is in free text uh
6:15 and if you push it to your repo it's
6:16 there so do not do this use environment
6:19 or something like that in production
6:22 this is just to Showcase now so I just
6:23 click back now because now I can select
6:26 the hugging face key I'm just going to
6:28 save this
6:30 The Next Step I need to do is to
6:32 actually uh tell huging face Which
6:35 models I want to use and the reasoning
6:36 for that is that hugging face has
6:39 800,000 models so we can't show a
6:41 preview of all those modules in the AI
6:44 API Explorer when you set up a automator
6:47 or something like that so you're going
6:49 to have to say which ones you want so
6:51 this just goes back to the uh to the mod
6:54 uh model you want you copy
6:58 it and you just paste it here and you
7:00 can save and now it's stored and there
7:04 is auto complete here so you see you can
7:07 actually search for it we all to
7:12 complete and the next step we can do now
7:15 is we can actually try it out so now
7:19 comes then I will show you case the
7:21 images so turn off now if you don't want
7:23 to see Justin Bieber in
7:25 underwear this is the image that's going
7:27 to fail
7:29 uh and this is just a another image we
7:33 try out with just Justin Bieber uh with
7:36 closeth
7:37 on uh just to see that this works so we
7:41 go to AI API Explorer so it's under
7:43 configuration and then there's all the
7:46 different operation types you can try
7:47 out with a provider we just take the
7:49 image classification Explorer we select
7:52 hugging face and we select Falcons Ai
7:56 and we choose the file we want to use so
7:58 we just start with the normal one one
8:00 there is labels here so there are image
8:03 classification models that might get
8:05 added later like the Asher or uh the
8:08 Gemini models that can actually uh do
8:11 classification on labels that use it so
8:13 you write a bicycle is there a bicycle
8:16 in the image or something like that
8:18 right but in this case with these small
8:20 models they have pre-trained labels and
8:23 you can usually read about them or you
8:25 can try them out here to see those
8:28 labels but you can also then use the AI
8:30 API Explorer so when I click here you're
8:33 going to see it has two labels normal or
8:36 not safe for work it says it's uh well
8:39 almost 100% normal it's
8:42 almost yeah there's very very little
8:45 chance that this is not safe work we do
8:48 the opposite then with the other image
8:50 and we get the opposite result
8:53 right so we know that not safe for work
8:56 is what we want to look for over maybe a
8:58 ah
9:00 uh uh accuracy of 0.8 so like 80% or
9:05 something like that um so now we just
9:09 have to then set up the field validation
9:11 rules and the way we're going to do this
9:15 now then is so let's set that up so it's
9:20 under
9:21 structure and fi validation so I will
9:25 just show you under content add content
9:28 article we just have the standard
9:29 article that comes with a standard
9:31 profile that has a image that's where we
9:33 want to put this so structure field
9:36 validations add field validation rule
9:39 set and we add it on content of type
9:42 article and we create then the first
9:46 validation on the article which should
9:48 be on the image field and we use the AI
9:52 image classification constraint for this
9:56 click
9:57 add we call it not save for work we have
10:01 to do it on the target ID the actual
10:03 image we're not testing the width or
10:04 height of the image or anything like
10:07 that uh we keep default mode on there
10:10 and we use our model to look for the not
10:13 save for work exact this term with a
10:17 minimum confidence of
10:18 [Music]
10:19 0.8 and uh this is a thing I added for
10:23 hugging faces specifically because
10:26 sometimes the models are cold so you're
10:28 going to get an error when when you try
10:29 to request them and you just tell what
10:32 you want to happen then should the
10:33 validation F fail or should you just
10:36 Skip and let them uh uh be able to
10:39 upload the image and then the error
10:41 image the sorry the error message the
10:45 image you try to upload is to NS so you
10:52 can set it on specific roles I'm not
10:54 going to do it now because I want to try
10:55 this myself as admin in this case but if
10:58 it's for your editor just set it on the
11:00 editors if it's on the end user end user
11:02 and so on so
11:05 now we need to try it out so I pretend
11:09 to be the editor now I'm GNA write about
11:11 Justin Bieber and I found this cool
11:14 image I wanted but now it tells me
11:18 theage you try to upload is to not save
11:20 for work so if I try to save this is
11:25 just going to kick in the validation I'm
11:27 not going to be able to upload this so
11:30 let's just try instead with our nicer
11:33 image here
11:35 and all of a sudden it
11:38 works
11:39 so uh that is basically it we make sure
11:43 that the images that gets uploaded are
11:47 safe for work
11:49 now uh before I end this I just want to
11:53 say that image classification is a
11:56 general term if you go to hugging phase
11:59 and search for the tag image
12:01 classification there are a lot of
12:04 different small small models you can do
12:06 uh use for different different use cases
12:09 so it doesn't have to be uh not safe for
12:11 work so you see here there's a Pokemon
12:13 classifier you need to know which
12:15 Pokemon this is uh there is a a food PL
12:20 fire here right facial emotions so there
12:22 are uh these models that specializes in
12:25 different image classifications and it's
12:28 also very uh I'm not going to say easy
12:32 uh if you if you have worked with uh
12:36 these models it's not super complex to
12:40 create your own image classification
12:42 models and there are software as a
12:44 service for this in asure and so on and
12:46 our hopes is that we're going to create
12:48 providers for this in the future as well
12:50 where where there's a easier path to to
12:53 your own image classification models but
12:56 as I said what's cool with these they
12:58 are very small so you can reuse them
13:01 thank you for watching
