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} 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()