Spaces:
Sleeping
Sleeping
File size: 2,656 Bytes
7ebc89a 96c489b 52dd252 2d22e60 b7bab80 2d22e60 52dd252 2d22e60 7ebc89a 2d22e60 c0bc9ab 2d22e60 c0bc9ab 2d22e60 96c489b 2d22e60 96c489b c0bc9ab 96c489b 2d22e60 96c489b 2d22e60 72e15e5 2d22e60 e1732a2 2d22e60 e1732a2 2d22e60 e1732a2 72e15e5 2d22e60 |
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 |
import gradio as gr
import openai
import os
from anthropic import Anthropic
# 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"
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 (Using Prompt Template)
anthropic_moderation_result = "Error in Anthropic Moderation"
try:
# Create the prompt template for Anthropic's moderation model
prompt_template = """
You are a content moderation expert tasked with categorizing user-generated text based on the following guidelines:
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
# Format the Anthropic moderation result
anthropic_moderation_result = f"Anthropic's moderation result: {response}"
except Exception as e:
anthropic_moderation_result = f"Error occurred with Anthropic: {e}"
return openai_moderation_result, anthropic_moderation_result
# Create the Gradio interface
iface = gr.Interface(
fn=moderate_text,
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
outputs=["text", "text"],
title="Content Moderation Tool",
description="Enter some text and get the moderation results from OpenAI and Anthropic."
)
if __name__ == "__main__":
iface.launch() |