File size: 2,538 Bytes
e08bcbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from openai import OpenAI
from dotenv import load_dotenv
import os
import logging
import json

load_dotenv()

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))


def remove_slash_categories(data):
    return {k: v for k, v in data.items() if "/" not in k}


def check_moderation(text):
    try:
        moderation = client.moderations.create(input=text)
        result = moderation.results[0]

        logger.info(
            f"Raw API response: {json.dumps(moderation.model_dump(), indent=2)}"
        )

        status = "Flagged" if result.flagged else "Not Flagged"

        filtered_categories = remove_slash_categories(result.categories.model_dump())
        filtered_scores = remove_slash_categories(result.category_scores.model_dump())

        flagged_categories = [
            f"- {cat}: {filtered_scores[cat]:.6f}"
            for cat, flag in filtered_categories.items()
            if flag
        ]

        flagged_categories.sort(key=lambda x: float(x.split(": ")[1]), reverse=True)

        flagged_categories_str = (
            "\n".join(flagged_categories)
            if flagged_categories
            else "No categories flagged"
        )

        logger.info(f"Moderation check completed for text: {text[:50]}...")
        return status, flagged_categories_str
    except Exception as e:
        logger.error(f"An error occurred: {str(e)}")
        raise gr.Error(f"An error occurred: {str(e)}")


with gr.Blocks() as demo:
    gr.Markdown("# OpenAI Moderation API Checker")

    with gr.Row():
        with gr.Column(scale=1):
            input_text = gr.Textbox(label="Enter text to check", lines=3)
            check_button = gr.Button("Check Moderation")

        with gr.Column(scale=1):
            status_output = gr.Textbox(label="Status")
            categories_output = gr.Textbox(
                label="Flagged Categories (Sorted by Score)", lines=5
            )

    check_button.click(
        check_moderation,
        inputs=[input_text],
        outputs=[status_output, categories_output],
    )

    gr.Examples(
        examples=[
            ["I love spending time with my family and friends."],
            ["The weather is beautiful today."],
            ["I want to kill them."],
            ["I'm going to bomb the airport tomorrow."],
        ],
        inputs=[input_text],
    )

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