Create Bad Word Filter Service

Image 4

As a bookstore owner, you aim to receive instant notifications in a Slack channel whenever a customer submits a new negative review comment. By leveraging Knative Function, you can set up a serverless function that contains a simple bad word filter service to tell whether the text contains any hateful/insultive speech.

If you ever get stuck, check the solution here.

Solution - Go to Deploy ML workflow: Bad word filter 3 - Create Bad Word Service - 图2

What Knative features will we learn about?

  • The easiness to use Knative Function to deploy your service, and make it be managed by Knative Serving, which gives you the ability to auto-scale your service to zero, and scale up to handle the demand.

What does the final deliverable look like?

Image 2

A running serverless Knative Function that contains a python application that receives the new review comments as CloudEvent and returns the result that tells your input text contains any inappropriate languages or not. The result is sent back as CloudEvent.

Info

We are using the profanity_check library to detect the bad words in the text. It is a open source library. Please see the disclaimer here. The result may not be 100% accurate.

The function’s output will be only from:

  • good
  • bad

Implementation

Image 10

The process is straightforward:

  1. Begin by utilizing the func create command to generate your code template.
  2. Next, incorporate your unique code into this template.
  3. Finally, execute func deploy to deploy your application seamlessly to the Kubernetes cluster.

This workflow ensures a smooth transition from development to deployment within the Knative Functions ecosystem.


Step 1: Create a Knative Function template

Image 6

  1. func deploy -b=s2i -v

Verify

The file tree will look like this:

  1. /start/bad-word-filter
  2. ├── func.yaml
  3. ├── .funcignore
  4. ├── .gitignore
  5. ├── requirements.txt
  6. ├── app.sh
  7. ├── test_func.py
  8. ├── README.md
  9. └── Procfile
  10. └── func.py

Step 2: Replace the generated code with the bad word filter logic

Image 5

bad-word-filter/func.py is the file that contains the code for the function. You can replace the generated code with the bad word filter logic. You can use the following code as a starting point:

bad-word-filter/func.py

  1. from parliament import Context
  2. from profanity_check import predict
  3. from cloudevents.http import CloudEvent
  4. # The function to convert the bad word filter result into a CloudEvent
  5. def create_cloud_event(inputText, data):
  6. attributes = {
  7. "type": "new-review-comment",
  8. "source": "book-review-broker",
  9. "datacontenttype": "application/json",
  10. "badwordfilter": data,
  11. }
  12. # Put the bad word filter result into a dictionary
  13. data = {"reviewText": inputText, "badWordResult": data}
  14. # Create a CloudEvent object
  15. event = CloudEvent(attributes, data)
  16. return event
  17. def inappropriate_language_filter(text):
  18. profanity_result = predict([text["reviewText"]])
  19. result = "good"
  20. if profanity_result[0] == 1:
  21. result = "bad"
  22. profanity_event = create_cloud_event(text["reviewText"], result)
  23. return profanity_event
  24. def main(context: Context):
  25. """
  26. Function template
  27. The context parameter contains the Flask request object and any
  28. CloudEvent received with the request.
  29. """
  30. print("Received CloudEvent: ", context.cloud_event)
  31. # Add your business logic here
  32. return inappropriate_language_filter(context.cloud_event.data)

Step 3: Configure the dependencies

Image 8 The content of bad-word-filter/requirements.txt:

bad-word-filter/requirements.txt

  1. parliament-functions==0.1.0
  2. alt-profanity-check==1.4.1.post1
  3. cloudevents==1.10.1

Step 4: Deploy the function to the cluster

Image 1

Note

Please enter /bad-word-filter when you are executing the following commands.

  1. func deploy -b=s2i -v

Verify

Expect to see the following message:

  1. Function deployed in namespace "default" and exposed at URL:
  2. http://bad-word-filter.default.svc.cluster.local

Verify

Image 7

  1. func invoke -f=cloudevent --data='{"reviewText":"I love Knative so much"}' -v

Verify

Expect to receive a CloudEvent response:

  1. Context Attributes,
  2. specversion: 1.0
  3. type: new-review-comment
  4. source: book-review-broker
  5. id: ebbcd761-3a78-4c44-92e3-de575d1f2d38
  6. time: 2024-05-27T04:44:07.549303Z
  7. datacontenttype: application/json
  8. Extensions,
  9. badwordfilter: good
  10. Data,
  11. {
  12. "reviewText": "I love Knative so much",
  13. "badWordResult": "good"
  14. }

If you see the response, it means that the function is running successfully.

Next Step

Image 9

In this tutorial, you learned how to create a serverless function for a simple service that can detect inappropriate languages in text with Knative.

Next, we’ll be learning how to use Knative Sequence to connect the 2 ML workflows and make sure they are executed in the order you want.

Go to Create Knative Sequence 3 - Create Bad Word Service - 图11