Skip to content

Commit a1b0e6c

Browse files
committed
Fix the content in the bad word filter knative function code
1 parent afebc06 commit a1b0e6c

File tree

1 file changed

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

1 file changed

+17
-11
lines changed
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
from parliament import Context
22
from profanity_check import predict
3-
from cloudevents.http import CloudEvent
3+
from cloudevents.http import CloudEvent, to_structured
44

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

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

1519
# Create a CloudEvent object
1620
event = CloudEvent(attributes, data)
1721

1822
return event
1923

20-
def inappropriate_language_filter(text: str | None):
21-
profanity_result = predict([text])
24+
def inappropriate_language_filter(text):
25+
profanity_result = predict([text["input"]])
2226
result = "good"
2327
if profanity_result[0] == 1:
2428
result = "bad"
2529

26-
profanity_event = create_cloud_event(result)
30+
profanity_event = create_cloud_event(text["input"],result)
2731
return profanity_event
2832

2933
def main(context: Context):
@@ -33,5 +37,7 @@ def main(context: Context):
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)