File size: 1,931 Bytes
1fd46a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()