示例#1
0
def azure_scan(file_name=None, file_url=None):
    client = ContentModeratorClient(
        f"https://{AZURE_LOC}.api.cognitive.microsoft.com/",
        CognitiveServicesCredentials(AZURE_TOKEN),
    )
    if file_name is not None:
        evaluation = client.image_moderation.evaluate_file_input(
            image_stream=open(file_name, "rb"), cache_image=True)
    else:
        evaluation = client.image_moderation.evaluate_url_input(
            content_type="application/json",
            cache_image=True,
            data_representation="URL",
            value=file_url,
        )

    results = [
        evaluation.adult_classification_score,
        evaluation.racy_classification_score,
    ]
    is_safe = all(x < AZURE_THRESHOLD for x in results)
    max_score = max(results)

    if max_score >= 0.9:
        likelihood = "very likely"
    elif max_score >= 0.75:
        likelihood = "likely"
    elif max_score >= 0.5:
        likelihood = "possible"
    elif max_score >= 0.25:
        likelihood = "unlikely"
    else:
        likelihood = "very unlikely"

    return is_safe, likelihood
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())
示例#3
0
def text_moderation(subscription_key):
    """TextModeration.

    This will moderate a given long text.
    """

    client = ContentModeratorClient(
        endpoint='https://' + CONTENTMODERATOR_LOCATION +
        '.api.cognitive.microsoft.com',
        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())
def image_review(subscription_key):
    """ImageReview.

    This will create a review for images.
    """

    # 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 Content Moderator 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"

    client = ContentModeratorClient(
        endpoint='https://' + CONTENTMODERATOR_LOCATION +
        '.api.cognitive.microsoft.com',
        credentials=CognitiveServicesCredentials(subscription_key))

    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
    )
    review_id = reviews[0]  # Ordered list of string of review ID

    print("\nGet review details")
    review_details = client.reviews.get_review(team_name=team_name,
                                               review_id=review_id)
    pprint(review_details.as_dict())

    input(
        "\nPerform manual reviews on the Content Moderator Review Site, and hit enter here."
    )

    print("\nGet review details")
    review_details = client.reviews.get_review(team_name=team_name,
                                               review_id=review_id)
    pprint(review_details.as_dict())
def image_review_jobs(subscription_key):
    """ImageReviewJobs.

    This will review an image using workflow and job.
    """

    # The moderation job will use this workflow that you defined earlier.
    # See the quickstart article to learn how to setup custom workflows.
    # https://docs.microsoft.com/azure/cognitive-services/content-moderator/review-tool-user-guide/workflows
    workflow_name = "insert your workflow name here"

    # 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 Content Moderator web site. Your team name is the Id
    # associated with your subscription.
    team_name = "insert your team name here"

    # An image with this text:
    # IF WE DID ALL THE THINGS WE ARE CAPABLE OF DOING, WE WOULD LITERALLY ASTOUND OURSELVE
    # Be sure your workflow create a review for this (e.g. OCR contains some words).
    image_url = "https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg"

    # Where you want to receive the approval/refuse event. This is the only way to get this information.
    call_back_endpoint = "https://requestb.in/1l64pe71"

    client = ContentModeratorClient(
        endpoint=os.environ[
            'CONTENT_MODERATOR_ENDPOINT'],  # Add your Content Moderator endpoint to your environment variables.
        credentials=CognitiveServicesCredentials(subscription_key))

    print("Create moderation job for an image.\n")
    job_result = client.reviews.create_job(
        team_name=team_name,
        content_type=
        "Image",  # Possible values include: 'Image', 'Text', 'Video'
        content_id="ContentID",  # Id/Name to identify the content submitted.
        workflow_name=workflow_name,
        # Possible values include: 'application/json', 'image/jpeg'
        job_content_type="application/json",
        content_value=image_url,
        call_back_endpoint=call_back_endpoint)
    job_id = job_result.job_id

    print("Get job status before review.")
    job_details = client.reviews.get_job_details(
        team_name=team_name,
        job_id=job_id,
    )
    pprint(job_details.as_dict())

    input(
        "\nPerform manual reviews on the Content Moderator Review Site, and hit enter here."
    )
    job_details = client.reviews.get_job_details(
        team_name=team_name,
        job_id=job_id,
    )
    pprint(job_details.as_dict())
