From a1b0e6c21a0e959fdf4c268f58dee8b51fe4109c Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 23 Apr 2024 14:15:13 -0400 Subject: [PATCH 1/3] Fix the content in the bad word filter knative function code --- .../ML-inappropriate-language-filter/func.py | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) 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 index 6d35345201c..8a2794c3b82 100644 --- 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 @@ -1,29 +1,33 @@ from parliament import Context from profanity_check import predict -from cloudevents.http import CloudEvent +from cloudevents.http import CloudEvent, to_structured -def create_cloud_event(data): +# The function to convert the bad word filter result into a CloudEvent +def create_cloud_event(inputText,data): attributes = { - "type": "knative.sampleapp.inappropriate-language-filter.response", - "source": "inappropriate-language-filter", - "datacontenttype": "application/json", + "type": "knative.sampleapp.badword.filter.response", + "source": "bad-word-filter", + "datacontenttype": "application/json", } - # Put the inappropriate language filter result into a dictionary - data = {"result": data} + # Put the bad word filter result into a dictionary + data = { + "input": inputText, + "result": data + } # Create a CloudEvent object event = CloudEvent(attributes, data) return event -def inappropriate_language_filter(text: str | None): - profanity_result = predict([text]) +def inappropriate_language_filter(text): + profanity_result = predict([text["input"]]) result = "good" if profanity_result[0] == 1: result = "bad" - profanity_event = create_cloud_event(result) + profanity_event = create_cloud_event(text["input"],result) return profanity_event def main(context: Context): @@ -33,5 +37,7 @@ def main(context: Context): CloudEvent received with the request. """ + print("Received CloudEvent: ", context.cloud_event) + # Add your business logic here - return inappropriate_language_filter(context.request.args.get("text")) + return inappropriate_language_filter(context.cloud_event.data) From e86d8668410c17be38b3c6838554bd23fc5c79a6 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Tue, 23 Apr 2024 14:17:27 -0400 Subject: [PATCH 2/3] Remove the unused import --- .../ML-inappropriate-language-filter/func.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 8a2794c3b82..daeb644ffc7 100644 --- 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 @@ -1,6 +1,6 @@ from parliament import Context from profanity_check import predict -from cloudevents.http import CloudEvent, to_structured +from cloudevents.http import CloudEvent # The function to convert the bad word filter result into a CloudEvent def create_cloud_event(inputText,data): From 6b438465cb0f4eba781b41d9b4d1e938e767708c Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 25 Apr 2024 16:31:41 -0400 Subject: [PATCH 3/3] fix: fix the python format by running black --- .../ML-inappropriate-language-filter/func.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 index daeb644ffc7..c9d89d487d2 100644 --- 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 @@ -2,34 +2,34 @@ from profanity_check import predict from cloudevents.http import CloudEvent + # The function to convert the bad word filter result into a CloudEvent -def create_cloud_event(inputText,data): +def create_cloud_event(inputText, data): attributes = { - "type": "knative.sampleapp.badword.filter.response", - "source": "bad-word-filter", - "datacontenttype": "application/json", + "type": "knative.sampleapp.badword.filter.response", + "source": "bad-word-filter", + "datacontenttype": "application/json", } # Put the bad word filter result into a dictionary - data = { - "input": inputText, - "result": data - } + data = {"input": inputText, "result": data} # Create a CloudEvent object event = CloudEvent(attributes, data) return event + def inappropriate_language_filter(text): profanity_result = predict([text["input"]]) result = "good" if profanity_result[0] == 1: result = "bad" - profanity_event = create_cloud_event(text["input"],result) + profanity_event = create_cloud_event(text["input"], result) return profanity_event + def main(context: Context): """ Function template