Azure Content Safety

Nov 15 2024 · Python 3.12, Microsoft Azure, JupyterLabs

Lesson 04: Advanced Content Moderation Strategies

Implementing Humans-in-the-loops Strategies

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this segment, you’ll make your moderation solution more robust by adding human-in-the-loop functionality.

Add Human-in-the-loop

To implement human-in-the-loop functionality, you will first update analyze_text and analyze_image function code to address the cases where the current way of classifying content as safe or unsafe for a particular category may not be correct at times.

violations = {}
if hate_result and hate_result.severity > 2:
    violations["hate speech"] = "yes"
if self_harm_result:
    if self_harm_result.severity > 4:
        violations["self-harm"] = "yes"
    elif self_harm_result.severity > 3:
        violations["self-harm"] = "likely"
if sexual_result:
    if sexual_result.severity > 1:
        violations["sexual"] = "yes"
    elif sexual_result.severity > 2:
        violations["sexual"] = "likely"
if violence_result and violence_result.severity > 2:
    violations["violent references"] = "yes"

Update check_content_safety Function

Next, head back to starter/business_logic.py and update code below the if statement that checks for any form of violation is found in the moderation results of text and image analysis:

# 1
text_violation_flags = text_analysis_result.values()
image_violation_flags = image_analysis_result.values()

# 2
if "likely" in text_violation_flags or "likely" in image_violation_flags:
    return {'status': "re-evaluation needed"}

# 3
status_detail = f'Your post contains references that violate our
  community guidelines.'

if text_analysis_result:
    status_detail = status_detail + '\n' + f'Violation found in text: {','
      .join(text_analysis_result)}'
if image_analysis_result:
    status_detail = status_detail + '\n' + f'Violation found in image: {','
      .join(image_analysis_result)}'

status_detail = status_detail + '\n' + 'Please modify your post to adhere to
  community guidelines.'

# 4
return {'status': "violations found", 'details': status_detail}

Re-run the App to Test Moderation System

Open the terminal in the VSCode and re-run the web app using the following command:

streamlit run app.py

Appeal the Platform for Re-evaluation

This time you get the following message - “Post Upload Failed”. At the bottom of the message, you can also find an option to submit an appeal request. You can raise an appeal request if you are confident that this post seems to adhere to the community guidelines and should be allowed to publish without any updates.

See forum comments
Cinema mode Download course materials from Github
Previous: Realtime Limitations of Azure Content Safety Next: Conclusion