lg3394 commited on
Commit
00b49d5
·
verified ·
1 Parent(s): 87a0b7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -9,8 +9,13 @@ 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_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
 
 
 
 
 
14
  anthropic_api_key = os.getenv("anthropickey")
15
 
16
  # Initialize Anthropic client
@@ -58,13 +63,13 @@ 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 - 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
 
 
9
  from azure.ai.contentsafety.models import AnalyzeTextOptions
10
  from transformers import pipeline # Importing Hugging Face pipeline for Toxic BERT
11
 
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)
19
  anthropic_api_key = os.getenv("anthropickey")
20
 
21
  # Initialize Anthropic client
 
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