Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from model_human import predict_human_nsfw
|
5 |
+
from model_anime import predict_anime_nsfw
|
6 |
+
|
7 |
+
def nsfw_detector(image: Image.Image, model_type: str):
|
8 |
+
if image is None:
|
9 |
+
return "<div class='result-box'>Pls upload an img</div>"
|
10 |
+
|
11 |
+
if model_type == "Human":
|
12 |
+
label, confidence = predict_human_nsfw(image)
|
13 |
+
else:
|
14 |
+
label, confidence = predict_anime_nsfw(image)
|
15 |
+
|
16 |
+
result = (
|
17 |
+
f"<div class='result-box'>"
|
18 |
+
f"<strong>Model:</strong> {model_type}<br>"
|
19 |
+
f"<strong>Prediction:</strong> {label}<br>"
|
20 |
+
f"<strong>Confidence:</strong> {confidence:.2%}"
|
21 |
+
f"</div>"
|
22 |
+
)
|
23 |
+
return result
|
24 |
+
|
25 |
+
custom_css = """
|
26 |
+
.result-box {
|
27 |
+
background-color: oklch(0.718 0.202 349.761);
|
28 |
+
padding: 20px;
|
29 |
+
border-radius: 12px;
|
30 |
+
box-shadow: 0 0 15px oklch(0.718 0.202 349.761);
|
31 |
+
color: white;
|
32 |
+
font-size: 1.2rem;
|
33 |
+
text-align: center;
|
34 |
+
font-weight: bold;
|
35 |
+
width: 100%;
|
36 |
+
}
|
37 |
+
.gradio-container { max-width: 900px; margin: auto; }
|
38 |
+
"""
|
39 |
+
|
40 |
+
with gr.Blocks(css=custom_css) as demo:
|
41 |
+
gr.Markdown("## NSFW Detector (Human + Anime/Cartoon)")
|
42 |
+
gr.Markdown(
|
43 |
+
"Upload an image and select the appropriate model for detection. "
|
44 |
+
"No data is stored. This is a side project — results may not be fully accurate."
|
45 |
+
)
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column(scale=1):
|
49 |
+
model_choice = gr.Radio(
|
50 |
+
["Human", "Anime"],
|
51 |
+
label="Select Model Type",
|
52 |
+
value="Human"
|
53 |
+
)
|
54 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
55 |
+
with gr.Column(scale=1):
|
56 |
+
output_box = gr.HTML("<div class='result-box'>Awaiting input...</div>")
|
57 |
+
|
58 |
+
# Trigger detection when image is uploaded
|
59 |
+
image_input.change(
|
60 |
+
fn=nsfw_detector,
|
61 |
+
inputs=[image_input, model_choice],
|
62 |
+
outputs=output_box
|
63 |
+
)
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
demo.launch()
|