lg3394 commited on
Commit
5ec2d79
·
verified ·
1 Parent(s): df7192b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -56
app.py CHANGED
@@ -7,10 +7,9 @@ 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
- 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
 
@@ -41,32 +40,40 @@ def analyze_text_azure(user_text):
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,50 +92,20 @@ def moderate_text(user_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(
@@ -138,13 +115,11 @@ iface = gr.Interface(
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()
 
7
  from azure.core.credentials import AzureKeyCredential
8
  from azure.core.exceptions import HttpResponseError
9
  from azure.ai.contentsafety.models import AnalyzeTextOptions
10
+ from transformers import pipeline # Importing Hugging Face pipeline for Toxic BERT
 
11
 
12
+ # Load OpenAI and Anthropic API Keys from environment variables
13
  openai.api_key = os.getenv("openaiapikey")
14
  anthropic_api_key = os.getenv("anthropickey")
15
 
 
40
  return f"Error occurred with Azure Content Safety: {e}"
41
 
42
  # Extract moderation results
43
+ results = []
44
+ hate_result = next((item for item in response.categories_analysis if item.category == TextCategory.HATE), None)
45
+ self_harm_result = next((item for item in response.categories_analysis if item.category == TextCategory.SELF_HARM), None)
46
+ sexual_result = next((item for item in response.categories_analysis if item.category == TextCategory.SEXUAL), None)
47
+ violence_result = next((item for item in response.categories_analysis if item.category == TextCategory.VIOLENCE), None)
48
+
49
+ if hate_result:
50
+ results.append(f"Hate severity: {hate_result.severity}")
51
+ if self_harm_result:
52
+ results.append(f"SelfHarm severity: {self_harm_result.severity}")
53
+ if sexual_result:
54
+ results.append(f"Sexual severity: {sexual_result.severity}")
55
+ if violence_result:
56
+ results.append(f"Violence severity: {violence_result.severity}")
57
+
58
+ return "\n".join(results) if results else "No flagged content detected in Azure Content Safety."
59
 
60
  def moderate_text(user_text):
61
  # OpenAI Moderation
62
+ openai_moderation_result = "Error in OpenAI Moderation"
63
  try:
64
  response = openai.Moderation.create(input=user_text)
65
  moderation_categories = response["results"][0]["categories"]
66
  moderation_flagged = response["results"][0]["flagged"]
67
 
68
  if moderation_flagged:
69
+ openai_moderation_result = f"OpenAI flags the text for the following categories: {', '.join([category for category, flagged in moderation_categories.items() if flagged])}"
 
70
  else:
71
+ openai_moderation_result = "The text is not flagged for any moderation issues by OpenAI."
72
  except Exception as e:
73
+ openai_moderation_result = f"Error occurred with OpenAI: {e}"
74
 
75
  # Anthropic Moderation
76
+ anthropic_moderation_result = "Error in Anthropic Moderation"
77
  try:
78
  prompt_template = """
79
  You are a content moderation expert tasked with categorizing user-generated text.
 
92
  messages=[{"role": "user", "content": prompt}]
93
  ).content[0].text
94
 
95
+ anthropic_moderation_result = f"Anthropic's moderation result: {response}"
96
+
 
 
 
 
97
  except Exception as e:
98
+ anthropic_moderation_result = f"Error occurred with Anthropic: {e}"
99
 
100
  # Azure Content Safety Moderation
101
+ azure_moderation_result = analyze_text_azure(user_text)
102
 
103
  # Toxic BERT Moderation (Hugging Face Model)
104
  toxic_result = toxic_bert(user_text)
105
  toxic_classification = "Blocked" if toxic_result[0]['label'] == 'LABEL_1' else "Allowed" # Toxic BERT classifies as "LABEL_1" for toxic
106
  toxic_severity = toxic_result[0]['score']
107
 
108
+ return openai_moderation_result, anthropic_moderation_result, azure_moderation_result, f"Toxic BERT classification: {toxic_classification}, Severity: {toxic_severity}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  # Create the Gradio interface with updated input and output labels
111
  iface = gr.Interface(
 
115
  gr.Textbox(label="OpenAI"),
116
  gr.Textbox(label="Anthropic"),
117
  gr.Textbox(label="Microsoft Azure"),
118
+ gr.Textbox(label="Toxic BERT")
 
 
119
  ],
120
  title="Content Moderation Tool",
121
+ description="Enter some text and get the moderation results from OpenAI, Anthropic, Azure Content Safety, and Toxic BERT."
122
  )
123
 
124
  if __name__ == "__main__":
125
+ iface.launch()