def image_moderation(subscription_key):
    """ImageModeration.

    This will review an image using workflow and job.
    """

    client = ContentModeratorClient(
        endpoint=os.environ['CONTENT_MODERATOR_ENDPOINT'], # Add your Content Moderator endpoint to your environment variables.
        credentials=CognitiveServicesCredentials(subscription_key)
    )

    for image_url in IMAGE_LIST:
        print("\nEvaluate image {}".format(image_url))

        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())

        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())

        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())
示例#7
0
def image_moderation(subscription_key):
    """ImageModeration.

    This will review an image using workflow and job.
    """
    
    client = ContentModeratorClient(
        CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
        CognitiveServicesCredentials(subscription_key)
    )
    
    for image_url in IMAGE_LIST:
        print("\nEvaluate image {}".format(image_url))

        print("\nEvaluate for adult and racy content.")
        evaluation = client.image_moderation.evaluate_url_input(
            "application/json",
            data_representation="URL",
            value=image_url,
            cache_image=True,
        )
        assert isinstance(evaluation, Evaluate)
        pprint(evaluation.as_dict())

        print("\nDetect and extract text.")
        evaluation = client.image_moderation.ocr_url_input(
            "eng",
            "application/json",
            data_representation="URL",
            value=image_url,
            cache_image=True,
        )
        assert isinstance(evaluation, OCR)
        pprint(evaluation.as_dict())

        print("\nDetect faces.")
        evaluation = client.image_moderation.find_faces_url_input(
            "application/json",
            data_representation="URL",
            value=image_url,
            cache_image=True,
        )
        assert isinstance(evaluation, FoundFaces)
        pprint(evaluation.as_dict())
示例#8
0
def text_moderation(url, key, text_to_moderate):
    '''
    This functon will moderate the input text.
    @param text, string --> text to be moderated
    '''

    client = ContentModeratorClient(url, CognitiveServicesCredentials(key))

    # Check for profanity in the input text
    screen = client.text_moderation.screen_text(
        text_content_type="text/plain",
        text_content=BytesIO(bytes(text_to_moderate, 'utf-8')),
        language="eng",
        autocorrect=True,
        classify=True)

    # Category1 refers to potential presence of language that may be considered sexually explicit or adult in certain situations.
    # Category2 refers to potential presence of language that may be considered sexually suggestive or mature in certain situations.
    # Category3 refers to potential presence of language that may be considered offensive in certain situations.
    # Score is between 0 and 1. The higher the score, the higher the model is predicting that the category may be applicable. This feature relies on a statistical model rather than manually coded outcomes. We recommend testing with your own content to determine how each category aligns to your requirements.

    assert isinstance(screen, Screen)
    return screen.as_dict()
def text_moderation(subscription_key):
    """TextModeration.

    This will moderate a given long text.
    """
    
    client = ContentModeratorClient(
        CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
        CognitiveServicesCredentials(subscription_key)
    )

    # Screen the input text: check for profanity, 
    # do autocorrect text, and check for personally identifying 
    # information (PII)
    screen = client.text_moderation.screen_text(
        "eng",
        "text/plain",
        TEXT,
        autocorrect=True,
        pii=True
    )
    assert isinstance(screen, Screen)
    pprint(screen.as_dict())
示例#10
0
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>
CONTENTMODERATOR_ENDPOINT = os.environ.get("CONTENT_MODERATOR_ENDPOINT")
subscription_key = os.environ.get("CONTENT_MODERATOR_SUBSCRIPTION_KEY")
# </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>
示例#11
0
    def get_client_lazy(self):
        from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient
        from msrest.authentication import CognitiveServicesCredentials

        return ContentModeratorClient(
            self.url, CognitiveServicesCredentials(self.get_secret()))
