File size: 13,075 Bytes
16f3614
 
db7977e
c4ae52a
16f3614
 
c4ae52a
2dff18f
 
db7977e
8ea30f5
db7977e
 
6eea4de
caa8f34
c4ae52a
 
bdef72a
c4ae52a
 
 
db7977e
 
c4ae52a
 
b41d531
abbfd6b
c4ae52a
db7977e
c4ae52a
db7977e
16f3614
c4ae52a
16f3614
c4ae52a
 
 
61d4800
 
 
 
 
 
 
 
c4ae52a
 
61d4800
c4ae52a
16f3614
14b33a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ae52a
14b33a7
16f3614
c4ae52a
 
b2c5903
 
c4ae52a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db7977e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ae52a
db7977e
 
 
 
 
c4ae52a
 
 
 
 
1228840
c4ae52a
abf6f7a
c4ae52a
 
 
 
 
 
2dff18f
c4ae52a
 
 
 
 
 
 
 
2dff18f
c4ae52a
 
 
 
 
 
 
 
cde6e53
16f3614
b2c5903
c4ae52a
 
e8d60f5
c4ae52a
db7977e
c4ae52a
db7977e
 
 
 
 
 
 
16f3614
db7977e
 
c4ae52a
db7977e
c4ae52a
db7977e
cde6e53
c4ae52a
87d68a6
c4ae52a
cde6e53
c0f0f00
 
 
16f3614
 
 
 
 
72f04b1
d804c96
87d68a6
 
72f04b1
 
87d68a6
 
 
d804c96
7c33ad6
2367852
d804c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16f3614
c4ae52a
16f3614
 
c4ae52a
b2c5903
 
16f3614
 
2dff18f
 
 
db7977e
 
 
 
 
 
16f3614
b2c5903
4af7a48
16f3614
c4ae52a
b2c5903
97f8200
16f3614
9487681
16f3614
 
9487681
34abc7f
b2c5903
 
16f3614
 
 
 
c4ae52a
16f3614
 
 
 
 
 
 
 
 
34abc7f
16f3614
 
 
 
 
 
 
 
b2c5903
 
16f3614
 
 
c4ae52a
c0f0f00
16f3614
 
 
 
c4ae52a
b2c5903
 
16f3614
 
 
 
db7977e
 
 
 
 
 
 
 
 
 
b2c5903
16f3614
 
 
 
c4ae52a
b2c5903
 
db7977e
 
16f3614
 
 
b2c5903
78a0909
 
db7977e
b2c5903
34abc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16f3614
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import gradio as gr
from PIL import Image
import torch
import torchvision.transforms as transforms
import json
import os
import numpy as np
import pandas as pd
import random
import onnxruntime as ort
from huggingface_hub import HfApi, hf_hub_download
from transformers import CLIPTokenizer, AutoImageProcessor, AutoModelForImageClassification
from safetensors.torch import load_file as safe_load
import subprocess
from datetime import datetime

# --- Config ---
HUB_REPO_ID = "CDL-AMLRT/OpenArenaLeaderboard"
HF_TOKEN = os.environ.get("HF_TOKEN")
LOCAL_JSON = "leaderboard.json"
HUB_JSON = "leaderboard.json"
MODEL_PATH = "model.safetensors"  # βœ… updated filename
MODEL_BACKBONE = "microsoft/swinv2-small-patch4-window16-256"
CLIP_IMAGE_ENCODER_PATH = "clip_image_encoder.onnx"
CLIP_TEXT_ENCODER_PATH = "clip_text_encoder.onnx"
PROMPT_CSV_PATH = "prompts_0.csv"
PROMPT_MATCH_THRESHOLD = 25  # percent

# --- No-op for HF Space ---
def load_assets():
    print("Skipping snapshot_download. Assuming files exist via Git LFS in HF Space.")

load_assets()

