Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import random
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
|
7 |
+
LEADERBOARD_FILE = "leaderboard.json"
|
8 |
+
|
9 |
+
# --- Load leaderboard from file if it exists ---
|
10 |
+
if os.path.exists(LEADERBOARD_FILE):
|
11 |
+
with open(LEADERBOARD_FILE, "r") as f:
|
12 |
+
leaderboard_scores = json.load(f)
|
13 |
+
else:
|
14 |
+
leaderboard_scores = {}
|
15 |
+
|
16 |
+
# --- Save leaderboard to file ---
|
17 |
+
def save_leaderboard():
|
18 |
+
with open(LEADERBOARD_FILE, "w") as f:
|
19 |
+
json.dump(leaderboard_scores, f)
|
20 |
+
|
21 |
+
# --- Main logic for prediction ---
|
22 |
+
def dummy_deepfake_detector(image: Image.Image, prompt: str, username: str):
|
23 |
+
if not username.strip():
|
24 |
+
return "Please enter your name.", None, [], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
25 |
+
|
26 |
+
prediction = random.choice(["Real", "Fake"])
|
27 |
+
score = 1 if prediction == "Real" else 0
|
28 |
+
|
29 |
+
# Update and persist leaderboard
|
30 |
+
leaderboard_scores[username] = leaderboard_scores.get(username, 0) + score
|
31 |
+
save_leaderboard()
|
32 |
+
|
33 |
+
# Create sorted leaderboard table
|
34 |
+
sorted_scores = sorted(leaderboard_scores.items(), key=lambda x: x[1], reverse=True)
|
35 |
+
leaderboard_table = [[name, points] for name, points in sorted_scores]
|
36 |
+
|
37 |
+
return (
|
38 |
+
f"Prediction: {prediction}",
|
39 |
+
image,
|
40 |
+
leaderboard_table,
|
41 |
+
gr.update(visible=False), # hide image input
|
42 |
+
gr.update(visible=False), # hide prompt input
|
43 |
+
gr.update(visible=False), # hide upload button
|
44 |
+
gr.update(visible=True) # show Try Again button
|
45 |
+
)
|
46 |
+
|
47 |
+
# --- Reset app state ---
|
48 |
+
def reset_app():
|
49 |
+
return (
|
50 |
+
"", # Clear prediction text
|
51 |
+
None, # Clear image output
|
52 |
+
[], # Clear leaderboard
|
53 |
+
gr.update(visible=True, value=""), # Show prompt input
|
54 |
+
gr.update(visible=True, value=None), # Show image input
|
55 |
+
gr.update(visible=True), # Show Upload button
|
56 |
+
gr.update(visible=False), # Hide Try Again button
|
57 |
+
gr.update(visible=True, value="") # Show username input
|
58 |
+
)
|
59 |
+
|
60 |
+
# --- Build Gradio UI ---
|
61 |
+
with gr.Blocks(css=".gr-button {font-size: 16px !important}") as demo:
|
62 |
+
gr.Markdown("## 🕵️♂️ Fool the Deepfake Detector")
|
63 |
+
gr.Markdown("Upload an image and try to fool the AI model into thinking it’s real. Your score will be saved!")
|
64 |
+
|
65 |
+
with gr.Group():
|
66 |
+
username_input = gr.Textbox(label="Your Name", placeholder="Enter your name")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
prompt_input = gr.Textbox(
|
70 |
+
label="Suggested Prompt",
|
71 |
+
placeholder="e.g., A portrait photograph of a politician delivering a speech...",
|
72 |
+
value="A portrait photograph of Barack Obama delivering a speech, with the United States flag in the background",
|
73 |
+
lines=2
|
74 |
+
)
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
submit_btn = gr.Button("Upload")
|
81 |
+
|
82 |
+
try_again_btn = gr.Button("Try Again", visible=False)
|
83 |
+
|
84 |
+
with gr.Group():
|
85 |
+
gr.Markdown("### 🎯 Result")
|
86 |
+
with gr.Row():
|
87 |
+
prediction_output = gr.Textbox(label="Prediction", interactive=False)
|
88 |
+
image_output = gr.Image(label="Submitted Image", show_label=False)
|
89 |
+
|
90 |
+
with gr.Group():
|
91 |
+
gr.Markdown("### 🏆 Leaderboard")
|
92 |
+
leaderboard = gr.Dataframe(
|
93 |
+
headers=["Username", "Score"],
|
94 |
+
datatype=["str", "number"],
|
95 |
+
interactive=False,
|
96 |
+
row_count=5
|
97 |
+
)
|
98 |
+
|
99 |
+
# Submit button logic
|
100 |
+
submit_btn.click(
|
101 |
+
fn=dummy_deepfake_detector,
|
102 |
+
inputs=[image_input, prompt_input, username_input],
|
103 |
+
outputs=[
|
104 |
+
prediction_output,
|
105 |
+
image_output,
|
106 |
+
leaderboard,
|
107 |
+
image_input,
|
108 |
+
prompt_input,
|
109 |
+
submit_btn,
|
110 |
+
try_again_btn
|
111 |
+
]
|
112 |
+
)
|
113 |
+
|
114 |
+
# Try Again button logic
|
115 |
+
try_again_btn.click(
|
116 |
+
fn=reset_app,
|
117 |
+
outputs=[
|
118 |
+
prediction_output,
|
119 |
+
image_output,
|
120 |
+
leaderboard,
|
121 |
+
prompt_input,
|
122 |
+
image_input,
|
123 |
+
submit_btn,
|
124 |
+
try_again_btn,
|
125 |
+
username_input
|
126 |
+
]
|
127 |
+
)
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
demo.launch()
|
131 |
+
|