lg3394 commited on
Commit
7050beb
·
verified ·
1 Parent(s): 4e06352

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
- import openai
3
  import os
 
4
  from anthropic import Anthropic
5
  from azure.ai.contentsafety import ContentSafetyClient
6
  from azure.ai.contentsafety.models import TextCategory
@@ -10,7 +10,7 @@ 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("OPENAI_API_KEY")
14
  anthropic_api_key = os.getenv("anthropickey")
15
 
16
  # Initialize Anthropic client
@@ -58,15 +58,20 @@ def analyze_text_azure(user_text):
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:
@@ -129,4 +134,4 @@ iface = gr.Interface(
129
  )
130
 
131
  if __name__ == "__main__":
132
- iface.launch()
 
1
  import gradio as gr
 
2
  import os
3
+ from openai import OpenAI
4
  from anthropic import Anthropic
5
  from azure.ai.contentsafety import ContentSafetyClient
6
  from azure.ai.contentsafety.models import TextCategory
 
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_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
14
  anthropic_api_key = os.getenv("anthropickey")
15
 
16
  # Initialize Anthropic client
 
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 - UPDATED to use new client
62
  openai_moderation_result = "Error in OpenAI Moderation"
63
  try:
64
+ # This is the updated line:
65
+ response = openai_client.moderations.create(input=user_text)
66
+
67
+ # These lines are updated to access properties on the object:
68
+ moderation_categories = response.results[0].categories
69
+ moderation_flagged = response.results[0].flagged
70
 
71
  if moderation_flagged:
72
+ # Convert the categories object to a dictionary
73
+ categories_dict = {k: v for k, v in vars(moderation_categories).items() if not k.startswith('_')}
74
+ openai_moderation_result = f"OpenAI flags the text for the following categories: {', '.join([category for category, flagged in categories_dict.items() if flagged])}"
75
  else:
76
  openai_moderation_result = "The text is not flagged for any moderation issues by OpenAI."
77
  except Exception as e:
 
134
  )
135
 
136
  if __name__ == "__main__":
137
+ iface.launch()