Spaces:
Sleeping
Sleeping
File size: 832 Bytes
7ebc89a 96c489b ee52c90 b7bab80 6d5858a 7ebc89a cb839c5 c0bc9ab 0174bb9 c0bc9ab 96c489b c0bc9ab 96c489b c0bc9ab 96c489b 6d5858a ee52c90 |
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 |
import gradio as gr
import openai
import os
openai.api_key = os.getenv("OPENAI_API_KEY")
def moderate_text(text):
text = str(text).strip() if text else ""
if not text:
return "Input text is empty. Please enter valid text."
try:
response = openai.Moderation.create(
input=text
)
moderation_categories = response["results"][0]["categories"]
moderation_flagged = response["results"][0]["flagged"]
if moderation_flagged:
flagged_categories = [category for category, flagged in moderation_categories.items() if flagged]
return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
else:
return "The text is not flagged for any moderation issues."
except AttributeError as ae:
|