NiranjanSathish commited on
Commit
ec859d2
·
verified ·
1 Parent(s): f84c257

Upload 25 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
+ best_model.keras filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,25 @@
1
- ---
2
- title: Interactive Human Vs AIDetector Game
3
- emoji: 📊
4
- colorFrom: yellow
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 6.3.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: Real vs AI Face Detection Game
3
+ emoji: 🤖
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.0.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # 🤖 Human vs. AI: Face Detection Showdown
13
+
14
+ Can you distinguish real human faces from AI-generated ones better than a Deep Learning model?
15
+
16
+ ## 🎮 How to Play
17
+ 1. A face image will appear.
18
+ 2. Click **Real** or **AI** to make your guess.
19
+ 3. See if you were right, and check if the AI Model got it right too!
20
+ 4. Compete to see who gets a higher score.
21
+
22
+ ## 🧠 Behind the Scenes
23
+ This game is powered by a **Convolutional Neural Network (CNN)** trained on a dataset of real and fake faces.
24
+
25
+ *Student Project for CS6180 - Deep Learning*
app.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import os
5
+ import random
6
+ from PIL import Image
7
+
8
+ # --- Config ---
9
+ # Get the absolute path of the directory containing this script
10
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
11
+ MODEL_PATH = os.path.join(BASE_DIR, "best_model.keras")
12
+ IMAGE_FOLDER = os.path.join(BASE_DIR, "test_images")
13
+ IMG_SIZE = (128, 128)
14
+
15
+ # --- Global State Helpers ---
16
+ def load_game_resources():
17
+ """Loads model and image list."""
18
+ # Load Model
19
+ try:
20
+ model = tf.keras.models.load_model(MODEL_PATH)
21
+ print("✅ Model loaded successfully.")
22
+ except Exception as e:
23
+ print(f"❌ Error loading model: {e}")
24
+ model = None
25
+
26
+ # Load Images
27
+ images = []
28
+ if os.path.exists(IMAGE_FOLDER):
29
+ for fname in os.listdir(IMAGE_FOLDER):
30
+ if fname.lower().endswith(('.png', '.jpg', '.jpeg')):
31
+ path = os.path.join(IMAGE_FOLDER, fname)
32
+ # Determine label from filename prefix created in prepare_assets.py
33
+ label = "Real" if "real_" in fname.lower() else "AI"
34
+ images.append((path, label))
35
+
36
+ random.shuffle(images)
37
+ print(f"✅ Loaded {len(images)} images.")
38
+ return model, images
39
+
40
+ # Load resources once on startup
41
+ global_model, global_images = load_game_resources()
42
+
43
+ # --- Game Logic ---
44
+
45
+ def start_new_game():
46
+ """Reset scores and reshuffle images."""
47
+ random.shuffle(global_images)
48
+ # State: [human_score, model_score, round_count, current_image_index, image_list]
49
+ return 0, 0, 0, 0, global_images, gr.update(visible=True), gr.update(visible=False)
50
+
51
+ def get_current_image(index, image_list):
52
+ if not image_list or index >= len(image_list):
53
+ return None, "Game Over! Restart to play again.", None
54
+
55
+ path, label = image_list[index]
56
+ return path, f"Round {index + 1}", label
57
+
58
+ def predict_and_score(user_guess, index, image_list, human_score, model_score, rounds):
59
+ if not image_list or index >= len(image_list):
60
+ return human_score, model_score, rounds, index, "Game Over", None, gr.update(visible=False), gr.update(visible=True)
61
+
62
+ path, true_label = image_list[index]
63
+
64
+ # 1. Model Prediction
65
+ model_guess_label = "Unsure"
66
+ confidence = 0.0
67
+
68
+ if global_model:
69
+ try:
70
+ img = Image.open(path).convert('RGB')
71
+ img = img.resize(IMG_SIZE)
72
+ img_array = np.array(img) / 255.0
73
+ img_array = np.expand_dims(img_array, axis=0)
74
+
75
+ # Prediction (Sigmoid: <0.5 = Class 0, >0.5 = Class 1)
76
+ # We need to know which class is which.
77
+ # Usually: 0=AI, 1=Real OR 0=Real, 1=AI.
78
+ # In the notebook: train_generator.class_indices would tell us.
79
+ # Assumption based on directory names: sorting usually puts AI first.
80
+ # So 0=AI, 1=Real. Let's verify this output if possible, but assume 0=AI for now.
81
+ prediction = global_model.predict(img_array, verbose=0)[0][0]
82
+
83
+ # Notebook typically uses 'training_AI' and 'training_real'.
84
+ # Alphabetical: AI=0, real=1.
85
+ is_real = prediction > 0.5
86
+ model_guess_label = "Real" if is_real else "AI"
87
+ confidence = prediction if is_real else 1 - prediction
88
+
89
+ except Exception as e:
90
+ print(f"Prediction Error: {e}")
91
+ model_guess_label = "Error"
92
+
93
+ # 2. Update Scores
94
+ human_correct = (user_guess == true_label)
95
+ model_correct = (model_guess_label == true_label)
96
+
97
+ if human_correct:
98
+ human_score += 1
99
+ if model_correct:
100
+ model_score += 1
101
+
102
+ rounds += 1
103
+ next_idx = index + 1
104
+
105
+ # 3. Message
106
+ result_text = f"## Result: {true_label}!\n\n"
107
+ result_text += f"**You guessed:** {user_guess} " + ("✅" if human_correct else "❌") + "\n"
108
+ result_text += f"**Model guessed:** {model_guess_label} " + ("✅" if model_correct else "❌") + f" ({confidence:.2f})"
109
+
110
+ # Prepare next round view
111
+ # Hide guess buttons, Show Next button
112
+ return human_score, model_score, rounds, next_idx, result_text, gr.update(visible=False), gr.update(visible=True)
113
+
114
+ def next_round_view(index, image_list):
115
+ path, round_text, _ = get_current_image(index, image_list)
116
+ if path is None:
117
+ return None, round_text, gr.update(visible=False), gr.update(visible=False), gr.update(visible=True) # End game
118
+
119
+ return path, round_text, gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
120
+
121
+ # --- UI Construction ---
122
+ with gr.Blocks() as demo:
123
+
124
+ # Game State
125
+ # Stores: [human_score, model_score, rounds, index, image_list]
126
+ state_human = gr.State(0)
127
+ state_model = gr.State(0)
128
+ state_rounds = gr.State(0)
129
+ state_index = gr.State(0)
130
+ state_images = gr.State(global_images)
131
+
132
+ gr.Markdown("# 🤖 Human vs. AI: Face Detection Showdown")
133
+ gr.Markdown("Can you beat the deep learning model at spotting AI-generated faces?")
134
+
135
+ with gr.Row():
136
+ # Left Column: Stats
137
+ with gr.Column(scale=1):
138
+ human_score_disp = gr.Number(label="Your Score", value=0)
139
+ model_score_disp = gr.Number(label="Model Score", value=0)
140
+ round_disp = gr.Number(label="Rounds Played", value=0)
141
+ restart_btn = gr.Button("🔄 Restart Game", variant="secondary")
142
+
143
+ # Right Column: Game Area
144
+ with gr.Column(scale=2):
145
+ round_label = gr.Markdown("### Round 1")
146
+ image_display = gr.Image(label="Is this face Real or AI?", type="filepath", height=300)
147
+
148
+ result_box = gr.Markdown("")
149
+
150
+ with gr.Row() as guess_row:
151
+ btn_real = gr.Button("Real 👤", variant="primary", size="lg")
152
+ btn_ai = gr.Button("AI 🤖", variant="primary", size="lg")
153
+
154
+ next_btn = gr.Button("Next Image ➡️", variant="primary", visible=False)
155
+
156
+ # --- Event Handlers ---
157
+
158
+ # Restart
159
+ restart_btn.click(
160
+ start_new_game,
161
+ outputs=[state_human, state_model, state_rounds, state_index, state_images, guess_row, next_btn]
162
+ ).then(
163
+ next_round_view,
164
+ inputs=[state_index, state_images],
165
+ outputs=[image_display, round_label, guess_row, next_btn, restart_btn]
166
+ ).then(
167
+ lambda: (0,0,0, ""), outputs=[human_score_disp, model_score_disp, round_disp, result_box]
168
+ )
169
+
170
+ # Guess Real
171
+ btn_real.click(
172
+ fn=predict_and_score,
173
+ inputs=[gr.State("Real"), state_index, state_images, state_human, state_model, state_rounds],
174
+ outputs=[state_human, state_model, state_rounds, state_index, result_box, guess_row, next_btn]
175
+ ).then(
176
+ lambda h, m, r: (h, m, r),
177
+ inputs=[state_human, state_model, state_rounds],
178
+ outputs=[human_score_disp, model_score_disp, round_disp]
179
+ )
180
+
181
+ # Guess AI
182
+ btn_ai.click(
183
+ fn=predict_and_score,
184
+ inputs=[gr.State("AI"), state_index, state_images, state_human, state_model, state_rounds],
185
+ outputs=[state_human, state_model, state_rounds, state_index, result_box, guess_row, next_btn]
186
+ ).then(
187
+ lambda h, m, r: (h, m, r),
188
+ inputs=[state_human, state_model, state_rounds],
189
+ outputs=[human_score_disp, model_score_disp, round_disp]
190
+ )
191
+
192
+ # Next Image
193
+ next_btn.click(
194
+ next_round_view,
195
+ inputs=[state_index, state_images],
196
+ outputs=[image_display, round_label, guess_row, next_btn, restart_btn]
197
+ ).then(
198
+ lambda: "", outputs=[result_box]
199
+ )
200
+
201
+ # Initial Load
202
+ demo.load(
203
+ start_new_game,
204
+ outputs=[state_human, state_model, state_rounds, state_index, state_images, guess_row, next_btn]
205
+ ).then(
206
+ next_round_view,
207
+ inputs=[state_index, state_images],
208
+ outputs=[image_display, round_label, guess_row, next_btn, restart_btn]
209
+ )
210
+
211
+ if __name__ == "__main__":
212
+ demo.launch(theme=gr.themes.Soft())
best_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:65ba1ed16509c07a86ac0a40efb806a1d5aff4c340afd5ea3abf588c8799f377
3
+ size 6123674
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ tensorflow
2
+ gradio
3
+ numpy
4
+ pillow
5
+ h5py
test_images/ai_2A2I5HCWUI.jpg ADDED
test_images/ai_2GRFCWT6QV.jpg ADDED
test_images/ai_2GUJ0LFBE0.jpg ADDED
test_images/ai_2GUVDA2GJK.jpg ADDED
test_images/ai_2P91Y92DSN.jpg ADDED
test_images/ai_3BM7O3838N.jpg ADDED
test_images/ai_3PJXB5NYWE.jpg ADDED
test_images/ai_3POKM7QXG4.jpg ADDED
test_images/ai_3XQV2MF4Q3.jpg ADDED
test_images/ai_9C9GZSN0SH.jpg ADDED
test_images/real_02120.jpg ADDED
test_images/real_02301.jpg ADDED
test_images/real_02443.jpg ADDED
test_images/real_02600.jpg ADDED
test_images/real_03204.jpg ADDED
test_images/real_03231.jpg ADDED
test_images/real_03265.jpg ADDED
test_images/real_18739.jpg ADDED
test_images/real_18849.jpg ADDED
test_images/real_18880.jpg ADDED
verify_model.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import os
5
+
6
+ MODEL_PATH = "best_model.keras"
7
+ IMG_SIZE = (128, 128)
8
+
9
+ def verify():
10
+ if not os.path.exists(MODEL_PATH):
11
+ print(f"❌ Model not found at {MODEL_PATH}")
12
+ return
13
+
14
+ try:
15
+ model = tf.keras.models.load_model(MODEL_PATH)
16
+ print("✅ Model loaded successfully.")
17
+
18
+ # Print input shape
19
+ input_shape = model.input_shape
20
+ print(f"Model Input Shape: {input_shape}")
21
+
22
+ # Test Prediction
23
+ dummy_input = np.random.rand(1, IMG_SIZE[0], IMG_SIZE[1], 3).astype(np.float32)
24
+ print(f"Testing with input shape: {dummy_input.shape}")
25
+
26
+ prediction = model.predict(dummy_input, verbose=0)
27
+ print(f"✅ Prediction Check: Output shape {prediction.shape}, Value: {prediction}")
28
+
29
+ except Exception as e:
30
+ print(f"❌ Error: {e}")
31
+
32
+ if __name__ == "__main__":
33
+ verify()