# --- Load leaderboard ---
def load_leaderboard():
    try:
        # Download the latest leaderboard from the dataset repo
        leaderboard_path = hf_hub_download(
            repo_id=HUB_REPO_ID,
            repo_type="dataset",
            filename=HUB_JSON,
            token=HF_TOKEN
        )
        with open(leaderboard_path, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception as e:
        print(f"Failed to load leaderboard from HF Hub: {e}")
        return {}

def load_entries():
    try:
        # grab the CSV of all past plays
        csv_path = hf_hub_download(
            repo_id=HUB_REPO_ID,
            repo_type="dataset",
            filename="test/leaderboard_entries.csv",
            token=HF_TOKEN
        )
        df = pd.read_csv(csv_path)

        return df

    except Exception as e:
        print(f"Failed to load leaderboard from HF Hub: {e}")
        # if anything goes wrong, fall back to empty
        return pd.Dataframe(columns=["file_name","prompt","label","model","split","prediction","user","timestamp"])

leaderboard_scores = load_leaderboard()
leaderboard_entries = load_entries()

def save_leaderboard():
    try:
        with open(HUB_JSON, "w", encoding="utf-8") as f:
            json.dump(leaderboard_scores, f, ensure_ascii=False)

        if HF_TOKEN is None:
            print("HF_TOKEN not set. Skipping push to hub.")
            return

        api = HfApi()
        api.upload_file(
            path_or_fileobj=HUB_JSON,
            path_in_repo=HUB_JSON,
            repo_id=HUB_REPO_ID,
            repo_type="dataset",
            token=HF_TOKEN,
            commit_message="Update leaderboard"
        )
    except Exception as e:
        print(f"Failed to save leaderboard to HF Hub: {e}")

# --- Load prompts from CSV ---
def load_prompts():
    try:
        df = pd.read_csv(PROMPT_CSV_PATH)
        if "prompt" in df.columns:
            return df["prompt"].dropna().tolist()
        else:
            print("CSV missing 'prompt' column.")
            return []
    except Exception as e:
        print(f"Failed to load prompts: {e}")
        return []

PROMPT_LIST = load_prompts()

# --- Load model + processor ---
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

processor = AutoImageProcessor.from_pretrained(MODEL_BACKBONE)
model = AutoModelForImageClassification.from_pretrained(MODEL_BACKBONE)
model.classifier = torch.nn.Linear(model.config.hidden_size, 2)

model.load_state_dict(safe_load(MODEL_PATH, device="cpu"), strict=False)
model.to(device)
model.eval()

# --- CLIP prompt matching ---
clip_image_sess = ort.InferenceSession(CLIP_IMAGE_ENCODER_PATH, providers=["CPUExecutionProvider"])
clip_text_sess = ort.InferenceSession(CLIP_TEXT_ENCODER_PATH, providers=["CPUExecutionProvider"])
clip_tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")

transform = transforms.Compose([
    transforms.Resize((256, 256)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

def compute_prompt_match(image: Image.Image, prompt: str) -> float:
    try:
        img_tensor = transform(image).unsqueeze(0).numpy().astype(np.float32)
        image_features = clip_image_sess.run(None, {clip_image_sess.get_inputs()[0].name: img_tensor})[0][0]
        image_features /= np.linalg.norm(image_features)

        inputs = clip_tokenizer(prompt, return_tensors="np", padding="max_length", truncation=True, max_length=77)
        input_ids = inputs["input_ids"]
        attention_mask = inputs["attention_mask"]
        text_features = clip_text_sess.run(None, {
            clip_text_sess.get_inputs()[0].name: input_ids,
            clip_text_sess.get_inputs()[1].name: attention_mask
        })[0][0]
        text_features /= np.linalg.norm(text_features)

        sim = np.dot(image_features, text_features)
        return round(sim * 100, 2)
    except Exception as e:
        print(f"CLIP ONNX match failed: {e}")
        return 0.0

# --- Main prediction logic ---
def detect_with_model(image: Image.Image, prompt: str, username: str, model_name: str):
    if not username.strip():
        return "Please enter your name.", None, [], gr.update(visible=True), gr.update(visible=False), username

    prompt_score = compute_prompt_match(image, prompt)
    if prompt_score < PROMPT_MATCH_THRESHOLD and model_name.lower() != "real":
        message = f"⚠️ Prompt match too low ({round(prompt_score, 2)}%). Please generate an image that better matches the prompt."
        return message, None, leaderboard, gr.update(visible=True), gr.update(visible=False), username

    # Run model inference
    inputs = processor(image, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        pred_class = torch.argmax(logits, dim=-1).item()
        prediction = "Real" if pred_class == 0 else "Fake"

        probs = torch.softmax(logits, dim=-1)[0]
        confidence = round(probs[pred_class].item() * 100, 2)

    score = 1 if prediction == "Real" else 0

    message = f"πŸ” Prediction: {prediction} ({confidence}% confidence)\n🧐 Prompt match: {round(prompt_score, 2)}%"
    if prediction == "Real" and model_name.lower() != "real":
        leaderboard_scores[username] = leaderboard_scores.get(username, 0) + score
        message += "\nπŸŽ‰ Nice! You fooled the AI. +1 point!"            
    else:
        if model_name.lower() == "real":
            message += "\n You uploaded a real image, this does not count toward the leaderboard!"
        else:
            message += "\nπŸ˜… The AI caught you this time. Try again!"

    save_leaderboard()

    sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
    leaderboard_table = [[name, points] for name, points in sorted_scores]
    
    type_image = "real" if model_name.lower() == "real" else "fake"
    image_dir = os.path.join("test", type_image)
    os.makedirs(image_dir, exist_ok=True)
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    image_filename = f"{timestamp}.jpg"
    image_path = os.path.join(image_dir, image_filename)
    image.save(image_path)

    csv_path = os.path.join("test", "leaderboard_entries.csv")
    leaderboard_entries.loc[len(leaderboard_entries)] = [f"test/{type_image}/{image_filename}", prompt, type_image, model_name.lower(), "test", prediction.lower(), username, datetime.now().isoformat()]
    leaderboard_entries.to_csv(csv_path, index=False)
    try:
        from huggingface_hub import HfApi

        api = HfApi()
        api.upload_file(
            path_or_fileobj=image_path,
            path_in_repo=f"test/{type_image}/{image_filename}",
            repo_id=HUB_REPO_ID,
            repo_type="dataset",
            token=HF_TOKEN,
            commit_message="Add passing image"
        )
        api.upload_file(
            path_or_fileobj=csv_path,
            path_in_repo="test/leaderboard_entries.csv",
            repo_id=HUB_REPO_ID,
            repo_type="dataset",
            token=HF_TOKEN,
            commit_message="Update leaderboard CSV"
        )
    except Exception as e:
        print(f"Failed to save image to HF Hub: {e}")

    return (
        message,
        image,
        leaderboard_table,
        gr.update(visible=False),
        gr.update(visible=True),
        username
    )

def get_random_prompt():
    return random.choice(PROMPT_LIST) if PROMPT_LIST else "A synthetic scene with dramatic lighting"

def load_initial_state():
    sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
    leaderboard_table = [[name, points] for name, points in sorted_scores]
    return gr.update(value=get_random_prompt()), leaderboard_table

# --- Gradio UI ---
with gr.Blocks(css=".gr-button {font-size: 16px !important}") as demo:
    gr.Markdown("## 🌝 OpenFake Arena")
    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.\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.")

    with gr.Group(visible=True) as input_section:
        username_input = gr.Textbox(label="Your Name", placeholder="Enter your name", interactive=True)
        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)

        # 🚫 Freeze this block: do not allow edits to the prompt input component's configuration.
        with gr.Row():
            prompt_input = gr.Textbox(
                interactive=False,
                label="Prompt to match",
                placeholder="e.g., ...",
                value="",
                lines=2
            )

        with gr.Row():
            image_input = gr.Image(type="pil", label="Upload Synthetic Image")

        with gr.Row():
            submit_btn = gr.Button("Upload")

    try_again_btn = gr.Button("Try Again", visible=False)

    with gr.Group():
        gr.Markdown("### 🎯 Result")
        with gr.Row():
            prediction_output = gr.Textbox(label="Prediction", interactive=False, elem_id="prediction_box")
            image_output = gr.Image(label="Submitted Image", show_label=False)

    with gr.Group():
        gr.Markdown("### πŸ† Leaderboard")
        leaderboard = gr.Dataframe(
            headers=["Username", "Score"],
            datatype=["str", "number"],
            interactive=False,
            row_count=5,
            visible=True
        )

    submit_btn.click(
        fn=detect_with_model,
        inputs=[image_input, prompt_input, username_input, model_input],
        outputs=[
            prediction_output,
            image_output,
            leaderboard,
            input_section,
            try_again_btn,
            username_input
        ]
    )

    try_again_btn.click(
        fn=lambda name: (
            "",               # Clear prediction text
            None,             # Clear uploaded image
            leaderboard,               # Clear leaderboard (temporarily, gets reloaded on next submit)
            gr.update(visible=True),   # Show input section
            gr.update(visible=False),  # Hide "Try Again" button
            name,             # Keep username
            gr.update(value=get_random_prompt()),  # Load new prompt
            None              # Clear image input
        ),
        inputs=[username_input],
        outputs=[
            prediction_output,
            image_output,
            leaderboard,
            input_section,
            try_again_btn,
            username_input,
            prompt_input,
            image_input        # ← added output to clear image
        ]
    )

    demo.load(
        fn=load_initial_state,
        outputs=[prompt_input, leaderboard]
    )


    gr.HTML("""
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        const target = document.getElementById('prediction_box');
        const observer = new MutationObserver(() => {
            if (target && target.innerText.trim() !== '') {
                window.scrollTo({ top: 0, behavior: 'smooth' });
            }
        });
        if (target) {
            observer.observe(target, { childList: true, subtree: true });
        }
    });
    </script>
    """)

if __name__ == "__main__":
    demo.launch()