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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -20
app.py CHANGED
@@ -1,33 +1,30 @@
1
  import gradio as gr
2
- from openai import OpenAI
3
 
4
- # Initialize OpenAI moderation client with your API key
5
- client = OpenAI(api_key="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")
6
 
7
  def moderate_text(text):
8
- # Ensure input is processed correctly
9
  text = str(text).strip() if text else ""
10
 
11
  if not text:
12
  return "Input text is empty. Please enter valid text."
13
 
14
- # Call OpenAI Moderation API
15
- response = client.moderations.create(
16
- model="omni-moderation-latest",
17
- input=text
18
- )
19
 
20
- # Extract moderation results
21
- moderation_categories = response["results"][0]["categories"]
22
- moderation_flagged = response["results"][0]["flagged"]
23
 
24
- # Handle flagged and non-flagged cases
25
- if moderation_flagged:
26
- flagged_categories = [category for category, flagged in moderation_categories.items() if flagged]
27
- return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
28
- else:
29
- return "The text is not flagged for any moderation issues."
 
30
 
31
- # Create Gradio interface
32
  iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
33
- iface.launch()
 
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 ""
8
 
9
  if not 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
 
21
+ if moderation_flagged:
22
+ flagged_categories = [category for category, flagged in moderation_categories.items() if flagged]
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()