Skip to content

Sample App: Fix the content in the bad word filter knative function code #5947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@
from profanity_check import predict
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 = {
"type": "knative.sampleapp.inappropriate-language-filter.response",
"source": "inappropriate-language-filter",
"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):
"""
Function template
The context parameter contains the Flask request object and any
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)