File size: 4,631 Bytes
7ebc89a
96c489b
52dd252
2d22e60
739e1bf
 
 
 
 
b7bab80
2d22e60
52dd252
2d22e60
7ebc89a
2d22e60
 
c0bc9ab
2d22e60
c0bc9ab
739e1bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be245e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
739e1bf
2d22e60
 
be245e8
96c489b
2d22e60
96c489b
 
c0bc9ab
96c489b
be245e8
96c489b
be245e8
2d22e60
be245e8
2d22e60
739e1bf
be245e8
2d22e60
 
739e1bf
2d22e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be245e8
 
72e15e5
be245e8
2d22e60
739e1bf
be245e8
 
 
 
e1732a2
739e1bf
e1732a2
 
739e1bf
be245e8
2d22e60
be245e8
e1732a2
 
72e15e5
be245e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
import openai
import os
from anthropic import Anthropic
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import TextCategory
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.ai.contentsafety.models import AnalyzeTextOptions

# Load OpenAI and Anthropic API Keys from environment variables
openai.api_key = os.getenv("openaiapikey")
anthropic_api_key = os.getenv("anthropickey")

# Initialize Anthropic client
client = Anthropic(api_key=anthropic_api_key)

MODEL_NAME = "claude-3-haiku-20240307"

# Function for Azure Content Safety analysis
def analyze_text_azure(user_text):
    # Retrieve Azure keys from Hugging Face secrets (as environment variables)
    key = os.getenv("azurekey")
    endpoint = os.getenv("azureendpoint")

    # Create Content Safety client
    client = ContentSafetyClient(endpoint, AzureKeyCredential(key))

    # Construct request
    request = AnalyzeTextOptions(text=user_text)

    # Analyze text
    try:
        response = client.analyze_text(request)
    except HttpResponseError as e:
        return f"Error occurred with Azure Content Safety: {e}"

    # Extract moderation results
    hate_result = next((item for item in response.categories_analysis if item.category == TextCategory.HATE), None)
    self_harm_result = next((item for item in response.categories_analysis if item.category == TextCategory.SELF_HARM), None)
    sexual_result = next((item for item in response.categories_analysis if item.category == TextCategory.SEXUAL), None)
    violence_result = next((item for item in response.categories_analysis if item.category == TextCategory.VIOLENCE), None)

    results = []
    if hate_result:
        results.append(f"Hate severity: {hate_result.severity}")
    if self_harm_result:
        results.append(f"SelfHarm severity: {self_harm_result.severity}")
    if sexual_result:
        results.append(f"Sexual severity: {sexual_result.severity}")
    if violence_result:
        results.append(f"Violence severity: {violence_result.severity}")

    return "\n".join(results) if results else "No flagged content detected in Azure Content Safety."

def moderate_text(user_text):
    # OpenAI Moderation
    openai_moderation_result = "Error in OpenAI Moderation"
    try:
        response = openai.Moderation.create(input=user_text)
        moderation_categories = response["results"][0]["categories"]
        moderation_flagged = response["results"][0]["flagged"]

        if moderation_flagged:
            openai_moderation_result = f"OpenAI flags the text for the following categories: {', '.join([category for category, flagged in moderation_categories.items() if flagged])}"
        else:
            openai_moderation_result = "The text is not flagged for any moderation issues by OpenAI."
    except Exception as e:
        openai_moderation_result = f"Error occurred with OpenAI: {e}"

    # Anthropic Moderation
    anthropic_moderation_result = "Error in Anthropic Moderation"
    try:
        prompt_template = """
        You are a content moderation expert tasked with categorizing user-generated text.
        Here is the user-generated text to categorize:
        <user_text>{user_text}</user_text>
        Based on the content, classify this text as either ALLOW or BLOCK. Return nothing else.
        """

        # Format the prompt with the user text
        prompt = prompt_template.format(user_text=user_text)

        # Send the prompt to Claude and get the response
        response = client.messages.create(
            model=MODEL_NAME,
            max_tokens=10,
            messages=[{"role": "user", "content": prompt}]
        ).content[0].text

        anthropic_moderation_result = f"Anthropic's moderation result: {response}"

    except Exception as e:
        anthropic_moderation_result = f"Error occurred with Anthropic: {e}"

    # Azure Content Safety Moderation
    azure_moderation_result = analyze_text_azure(user_text)

    return openai_moderation_result, anthropic_moderation_result, azure_moderation_result


# Create the Gradio interface with updated input and output labels
iface = gr.Interface(
    fn=moderate_text,
    inputs=gr.Textbox(lines=2, placeholder="Please write your text here..."),
    outputs=[gr.Textbox(label="OpenAI"), gr.Textbox(label="Anthropic"), gr.Textbox(label="Microsoft Azure")],
    title="Content Moderation Tool",
    description="Enter some text and get the moderation results from OpenAI, Anthropic, and Azure Content Safety."
)

if __name__ == "__main__":
    iface.launch()