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