lg3394 commited on
Commit
4b1da5c
·
verified ·
1 Parent(s): 00b49d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -12,7 +12,8 @@ from transformers import pipeline # Importing Hugging Face pipeline for Toxic B
12
  # Try to get the API key from either environment variable
13
  api_key = os.getenv("OPENAI_API_KEY") or os.getenv("openaiapikey")
14
  if not api_key:
15
- raise ValueError("No OpenAI API key found in environment variables!")
 
16
 
17
  # Initialize OpenAI client
18
  openai_client = OpenAI(api_key=api_key)
@@ -63,24 +64,36 @@ def analyze_text_azure(user_text):
63
  return "\n".join(results) if results else "No flagged content detected in Azure Content Safety."
64
 
65
  def moderate_text(user_text):
66
- # OpenAI Moderation
67
- openai_moderation_result = "Error in OpenAI Moderation"
68
  try:
69
- # Updated to use the client we defined at the top
70
  response = openai_client.moderations.create(input=user_text)
71
 
72
- # Updated to access properties on the object
73
  moderation_categories = response.results[0].categories
74
  moderation_flagged = response.results[0].flagged
75
 
76
  if moderation_flagged:
77
- # Convert the categories object to a dictionary
78
  categories_dict = {k: v for k, v in vars(moderation_categories).items() if not k.startswith('_')}
79
  openai_moderation_result = f"OpenAI flags the text for the following categories: {', '.join([category for category, flagged in categories_dict.items() if flagged])}"
80
  else:
81
  openai_moderation_result = "The text is not flagged for any moderation issues by OpenAI."
82
  except Exception as e:
83
- openai_moderation_result = f"Error occurred with OpenAI: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  # Anthropic Moderation
86
  anthropic_moderation_result = "Error in Anthropic Moderation"
@@ -135,7 +148,7 @@ iface = gr.Interface(
135
  gr.Textbox(label="Toxic BERT")
136
  ],
137
  title="Content Moderation Model Comparison Tool",
138
- description="Enter some text and get the moderation results from OpenAI, Anthropic/Claude, Microsoft Azure Content Safety, and Toxic BERT."
139
  )
140
 
141
  if __name__ == "__main__":
 
12
  # Try to get the API key from either environment variable
13
  api_key = os.getenv("OPENAI_API_KEY") or os.getenv("openaiapikey")
14
  if not api_key:
15
+ print("WARNING: No OpenAI API key found in environment variables!")
16
+ api_key = "placeholder_key_for_initialization" # This will cause a controlled error
17
 
18
  # Initialize OpenAI client
19
  openai_client = OpenAI(api_key=api_key)
 
64
  return "\n".join(results) if results else "No flagged content detected in Azure Content Safety."
65
 
66
  def moderate_text(user_text):
67
+ # OpenAI Moderation - WITH GRACEFUL ERROR HANDLING
 
68
  try:
69
+ # Attempt to use the real API
70
  response = openai_client.moderations.create(input=user_text)
71
 
 
72
  moderation_categories = response.results[0].categories
73
  moderation_flagged = response.results[0].flagged
74
 
75
  if moderation_flagged:
 
76
  categories_dict = {k: v for k, v in vars(moderation_categories).items() if not k.startswith('_')}
77
  openai_moderation_result = f"OpenAI flags the text for the following categories: {', '.join([category for category, flagged in categories_dict.items() if flagged])}"
78
  else:
79
  openai_moderation_result = "The text is not flagged for any moderation issues by OpenAI."
80
  except Exception as e:
81
+ # Create a professional-looking fallback response that shows API connectivity issues
82
+ openai_moderation_result = """
83
+ ⚠️ OpenAI API Connection Error ⚠️
84
+
85
+ The OpenAI Moderation API is currently unavailable. This may be due to:
86
+ - Network connectivity issues in the hosting environment
87
+ - API rate limits
88
+ - API key configuration
89
+
90
+ Example output (if connected):
91
+ - For harmful content: "OpenAI flags the text for the following categories: violence, hate, self-harm"
92
+ - For safe content: "The text is not flagged for any moderation issues by OpenAI."
93
+
94
+ [This is a demonstration of a multi-model content moderation system that compares results across different providers]
95
+ """
96
+ print(f"Debug - OpenAI Error: {str(e)}")
97
 
98
  # Anthropic Moderation
99
  anthropic_moderation_result = "Error in Anthropic Moderation"
 
148
  gr.Textbox(label="Toxic BERT")
149
  ],
150
  title="Content Moderation Model Comparison Tool",
151
+ description="Enter some text and get the moderation results from OpenAI, Anthropic/Claude, Microsoft Azure Content Safety, and Toxic BERT. Note: The OpenAI API connection may be unavailable in this demo."
152
  )
153
 
154
  if __name__ == "__main__":