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( 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: {e}" iface = gr.Interface( fn=moderate_text, inputs=gr.Textbox(lines=2, placeholder="Enter text here..."), outputs="text", title="Text Moderation Tool", description="Paste some text, and this tool will use OpenAI's Moderation API to determine if it contains sensitive or flagged content." ) if __name__ == "__main__": iface.launch()