示例#12
0
def video_review(subscription_key):
    """VideoReview.

    This will create and publish a review for video
    """

    # 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 Content Moderator web site. Your team name is the Id
    # associated with your subscription.
    team_name = "insert your team name here"

    # Create a review with the content pointing to a streaming endpoint (manifest)
    streamingcontent = "https://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest"

    frame1_url = "https://blobthebuilder.blob.core.windows.net/sampleframes/ams-video-frame1-00-17.PNG"
    frame2_url = "https://blobthebuilder.blob.core.windows.net/sampleframes/ams-video-frame-2-01-04.PNG"
    frame3_url = "https://blobthebuilder.blob.core.windows.net/sampleframes/ams-video-frame-3-02-24.PNG"

    client = ContentModeratorClient(
        endpoint=os.environ[
            'CONTENT_MODERATOR_ENDPOINT'],  # Add your Content Moderator endpoint to your environment variables.
        credentials=CognitiveServicesCredentials(subscription_key))

    #
    # Create a video review
    #
    print("Create review for {}.\n".format(streamingcontent))
    review_item = {
        "content": streamingcontent,  # How to download the image
        "content_id": uuid.uuid4(),  # Random id
        # Note: to create a published review, set the Status to "Pending".
        # However, you cannot add video frames or a transcript to a published review.
        "status": "Unpublished"
    }

    reviews = client.reviews.create_video_reviews(
        content_type="application/json",
        team_name=team_name,
        # As many review item as you need
        create_video_reviews_body=[review_item])
    review_id = reviews[0]  # Ordered list of string of review ID

    #
    # Add the frames from 17, 64, and 144 seconds.
    #
    print("\nAdding frames to the review {}".format(review_id))

    def create_frames_to_add_to_reviews(timestamp_seconds, url):
        return {
            'timestamp':
            timestamp_seconds * 1000,
            'frame_image':
            url,
            'reviewer_result_tags': [
                # Note: All non str value will be casted using "str()"
                {
                    'key': 'reviewRecommended',
                    'value': True
                },
                {
                    'key': 'adultScore',
                    'value': random()
                },
                {
                    'key': 'a',
                    'value': False
                },
                {
                    'key': 'racyScore',
                    'value': random()
                },
                {
                    'key': 'a',
                    'value': False
                },
            ],
            'metadata': [
                # Note: All non str value will be casted using "str()"
                {
                    'key': 'tag1',
                    'value': 'tag1'
                },
            ]
        }

    client.reviews.add_video_frame_url(
        content_type="application/json",
        team_name=team_name,
        review_id=review_id,
        video_frame_body=[
            create_frames_to_add_to_reviews(17, frame1_url),
            create_frames_to_add_to_reviews(64, frame2_url),
            create_frames_to_add_to_reviews(144, frame3_url)
        ])

    #
    # Get frames
    #
    print("\nGetting frames for the review with ID {}".format(review_id))
    frames = client.reviews.get_video_frames(team_name=team_name,
                                             review_id=review_id,
                                             start_seed=0,
                                             no_of_records=100)
    assert isinstance(frames, Frames)
    pprint(frames.as_dict())

    #
    # Get reviews details
    #
    print(
        "\nGetting review details for the review with ID {}".format(review_id))
    review_details = client.reviews.get_review(team_name=team_name,
                                               review_id=review_id)
    pprint(review_details.as_dict())

    #
    # Public review
    #
    client.reviews.publish_video_review(team_name=team_name,
                                        review_id=review_id)

    print(
        "\nOpen your Content Moderator Dashboard and select Review > Video to see the review."
    )
