CoolFace
Apppublic

Sverd/image_captioner

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
moderator_mc.py40 linesDownload Raw Back to root
1import os2# import json3import requests4from dotenv import load_dotenv5 6load_dotenv()7 8 9def moderate_image(image_url):10    """11        Process an image by moderating it and extracting a caption if not moderated.12 13        Args:14        - image_url (str): URL of the image to be processed.15 16        Returns:17        - str: If the image is moderated, returns "moderated".18               If not moderated, returns the extracted caption.19        """20    mc_key = os.getenv('MODERATE_CONTENT_KEY')21    payload = {22        'key': mc_key,23        'url': image_url24    }25    endpoint = 'https://api.moderatecontent.com/moderate/'26    response = requests.post(endpoint, data=payload)27    if response.status_code == 200:28        response_json = response.json()29        return response_json['rating_index']30    else:31        print(response.status_code)32        return None33 34# Example usage35 36 37# url = "https://www.rainforest-alliance.org/wp-content/uploads/2021/06/capybara-square-1-400x400.jpg.webp"38# result = moderate_image(url)39# print(result)40