Spaces:
Runtime error
Runtime error
import os | |
# import json | |
import requests | |
from dotenv import load_dotenv | |
load_dotenv() | |
def moderate_image(image_url): | |
""" | |
Process an image by moderating it and extracting a caption if not moderated. | |
Args: | |
- image_url (str): URL of the image to be processed. | |
Returns: | |
- str: If the image is moderated, returns "moderated". | |
If not moderated, returns the extracted caption. | |
""" | |
mc_key = os.getenv('MODERATE_CONTENT_KEY') | |
payload = { | |
'key': mc_key, | |
'url': image_url | |
} | |
endpoint = 'https://api.moderatecontent.com/moderate/' | |
response = requests.post(endpoint, data=payload) | |
if response.status_code == 200: | |
response_json = response.json() | |
return response_json['rating_index'] | |
else: | |
print(response.status_code) | |
return None | |
# 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) | |