示例#13
0
def terms_lists(subscription_key):
    """TermsList.

    This will screen text using a term list.
    """
    
    client = ContentModeratorClient(
        CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com',
        CognitiveServicesCredentials(subscription_key)
    )

    #
    # Create list
    #

    print("\nCreating list")
    custom_list = client.list_management_term_lists.create(
        "application/json",
        {
            "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

    #
    # Update list details
    #
    print("\nUpdating details for list {}".format(list_id))
    updated_list = client.list_management_term_lists.update(
        list_id,
        "application/json",
        {
            "name": "New name",
            "description": "New description"
        }
    )
    assert isinstance(updated_list, TermList)
    pprint(updated_list.as_dict())

    #
    # Add terms
    #
    print("\nAdding terms to list {}".format(list_id))
    client.list_management_term.add_term(list_id, "term1", "eng")
    client.list_management_term.add_term(list_id, "term2", "eng")

    #
    # Get all terms ids
    #
    print("\nGetting all term IDs for list {}".format(list_id))
    terms = client.list_management_term.get_all_terms(list_id, "eng")
    assert isinstance(terms, Terms)
    terms_data = terms.data
    assert isinstance(terms_data, TermsData)
    pprint(terms_data.as_dict())
    
    #
    # 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, "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)

    #
    # Screen text
    #
    text = 'This text contains the terms "term1" and "term2".'
    print('\nScreening text "{}" using term list {}'.format(text, list_id))
    screen = client.text_moderation.screen_text(
        "eng",
        "text/plain",
        text,
        autocorrect=False,
        pii=False,
        list_id=list_id
    )
    assert isinstance(screen, Screen)
    pprint(screen.as_dict())


    #
    # 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,
        term_to_remove,
        "eng"
    )

    #
    # 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, "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
    #
    text = 'This text contains the terms "term1" and "term2".'
    print('\nScreening text "{}" using term list {}'.format(text, list_id))
    screen = client.text_moderation.screen_text(
        "eng",
        "text/plain",
        text,
        autocorrect=False,
        pii=False,
        list_id=list_id
    )
    assert isinstance(screen, Screen)
    pprint(screen.as_dict())

    #
    # Delete all terms
    #
    print("\nDelete all terms in the image list {}".format(list_id))
    client.list_management_term.delete_all_terms(list_id, "eng")

    #
    # Delete list
    #
    print("\nDelete the term list {}".format(list_id))
    client.list_management_term_lists.delete(list_id)
def video_transcript_review(subscription_key):
    """VideoTranscriptReview.

    This will create and publish a transcript review for video
    """

    # 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 Content Moderator web site. Your team name is the Id
    # associated with your subscription.
    team_name = "insert your team name here"

    # Create a review with the content pointing to a streaming endpoint (manifest)
    streamingcontent = "https://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest"

    transcript = b"""WEBVTT

        01:01.000 --> 02:02.000
        First line with a crap word in a transcript.

        02:03.000 --> 02:25.000
        This is another line in the transcript.
    """

    client = ContentModeratorClient(
        endpoint=os.environ[
            'CONTENT_MODERATOR_ENDPOINT'],  # Add your Content Moderator endpoint to your environment variables.
        credentials=CognitiveServicesCredentials(subscription_key))

    #
    # Create a video review
    #
    print("Create review for {}.\n".format(streamingcontent))
    review_item = {
        "content": streamingcontent,  # How to download the image
        "content_id": uuid.uuid4(),  # Random id
        # Note: to create a published review, set the Status to "Pending".
        # However, you cannot add video frames or a transcript to a published review.
        "status": "Unpublished"
    }

    reviews = client.reviews.create_video_reviews(
        content_type="application/json",
        team_name=team_name,
        # As many review item as you need
        create_video_reviews_body=[review_item])
    review_id = reviews[0]  # Ordered list of string of review ID

    #
    # Add transcript
    #
    print("\nAdding transcript to the review {}".format(review_id))
    client.reviews.add_video_transcript(
        team_name=team_name,
        review_id=review_id,
        # Can be a file descriptor, as long as its stream type
        vt_tfile=BytesIO(transcript),
    )

    #
    # Add transcript moderation result
    #
    print("\nAdding a transcript moderation result to the review with ID {}".
          format(review_id))
    with open(
            os.path.join(TEXT_FOLDER,
                         'content_moderator_video_transcript.txt'),
            "rb") as text_fd:
        screen = client.text_moderation.screen_text(
            text_content_type="text/plain",
            text_content=text_fd,
            language="eng")
        assert isinstance(screen, Screen)
        pprint(screen.as_dict())

        # Build a terms list with index
        terms = []
        for term in screen.terms:
            terms.append({"index": term.index, "term": term.term})

        client.reviews.add_video_transcript_moderation_result(
            content_type="application/json",
            team_name=team_name,
            review_id=review_id,
            transcript_moderation_body=[{
                "timestamp": 0,
                "terms": terms
            }])

    #
    # Public review
    #
    client.reviews.publish_video_review(team_name=team_name,
                                        review_id=review_id)

    print(
        "\nOpen your Content Moderator Dashboard and select Review > Video to see the review."
    )
                                                                    Evaluate)
from msrest.authentication import CognitiveServicesCredentials
from io import BytesIO
from zoidbergbot.config import SUBSCRIPTION_KEY, CONTENT_MODERATOR_ENDPOINT

# 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
client = ContentModeratorClient(
    endpoint=CONTENT_MODERATOR_ENDPOINT,
    # Add your Content Moderator endpoint to your environment variables.
    credentials=CognitiveServicesCredentials(SUBSCRIPTION_KEY))


