File size: 1,247 Bytes
7ebc89a
cb839c5
b7bab80
716822e
 
7ebc89a
cb839c5
c0bc9ab
 
 
0174bb9
 
c0bc9ab
 
cb839c5
 
 
 
c0bc9ab
 
cb839c5
 
c0bc9ab
 
cb839c5
 
 
 
 
7ebc89a
c0bc9ab
cb839c5
c0bc9ab
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
import gradio as gr
from openai import OpenAI

# Initialize OpenAI moderation client with your API key
client = OpenAI(api_key="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")

def moderate_text(text):
    # Ensure input is processed correctly
    text = str(text).strip() if text else ""

    if not text:
        return "Input text is empty. Please enter valid text."

    # Call OpenAI Moderation API
    response = client.moderations.create(
        model="omni-moderation-latest",
        input=text
    )

    # Extract moderation results
    moderation_categories = response["results"][0]["categories"]
    moderation_flagged = response["results"][0]["flagged"]

    # Handle flagged and non-flagged cases
    if moderation_flagged:
        flagged_categories = [category for category, flagged in moderation_categories.items() if flagged]
        return f"The text is flagged for moderation due to: {', '.join(flagged_categories)}"
    else:
        return "The text is not flagged for any moderation issues."

# Create Gradio interface
iface = gr.Interface(fn=moderate_text, inputs="text", outputs="text")
iface.launch()