File size: 4,119 Bytes
cd24b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SynthScope quickstart\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SMSD75/SynthScope/blob/main/notebooks/synthscope_quickstart.ipynb)\n",
    "\n",
    "Run state-of-the-art **AI-generated image & video detectors** in a few lines with the [`synthscope`](https://github.com/SMSD75/SynthScope) library.\n",
    "\n",
    "> Tip: switch the runtime to a **GPU** (Runtime \u2192 Change runtime type) \u2014 the backbones are large."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Install\n",
    "\n",
    "Installs `synthscope` (which pulls PyTorch, transformers, etc.) and OpenAI CLIP (needed by a couple of detectors). Takes a few minutes on first run."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "%pip install -q \"git+https://github.com/SMSD75/SynthScope.git\" \"git+https://github.com/openai/CLIP.git\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Get detector weights\n",
    "\n",
    "This notebook uses **CuRe**, whose authors publish their weights on GitHub (a 25 MB adapter). The first prediction also downloads the frozen PE-Core backbone (2.7 GB) from Hugging Face. Other detectors need their weights as `<key>.pth` in the same folder; each model's README links the official download."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import os, urllib.request\n",
    "\n",
    "CKPT = \"/content/synthscope_ckpts\" if os.path.isdir(\"/content\") else os.path.expanduser(\"~/synthscope_ckpts\")\n",
    "os.makedirs(CKPT, exist_ok=True)\n",
    "os.environ[\"SYNTHSCOPE_CKPT_DIR\"] = CKPT          # must be set before importing synthscope\n",
    "\n",
    "if not os.path.exists(f\"{CKPT}/cure.pth\"):\n",
    "    urllib.request.urlretrieve(\n",
    "        \"https://raw.githubusercontent.com/manic-cui/CuRe/main/weights/cure_adapter.pt\", f\"{CKPT}/cure.pth\")\n",
    "\n",
    "import synthscope\n",
    "[m[\"key\"] for m in synthscope.list_models() if m[\"available\"]]   # detectors that can run here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Upload an image (or video)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "try:\n",
    "    from google.colab import files\n",
    "    uploaded = files.upload()\n",
    "    path = next(iter(uploaded))\n",
    "except Exception:\n",
    "    path = \"your_image.png\"  # running outside Colab: set a local path\n",
    "print(\"file:\", path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Detect"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "det = synthscope.load(\"cure\")\n",
    "result = det.predict(path)\n",
    "print(f\"Verdict: {result['verdict'].upper()}  |  P(AI-generated) = {result['fake_probability']:.3f}\")\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Next steps\n",
    "\n",
    "- Add more detectors: put their weights in the `CKPT` folder as `<key>.pth` (see each model's README), then `synthscope.load(key)`.\n",
    "- Training-free video detectors need no weights: `synthscope.load('d3').predict('clip.mp4')`.\n",
    "- Use your own trained checkpoint: `synthscope.load('cnn', checkpoint='/path/to.pth')`.\n",
    "- See the [repo](https://github.com/SMSD75/SynthScope) for the web demo, the benchmarks (datasets download themselves), and how to add a detector."
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}