image_captioner / moderator.py
Sverd's picture
upload from local pc
1352a28 verified
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)