lg3394 commited on
Commit
2d22e60
·
verified ·
1 Parent(s): 305db5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -16
app.py CHANGED
@@ -1,38 +1,72 @@
1
  import gradio as gr
2
  import openai
3
  import os
 
4
 
 
5
  openai.api_key = os.getenv("openaiapikey")
 
6
 
7
- def moderate_text(text):
8
- text = str(text).strip() if text else ""
9
 
10
- if not text:
11
- return "Input text is empty. Please enter valid text."
12
 
 
 
 
13
  try:
14
- response = openai.Moderation.create(
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: {e}"
 
 
 
28
 
 
29
  iface = gr.Interface(
30
  fn=moderate_text,
31
  inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
32
- outputs="text",
33
- title="Text Moderation Tool",
34
- description="Paste some text, and this tool will use OpenAI's Moderation API to determine if it contains sensitive or flagged content."
35
  )
36
 
37
  if __name__ == "__main__":
38
- iface.launch()
 
1
  import gradio as gr
2
  import openai
3
  import os
4
+ from anthropic import Anthropic
5
 
6
+ # Load OpenAI and Anthropic API Keys from environment variables
7
  openai.api_key = os.getenv("openaiapikey")
8
+ anthropic_api_key = os.getenv("anthropickey")
9
 
10
+ # Initialize Anthropic client
11
+ client = Anthropic(api_key=anthropic_api_key)
12
 
13
+ MODEL_NAME = "claude-3-haiku-20240307"
 
14
 
15
+ def moderate_text(user_text):
16
+ # OpenAI Moderation
17
+ openai_moderation_result = "Error in OpenAI Moderation"
18
  try:
19
+ response = openai.Moderation.create(input=user_text)
 
 
 
20
  moderation_categories = response["results"][0]["categories"]
21
  moderation_flagged = response["results"][0]["flagged"]
22
 
23
  if moderation_flagged:
24
+ openai_moderation_result = f"OpenAI flags the text for the following categories: {', '.join([category for category, flagged in moderation_categories.items() if flagged])}"
 
25
  else:
26
+ openai_moderation_result = "The text is not flagged for any moderation issues by OpenAI."
27
+ except Exception as e:
28
+ openai_moderation_result = f"Error occurred with OpenAI: {e}"
29
+
30
+ # Anthropic Moderation (Using Prompt Template)
31
+ anthropic_moderation_result = "Error in Anthropic Moderation"
32
+ try:
33
+ # Create the prompt template for Anthropic's moderation model
34
+ prompt_template = """
35
+ You are a content moderation expert tasked with categorizing user-generated text based on the following guidelines:
36
+
37
+ Here is the user-generated text to categorize:
38
+ <user_text>{user_text}</user_text>
39
+
40
+ Based on the content, classify this text as either ALLOW or BLOCK. Return nothing else.
41
+ """
42
+
43
+ # Format the prompt with the user text
44
+ prompt = prompt_template.format(user_text=user_text)
45
+
46
+ # Send the prompt to Claude and get the response
47
+ response = client.messages.create(
48
+ model=MODEL_NAME,
49
+ max_tokens=10,
50
+ messages=[{"role": "user", "content": prompt}]
51
+ ).content[0].text
52
+
53
+ # Format the Anthropic moderation result
54
+ anthropic_moderation_result = f"Anthropic's moderation result: {response}"
55
+
56
  except Exception as e:
57
+ anthropic_moderation_result = f"Error occurred with Anthropic: {e}"
58
+
59
+ return openai_moderation_result, anthropic_moderation_result
60
+
61
 
62
+ # Create the Gradio interface
63
  iface = gr.Interface(
64
  fn=moderate_text,
65
  inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
66
+ outputs=["text", "text"],
67
+ title="Content Moderation Tool",
68
+ description="Enter some text and get the moderation results from OpenAI and Anthropic."
69
  )
70
 
71
  if __name__ == "__main__":
72
+ iface.launch()