-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathContentModeratorQuickstart.py
More file actions
541 lines (486 loc) · 17.2 KB
/
Copy pathContentModeratorQuickstart.py
File metadata and controls
541 lines (486 loc) · 17.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# <snippet_imports>
import os.path
from pprint import pprint
import time
from io import BytesIO
from random import random
import uuid
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
import azure.cognitiveservices.vision.contentmoderator.models
from msrest.authentication import CognitiveServicesCredentials
# </snippet_imports>
# <snippet_vars>
CONTENT_MODERATOR_ENDPOINT = "PASTE_YOUR_CONTENT_MODERATOR_ENDPOINT_HERE"
subscription_key = "PASTE_YOUR_CONTENT_MODERATOR_SUBSCRIPTION_KEY_HERE"
# </snippet_vars>
# <snippet_client>
client = ContentModeratorClient(
endpoint=CONTENT_MODERATOR_ENDPOINT,
credentials=CognitiveServicesCredentials(subscription_key)
)
# </snippet_client>
# <snippet_textfolder>
TEXT_FOLDER = os.path.join(os.path.dirname(
os.path.realpath(__file__)), "text_files")
# </snippet_textfolder>
# <snippet_imagemodvars>
IMAGE_LIST = [
"https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png"
]
# </snippet_imagemodvars>
# <snippet_imagelistvars>
IMAGE_LIST = {
"Sports": [
"https://moderatorsampleimages.blob.core.windows.net/samples/sample4.png",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample6.png",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample9.png"
],
"Swimsuit": [
"https://moderatorsampleimages.blob.core.windows.net/samples/sample1.jpg",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample3.png",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample4.png",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
]
}
IMAGES_TO_MATCH = [
"https://moderatorsampleimages.blob.core.windows.net/samples/sample1.jpg",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample4.png",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png",
"https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
]
# </snippet_imagelistvars>
def text_moderation():
"""TextModeration.
This will moderate a given long text.
"""
# <snippet_textmod>
# 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())
# </snippet_textmod>
def terms_lists():
"""TermsList.
This will screen text using a term list.
"""
# <snippet_termslist_create>
#
# Create list
#
print("\nCreating list")
custom_list = client.list_management_term_lists.create(
content_type="application/json",
body={
"name": "Term list name",
"description": "Term list description",
}
)
print("List created:")
assert isinstance(custom_list, TermList)
pprint(custom_list.as_dict())
list_id = custom_list.id
# </snippet_termslist_create>
# <snippet_termslist_details>
#
# Update list details
#
print("\nUpdating details for list {}".format(list_id))
updated_list = client.list_management_term_lists.update(
list_id=list_id,
content_type="application/json",
body={
"name": "New name",
"description": "New description"
}
)
assert isinstance(updated_list, TermList)
pprint(updated_list.as_dict())
# </snippet_termslist_details>
# <snippet_termslist_add>
#
# Add terms
#
print("\nAdding terms to list {}".format(list_id))
client.list_management_term.add_term(
list_id=list_id,
term="term1",
language="eng"
)
client.list_management_term.add_term(
list_id=list_id,
term="term2",
language="eng"
)
# </snippet_termslist_add>
# <snippet_termslist_getterms>
#
# Get all terms ids
#
print("\nGetting all term IDs for list {}".format(list_id))
terms = client.list_management_term.get_all_terms(
list_id=list_id, language="eng")
assert isinstance(terms, Terms)
terms_data = terms.data
assert isinstance(terms_data, TermsData)
pprint(terms_data.as_dict())
# </snippet_termslist_getterms>
# <snippet_termslist_refresh>
#
# Refresh the index
#
print("\nRefreshing the search index for list {}".format(list_id))
refresh_index = client.list_management_term_lists.refresh_index_method(
list_id=list_id, language="eng")
assert isinstance(refresh_index, RefreshIndex)
pprint(refresh_index.as_dict())
print("\nWaiting {} minutes to allow the server time to propagate the index changes.".format(
LATENCY_DELAY))
time.sleep(LATENCY_DELAY * 60)
# </snippet_termslist_refresh>
# <snippet_termslist_screen>
#
# Screen text
#
with open(os.path.join(TEXT_FOLDER, 'content_moderator_term_list.txt'), "rb") as text_fd:
screen = client.text_moderation.screen_text(
text_content_type="text/plain",
text_content=text_fd,
language="eng",
autocorrect=False,
pii=False,
list_id=list_id
)
assert isinstance(screen, Screen)
pprint(screen.as_dict())
# </snippet_termslist_screen>
# <snippet_termslist_remove>
#
# Remove terms
#
term_to_remove = "term1"
print("\nRemove term {} from list {}".format(term_to_remove, list_id))
client.list_management_term.delete_term(
list_id=list_id,
term=term_to_remove,
language="eng"
)
# </snippet_termslist_remove>
#
# Refresh the index
#
print("\nRefreshing the search index for list {}".format(list_id))
refresh_index = client.list_management_term_lists.refresh_index_method(
list_id=list_id, language="eng")
assert isinstance(refresh_index, RefreshIndex)
pprint(refresh_index.as_dict())
print("\nWaiting {} minutes to allow the server time to propagate the index changes.".format(
LATENCY_DELAY))
time.sleep(LATENCY_DELAY * 60)
#
# Re-Screen text
#
with open(os.path.join(TEXT_FOLDER, 'content_moderator_term_list.txt'), "rb") as text_fd:
print('\nScreening text "{}" using term list {}'.format(text, list_id))
screen = client.text_moderation.screen_text(
text_content_type="text/plain",
text_content=text_fd,
language="eng",
autocorrect=False,
pii=False,
list_id=list_id
)
assert isinstance(screen, Screen)
pprint(screen.as_dict())
# <snippet_termslist_removeall>
#
# Delete all terms
#
print("\nDelete all terms in the image list {}".format(list_id))
client.list_management_term.delete_all_terms(
list_id=list_id, language="eng")
# </snippet_termslist_removeall>
# <snippet_termslist_deletelist>
#
# Delete list
#
print("\nDelete the term list {}".format(list_id))
client.list_management_term_lists.delete(list_id=list_id)
# </snippet_termslist_deletelist>
def image_moderation():
"""ImageModeration.
This will review an image using workflow and job.
"""
# <snippet_imagemod_iterate>
for image_url in IMAGE_LIST:
print("\nEvaluate image {}".format(image_url))
# </snippet_imagemod_iterate>
# <snippet_imagemod_ar>
print("\nEvaluate for adult and racy content.")
evaluation = client.image_moderation.evaluate_url_input(
content_type="application/json",
cache_image=True,
data_representation="URL",
value=image_url
)
assert isinstance(evaluation, Evaluate)
pprint(evaluation.as_dict())
# </snippet_imagemod_ar>
# <snippet_imagemod_text>
print("\nDetect and extract text.")
evaluation = client.image_moderation.ocr_url_input(
language="eng",
content_type="application/json",
data_representation="URL",
value=image_url,
cache_image=True,
)
assert isinstance(evaluation, OCR)
pprint(evaluation.as_dict())
# </snippet_imagemod_text>
# <snippet_imagemod_face>
print("\nDetect faces.")
evaluation = client.image_moderation.find_faces_url_input(
content_type="application/json",
cache_image=True,
data_representation="URL",
value=image_url
)
assert isinstance(evaluation, FoundFaces)
pprint(evaluation.as_dict())
# </snippet_imagemod_face>
def image_lists():
"""ImageList.
This will review an image using workflow and job.
"""
# <snippet_imagelist_create>
#
# Create list
#
print("Creating list MyList\n")
custom_list = client.list_management_image_lists.create(
content_type="application/json",
body={
"name": "MyList",
"description": "A sample list",
"metadata": {
"key_one": "Acceptable",
"key_two": "Potentially racy"
}
}
)
print("List created:")
assert isinstance(custom_list, ImageList)
pprint(custom_list.as_dict())
list_id = custom_list.id
# </snippet_imagelist_create>
# <snippet_imagelist_addhelper>
#
# Add images
#
def add_images(list_id, image_url, label):
"""Generic add_images from url and label."""
print("\nAdding image {} to list {} with label {}.".format(
image_url, list_id, label))
try:
added_image = client.list_management_image.add_image_url_input(
list_id=list_id,
content_type="application/json",
data_representation="URL",
value=image_url,
label=label
)
except APIErrorException as err:
# sample4 will fail
print("Unable to add image to list: {}".format(err))
else:
assert isinstance(added_image, Image)
pprint(added_image.as_dict())
return added_image
# </snippet_imagelist_addhelper>
# <snippet_imagelist_add>
print("\nAdding images to list {}".format(list_id))
index = {} # Keep an index url to id for later removal
for label, urls in IMAGE_LIST.items():
for url in urls:
image = add_images(list_id, url, label)
if image:
index[url] = image.content_id
# </snippet_imagelist_add>
# <snippet_imagelist_getimages>
#
# Get all images ids
#
print("\nGetting all image IDs for list {}".format(list_id))
image_ids = client.list_management_image.get_all_image_ids(list_id=list_id)
assert isinstance(image_ids, ImageIds)
pprint(image_ids.as_dict())
# </snippet_imagelist_getimages>
# <snippet_imagelist_updatedetails>
#
# Update list details
#
print("\nUpdating details for list {}".format(list_id))
updated_list = client.list_management_image_lists.update(
list_id=list_id,
content_type="application/json",
body={
"name": "Swimsuits and sports"
}
)
assert isinstance(updated_list, ImageList)
pprint(updated_list.as_dict())
# </snippet_imagelist_updatedetails>
# <snippet_imagelist_getdetails>
#
# Get list details
#
print("\nGetting details for list {}".format(list_id))
list_details = client.list_management_image_lists.get_details(
list_id=list_id)
assert isinstance(list_details, ImageList)
pprint(list_details.as_dict())
# </snippet_imagelist_getdetails>
# <snippet_imagelist_refresh>
#
# Refresh the index
#
print("\nRefreshing the search index for list {}".format(list_id))
refresh_index = client.list_management_image_lists.refresh_index_method(
list_id=list_id)
assert isinstance(refresh_index, RefreshIndex)
pprint(refresh_index.as_dict())
print("\nWaiting {} minutes to allow the server time to propagate the index changes.".format(
LATENCY_DELAY))
time.sleep(LATENCY_DELAY * 60)
# </snippet_imagelist_refresh>
# <snippet_imagelist_match>
#
# Match images against the image list.
#
for image_url in IMAGES_TO_MATCH:
print("\nMatching image {} against list {}".format(image_url, list_id))
match_result = client.image_moderation.match_url_input(
content_type="application/json",
list_id=list_id,
data_representation="URL",
value=image_url,
)
assert isinstance(match_result, MatchResponse)
print("Is match? {}".format(match_result.is_match))
print("Complete match details:")
pprint(match_result.as_dict())
# </snippet_imagelist_match>
# <snippet_imagelist_remove>
#
# Remove images
#
correction = "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
print("\nRemove image {} from list {}".format(correction, list_id))
client.list_management_image.delete_image(
list_id=list_id,
image_id=index[correction]
)
# </snippet_imagelist_remove>
#
# Refresh the index
#
print("\nRefreshing the search index for list {}".format(list_id))
client.list_management_image_lists.refresh_index_method(list_id=list_id)
print("\nWaiting {} minutes to allow the server time to propagate the index changes.".format(
LATENCY_DELAY))
time.sleep(LATENCY_DELAY * 60)
#
# Re-match
#
print("\nMatching image. The removed image should not match")
for image_url in IMAGES_TO_MATCH:
print("\nMatching image {} against list {}".format(image_url, list_id))
match_result = client.image_moderation.match_url_input(
content_type="application/json",
list_id=list_id,
data_representation="URL",
value=image_url,
)
assert isinstance(match_result, MatchResponse)
print("Is match? {}".format(match_result.is_match))
print("Complete match details:")
pprint(match_result.as_dict())
# <snippet_imagelist_removeall>
#
# Delete all images
#
print("\nDelete all images in the image list {}".format(list_id))
client.list_management_image.delete_all_images(list_id=list_id)
# </snippet_imagelist_removeall>
# <snippet_imagelist_delete>
#
# Delete list
#
print("\nDelete the image list {}".format(list_id))
client.list_management_image_lists.delete(list_id=list_id)
# </snippet_imagelist_delete>
#
# Get all list ids
#
print("\nVerify that the list {} was deleted.".format(list_id))
image_lists = client.list_management_image_lists.get_all_image_lists()
assert not any(list_id == image_list.id for image_list in image_lists)
def image_review(subscription_key):
"""ImageReview.
This will create a review for images.
"""
# <snippet_imagereview_vars>
# The name of the team to assign the job to.
# This must be the team name you used to create your Content Moderator account. You can
# retrieve your team name from the Review tool web site. Your team name is the Id
# associated with your subscription.
team_name = "<insert your team name here>"
# An image to review
image_url = "https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png"
# Where you want to receive the approval/refuse event. This is the only way to get this information.
call_back_endpoint = "https://requestb.in/qmsakwqm"
# </snippet_imagereview_vars>
# <snippet_imagereview_create>
# Create review
print("Create review for {}.\n".format(image_url))
review_item = {
"type": "Image", # Possible values include: 'Image', 'Text'
"content": image_url, # How to download the image
"content_id": uuid.uuid4(), # Random id
"callback_endpoint": call_back_endpoint,
"metadata": [{
"key": "sc",
"value": True # will be sent to Azure as "str" cast.
}]
}
reviews = client.reviews.create_reviews(
url_content_type="application/json",
team_name=team_name,
create_review_body=[review_item] # As many review item as you need
)
# Get review ID
review_id = reviews[0] # Ordered list of string of review ID
# </snippet_imagereview_create>
# <snippet_imagereview_getdetails>
print("\nGet review details")
review_details = client.reviews.get_review(
team_name=team_name, review_id=review_id)
pprint(review_details.as_dict())
# </snippet_imagereview_getdetails>
# wait for user input through the Review tool web portal
input("\nPerform manual reviews on the Content Moderator Review Site, and hit enter here.")
# Check the results of the human review
print("\nGet review details")
review_details = client.reviews.get_review(
team_name=team_name, review_id=review_id)
pprint(review_details.as_dict())