Skip to content

Commit f389865

Browse files
Leo6Leoprushh
authored andcommitted
Sample App: Fix the content in the bad word filter knative function code (knative#5947)
* Fix the content in the bad word filter knative function code * Remove the unused import * fix: fix the python format by running black
1 parent e9f1f7c commit f389865

File tree

1 file changed

+15
-9
lines changed
  • code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter

1 file changed

+15
-9
lines changed

code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,42 @@
22
from profanity_check import predict
33
from cloudevents.http import CloudEvent
44

5-
def create_cloud_event(data):
5+
6+
# The function to convert the bad word filter result into a CloudEvent
7+
def create_cloud_event(inputText, data):
68
attributes = {
7-
"type": "knative.sampleapp.inappropriate-language-filter.response",
8-
"source": "inappropriate-language-filter",
9+
"type": "knative.sampleapp.badword.filter.response",
10+
"source": "bad-word-filter",
911
"datacontenttype": "application/json",
1012
}
1113

12-
# Put the inappropriate language filter result into a dictionary
13-
data = {"result": data}
14+
# Put the bad word filter result into a dictionary
15+
data = {"input": inputText, "result": data}
1416

1517
# Create a CloudEvent object
1618
event = CloudEvent(attributes, data)
1719

1820
return event
1921

20-
def inappropriate_language_filter(text: str | None):
21-
profanity_result = predict([text])
22+
23+
def inappropriate_language_filter(text):
24+
profanity_result = predict([text["input"]])
2225
result = "good"
2326
if profanity_result[0] == 1:
2427
result = "bad"
2528

26-
profanity_event = create_cloud_event(result)
29+
profanity_event = create_cloud_event(text["input"], result)
2730
return profanity_event
2831

32+
2933
def main(context: Context):
3034
"""
3135
Function template
3236
The context parameter contains the Flask request object and any
3337
CloudEvent received with the request.
3438
"""
3539

40+
print("Received CloudEvent: ", context.cloud_event)
41+
3642
# Add your business logic here
37-
return inappropriate_language_filter(context.request.args.get("text"))
43+
return inappropriate_language_filter(context.cloud_event.data)

0 commit comments

Comments
 (0)