Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,10 @@ from azure.ai.contentsafety.models import TextCategory
|
|
7 |
from azure.core.credentials import AzureKeyCredential
|
8 |
from azure.core.exceptions import HttpResponseError
|
9 |
from azure.ai.contentsafety.models import AnalyzeTextOptions
|
|
|
|
|
10 |
|
11 |
-
# Load OpenAI
|
12 |
openai.api_key = os.getenv("openaiapikey")
|
13 |
anthropic_api_key = os.getenv("anthropickey")
|
14 |
|
@@ -17,6 +19,9 @@ client = Anthropic(api_key=anthropic_api_key)
|
|
17 |
|
18 |
MODEL_NAME = "claude-3-haiku-20240307"
|
19 |
|
|
|
|
|
|
|
20 |
# Function for Azure Content Safety analysis
|
21 |
def analyze_text_azure(user_text):
|
22 |
# Retrieve Azure keys from Hugging Face secrets (as environment variables)
|
@@ -36,40 +41,32 @@ def analyze_text_azure(user_text):
|
|
36 |
return f"Error occurred with Azure Content Safety: {e}"
|
37 |
|
38 |
# Extract moderation results
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
if
|
46 |
-
results.append(f"Hate severity: {hate_result.severity}")
|
47 |
-
if self_harm_result:
|
48 |
-
results.append(f"SelfHarm severity: {self_harm_result.severity}")
|
49 |
-
if sexual_result:
|
50 |
-
results.append(f"Sexual severity: {sexual_result.severity}")
|
51 |
-
if violence_result:
|
52 |
-
results.append(f"Violence severity: {violence_result.severity}")
|
53 |
-
|
54 |
-
return "\n".join(results) if results else "No flagged content detected in Azure Content Safety."
|
55 |
|
56 |
def moderate_text(user_text):
|
57 |
# OpenAI Moderation
|
58 |
-
|
59 |
try:
|
60 |
response = openai.Moderation.create(input=user_text)
|
61 |
moderation_categories = response["results"][0]["categories"]
|
62 |
moderation_flagged = response["results"][0]["flagged"]
|
63 |
|
64 |
if moderation_flagged:
|
65 |
-
|
|
|
66 |
else:
|
67 |
-
|
68 |
except Exception as e:
|
69 |
-
|
70 |
|
71 |
# Anthropic Moderation
|
72 |
-
|
73 |
try:
|
74 |
prompt_template = """
|
75 |
You are a content moderation expert tasked with categorizing user-generated text.
|
@@ -88,25 +85,66 @@ def moderate_text(user_text):
|
|
88 |
messages=[{"role": "user", "content": prompt}]
|
89 |
).content[0].text
|
90 |
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
except Exception as e:
|
94 |
-
|
95 |
|
96 |
# Azure Content Safety Moderation
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
# Create the Gradio interface with updated input and output labels
|
103 |
iface = gr.Interface(
|
104 |
fn=moderate_text,
|
105 |
inputs=gr.Textbox(lines=2, placeholder="Please write your text here..."),
|
106 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
title="Content Moderation Tool",
|
108 |
-
description="Enter some text and get the moderation results from OpenAI, Anthropic,
|
109 |
)
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
-
iface.launch()
|
|
|
7 |
from azure.core.credentials import AzureKeyCredential
|
8 |
from azure.core.exceptions import HttpResponseError
|
9 |
from azure.ai.contentsafety.models import AnalyzeTextOptions
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
from transformers import pipeline
|
12 |
|
13 |
+
# Load OpenAI, Anthropic API Keys from environment variables
|
14 |
openai.api_key = os.getenv("openaiapikey")
|
15 |
anthropic_api_key = os.getenv("anthropickey")
|
16 |
|
|
|
19 |
|
20 |
MODEL_NAME = "claude-3-haiku-20240307"
|
21 |
|
22 |
+
# Load Toxic BERT model from Hugging Face
|
23 |
+
toxic_bert = pipeline("text-classification", model="unitary/toxic-bert")
|
24 |
+
|
25 |
# Function for Azure Content Safety analysis
|
26 |
def analyze_text_azure(user_text):
|
27 |
# Retrieve Azure keys from Hugging Face secrets (as environment variables)
|
|
|
41 |
return f"Error occurred with Azure Content Safety: {e}"
|
42 |
|
43 |
# Extract moderation results
|
44 |
+
results = {}
|
45 |
+
for category in [TextCategory.HATE, TextCategory.SELF_HARM, TextCategory.SEXUAL, TextCategory.VIOLENCE]:
|
46 |
+
result = next((item for item in response.categories_analysis if item.category == category), None)
|
47 |
+
if result:
|
48 |
+
results[category] = result.severity
|
49 |
+
|
50 |
+
return results if results else {"No flagged content detected": 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def moderate_text(user_text):
|
53 |
# OpenAI Moderation
|
54 |
+
openai_result = {"Classification": "Error in OpenAI Moderation", "Severity": 0}
|
55 |
try:
|
56 |
response = openai.Moderation.create(input=user_text)
|
57 |
moderation_categories = response["results"][0]["categories"]
|
58 |
moderation_flagged = response["results"][0]["flagged"]
|
59 |
|
60 |
if moderation_flagged:
|
61 |
+
openai_result["Classification"] = "Blocked"
|
62 |
+
openai_result["Severity"] = max([category for category, flagged in moderation_categories.items() if flagged], key=moderation_categories.get)
|
63 |
else:
|
64 |
+
openai_result["Classification"] = "Allowed"
|
65 |
except Exception as e:
|
66 |
+
openai_result = {"Classification": f"Error occurred with OpenAI: {e}", "Severity": 0}
|
67 |
|
68 |
# Anthropic Moderation
|
69 |
+
anthropic_result = {"Classification": "Error in Anthropic Moderation", "Severity": 0}
|
70 |
try:
|
71 |
prompt_template = """
|
72 |
You are a content moderation expert tasked with categorizing user-generated text.
|
|
|
85 |
messages=[{"role": "user", "content": prompt}]
|
86 |
).content[0].text
|
87 |
|
88 |
+
if "blocked" in response.lower():
|
89 |
+
anthropic_result["Classification"] = "Blocked"
|
90 |
+
anthropic_result["Severity"] = 1 # Assigning severity for blocked content
|
91 |
+
else:
|
92 |
+
anthropic_result["Classification"] = "Allowed"
|
93 |
+
anthropic_result["Severity"] = 0
|
94 |
except Exception as e:
|
95 |
+
anthropic_result = {"Classification": f"Error occurred with Anthropic: {e}", "Severity": 0}
|
96 |
|
97 |
# Azure Content Safety Moderation
|
98 |
+
azure_result = analyze_text_azure(user_text)
|
99 |
+
|
100 |
+
# Toxic BERT Moderation (Hugging Face Model)
|
101 |
+
toxic_result = toxic_bert(user_text)
|
102 |
+
toxic_classification = "Blocked" if toxic_result[0]['label'] == 'LABEL_1' else "Allowed" # Toxic BERT classifies as "LABEL_1" for toxic
|
103 |
+
toxic_severity = toxic_result[0]['score']
|
104 |
+
|
105 |
+
# Combine results and generate bar chart
|
106 |
+
categories = ["OpenAI", "Anthropic", "Microsoft Azure", "Toxic BERT"]
|
107 |
+
classifications = [openai_result["Severity"], anthropic_result["Severity"], sum(azure_result.values()) / len(azure_result) if azure_result else 0, toxic_severity]
|
108 |
+
|
109 |
+
bar_chart = create_comparison_chart(categories, classifications)
|
110 |
+
|
111 |
+
# Safe text suggestion for blocked content
|
112 |
+
suggestions = ""
|
113 |
+
if openai_result["Classification"] == "Blocked":
|
114 |
+
suggestions += "OpenAI flagged the text for harmful content. Suggested Rephrase: 'Please use more respectful language.'\n"
|
115 |
+
if anthropic_result["Classification"] == "Blocked":
|
116 |
+
suggestions += "Anthropic flagged the text. Suggested Rephrase: 'Avoid harmful or offensive language.'\n"
|
117 |
+
if any(value > 0.5 for value in azure_result.values()):
|
118 |
+
suggestions += "Azure flagged some content. Suggested Rephrase: 'Try to avoid sensitive topics and ensure respectful language.'\n"
|
119 |
+
if toxic_classification == "Blocked":
|
120 |
+
suggestions += "Toxic BERT flagged the text. Suggested Rephrase: 'Please ensure your language is respectful and non-toxic.'"
|
121 |
+
|
122 |
+
return openai_result, anthropic_result, azure_result, toxic_result, bar_chart, suggestions
|
123 |
+
|
124 |
+
def create_comparison_chart(categories, values):
|
125 |
+
fig, ax = plt.subplots()
|
126 |
+
ax.bar(categories, values, color=['red', 'orange', 'green', 'blue'])
|
127 |
+
ax.set_title("Content Moderation Comparison")
|
128 |
+
ax.set_ylabel("Severity Score")
|
129 |
+
ax.set_ylim(0, 1)
|
130 |
+
ax.set_xlabel("Moderation Tool")
|
131 |
+
return fig
|
132 |
|
133 |
# Create the Gradio interface with updated input and output labels
|
134 |
iface = gr.Interface(
|
135 |
fn=moderate_text,
|
136 |
inputs=gr.Textbox(lines=2, placeholder="Please write your text here..."),
|
137 |
+
outputs=[
|
138 |
+
gr.Textbox(label="OpenAI"),
|
139 |
+
gr.Textbox(label="Anthropic"),
|
140 |
+
gr.Textbox(label="Microsoft Azure"),
|
141 |
+
gr.Textbox(label="Toxic BERT"),
|
142 |
+
gr.Plot(label="Comparison Bar Chart"),
|
143 |
+
gr.Textbox(label="Safe Text Suggestions")
|
144 |
+
],
|
145 |
title="Content Moderation Tool",
|
146 |
+
description="Enter some text and get the moderation results from OpenAI, Anthropic, Azure Content Safety, Toxic BERT, and suggestions for safe rephrasing."
|
147 |
)
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
+
iface.launch()
|