Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
|
4 |
-
|
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 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
moderation_flagged = response["results"][0]["flagged"]
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
# Create Gradio interface
|
32 |
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
|
33 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
|
4 |
+
openai.api_key = "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"
|
|
|
5 |
|
6 |
def moderate_text(text):
|
|
|
7 |
text = str(text).strip() if text else ""
|
8 |
|
9 |
if not 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 |
|
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 |
+
except Exception as e:
|
27 |
+
return f"An error occurred while processing the input: {str(e)}"
|
28 |
|
|
|
29 |
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
|
30 |
+
iface.launch()
|