lg3394 commited on
Commit
ee52c90
·
verified ·
1 Parent(s): 96c489b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import gradio as gr
2
  import openai
 
3
 
4
- openai.api_key = "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"
 
5
 
6
  def moderate_text(text):
7
  text = str(text).strip() if text else ""
@@ -10,11 +12,12 @@ def moderate_text(text):
10
  return "Input text is empty. Please enter valid text."
11
 
12
  try:
 
13
  response = openai.Moderation.create(
14
- model="text-moderation-latest", # Updated to the correct model name if required
15
  input=text
16
  )
17
 
 
18
  moderation_categories = response["results"][0]["categories"]
19
  moderation_flagged = response["results"][0]["flagged"]
20
 
@@ -23,8 +26,5 @@ def moderate_text(text):
23
  return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
24
  else:
25
  return "The text is not flagged for any moderation issues."
26
- except Exception as e:
27
- return f"An error occurred while processing the input: {str(e)}"
28
-
29
- iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
30
- iface.launch()
 
1
  import gradio as gr
2
  import openai
3
+ import os
4
 
5
+ # Set your API key securely
6
+ openai.api_key = os.getenv("OPENAI_API_KEY") # Store your key in an environment variable
7
 
8
  def moderate_text(text):
9
  text = str(text).strip() if text else ""
 
12
  return "Input text is empty. Please enter valid text."
13
 
14
  try:
15
+ # Check if Moderation is supported
16
  response = openai.Moderation.create(
 
17
  input=text
18
  )
19
 
20
+ # Extract moderation results
21
  moderation_categories = response["results"][0]["categories"]
22
  moderation_flagged = response["results"][0]["flagged"]
23
 
 
26
  return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
27
  else:
28
  return "The text is not flagged for any moderation issues."
29
+ except AttributeError as ae:
30
+