Upload folder using huggingface_hub
Browse files- README.md +2 -8
- imgclass2.py +46 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.31.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: imgclass
|
| 3 |
+
app_file: imgclass2.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.31.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
imgclass2.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and processor
|
| 7 |
+
model_name = "prithivMLmods/vit-mini-explicit-content" # Updated model path
|
| 8 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
| 9 |
+
processor = ViTImageProcessor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Updated label mapping
|
| 12 |
+
labels = {
|
| 13 |
+
"0": "Anime Picture",
|
| 14 |
+
"1": "Enticing & Sensual",
|
| 15 |
+
"2": "Hentai",
|
| 16 |
+
"3": "Pornography",
|
| 17 |
+
"4": "Safe for Work"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def explicit_content_detection(image):
|
| 21 |
+
"""Predicts the type of content in the image."""
|
| 22 |
+
image = Image.fromarray(image).convert("RGB")
|
| 23 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 24 |
+
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model(**inputs)
|
| 27 |
+
logits = outputs.logits
|
| 28 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
| 29 |
+
|
| 30 |
+
predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
|
| 31 |
+
|
| 32 |
+
return predictions
|
| 33 |
+
|
| 34 |
+
# Create Gradio interface
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=explicit_content_detection,
|
| 37 |
+
inputs=gr.Image(type="numpy"),
|
| 38 |
+
outputs=gr.Label(label="Prediction Scores"),
|
| 39 |
+
title="vit-mini-explicit-content",
|
| 40 |
+
description="Upload an image to classify whether it is anime, enticing & sensual, hentai, pornographic, or safe for work."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Launch the app
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
iface.launch(share=True)
|
| 46 |
+
|