mirror of https://github.com/knative/docs.git
Sample App: Fix the content in the bad word filter knative function code (#5947)
* Fix the content in the bad word filter knative function code * Remove the unused import * fix: fix the python format by running black
This commit is contained in:
parent
87d2c89ab0
commit
61e322af5b
|
|
@ -2,30 +2,34 @@ from parliament import Context
|
||||||
from profanity_check import predict
|
from profanity_check import predict
|
||||||
from cloudevents.http import CloudEvent
|
from cloudevents.http import CloudEvent
|
||||||
|
|
||||||
def create_cloud_event(data):
|
|
||||||
|
# The function to convert the bad word filter result into a CloudEvent
|
||||||
|
def create_cloud_event(inputText, data):
|
||||||
attributes = {
|
attributes = {
|
||||||
"type": "knative.sampleapp.inappropriate-language-filter.response",
|
"type": "knative.sampleapp.badword.filter.response",
|
||||||
"source": "inappropriate-language-filter",
|
"source": "bad-word-filter",
|
||||||
"datacontenttype": "application/json",
|
"datacontenttype": "application/json",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Put the inappropriate language filter result into a dictionary
|
# Put the bad word filter result into a dictionary
|
||||||
data = {"result": data}
|
data = {"input": inputText, "result": data}
|
||||||
|
|
||||||
# Create a CloudEvent object
|
# Create a CloudEvent object
|
||||||
event = CloudEvent(attributes, data)
|
event = CloudEvent(attributes, data)
|
||||||
|
|
||||||
return event
|
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"
|
result = "good"
|
||||||
if profanity_result[0] == 1:
|
if profanity_result[0] == 1:
|
||||||
result = "bad"
|
result = "bad"
|
||||||
|
|
||||||
profanity_event = create_cloud_event(result)
|
profanity_event = create_cloud_event(text["input"], result)
|
||||||
return profanity_event
|
return profanity_event
|
||||||
|
|
||||||
|
|
||||||
def main(context: Context):
|
def main(context: Context):
|
||||||
"""
|
"""
|
||||||
Function template
|
Function template
|
||||||
|
|
@ -33,5 +37,7 @@ def main(context: Context):
|
||||||
CloudEvent received with the request.
|
CloudEvent received with the request.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
print("Received CloudEvent: ", context.cloud_event)
|
||||||
|
|
||||||
# Add your business logic here
|
# Add your business logic here
|
||||||
return inappropriate_language_filter(context.request.args.get("text"))
|
return inappropriate_language_filter(context.cloud_event.data)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue