Spaces:
Sleeping
Sleeping
import gradio as gr | |
from openai import OpenAI | |
# Initialize OpenAI moderation client | |
client = OpenAI() | |
def moderate_text(text): | |
response = client.moderations.create( | |
model="omni-moderation-latest", | |
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." | |
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text") | |
iface.launch() |