From the course: Azure AI Services Essential Training (2023)
Implement text moderation - Azure Tutorial
From the course: Azure AI Services Essential Training (2023)
Implement text moderation
- [Instructor] In this demo, I'm going to show you the content moderator API to implement text moderation, okay? So as usual, the first thing you need to do is create a resource in Azure. As you can see over here I have created a content moderator resource. Just go ahead and click on create a resource button and look for content moderator and it's like creating any other Azure resource. So let's dive in here and see what it looks like. As usual, the things that I am interested in are the keys and the location slash endpoint. So grab these and I've already put them in my code. So let's go ahead and look at our code. As you see, I have a config dot JSON, index dot js, it's just a post API we're going to do, so this is really simple. The endpoint over here, I'm calling this specific endpoint slash content moderator. Let me scroll to the right and show you the full endpoint. And the keys I've pasted over here. Obviously, use your own keys and remember to make sure you point it to the correct region. Let's dive into index dot js and see what this looks like. Now at the get go, let me just say, this is probably the hardest demo. It's not hard because the code is difficult, It's hard because I can't show you what this offensive text is. This is a professional platform you're watching this course on. So let me just tell you that I've created two variables, offensive text and non-offensive text which I have cleverly hidden in this region that you see on line 28. So I have to get a little crafty in this demo. What I'm going to do is I'm going to run this demo and right before we actually see the output, I'm going to pause it, okay? And then I'm going to copy paste the output and I'm going to remove the offensive parts. Remember, this is a professional platform so I cannot show you these offensive words even though the output is going to show me those offensive words. So I'm going to pause recording, I'm going to censor out those offensive words. But when you actually run this code on your machine, feel free to play with this offensive text variable. Put whatever you want in it and you'll see that this API works very nicely, okay? So let's go up here. Now I have three methods here. One is called as log output, get request options and a function called moderate text. So obviously this is where we're going to do the post call, log output is where we're going to see the output. Let me go ahead and set a break point here, okay? And under get request options is where we are creating the authentication header. This is like all other cognitive services, right? So OCP APIM subscription key, pass in the key. It works with Azure AD authentication as well. Out of scope for this course, but I have other courses on LinkedIn learning that dive deep into Azure AD fundamentals. So feel free to check them out. And there's a function called moderate text. Now, under moderate text, I am appending the endpoint with some more details over here. You can check the documentation for that and on line 8 I am doing a post. And the post is going to write out the log output over here. Again, I can't show you that output unfortunately. So now I'm going to hit F5, run this and when this break point comes around, I'm going to pause recording, I'm going to copy paste the outputs, clean them and we'll catch each other in a moment. So let's hit F5 to run this code, okay? Now I'm not going to show you this output so let me go ahead and pause recording and I'll catch you in a minute. So I copy pasted the outputs from console and I pasted them in this file over here, and I replaced the offending word with star, star, star, star. Now you can use your imagination to think what that was but that's not the point here. The point is that this was an offensive sentence. And as you can see over here that it analyzed it and it found some offensive terms in there. So it thought this was offensive, that was offensive, and so on and so forth. And status code 3000. Okay, we know that this is an offensive sentence and now we can run our code to see what we want to do with this offensive sentence. This is text moderation. Now it's up to you how you want to analyze these results. On the flip side, I had a clearly positive sentence. "Pleased to meet you, sir." Well, that's not offensive at all. So I can go down here, and this looks like I did not find any offensive terms in here. So this is how content moderation for text works in Azure cognitive services for decision. There's also the same capability available for images. Feel free to try this with certain images and all the other capabilities that I've talked about in this module. The process is the same, just a rest API call. You can do it.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.