lg3394's picture
Update app.py
96c489b verified
raw
history blame
1.2 kB
import gradio as gr
import openai
openai.api_key = "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"
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(
model="text-moderation-latest", # Updated to the correct model name if required
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 Exception as e:
return f"An error occurred while processing the input: {str(e)}"
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
iface.launch()