AMontiB commited on
Commit
c4321f9
Β·
1 Parent(s): 142f504

update ALL

Browse files
__pycache__/app.cpython-310.pyc CHANGED
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
 
app.py CHANGED
@@ -163,10 +163,10 @@ def predict(image_path, detector_name):
163
 
164
  votes_real = 0.0
165
  votes_fake = 0.0
 
166
  confidences = []
167
  labels = []
168
  colors = []
169
- total_conf = 0
170
 
171
  for det, res in results:
172
  pred = res.get('prediction', 'Unknown')
@@ -183,16 +183,19 @@ def predict(image_path, detector_name):
183
  labels.append(det)
184
  confidences.append(score)
185
  colors.append(color)
186
- total_conf += score
187
 
188
  # Weighted Voting logic
189
  # Only count vote if confidence > 0.6
190
  if score > 0.6:
191
  weights = DETECTOR_WEIGHTS.get(det, {'real': 1.0, 'fake': 1.0})
192
  if pred == 'fake':
193
- votes_fake += weights['fake']
 
 
194
  elif pred == 'real':
195
- votes_real += weights['real']
 
 
196
 
197
  # Majority Voting
198
  if votes_real > votes_fake:
@@ -202,15 +205,19 @@ def predict(image_path, detector_name):
202
  else:
203
  verdict = "UNCERTAIN"
204
 
205
- avg_conf = total_conf / len(results) if results else 0
 
 
 
 
206
 
207
  # Explanation
208
  if verdict == "REAL":
209
- explanation = f"Considering the results obtained by all models (weighted by their historical performance), the analyzed image results, with an average confidence of {avg_conf:.4f}, not produced by a generative AI."
210
  elif verdict == "FAKE":
211
- explanation = f"Considering the results obtained by all models (weighted by their historical performance), the analyzed image results, with an average confidence of {avg_conf:.4f}, produced by a generative AI."
212
  else:
213
- explanation = f"The result is uncertain. The detectors produced unconsistent results. The average confidence is {avg_conf:.4f}."
214
 
215
  # Plotting
216
  fig, ax = plt.subplots(figsize=(10, 5))
@@ -295,7 +302,8 @@ with demo:
295
 
296
  ### Understanding the Results produced by "ALL"
297
  * Runs all available detectors (R50_TF, R50_nodown, CLIP-D, P2G, NPR) sequentially on the input image.
298
- * Produces a **Weighted Majority Vote** verdict (Real/Fake). Each model's vote is weighted by its historical performance (TPR for Fakes, TNR for Reals) on benchmark datasets. Only confident predictions (> 0.6) are counted.
 
299
  * Also generates a **Confidence Plot** visualizing each model's score and a textual **Explanation** of the consensus.
300
  * In the plot, **Green** bars indicate a **Real** prediction, while **Red** bars indicate a **Fake** prediction.
301
 
@@ -325,6 +333,20 @@ with demo:
325
  )
