NSFW_Detector / app.py
kaisex's picture
Create app.py
1fd46a3 verified
raw
history blame
1.93 kB
import gradio as gr
from PIL import Image
from model_human import predict_human_nsfw
from model_anime import predict_anime_nsfw
def nsfw_detector(image: Image.Image, model_type: str):
if image is None:
return "<div class='result-box'>Pls upload an img</div>"
if model_type == "Human":
label, confidence = predict_human_nsfw(image)
else:
label, confidence = predict_anime_nsfw(image)
result = (
f"<div class='result-box'>"
f"<strong>Model:</strong> {model_type}<br>"
f"<strong>Prediction:</strong> {label}<br>"
f"<strong>Confidence:</strong> {confidence:.2%}"
f"</div>"
)
return result
custom_css = """
.result-box {
background-color: oklch(0.718 0.202 349.761);
padding: 20px;
border-radius: 12px;
box-shadow: 0 0 15px oklch(0.718 0.202 349.761);
color: white;
font-size: 1.2rem;
text-align: center;
font-weight: bold;
width: 100%;
}
.gradio-container { max-width: 900px; margin: auto; }
"""
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("## NSFW Detector (Human + Anime/Cartoon)")
gr.Markdown(
"Upload an image and select the appropriate model for detection. "
"No data is stored. This is a side project β€” results may not be fully accurate."
)
with gr.Row():
with gr.Column(scale=1):
model_choice = gr.Radio(
["Human", "Anime"],
label="Select Model Type",
value="Human"
)
image_input = gr.Image(type="pil", label="Upload Image")
with gr.Column(scale=1):
output_box = gr.HTML("<div class='result-box'>Awaiting input...</div>")
# Trigger detection when image is uploaded
image_input.change(
fn=nsfw_detector,
inputs=[image_input, model_choice],
outputs=output_box
)
if __name__ == "__main__":
demo.launch()