diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.funcignore b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.funcignore new file mode 100644 index 0000000000..e8e281c661 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.funcignore @@ -0,0 +1,5 @@ + +# Use the .funcignore file to exclude files which should not be +# tracked in the image build. To instruct the system not to track +# files in the image build, add the regex pattern or file information +# to this file. diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.gitignore b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.gitignore new file mode 100644 index 0000000000..965f0d4ef0 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.gitignore @@ -0,0 +1,5 @@ + +# Functions use the .func directory for local runtime data which should +# generally not be tracked in source control. To instruct the system to track +# .func in source control, comment the following line (prefix it with '# '). +/.func diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/Procfile b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/Procfile new file mode 100644 index 0000000000..bb57f5a81c --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/Procfile @@ -0,0 +1 @@ +web: python -m parliament . diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/app.sh b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/app.sh new file mode 100755 index 0000000000..4da37d4d8d --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/app.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec python -m parliament "$(dirname "$0")" diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.py b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.py new file mode 100644 index 0000000000..6d35345201 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.py @@ -0,0 +1,37 @@ +from parliament import Context +from profanity_check import predict +from cloudevents.http import CloudEvent + +def create_cloud_event(data): + attributes = { + "type": "knative.sampleapp.inappropriate-language-filter.response", + "source": "inappropriate-language-filter", + "datacontenttype": "application/json", + } + + # Put the inappropriate language filter result into a dictionary + data = {"result": data} + + # Create a CloudEvent object + event = CloudEvent(attributes, data) + + return event + +def inappropriate_language_filter(text: str | None): + profanity_result = predict([text]) + result = "good" + if profanity_result[0] == 1: + result = "bad" + + profanity_event = create_cloud_event(result) + return profanity_event + +def main(context: Context): + """ + Function template + The context parameter contains the Flask request object and any + CloudEvent received with the request. + """ + + # Add your business logic here + return inappropriate_language_filter(context.request.args.get("text")) diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.yaml b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.yaml new file mode 100644 index 0000000000..779c38d71f --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.yaml @@ -0,0 +1,4 @@ +specVersion: 0.36.0 +name: inappropriate-language-filter +runtime: python +created: 2024-03-27T23:12:06.178272+08:00 diff --git a/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/requirements.txt b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/requirements.txt new file mode 100644 index 0000000000..69aa57d8e1 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/requirements.txt @@ -0,0 +1,3 @@ +parliament-functions==0.1.0 +alt-profanity-check==1.4.1.post1 +cloudevents==1.10.1