Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,24 +5,29 @@ from openai import OpenAI
|
|
5 |
client = OpenAI(api_key="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")
|
6 |
|
7 |
def moderate_text(text):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
if not text:
|
13 |
return "Input text is empty. Please enter valid text."
|
14 |
-
|
|
|
15 |
response = client.moderations.create(
|
16 |
model="omni-moderation-latest",
|
17 |
input=text
|
18 |
)
|
|
|
|
|
19 |
moderation_categories = response["results"][0]["categories"]
|
20 |
moderation_flagged = response["results"][0]["flagged"]
|
|
|
|
|
21 |
if moderation_flagged:
|
22 |
flagged_categories = [category for category, flagged in moderation_categories.items() if flagged]
|
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 |
|
|
|
27 |
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
|
28 |
-
iface.launch()
|
|
|
5 |
client = OpenAI(api_key="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")
|
6 |
|
7 |
def moderate_text(text):
|
8 |
+
# Ensure input is processed correctly
|
9 |
+
text = str(text).strip() if text else ""
|
10 |
+
|
|
|
11 |
if not text:
|
12 |
return "Input text is empty. Please enter valid text."
|
13 |
+
|
14 |
+
# Call OpenAI Moderation API
|
15 |
response = client.moderations.create(
|
16 |
model="omni-moderation-latest",
|
17 |
input=text
|
18 |
)
|
19 |
+
|
20 |
+
# Extract moderation results
|
21 |
moderation_categories = response["results"][0]["categories"]
|
22 |
moderation_flagged = response["results"][0]["flagged"]
|
23 |
+
|
24 |
+
# Handle flagged and non-flagged cases
|
25 |
if moderation_flagged:
|
26 |
flagged_categories = [category for category, flagged in moderation_categories.items() if flagged]
|
27 |
return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
|
28 |
else:
|
29 |
return "The text is not flagged for any moderation issues."
|
30 |
|
31 |
+
# Create Gradio interface
|
32 |
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
|
33 |
+
iface.launch()
|