-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathcontent_moderator_text_moderation_samples.py
51 lines (40 loc) · 1.75 KB
/
content_moderator_text_moderation_samples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os, os.path
from pprint import pprint
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
from azure.cognitiveservices.vision.contentmoderator.models import (
Screen
)
from msrest.authentication import CognitiveServicesCredentials
# Add your Azure Content Moderator subscription key to your environment variables.
SUBSCRIPTION_KEY = os.environ['CONTENT_MODERATOR_SUBSCRIPTION_KEY']
TEXT_FOLDER = os.path.join(os.path.dirname(
os.path.realpath(__file__)), "text_files")
# The number of minutes to delay after updating the search index before
# performing image match operations against the list.
LATENCY_DELAY = 0.5
def text_moderation(subscription_key):
"""TextModeration.
This will moderate a given long text.
"""
client = ContentModeratorClient(
endpoint=os.environ['CONTENT_MODERATOR_ENDPOINT'], # Add your Content Moderator endpoint to your environment variables.
credentials=CognitiveServicesCredentials(subscription_key)
)
# Screen the input text: check for profanity,
# do autocorrect text, and check for personally identifying
# information (PII)
with open(os.path.join(TEXT_FOLDER, 'content_moderator_text_moderation.txt'), "rb") as text_fd:
screen = client.text_moderation.screen_text(
text_content_type="text/plain",
text_content=text_fd,
language="eng",
autocorrect=True,
pii=True
)
assert isinstance(screen, Screen)
pprint(screen.as_dict())
if __name__ == "__main__":
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..", "..")))
from samples.tools import execute_samples
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)