Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import openai | |
| import os | |
| # Set your API key securely | |
| openai.api_key = os.getenv("OPENAI_API_KEY") # Store your key in an environment variable | |
| def moderate_text(text): | |
| text = str(text).strip() if text else "" | |
| if not text: | |
| return "Input text is empty. Please enter valid text." | |
| try: | |
| # Check if Moderation is supported | |
| response = openai.Moderation.create( | |
| input=text | |
| ) | |
| # Extract moderation results | |
| 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: | |