326
  plot_output = gr.Plot(label="Confidence Scores")
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  with gr.Accordion("πŸ“š Model Details", open=False):
329
  gr.Markdown("""
330
  ### **ALL**
 
163
 
164
  votes_real = 0.0
165
  votes_fake = 0.0
166
+ total_weight_used = 0.0
167
  confidences = []
168
  labels = []
169
  colors = []
 
170
 
171
  for det, res in results:
172
  pred = res.get('prediction', 'Unknown')
 
183
  labels.append(det)
184
  confidences.append(score)
185
  colors.append(color)
 
186
 
187
  # Weighted Voting logic
188
  # Only count vote if confidence > 0.6
189
  if score > 0.6:
190
  weights = DETECTOR_WEIGHTS.get(det, {'real': 1.0, 'fake': 1.0})
191
  if pred == 'fake':
192
+ w = weights['fake']
193
+ votes_fake += w * score
194
+ total_weight_used += w
195
  elif pred == 'real':
196
+ w = weights['real']
197
+ votes_real += w * score
198
+ total_weight_used += w
199
 
200
  # Majority Voting
201
  if votes_real > votes_fake:
 
205
  else:
206
  verdict = "UNCERTAIN"
207
 
208
+ # Calculate weighted average confidence
209
+ if total_weight_used > 0:
210
+ weighted_conf = (votes_real + votes_fake) / total_weight_used
211
+ else:
212
+ weighted_conf = 0.0
213
 
214
  # Explanation
215
  if verdict == "REAL":
216
+ explanation = f"Considering the results obtained by all models (weighted by their historical performance), the analyzed image results, with a weighted confidence of {weighted_conf:.4f}, not produced by a generative AI."
217
  elif verdict == "FAKE":
218
+ explanation = f"Considering the results obtained by all models (weighted by their historical performance), the analyzed image results, with a weighted confidence of {weighted_conf:.4f}, produced by a generative AI."
219
  else:
220
+ explanation = f"The result is uncertain. The detectors produced unconsistent results. The weighted confidence is {weighted_conf:.4f}."
221
 
222
  # Plotting
223
  fig, ax = plt.subplots(figsize=(10, 5))
 
302
 
303
  ### Understanding the Results produced by "ALL"
304
  * Runs all available detectors (R50_TF, R50_nodown, CLIP-D, P2G, NPR) sequentially on the input image.
305
+ * Produces a **Weighted Majority Vote** verdict (Real/Fake). Each model's vote is weighted by its historical performance (TPR for Fakes, TNR for Reals) on benchmark datasets **and its confidence score**. Only confident predictions (> 0.6) are counted.
306
+ * You can find the specific weights used for each model in the **"βš–οΈ Weight Details"** menu below.
307
  * Also generates a **Confidence Plot** visualizing each model's score and a textual **Explanation** of the consensus.
308
  * In the plot, **Green** bars indicate a **Real** prediction, while **Red** bars indicate a **Fake** prediction.
309
 
 
333
  )
334
  plot_output = gr.Plot(label="Confidence Scores")
335
 
336
+ with gr.Accordion("βš–οΈ Weight Details", open=False):
337
+ gr.Markdown(f"""
338
+ ### **Detector Weights**
339
+ These weights are derived from performance on benchmark datasets (FFHQ, FORLAB, SG2, SDXL) and social media data (FB, TL, X).
340
+
341
+ | Detector | Real (TNR) Weight | Fake (TPR) Weight |
342
+ | :--- | :---: | :---: |
343
+ | **R50_TF** | {DETECTOR_WEIGHTS['R50_TF']['real']:.2f} | {DETECTOR_WEIGHTS['R50_TF']['fake']:.2f} |
344
+ | **R50_nodown** | {DETECTOR_WEIGHTS['R50_nodown']['real']:.2f} | {DETECTOR_WEIGHTS['R50_nodown']['fake']:.2f} |
345
+ | **CLIP-D** | {DETECTOR_WEIGHTS['CLIP-D']['real']:.2f} | {DETECTOR_WEIGHTS['CLIP-D']['fake']:.2f} |
346
+ | **P2G** | {DETECTOR_WEIGHTS['P2G']['real']:.2f} | {DETECTOR_WEIGHTS['P2G']['fake']:.2f} |
347
+ | **NPR** | {DETECTOR_WEIGHTS['NPR']['real']:.2f} | {DETECTOR_WEIGHTS['NPR']['fake']:.2f} |
348
+ """)
349
+
350
  with gr.Accordion("πŸ“š Model Details", open=False):
351
  gr.Markdown("""
352
  ### **ALL**
verify_all_option.py CHANGED
@@ -41,10 +41,10 @@ def test_all_option():
41
 
42
  if isinstance(fig, plt.Figure):
43
  print("Figure created successfully.")
44
- if "weighted by their historical performance" in text:
45
- print("Text output confirms weighted voting is used.")
46
  else:
47
- print("Text output MISSING weighted voting confirmation.")
48
 
49
  expected_verdict = "produced by a generative AI" # Majority Fake
50
  if expected_verdict in text:
 
41
 
42
  if isinstance(fig, plt.Figure):
43
  print("Figure created successfully.")
44
+ if "weighted confidence" in text:
45
+ print("Text output confirms weighted confidence is reported.")
46
  else:
47
+ print("Text output MISSING weighted confidence confirmation.")
48
 
49
  expected_verdict = "produced by a generative AI" # Majority Fake
50
  if expected_verdict in text: