Yuta Hayashi
commited on
Commit
·
e08bcbd
1
Parent(s):
579e659
Add application file
Browse files- app.py +88 -0
- pyproject.toml +11 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
import logging
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
logging.basicConfig(
|
| 11 |
+
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
|
| 12 |
+
)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def remove_slash_categories(data):
|
| 19 |
+
return {k: v for k, v in data.items() if "/" not in k}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def check_moderation(text):
|
| 23 |
+
try:
|
| 24 |
+
moderation = client.moderations.create(input=text)
|
| 25 |
+
result = moderation.results[0]
|
| 26 |
+
|
| 27 |
+
logger.info(
|
| 28 |
+
f"Raw API response: {json.dumps(moderation.model_dump(), indent=2)}"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
status = "Flagged" if result.flagged else "Not Flagged"
|
| 32 |
+
|
| 33 |
+
filtered_categories = remove_slash_categories(result.categories.model_dump())
|
| 34 |
+
filtered_scores = remove_slash_categories(result.category_scores.model_dump())
|
| 35 |
+
|
| 36 |
+
flagged_categories = [
|
| 37 |
+
f"- {cat}: {filtered_scores[cat]:.6f}"
|
| 38 |
+
for cat, flag in filtered_categories.items()
|
| 39 |
+
if flag
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
flagged_categories.sort(key=lambda x: float(x.split(": ")[1]), reverse=True)
|
| 43 |
+
|
| 44 |
+
flagged_categories_str = (
|
| 45 |
+
"\n".join(flagged_categories)
|
| 46 |
+
if flagged_categories
|
| 47 |
+
else "No categories flagged"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
logger.info(f"Moderation check completed for text: {text[:50]}...")
|
| 51 |
+
return status, flagged_categories_str
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logger.error(f"An error occurred: {str(e)}")
|
| 54 |
+
raise gr.Error(f"An error occurred: {str(e)}")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("# OpenAI Moderation API Checker")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column(scale=1):
|
| 62 |
+
input_text = gr.Textbox(label="Enter text to check", lines=3)
|
| 63 |
+
check_button = gr.Button("Check Moderation")
|
| 64 |
+
|
| 65 |
+
with gr.Column(scale=1):
|
| 66 |
+
status_output = gr.Textbox(label="Status")
|
| 67 |
+
categories_output = gr.Textbox(
|
| 68 |
+
label="Flagged Categories (Sorted by Score)", lines=5
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
check_button.click(
|
| 72 |
+
check_moderation,
|
| 73 |
+
inputs=[input_text],
|
| 74 |
+
outputs=[status_output, categories_output],
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
gr.Examples(
|
| 78 |
+
examples=[
|
| 79 |
+
["I love spending time with my family and friends."],
|
| 80 |
+
["The weather is beautiful today."],
|
| 81 |
+
["I want to kill them."],
|
| 82 |
+
["I'm going to bomb the airport tomorrow."],
|
| 83 |
+
],
|
| 84 |
+
inputs=[input_text],
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "playground-openai-moderation-api"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.12"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"gradio>=4.42.0",
|
| 9 |
+
"openai>=1.43.0",
|
| 10 |
+
"python-dotenv>=1.0.1",
|
| 11 |
+
]
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.42.0
|
| 2 |
+
openai>=1.43.0
|
| 3 |
+
python-dotenv>=1.0.1
|