File size: 4,500 Bytes
d2a5d92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c56431c
d2a5d92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25a4ab
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""AI Image Detector (Open Baseline) β€” Scam.AI

Wraps the open-source Organika/sdxl-detector model in a Gradio interface.
The output explicitly positions this as a community baseline, with a CTA
toward Scam.AI's production-grade detection systems.
"""

import gradio as gr
from transformers import pipeline

# Open-source baseline (192K downloads, Swin-based, ~110MB)
BASELINE_MODEL = "Organika/sdxl-detector"

print(f"Loading baseline model: {BASELINE_MODEL}")
clf = pipeline("image-classification", model=BASELINE_MODEL)
print("Model loaded.")


def detect(img):
    if img is None:
        return None, "Upload an image to analyze."

    results = clf(img)
    # results: list of dicts {label, score}
    scores = {r["label"]: float(r["score"]) for r in results}

    # Find the AI-generated probability
    ai_keys = [k for k in scores if "artificial" in k.lower() or "ai" in k.lower() or "fake" in k.lower() or "generated" in k.lower()]
    if ai_keys:
        ai_prob = scores[ai_keys[0]]
    else:
        # fallback β€” highest score label is "ai" if model uses different naming
        ai_prob = max(scores.values()) if list(scores.keys())[0].lower() not in ("human", "real") else 1 - max(scores.values())

    if ai_prob > 0.85:
        verdict = "πŸ€– **Likely AI-generated**"
        explanation = (
            f"This open-source baseline is **{ai_prob*100:.0f}% confident** "
            f"the image is AI-generated."
        )
    elif ai_prob > 0.5:
        verdict = "⚠️ **Possibly AI-generated**"
        explanation = (
            f"The baseline leans toward AI ({ai_prob*100:.0f}%) but with low "
            f"confidence. In our experience this is exactly the regime where "
            f"open models fail β€” they catch the obvious cases but miss the "
            f"sophisticated ones."
        )
    else:
        verdict = "πŸ“· **Likely real photograph**"
        explanation = (
            f"Baseline confidence in 'real': {(1-ai_prob)*100:.0f}%. "
            f"Note: open baselines have well-known false-negative blind spots "
            f"on newer generators (GPT-Image-2, FLUX, etc)."
        )

    cta = (
        "\n\n---\n\n"
        "### πŸ“Œ About this baseline\n\n"
        f"Model: [`{BASELINE_MODEL}`](https://huggingface.co/{BASELINE_MODEL}) "
        "β€” a community Swin-Transformer trained for SDXL detection. "
        "Useful as a sanity check but does **not** generalize well to:\n"
        "- Newer generators (GPT-Image-2, FLUX.1, Imagen 3)\n"
        "- Heavily post-processed images (super-resolution, JPEG re-compression)\n"
        "- Domain shift (medical, document, surveillance)\n\n"
        "Our team published [a comprehensive benchmark](https://huggingface.co/datasets/Scam-AI/gpt-image-2) "
        "showing 30+ AUC-point gaps between in-distribution academic tests "
        "and real-world performance.\n\n"
        "**For production deployment** β€” APIs, on-premise inference, custom "
        "domain fine-tuning β€” talk to us at **[scam.ai](https://www.scam.ai)**."
    )

    return scores, verdict + "\n\n" + explanation + cta


with gr.Blocks(title="AI Image Detector β€” Scam.AI") as demo:
    gr.Markdown(
        "# πŸ›‘οΈ AI Image Detector\n"
        "*Drop an image to check whether an open-source baseline thinks it's "
        "AI-generated.*\n\n"
        "*Built by [Scam.AI](https://www.scam.ai) Β· Powered by "
        f"[`{BASELINE_MODEL}`](https://huggingface.co/{BASELINE_MODEL})*"
    )

    with gr.Row():
        with gr.Column():
            inp = gr.Image(type="pil", label="Upload an image")
            btn = gr.Button("Analyze", variant="primary", size="lg")
            gr.Examples(
                examples=[],
                inputs=inp,
            )
        with gr.Column():
            scores = gr.Label(label="Class scores")
            verdict = gr.Markdown()

    btn.click(detect, inputs=inp, outputs=[scores, verdict])
    inp.change(detect, inputs=inp, outputs=[scores, verdict])

    gr.Markdown(
        "---\n"
        "**About:** This Space uses a community-trained open baseline to "
        "demonstrate the AI-image-detection task. Real production systems "
        "(higher accuracy, calibrated probabilities, robust to adversarial "
        "post-processing) are not open. Visit [scam.ai](https://www.scam.ai) "
        "if you need detection for actual deployment."
    )


if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft(primary_hue="blue"), ssr_mode=False)