Skip to content

Commit af7b1f0

Browse files
authored
Content moderator (#3)
* Content Moderator samples * Image Review * More samples * Fix Readme
1 parent 9316004 commit af7b1f0

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This project framework provides examples for the following services:
2323
### Vision
2424

2525
* Using the **Computer Vision SDK** [azure-cognititiveservices-vision-computervision](http://pypi.python.org/pypi/azure-cognititiveservices-vision-computervision) for the [Computer Vision API](https://azure.microsoft.com/services/cognitive-services/computer-vision/)
26+
* Using the **Content Moderator SDK** [azure-cognititiveservices-vision-contentmoderator](http://pypi.python.org/pypi/azure-cognititiveservices-vision-contentmoderator) for the [Content Moderator API](https://azure.microsoft.com/services/cognitive-services/content-moderator/)
2627

2728
We provide several meta-packages to help you install several packages at a time. Please note that meta-packages are only recommended for development purpose. It's recommended in production to always pin specific version of individual packages.
2829

@@ -76,7 +77,8 @@ We provide several meta-packages to help you install several packages at a time.
7677
4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests.
7778
4. Set up the environment variable `VIDEOSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute VideoSearch tests.
7879
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute WebSearch tests.
79-
4. Set up the environment variable `COMPUTERVISION_SUBSCRIPTION_KEY` with your key if you want to execute SpellCheck tests. You might override too `COMPUTERVISION_LOCATION` (westcentralus by default).
80+
4. Set up the environment variable `COMPUTERVISION_SUBSCRIPTION_KEY` with your key if you want to execute Computer Vision tests. You might override too `COMPUTERVISION_LOCATION` (westcentralus by default).
81+
4. Set up the environment variable `CONTENTMODERATOR_SUBSCRIPTION_KEY` with your key if you want to execute Content Moderator tests. You might override too `CONTENTMODERATOR_LOCATION` (westcentralus by default).
8082
8183
## Demo
8284

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ azure-cognitiveservices-search-imagesearch
66
azure-cognitiveservices-search-newssearch
77
azure-cognitiveservices-search-videosearch
88
azure-cognitiveservices-search-websearch
9-
azure-cognitiveservices-vision-computervision
9+
azure-cognitiveservices-vision-computervision
10+
azure-cognitiveservices-vision-contentmoderator
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import os.path
2+
from pprint import pprint
3+
4+
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
5+
from azure.cognitiveservices.vision.contentmoderator.models import Content
6+
from msrest.authentication import CognitiveServicesCredentials
7+
8+
SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY"
9+
CONTENTMODERATOR_LOCATION = os.environ.get("CONTENTMODERATOR_LOCATION", "westcentralus")
10+
11+
def image_review_jobs(subscription_key):
12+
"""ImageReviewJobs.
13+
14+
This will review an image using workflow and job.
15+
"""
16+
17+
# The moderation job will use this workflow that you defined earlier.
18+
# See the quickstart article to learn how to setup custom workflows.
19+
# https://docs.microsoft.com/azure/cognitive-services/content-moderator/review-tool-user-guide/workflows
20+
workflow_name = "OCR"
21+
22+
# The name of the team to assign the job to.
23+
# This must be the team name you used to create your Content Moderator account. You can
24+
# retrieve your team name from the Content Moderator web site. Your team name is the Id
25+
# associated with your subscription.
26+
team_name = "pysdktesting"
27+
28+
# An image with this text:
29+
# IF WE DID ALL THE THINGS WE ARE CAPABLE OF DOING, WE WOULD LITERALLY ASTOUND OURSELVE
30+
# Be sure your workflow create a review for this (e.g. OCR contains some words).
31+
image_url = "https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg"
32+
33+
# Where you want to receive the approval/refuse event. This is the only way to get this information.
34+
call_back_endpoint = "https://requestb.in/1l64pe71"
35+
36+
client = ContentModeratorClient(
37+
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
38+
CognitiveServicesCredentials(subscription_key)
39+
)
40+
41+
print("Create moderation job for an image.\n")
42+
job_result = client.reviews.create_job(
43+
team_name,
44+
"Image", # Possible values include: 'Image', 'Text', 'Video'
45+
"ContentID", # Id/Name to identify the content submitted.
46+
workflow_name,
47+
"application/json", # Possible values include: 'application/json', 'image/jpeg'
48+
image_url,
49+
call_back_endpoint=call_back_endpoint
50+
)
51+
job_id = job_result.job_id
52+
53+
print("Get job status before review.")
54+
job_details = client.reviews.get_job_details(
55+
team_name,
56+
job_id,
57+
)
58+
pprint(job_details.as_dict())
59+
60+
input("\nPerform manual reviews on the Content Moderator Review Site, and hit enter here.")
61+
job_details = client.reviews.get_job_details(
62+
team_name,
63+
job_id,
64+
)
65+
pprint(job_details.as_dict())
66+
67+
# Your call back endpoint should have received an event like this:
68+
# {
69+
# "ReviewId": "201802i2957280730e04578aefe678ba6e5ea24",
70+
# "ModifiedOn": "2018-02-06T23:46:26.3121698Z",
71+
# "ModifiedBy": "unknown",
72+
# "CallBackType": "Review",
73+
# "ContentId": "ContentID",
74+
# "ContentType": "Image",
75+
# "Metadata": {
76+
# "hastext": "True",
77+
# "ocrtext": "IF WE DID \r\nALL \r\nTHE THINGS \r\nWE ARE \r\nCAPABLE \r\nOF DOING, \r\nWE WOULD \r\nLITERALLY \r\nASTOUND \r\nOURSELVE \r\n",
78+
# "imagename": "ContentID"
79+
# },
80+
# "ReviewerResultTags": {
81+
# "a": "True",
82+
# "r": "False"
83+
# }
84+
# }
85+
86+
87+
if __name__ == "__main__":
88+
import sys, os.path
89+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
90+
from tools import execute_samples
91+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os.path
2+
from pprint import pprint
3+
4+
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
5+
from msrest.authentication import CognitiveServicesCredentials
6+
7+
SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY"
8+
CONTENTMODERATOR_LOCATION = os.environ.get("CONTENTMODERATOR_LOCATION", "westcentralus")
9+
10+
11+
def image_lists(subscription_key):
12+
"""ImageList.
13+
14+
This will review an image using workflow and job.
15+
"""
16+
17+
client = ContentModeratorClient(
18+
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
19+
CognitiveServicesCredentials(subscription_key)
20+
)
21+
22+
print("Creating list MyList\n")
23+
custom_list = client.list_management_image_lists.create(
24+
"application/json",
25+
{
26+
"name": "MyList",
27+
"description": "A sample list",
28+
"metadata": {
29+
"key_one": "Acceptable",
30+
"key_two": "Potentially racy"
31+
}
32+
}
33+
)
34+
print("Item created:")
35+
pprint(custom_list.as_dict())
36+
list_id = custom_list.id
37+
38+
def add_images(list_id, image_url, label):
39+
print("Adding image {} to list {} with label {}.".format(image_url, list_id, label))
40+
added_image = client.list_management_image.add_image_url_input(
41+
list_id,
42+
"application/json",
43+
data_representation="URL",
44+
value=image_url,
45+
label=label
46+
)
47+
pprint(added_image.as_dict())
48+
49+
if __name__ == "__main__":
50+
import sys, os.path
51+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
52+
from tools import execute_samples
53+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)