-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoderation.py
More file actions
45 lines (37 loc) · 1.48 KB
/
moderation.py
File metadata and controls
45 lines (37 loc) · 1.48 KB
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
import logging
import requests
from django.conf import settings
logger = logging.getLogger(__name__)
OPENAI_MODERATION_API_URL = "https://api.openai.com/v1/moderations"
def moderate_content(text, image_urls=None):
"""
Call OpenAI's Moderation API with text and optional image URLs.
Returns True if ALL content is safe, False if any is flagged.
"""
api_key = settings.OPENAI_API_KEY
if not api_key:
logger.warning("OPENAI_API_KEY not set, skipping moderation")
return True
inputs = [{"type": "text", "text": text}]
for url in image_urls or []:
inputs.append({"type": "image_url", "image_url": {"url": url}})
try:
response = requests.post(
OPENAI_MODERATION_API_URL,
headers={"Authorization": f"Bearer {api_key}"},
json={"model": "omni-moderation-latest", "input": inputs},
timeout=30,
)
response.raise_for_status()
result = response.json()
flagged = any(r["flagged"] for r in result["results"])
logger.info("Moderation API response: flagged=%s", flagged)
for r in result["results"]:
scores = r.get("category_scores", {})
high_scores = {k: v for k, v in scores.items() if v > 0.01}
if high_scores:
logger.info(" high scores: %s", high_scores)
return not flagged
except Exception:
logger.exception("Moderation API call failed, defaulting to approved")
return True