File size: 1,429 Bytes
1352a28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from dotenv import load_dotenv

load_dotenv()


def moderate_image(image_url):
    """
        Uses Microsoft Azure Content Moderator API to evaluate an image's content.

        Args:
        - image_url (str): The URL of the image to be moderated.

        Returns:
        - str: Returns "Moderated" if the image is classified as adult or racy,
               otherwise returns "Passed".
        """

    subscription_key = os.getenv('AZURE_SUBSCRIPTION_KEY')
    endpoint = "https://eastus.api.cognitive.microsoft.com"

    moderator_url = endpoint + "/contentmoderator/moderate/v1.0/ProcessImage/Evaluate"

    # Define the headers for the HTTP request
    headers = {
        "Content-Type": "application/json",
        "Ocp-Apim-Subscription-Key": subscription_key
    }

    data = {
        "DataRepresentation": 'URL',
        'Value': image_url
    }

    # Send the image to the API
    response = requests.post(moderator_url, headers=headers, json=data)

    # Parse the response
    response_json = response.json()

    # Check if the image is classified as adult or racy
    if response_json["IsImageAdultClassified"] or response_json["IsImageRacyClassified"]:
        return True
    else:
        return False

# Example usage
#
#
# url = "https://www.rainforest-alliance.org/wp-content/uploads/2021/06/capybara-square-1-400x400.jpg.webp"
# result = moderate_image(url)
# print(result)