1
1
from parliament import Context
2
2
from profanity_check import predict
3
- from cloudevents .http import CloudEvent
3
+ from cloudevents .http import CloudEvent , to_structured
4
4
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 ):
6
7
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" ,
10
11
}
11
12
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
+ }
14
18
15
19
# Create a CloudEvent object
16
20
event = CloudEvent (attributes , data )
17
21
18
22
return event
19
23
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" ] ])
22
26
result = "good"
23
27
if profanity_result [0 ] == 1 :
24
28
result = "bad"
25
29
26
- profanity_event = create_cloud_event (result )
30
+ profanity_event = create_cloud_event (text [ "input" ], result )
27
31
return profanity_event
28
32
29
33
def main (context : Context ):
@@ -33,5 +37,7 @@ def main(context: Context):
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