def text_moderation(text):
    """TextModeration.
    This will moderate a given long text.
    """
    text = bytes(text, "utf-8")
    text_bytes = BytesIO(text)

    screen = client.text_moderation.screen_text(text_content_type="text/plain",
                                                text_content=text_bytes,
                                                language="eng",
                                                autocorrect=True,
                                                pii=True)
'''

subscription_key = "PASTE_YOUR_CONTENT_MODERATOR_SUBSCRIPTION_KEY_HERE"
endpoint = "PASTE_YOUR_CONTENT_MODERATOR_ENDPOINT_HERE"

# List of URL images used to moderate.
IMAGE_LIST = [
    "https://moderatorsampleimages.blob.core.windows.net/samples/sample2.jpg",
    "https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png"
]
'''
AUTHENTICATE
Create a Content Moderator client.
'''
client = ContentModeratorClient(
    endpoint=endpoint,
    credentials=CognitiveServicesCredentials(subscription_key))
'''
CONTENT MODERATOR
This quickstart moderates an image, then text and faces within the image.
'''
print('IMAGE MODERATION')
print()

# Image moderation, using image at [0]
print("Evaluate the image '{}' for adult and racy content:".format(
    os.path.basename(IMAGE_LIST[0])))
mod_image = client.image_moderation.evaluate_url_input(
    content_type="application/json",
    cache_image=True,
    data_representation="URL",
Create a single client for all samples, except for human reviews. It needs its own client.
'''
# <authentication>
# Set your environment variables with the values of your key and region, "westus" is the default region.
SUBSCRIPTION_KEY = os.environ.get('CONTENT_MODERATOR_SUBSCRIPTION_KEY')
ENDPOINT = os.environ.get('CONTENT_MODERATOR_ENDPOINT',
                          'https://westus.api.cognitive.microsoft.com')
# </authentication>

# Get region for the human reviews callback endpoint
REGION = os.environ.get('CONTENT_MODERATOR_REGION', 'westus')

# <client>
# Create client
client = ContentModeratorClient(
    endpoint=ENDPOINT,
    credentials=CognitiveServicesCredentials(SUBSCRIPTION_KEY))
# </client>

# Need a special client for the human reviews (due to unique key)
# Need to get key from Content Moderator website,
# go to gear symbol (Settings) --> Credentials --> Ocp-Apim-Subscription-Key
# Set your key and region in your environment variables.
REVIEWS_SUBSCRIPTION_KEY = os.environ.get('CONTENT_MODERATOR_REVIEWS_KEY')
# Where the image for review gets sent to.
call_back_endpoint = 'https://{}.api.cognitive.microsoft.com/contentmoderator/review/v1.0'.format(
    REGION)
client_reviews = ContentModeratorClient(
    endpoint=ENDPOINT,
    credentials=CognitiveServicesCredentials(SUBSCRIPTION_KEY))
# The name of the team to assign to the human review job. Add to your environment variables.
def image_lists(subscription_key):
    """ImageList.

    This will review an image using workflow and job.
    """

    client = ContentModeratorClient(
        endpoint=os.environ[
            'CONTENT_MODERATOR_ENDPOINT'],  # Add your Content Moderator endpoint to your environment variables.
        credentials=CognitiveServicesCredentials(subscription_key))

    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

    #
    # 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

    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

    #
    # 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())

    #
    # 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())

    #
    # 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())

    #
    # 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)

    #
    # 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())

    #
    # 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])

    #
    # 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())

    #
    # 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)

    #
    # Delete list
    #
    print("\nDelete the image list {}".format(list_id))
    client.list_management_image_lists.delete(list_id=list_id)

    #
    # 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)
示例#19
0
def terms_lists(subscription_key):
    """TermsList.

    This will screen text using a term list.
    """

    client = ContentModeratorClient(
        endpoint=os.environ['CONTENT_MODERATOR_ENDPOINT'], # Add your Content Moderator endpoint to your environment variables.
        credentials=CognitiveServicesCredentials(subscription_key)
    )

    #
    # 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

    #
    # 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())

    #
    # 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"
    )

    #
    # 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())

    #
    # 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)

    #
    # Screen text
    #
    text = 'This text contains the terms "term1" and "term2".'
    print('\nScreening text "{}" using term list {}'.format(text, list_id))
    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())

    #
    # 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"
    )

    #
    # 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())

    #
    # 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")

    #
    # Delete list
    #
    print("\nDelete the term list {}".format(list_id))
    client.list_management_term_lists.delete(list_id=list_id)