telecomadm1145 commited on
Commit
6211642
·
verified ·
1 Parent(s): 9b3640f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -19
app.py CHANGED
@@ -1,24 +1,26 @@
1
  # -*- coding: utf-8 -*-
2
  """
3
- Swin-Large AI detection
4
  -------------------------------------------------------------------
5
- • V2 / V4 : 2-class (AI vs. Non-AI)
6
- • V7 / V8 / V9 : 4-class (photo / anime × AI / Non-AI)
 
7
  -------------------------------------------------------------------
8
  Author: telecomadm1145
9
  """
10
 
11
- import os, torch, timm, math, numpy as np
12
  import torch.nn as nn
13
  import torch.nn.functional as F
14
  from PIL import Image
15
  import gradio as gr
16
  from huggingface_hub import hf_hub_download
 
17
 
18
  # --------------------------------------------------
19
  # 1. Model & Checkpoint Meta-data
20
  # --------------------------------------------------
21
- REPO_ID = "telecomadm1145/swin-ai-detection" # 同一个 repo 存两种 ckpt 也 OK
22
  HF_FILENAMES = {
23
  "V2": "swin_classifier_stage1_v2_epoch_3.pth",
24
  "V4": "swin_classifier_stage1_v4.pth",
@@ -26,25 +28,27 @@ HF_FILENAMES = {
26
  "V7": "swin_classifier_4class_fp16_v7.pth",
27
  "V8": "swin_classifier_4class_fp16_v8_epoch7_acc9740.pth",
28
  "V9": "swin_classifier_4class_fp16_v9_acc9861.pth",
 
29
  }
30
 
31
  CKPT_META = {
32
- "V2": { "n_cls": 2, "head": "v4",
33
  "names": ["Non-AI Generated", "AI Generated"]},
34
- "V4": { "n_cls": 2, "head": "v4",
35
  "names": ["Non-AI Generated", "AI Generated"]},
36
- #"V5(underfitting)": { "n_cls": 2, "head": "v5",
37
  # "names": ["Non-AI Generated", "AI Generated"]},
38
- "V7": { "n_cls": 4, "head": "v7",
39
  "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
40
- "V8": { "n_cls": 4, "head": "v7",
41
  "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
42
- "V9": { "n_cls": 4, "head": "v7",
 
 
43
  "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
44
  }
45
 
46
- DEFAULT_CKPT = "V9"
47
- MODEL_NAME = "swin_large_patch4_window12_384"
48
  LOCAL_CKPT_DIR = "./checkpoints"
49
  SEED = 4421
50
  DROP_RATE = 0.1
@@ -56,6 +60,7 @@ print(f"Using device: {device}")
56
  model, current_ckpt = None, None
57
  current_meta = None
58
 
 
59
  class SwinClassifier(nn.Module):
60
  def __init__(self, model_name, num_classes, pretrained=True,
61
  head_version="v4"):
@@ -66,7 +71,7 @@ class SwinClassifier(nn.Module):
66
  self.data_config = timm.data.resolve_data_config({}, model=self.backbone)
67
 
68
  # ------- 根据版本选择不同 head -------
69
- if head_version == "v7": # <-- V7: 极简 64-hidden, GELU
70
  self.classifier = nn.Sequential(
71
  nn.Dropout(DROP_RATE),
72
  nn.Linear(self.backbone.num_features, 64),
@@ -120,23 +125,28 @@ def load_model(ckpt_name: str):
120
 
121
  print(f"\n🔄 Switching to {ckpt_name} ...")
122
  meta = CKPT_META[ckpt_name]
 
123
  ckpt_file = hf_hub_download(
124
  repo_id=REPO_ID,
125
- filename=HF_FILENAMES[ckpt_name],
126
  local_dir=LOCAL_CKPT_DIR, force_download=False
127
  )
128
  print(f"Checkpoint: {ckpt_file}")
129
 
130
  # Build model structure
131
  model = SwinClassifier(
132
- MODEL_NAME,
133
  num_classes = meta["n_cls"],
134
  pretrained = False,
135
  head_version = meta["head"]
136
  ).to(device)
137
 
138
- # compatible load
139
- state = torch.load(ckpt_file, map_location=device, weights_only=False)
 
 
 
 
140
  model.load_state_dict(state.get("model_state_dict", state), strict=True)
141
  model.eval()
142
 
@@ -180,7 +190,7 @@ def launch():
180
  load_model(DEFAULT_CKPT) # 预加载
181
 
182
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
183
- gr.Markdown("# 🖼️ Swin-Large — AI / Non-AI Detector (V2)")
184
  gr.Markdown(
185
  "Choose a model checkpoint on the left, upload an image, "
186
  "and click **Run** to see predictions. Checkpoint V7+ outputs 4 classes."
 
1
  # -*- coding: utf-8 -*-
2
  """
3
+ Swin/CAFormer AI detection
4
  -------------------------------------------------------------------
5
+ Swin-V2 / V4 : 2-class (AI vs. Non-AI)
6
+ Swin-V7 / V8 / V9 : 4-class (photo / anime × AI / Non-AI)
7
+ • CAFormer-V10 : 4-class (photo / anime × AI / Non-AI)
8
  -------------------------------------------------------------------
9
  Author: telecomadm1145
10
  """
11
 
12
+ import os, torch, timm, numpy as np
13
  import torch.nn as nn
14
  import torch.nn.functional as F
15
  from PIL import Image
16
  import gradio as gr
17
  from huggingface_hub import hf_hub_download
18
+ from safetensors.torch import load_file # Added for .safetensors support
19
 
20
  # --------------------------------------------------
21
  # 1. Model & Checkpoint Meta-data
22
  # --------------------------------------------------
23
+ REPO_ID = "telecomadm1145/swin-ai-detection"
24
  HF_FILENAMES = {
25
  "V2": "swin_classifier_stage1_v2_epoch_3.pth",
26
  "V4": "swin_classifier_stage1_v4.pth",
 
28
  "V7": "swin_classifier_4class_fp16_v7.pth",
29
  "V8": "swin_classifier_4class_fp16_v8_epoch7_acc9740.pth",
30
  "V9": "swin_classifier_4class_fp16_v9_acc9861.pth",
31
+ "V1-CAFormer": "caformer_b36_4class.safetensors",
32
  }
33
 
34
  CKPT_META = {
35
+ "V2": { "n_cls": 2, "head": "v4", "backbone": "swin_large_patch4_window12_384",
36
  "names": ["Non-AI Generated", "AI Generated"]},
37
+ "V4": { "n_cls": 2, "head": "v4", "backbone": "swin_large_patch4_window12_384",
38
  "names": ["Non-AI Generated", "AI Generated"]},
39
+ #"V5(underfitting)": { "n_cls": 2, "head": "v5", "backbone": "swin_large_patch4_window12_384",
40
  # "names": ["Non-AI Generated", "AI Generated"]},
41
+ "V7": { "n_cls": 4, "head": "v7", "backbone": "swin_large_patch4_window12_384",
42
  "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
43
+ "V8": { "n_cls": 4, "head": "v7", "backbone": "swin_large_patch4_window12_384",
44
  "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
45
+ "V9": { "n_cls": 4, "head": "v7", "backbone": "swin_large_patch4_window12_384",
46
+ "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
47
+ "V1-CAFormer": { "n_cls": 4, "head": "v7", "backbone": "caformer_b36.sail_in22k_ft_in1k_384",
48
  "names": ["non_ai", "ai", "ani_non_ai", "ani_ai"]},
49
  }
50
 
51
+ DEFAULT_CKPT = "V1-CAFormer"
 
52
  LOCAL_CKPT_DIR = "./checkpoints"
53
  SEED = 4421
54
  DROP_RATE = 0.1
 
60
  model, current_ckpt = None, None
61
  current_meta = None
62
 
63
+ # Renamed to ImageClassifier for clarity, but keeping original name to avoid breaking changes if subclassed elsewhere.
64
  class SwinClassifier(nn.Module):
65
  def __init__(self, model_name, num_classes, pretrained=True,
66
  head_version="v4"):
 
71
  self.data_config = timm.data.resolve_data_config({}, model=self.backbone)
72
 
73
  # ------- 根据版本选择不同 head -------
74
+ if head_version == "v7": # <-- V7, V8, V9, V10: 极简 64-hidden, GELU
75
  self.classifier = nn.Sequential(
76
  nn.Dropout(DROP_RATE),
77
  nn.Linear(self.backbone.num_features, 64),
 
125
 
126
  print(f"\n🔄 Switching to {ckpt_name} ...")
127
  meta = CKPT_META[ckpt_name]
128
+ ckpt_filename = HF_FILENAMES[ckpt_name]
129
  ckpt_file = hf_hub_download(
130
  repo_id=REPO_ID,
131
+ filename=ckpt_filename,
132
  local_dir=LOCAL_CKPT_DIR, force_download=False
133
  )
134
  print(f"Checkpoint: {ckpt_file}")
135
 
136
  # Build model structure
137
  model = SwinClassifier(
138
+ meta["backbone"], # Use backbone from meta
139
  num_classes = meta["n_cls"],
140
  pretrained = False,
141
  head_version = meta["head"]
142
  ).to(device)
143
 
144
+ # Compatible load for .pth and .safetensors
145
+ if ckpt_filename.endswith(".safetensors"):
146
+ state = load_file(ckpt_file, device=device)
147
+ else:
148
+ state = torch.load(ckpt_file, map_location=device, weights_only=False)
149
+
150
  model.load_state_dict(state.get("model_state_dict", state), strict=True)
151
  model.eval()
152
 
 
190
  load_model(DEFAULT_CKPT) # 预加载
191
 
192
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
193
+ gr.Markdown("# AI Detector")
194
  gr.Markdown(
195
  "Choose a model checkpoint on the left, upload an image, "
196
  "and click **Run** to see predictions. Checkpoint V7+ outputs 4 classes."