Spaces:
Running
Running
Download app.py from Dominik72/Slopscan-Anime: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/Dominik72/Slopscan-Anime/resolve/main/app.py
- Command line
-
hf download hf://spaces/Dominik72/Slopscan-Anime/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Dominik72/Slopscan-Anime/resolve/main/app.py
3.11 kB
| """Slopscan HF Space: drag & drop or paste an image, get AI vs human.""" | |
| import glob | |
| import os | |
| import gradio as gr | |
| import timm | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from PIL import Image | |
| from torchvision import transforms | |
| MODEL_ID = "Dominik72/Slopscan-Anime" | |
| BACKBONE = "convnextv2_base.fcmae_ft_in22k_in1k_384" | |
| IMG_SIZE = 1024 | |
| MEAN = [0.485, 0.456, 0.406] | |
| STD = [0.229, 0.224, 0.225] | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| EXAMPLES_DIR = os.path.join(HERE, "examples") | |
| class Slopscan: | |
| def __init__(self, model_path): | |
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| ckpt = torch.load(model_path, map_location="cpu", weights_only=False) | |
| state = ckpt.get("state_dict") or ckpt.get("ema_state") or ckpt | |
| self.model = timm.create_model(BACKBONE, pretrained=False, num_classes=2) | |
| self.model.load_state_dict(state) | |
| self.model.to(self.device).eval() | |
| self.transform = transforms.Compose([ | |
| transforms.Resize(IMG_SIZE + 32), | |
| transforms.CenterCrop(IMG_SIZE), | |
| transforms.ToTensor(), | |
| transforms.Normalize(MEAN, STD), | |
| ]) | |
| def classify(self, image): | |
| img = image.convert("RGB") if isinstance(image, Image.Image) else Image.open(image) | |
| x = self.transform(img).unsqueeze(0).to(self.device) | |
| return torch.softmax(self.model(x), dim=1)[0, 1].item() | |
| best_pt = os.path.join(HERE, "best.pt") | |
| model = Slopscan(best_pt if os.path.exists(best_pt) else hf_hub_download(MODEL_ID, "best.pt")) | |
| def run(image, threshold): | |
| if image is None: | |
| return None | |
| prob = model.classify(image) | |
| verdict = "AI generated" if prob >= threshold else "Human drawn" | |
| return {verdict: prob} | |
| EXAMPLES = sorted( | |
| p for ext in ("png", "jpg", "jpeg", "webp") | |
| for p in glob.glob(os.path.join(EXAMPLES_DIR, f"*.{ext}")) | |
| ) | |
| with gr.Blocks(title="Slopscan") as demo: | |
| gr.Markdown( | |
| "# Slopscan\n" | |
| "Detects if an anime image was generated by AI or drawn by a human using [Dominik72/Slopscan-Anime](https://huggingface.co/Dominik72/Slopscan-Anime)." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| image = gr.Image(label="Image", sources=["upload", "clipboard"], type="pil", height=420) | |
| threshold = gr.Slider(0.0, 1.0, value=0.42, step=0.01, | |
| label="Threshold", info="Lower flags more images as AI") | |
| with gr.Row(): | |
| clear = gr.Button("Clear") | |
| submit = gr.Button("Submit", variant="primary") | |
| with gr.Column(): | |
| result = gr.Label() | |
| if EXAMPLES: | |
| gr.Examples( | |
| examples=[[p] for p in EXAMPLES], | |
| example_labels=[os.path.basename(p) for p in EXAMPLES], | |
| inputs=[image], | |
| cache_examples=False, | |
| ) | |
| submit.click(run, inputs=[image, threshold], outputs=result) | |
| clear.click(lambda: (None, None), outputs=[image, result]) | |
| image.change(lambda: None, outputs=result) | |
| if __name__ == "__main__": | |
| demo.launch() | |