Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Inappropriate language filter python script (#5935)
Signed-off-by: tico88612 <17496418+tico88612@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.funcignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
# Use the .funcignore file to exclude files which should not be | ||
# tracked in the image build. To instruct the system not to track | ||
# files in the image build, add the regex pattern or file information | ||
# to this file. |
5 changes: 5 additions & 0 deletions
5
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
# Functions use the .func directory for local runtime data which should | ||
# generally not be tracked in source control. To instruct the system to track | ||
# .func in source control, comment the following line (prefix it with '# '). | ||
/.func |
1 change: 1 addition & 0 deletions
1
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/Procfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: python -m parliament . |
3 changes: 3 additions & 0 deletions
3
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/app.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
exec python -m parliament "$(dirname "$0")" |
37 changes: 37 additions & 0 deletions
37
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from parliament import Context | ||
from profanity_check import predict | ||
from cloudevents.http import CloudEvent | ||
|
||
def create_cloud_event(data): | ||
attributes = { | ||
"type": "knative.sampleapp.inappropriate-language-filter.response", | ||
"source": "inappropriate-language-filter", | ||
"datacontenttype": "application/json", | ||
} | ||
|
||
# Put the inappropriate language filter result into a dictionary | ||
data = {"result": data} | ||
|
||
# Create a CloudEvent object | ||
event = CloudEvent(attributes, data) | ||
|
||
return event | ||
|
||
def inappropriate_language_filter(text: str | None): | ||
profanity_result = predict([text]) | ||
result = "good" | ||
if profanity_result[0] == 1: | ||
result = "bad" | ||
|
||
profanity_event = create_cloud_event(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. | ||
""" | ||
|
||
# Add your business logic here | ||
return inappropriate_language_filter(context.request.args.get("text")) |
4 changes: 4 additions & 0 deletions
4
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/func.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
specVersion: 0.36.0 | ||
name: inappropriate-language-filter | ||
runtime: python | ||
created: 2024-03-27T23:12:06.178272+08:00 |
3 changes: 3 additions & 0 deletions
3
code-samples/eventing/bookstore-sample-app/ML-inappropriate-language-filter/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
parliament-functions==0.1.0 | ||
alt-profanity-check==1.4.1.post1 | ||
cloudevents==1.10.1 |