SynthScope / demo /app.py
SMSD75's picture
Update SynthScope Space from the GitHub repository
cd24b4a verified
Raw History Blame Contribute Delete
3.2 kB
"""SynthScope Gradio demo — runs any detector on an uploaded image or video.
Deployable as a Hugging Face Space (see demo/README.md) or locally:
pip install -e ".[demo]"
python demo/app.py
On a Space (or any machine without local weights), detectors auto-download from the model zoo;
set the ``SYNTHSCOPE_HUB_REPO`` env var to your checkpoints repo.
"""
from __future__ import annotations
import synthscope
# Detectors that need gated backbones we can't host on a public demo (DINOv3 weights).
_GATED = {"dinov3", "stall"}
def _model_choices():
"""Dropdown choices as (label, key), available/runnable detectors first."""
models = [m for m in synthscope.list_models() if m["key"] not in _GATED]
models.sort(key=lambda m: (not m["available"], m["modality"], m["key"]))
return [(f"{m['name']} · {m['paper']} ({m['modality']})", m["key"]) for m in models]
def run_detection(file_path, model_key):
"""Score ``file_path`` with detector ``model_key``; return (label_dict, details_dict)."""
if not file_path:
return {}, {"error": "Upload an image or video first."}
if not model_key:
return {}, {"error": "Pick a detector."}
try:
det = synthscope.load(model_key)
res = det.predict(file_path)
except FileNotFoundError:
return {}, {"error": f"No weights available for '{model_key}' "
"(not in the model zoo, or gated). Try another detector."}
except Exception as exc: # surface decode/runtime errors to the UI
return {}, {"error": f"{type(exc).__name__}: {exc}"}
label = {
"AI-generated (fake)": res["fake_probability"],
"Real": res["real_probability"],
}
return label, res
def build_demo():
import gradio as gr
choices = _model_choices()
default = choices[0][1] if choices else None
with gr.Blocks(title="SynthScope — AI-generated media detection") as demo:
gr.Markdown(
"# SynthScope — AI-generated image & video detection\n"
"Upload an **image or video** and pick a detector to estimate the probability that it "
"is **AI-generated**. Each detector is a re-implementation benchmarked in "
"[SynthScope](https://github.com/SMSD75/SynthScope)."
)
with gr.Row():
with gr.Column():
file_in = gr.File(label="Image or video", type="filepath",
file_types=["image", "video"])
model_in = gr.Dropdown(choices=choices, value=default, label="Detector")
btn = gr.Button("Detect", variant="primary")
with gr.Column():
label_out = gr.Label(label="Prediction", num_top_classes=2)
json_out = gr.JSON(label="Details")
btn.click(run_detection, inputs=[file_in, model_in], outputs=[label_out, json_out])
gr.Markdown(
"_Per-generator generalization is the hard part of this task; a single score is a demo, "
"not a verdict. See the repo's reproduction table for benchmarked accuracy._"
)
return demo
if __name__ == "__main__":
build_demo().launch(server_name="0.0.0.0")