|
2 | 2 | from profanity_check import predict
|
3 | 3 | from cloudevents.http import CloudEvent
|
4 | 4 |
|
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): |
6 | 8 | 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", |
9 | 11 | "datacontenttype": "application/json",
|
10 | 12 | }
|
11 | 13 |
|
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} |
14 | 16 |
|
15 | 17 | # Create a CloudEvent object
|
16 | 18 | event = CloudEvent(attributes, data)
|
17 | 19 |
|
18 | 20 | return event
|
19 | 21 |
|
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"]]) |
22 | 25 | result = "good"
|
23 | 26 | if profanity_result[0] == 1:
|
24 | 27 | result = "bad"
|
25 | 28 |
|
26 |
| - profanity_event = create_cloud_event(result) |
| 29 | + profanity_event = create_cloud_event(text["input"], result) |
27 | 30 | return profanity_event
|
28 | 31 |
|
| 32 | + |
29 | 33 | def main(context: Context):
|
30 | 34 | """
|
31 | 35 | Function template
|
32 | 36 | The context parameter contains the Flask request object and any
|
33 | 37 | CloudEvent received with the request.
|
34 | 38 | """
|
35 | 39 |
|
| 40 | + print("Received CloudEvent: ", context.cloud_event) |
| 41 | + |
36 | 42 | # 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