Anonymous460 commited on
Commit
d943bfc
Β·
verified Β·
1 Parent(s): 30cc361

Upload 9 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ prompts_0.csv filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: OpenFakeArena
3
- emoji: πŸ’»
4
- colorFrom: pink
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 6.0.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Fool The AI Detector
3
+ emoji: 🌍
4
+ colorFrom: red
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.29.0
8
  app_file: app.py
9
  pinned: false
10
+ short_description: Fooling an synthetic image detector
11
  ---
12
 
13
+ This is the OpenFake Arena.
app.py ADDED
@@ -0,0 +1,282 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ import torchvision.transforms as transforms
5
+ import json
6
+ import os
7
+ import numpy as np
8
+ import pandas as pd
9
+ import random
10
+ import onnxruntime as ort
11
+ from transformers import CLIPTokenizer, AutoImageProcessor, AutoModelForImageClassification
12
+ from safetensors.torch import load_file as safe_load
13
+ from datetime import datetime
14
+
15
+ # --- Config ---
16
+ LEADERBOARD_JSON = "leaderboard.json"
17
+ MODEL_PATH = "model.safetensors" # βœ… updated filename
18
+ MODEL_BACKBONE = "microsoft/swinv2-small-patch4-window16-256"
19
+ CLIP_IMAGE_ENCODER_PATH = "clip_image_encoder.onnx"
20
+ CLIP_TEXT_ENCODER_PATH = "clip_text_encoder.onnx"
21
+ PROMPT_CSV_PATH = "prompts_0.csv"
22
+ PROMPT_MATCH_THRESHOLD = 25 # percent
23
+
24
+ # --- No-op for HF Space ---
25
+ def load_assets():
26
+ print("Skipping snapshot_download. Assuming files exist via Git LFS in HF Space.")
27
+
28
+ load_assets()
29
+
30
+ # --- Load leaderboard ---
31
+ def load_leaderboard():
32
+ try:
33
+ with open(LEADERBOARD_JSON, "r", encoding="utf-8") as f:
34
+ return json.load(f)
35
+ except Exception as e:
36
+ print(f"Failed to load leaderboard: {e}")
37
+ return {}
38
+
39
+ leaderboard_scores = load_leaderboard()
40
+
41
+ def save_leaderboard():
42
+ try:
43
+ with open(LEADERBOARD_JSON, "w", encoding="utf-8") as f:
44
+ json.dump(leaderboard_scores, f, ensure_ascii=False)
45
+ except Exception as e:
46
+ print(f"Failed to save leaderboard: {e}")
47
+
48
+ # --- Load prompts from CSV ---
49
+ def load_prompts():
50
+ try:
51
+ df = pd.read_csv(PROMPT_CSV_PATH)
52
+ if "prompt" in df.columns:
53
+ return df["prompt"].dropna().tolist()
54
+ else:
55
+ print("CSV missing 'prompt' column.")
56
+ return []
57
+ except Exception as e:
58
+ print(f"Failed to load prompts: {e}")
59
+ return []
60
+
61
+ PROMPT_LIST = load_prompts()
62
+
63
+ # --- Load model + processor ---
64
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
65
+
66
+ processor = AutoImageProcessor.from_pretrained(MODEL_BACKBONE)
67
+ model = AutoModelForImageClassification.from_pretrained(MODEL_BACKBONE)
68
+ model.classifier = torch.nn.Linear(model.config.hidden_size, 2)
69
+
70
+ model.load_state_dict(safe_load(MODEL_PATH, device="cpu"), strict=False)
71
+ model.to(device)
72
+ model.eval()
73
+
74
+ # --- CLIP prompt matching ---
75
+ clip_image_sess = ort.InferenceSession(CLIP_IMAGE_ENCODER_PATH, providers=["CPUExecutionProvider"])
76
+ clip_text_sess = ort.InferenceSession(CLIP_TEXT_ENCODER_PATH, providers=["CPUExecutionProvider"])
77
+ clip_tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")
78
+
79
+ transform = transforms.Compose([
80
+ transforms.Resize((256, 256)),
81
+ transforms.ToTensor(),
82
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
83
+ ])
84
+
85
+ def compute_prompt_match(image: Image.Image, prompt: str) -> float:
86
+ try:
87
+ img_tensor = transform(image).unsqueeze(0).numpy().astype(np.float32)
88
+ image_features = clip_image_sess.run(None, {clip_image_sess.get_inputs()[0].name: img_tensor})[0][0]
89
+ image_features /= np.linalg.norm(image_features)
90
+
91
+ inputs = clip_tokenizer(prompt, return_tensors="np", padding="max_length", truncation=True, max_length=77)
92
+ input_ids = inputs["input_ids"]
93
+ attention_mask = inputs["attention_mask"]
94
+ text_features = clip_text_sess.run(None, {
95
+ clip_text_sess.get_inputs()[0].name: input_ids,
96
+ clip_text_sess.get_inputs()[1].name: attention_mask
97
+ })[0][0]
98
+ text_features /= np.linalg.norm(text_features)
99
+
100
+ sim = np.dot(image_features, text_features)
101
+ return round(sim * 100, 2)
102
+ except Exception as e:
103
+ print(f"CLIP ONNX match failed: {e}")
104
+ return 0.0
105
+
106
+ # --- Main prediction logic ---
107
+ def detect_with_model(image: Image.Image, prompt: str, username: str, model_name: str):
108
+ if not username.strip():
109
+ return "Please enter your name.", None, [], gr.update(visible=True), gr.update(visible=False), username
110
+
111
+ prompt_score = compute_prompt_match(image, prompt)
112
+ if prompt_score < PROMPT_MATCH_THRESHOLD and (model_name.lower() != "real" or model_name != ""):
113
+ message = f"⚠️ Prompt match too low ({round(prompt_score, 2)}%). Please generate an image that better matches the prompt."
114
+ return message, None, leaderboard, gr.update(visible=True), gr.update(visible=False), username
115
+
116
+ # Run model inference
117
+ inputs = processor(image, return_tensors="pt").to(device)
118
+ with torch.no_grad():
119
+ outputs = model(**inputs)
120
+ logits = outputs.logits
121
+ pred_class = torch.argmax(logits, dim=-1).item()
122
+ prediction = "Real" if pred_class == 0 else "Fake"
123
+
124
+ probs = torch.softmax(logits, dim=-1)[0]
125
+ confidence = round(probs[pred_class].item() * 100, 2)
126
+
127
+ score = 1 if prediction == "Real" else 0
128
+
129
+ message = f"πŸ” Prediction: {prediction} ({confidence}% confidence)\n🧐 Prompt match: {round(prompt_score, 2)}%"
130
+ if prediction == "Real" and model_name.lower() != "real":
131
+ leaderboard_scores[username] = leaderboard_scores.get(username, 0) + score
132
+ message += "\nπŸŽ‰ Nice! You fooled the AI. +1 point!"
133
+ else:
134
+ if model_name.lower() == "real":
135
+ message += "\n You uploaded a real image, this does not count toward the leaderboard!"
136
+ else:
137
+ message += "\nπŸ˜… The AI caught you this time. Try again!"
138
+
139
+ save_leaderboard()
140
+
141
+ sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
142
+ leaderboard_table = [[name, points] for name, points in sorted_scores]
143
+
144
+ image_path = None
145
+ try:
146
+ type_image = "real" if (model_name.lower() == "real" or model_name == "") else "fake"
147
+ image_dir = os.path.join("test", type_image)
148
+ os.makedirs(image_dir, exist_ok=True)
149
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
150
+ image_filename = f"{timestamp}.jpg"
151
+ image_path = os.path.join(image_dir, image_filename)
152
+ image.save(image_path)
153
+ except Exception as e:
154
+ print(f"Failed to save image locally: {e}")
155
+ finally:
156
+ if image_path and os.path.exists(image_path):
157
+ try:
158
+ os.remove(image_path)
159
+ except Exception as cleanup_error:
160
+ print(f"Failed to delete temporary image: {cleanup_error}")
161
+
162
+ return (
163
+ message,
164
+ image,
165
+ leaderboard_table,
166
+ gr.update(visible=False),
167
+ gr.update(visible=True),
168
+ username
169
+ )
170
+
171
+ def get_random_prompt():
172
+ return random.choice(PROMPT_LIST) if PROMPT_LIST else "A synthetic scene with dramatic lighting"
173
+
174
+ def load_initial_state():
175
+ sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
176
+ leaderboard_table = [[name, points] for name, points in sorted_scores]
177
+ return gr.update(value=get_random_prompt()), leaderboard_table
178
+
179
+ # --- Gradio UI ---
180
+ with gr.Blocks(css=".gr-button {font-size: 16px !important}") as demo:
181
+ gr.Markdown("## 🌝 OpenFake Arena")
182
+ gr.Markdown("Welcome to the OpenFake Arena!\n\n**Your mission:** Generate a synthetic image for the prompt, upload it, and try to fool the AI detector into thinking it’s real.\n\n**Rules:**\n\n- You can modify the prompt on your end, but the image needs to have the same content. We verify the content with a CLIP similarity threshold.\n\n- Enter \"real\" in the model used to upload and test a real image. You don't need to follow the prompt for real images. Tips: you can also enter \"real\" if you just want to test the detector! We won't be collecting those images. \n\n- It is important to enter the correct model name for licensing.\n\n- Only synthetic images count toward the leaderboard!\n\n\nNote: The detector is still in early development. The prompt is not used for prediction, only the image.")
183
+
184
+ with gr.Group(visible=True) as input_section:
185
+ username_input = gr.Textbox(label="Your Name", placeholder="Enter your name", interactive=True)
186
+ model_input = gr.Textbox(label="Model used, specify the version (e.g., Imagen 3, Dall-e 3, Midjourney 6). Write \"Real\" when uploading a real image.", placeholder="Name of the model used to generate the image", interactive=True)
187
+
188
+ # 🚫 Freeze this block: do not allow edits to the prompt input component's configuration.
189
+ with gr.Row():
190
+ prompt_input = gr.Textbox(
191
+ interactive=False,
192
+ label="Prompt to match",
193
+ placeholder="e.g., ...",
194
+ value="",
195
+ lines=2
196
+ )
197
+
198
+ with gr.Row():
199
+ image_input = gr.Image(type="pil", label="Upload Synthetic Image")
200
+
201
+ with gr.Row():
202
+ submit_btn = gr.Button("Upload")
203
+
204
+ try_again_btn = gr.Button("Try Again", visible=False)
205
+
206
+ with gr.Group():
207
+ gr.Markdown("### 🎯 Result")
208
+ with gr.Row():
209
+ prediction_output = gr.Textbox(label="Prediction", interactive=False, elem_id="prediction_box")
210
+ image_output = gr.Image(label="Submitted Image", show_label=False)
211
+
212
+ with gr.Group():
213
+ gr.Markdown("### πŸ† Leaderboard")
214
+ leaderboard = gr.Dataframe(
215
+ headers=["Username", "Score"],
216
+ datatype=["str", "number"],
217
+ interactive=False,
218
+ row_count=5,
219
+ visible=True
220
+ )
221
+
222
+ submit_btn.click(
223
+ fn=detect_with_model,
224
+ inputs=[image_input, prompt_input, username_input, model_input],
225
+ outputs=[
226
+ prediction_output,
227
+ image_output,
228
+ leaderboard,
229
+ input_section,
230
+ try_again_btn,
231
+ username_input
232
+ ]
233
+ )
234
+
235
+ try_again_btn.click(
236
+ fn=lambda name: (
237
+ "", # Clear prediction text
238
+ None, # Clear uploaded image
239
+ leaderboard, # Clear leaderboard (temporarily, gets reloaded on next submit)
240
+ gr.update(visible=True), # Show input section
241
+ gr.update(visible=False), # Hide "Try Again" button
242
+ name, # Keep username
243
+ gr.update(value=get_random_prompt()), # Load new prompt
244
+ None # Clear image input
245
+ ),
246
+ inputs=[username_input],
247
+ outputs=[
248
+ prediction_output,
249
+ image_output,
250
+ leaderboard,
251
+ input_section,
252
+ try_again_btn,
253
+ username_input,
254
+ prompt_input,
255
+ image_input # ← added output to clear image
256
+ ]
257
+ )
258
+
259
+ demo.load(
260
+ fn=load_initial_state,
261
+ outputs=[prompt_input, leaderboard]
262
+ )
263
+
264
+
265
+ gr.HTML("""
266
+ <script>
267
+ document.addEventListener('DOMContentLoaded', function () {
268
+ const target = document.getElementById('prediction_box');
269
+ const observer = new MutationObserver(() => {
270
+ if (target && target.innerText.trim() !== '') {
271
+ window.scrollTo({ top: 0, behavior: 'smooth' });
272
+ }
273
+ });
274
+ if (target) {
275
+ observer.observe(target, { childList: true, subtree: true });
276
+ }
277
+ });
278
+ </script>
279
+ """)
280
+
281
+ if __name__ == "__main__":
282
+ demo.launch()
clip_image_encoder.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e557b21fad17709c7a0f4133ad24206cc887f551e58683c200d4afecaba2289c
3
+ size 351635998
clip_text_encoder.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:66bab8550f6f98ad825b5df3cf07f31c4d61e2e15dae4c9bc6231949c8faba51
3
+ size 253958912
leaderboard.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"Anonymous": 1}
mobilenet_v2_fake_detector.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2fd71df3f186e80b862a4d367ef19e2d19b29ae70588092dce16bfb92e7abbe3
3
+ size 8878521
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3fdae478dc277ab1284a8e06b7934334ed8277d21371a8c95a8abfcd67368631
3
+ size 195905448
prompts_0.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:123f0b62272dba0ecc69b565a09df93893242cbe2a41f5171858a281f7c6cd40
3
+ size 45613056
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ pillow
3
+ onnxruntime
4
+ scikit-image
5
+ torchvision
6
+ torch
7
+ transformers