Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
def moderate_text(text):
|
| 7 |
text = str(text).strip() if text else ""
|
|
@@ -10,11 +12,12 @@ def moderate_text(text):
|
|
| 10 |
return "Input text is empty. Please enter valid text."
|
| 11 |
|
| 12 |
try:
|
|
|
|
| 13 |
response = openai.Moderation.create(
|
| 14 |
-
model="text-moderation-latest", # Updated to the correct model name if required
|
| 15 |
input=text
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
moderation_categories = response["results"][0]["categories"]
|
| 19 |
moderation_flagged = response["results"][0]["flagged"]
|
| 20 |
|
|
@@ -23,8 +26,5 @@ def moderate_text(text):
|
|
| 23 |
return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
|
| 24 |
else:
|
| 25 |
return "The text is not flagged for any moderation issues."
|
| 26 |
-
except
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
|
| 30 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Set your API key securely
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Store your key in an environment variable
|
| 7 |
|
| 8 |
def moderate_text(text):
|
| 9 |
text = str(text).strip() if text else ""
|
|
|
|
| 12 |
return "Input text is empty. Please enter valid text."
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# Check if Moderation is supported
|
| 16 |
response = openai.Moderation.create(
|
|
|
|
| 17 |
input=text
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Extract moderation results
|
| 21 |
moderation_categories = response["results"][0]["categories"]
|
| 22 |
moderation_flagged = response["results"][0]["flagged"]
|
| 23 |
|
|
|
|
| 26 |
return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
|
| 27 |
else:
|
| 28 |
return "The text is not flagged for any moderation issues."
|
| 29 |
+
except AttributeError as ae:
|
| 30 |
+
|
|
|
|
|
|
|
|
|