MetaQu commited on
Commit
267b575
·
verified ·
1 Parent(s): 555fed5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -44
app.py CHANGED
@@ -1,67 +1,61 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from PIL import Image
 
4
 
5
- # === Load 3 model publik khusus AI vs Real ===
6
- model1 = pipeline("image-classification", model="prithivMLmods/AI-vs-Deepfake-vs-Real")
7
- model2 = pipeline("image-classification", model="dima806/ai_vs_real_image_detection")
8
- model3 = pipeline("image-classification", model="Hemg/AI-VS-REAL-IMAGE-DETECTION")
9
 
10
  def detect_image(img: Image.Image):
11
  results = {}
12
 
13
- # Model 1
14
  res1 = model1(img)
15
- results["Model 1 (prithivMLmods)"] = res1
16
-
17
- # Model 2
18
  res2 = model2(img)
19
- results["Model 2 (dima806)"] = res2
20
-
21
- # Model 3
22
  res3 = model3(img)
23
- results["Model 3 (Hemg)"] = res3
24
 
25
- # --- Ensemble dengan threshold agar lebih stabil ---
26
- ai_score, real_score = 0, 0
27
- for res in [res1, res2, res3]:
28
- for item in res:
29
- label = item["label"].lower()
30
- score = item["score"]
31
- if "real" in label:
32
- real_score += score
33
- elif "fake" in label or "deepfake" in label or "artificial" in label:
34
- ai_score += score
35
 
36
- # Normalisasi
37
- total = ai_score + real_score + 1e-9
38
- ai_percent = (ai_score / total) * 100
39
- real_percent = (real_score / total) * 100
40
 
41
- # Keputusan akhir dengan threshold
42
- if real_percent > ai_percent and real_percent > 55:
43
- verdict = f"✅ Foto Asli ({real_percent:.2f}%)"
44
- elif ai_percent > real_percent and ai_percent > 55:
45
- verdict = f"⚠️ AI Generated ({ai_percent:.2f}%)"
46
  else:
47
- verdict = f"⚠️ Tidak Pasti (Real {real_percent:.2f}%, AI {ai_percent:.2f}%)"
 
 
48
 
49
- # Format output
50
- output = f"## 📊 Ringkasan Deteksi\n"
51
- for name, res in results.items():
52
- output += f"\n🔹 **{name}**: {res}\n"
53
 
54
- output += f"\n=== 🧠 ENSEMBLE HASIL AKHIR ===\n{verdict}\n"
 
 
 
 
55
 
56
- return output
57
 
58
- iface = gr.Interface(
 
59
  fn=detect_image,
60
  inputs=gr.Image(type="pil"),
61
- outputs="markdown",
62
- title="Deteksi AI vs Foto Asli (Ensemble 3 Model Publik)",
63
- description="Menggunakan 3 model publik Hugging Face (prithivMLmods, dima806, Hemg) dengan threshold untuk mendeteksi apakah gambar AI-generated atau asli."
64
  )
65
 
66
  if __name__ == "__main__":
67
- iface.launch()
 
1
  import gradio as gr
 
2
  from PIL import Image
3
+ from transformers import pipeline
4
 
5
+ # Load 3 model Hugging Face
6
+ model1 = pipeline("image-classification", model="prithivMLmods/deepfake-vs-real-image-detection")
7
+ model2 = pipeline("image-classification", model="dima806/ai-image-detector")
8
+ model3 = pipeline("image-classification", model="Hemg/A-real-and-fake-image-detection")
9
 
10
  def detect_image(img: Image.Image):
11
  results = {}
12
 
13
+ # Prediksi masing-masing model
14
  res1 = model1(img)
 
 
 
15
  res2 = model2(img)
 
 
 
16
  res3 = model3(img)
 
17
 
18
+ # Ambil skor Real/Artificial sesuai output
19
+ score1_real = next((r["score"] for r in res1 if "real" in r["label"].lower()), 0)
20
+ score1_fake = next((r["score"] for r in res1 if "fake" in r["label"].lower() or "artificial" in r["label"].lower()), 0)
21
+
22
+ score2_real = next((r["score"] for r in res2 if "real" in r["label"].lower()), 0)
23
+ score2_fake = next((r["score"] for r in res2 if "fake" in r["label"].lower()), 0)
24
+
25
+ score3_real = next((r["score"] for r in res3 if "real" in r["label"].lower()), 0)
26
+ score3_fake = next((r["score"] for r in res3 if "fake" in r["label"].lower()), 0)
 
27
 
28
+ # Voting: jika 2 model yakin REAL (>0.5), maka final REAL
29
+ votes_real = sum([score1_real > 0.5, score2_real > 0.5, score3_real > 0.5])
 
 
30
 
31
+ if votes_real >= 2:
32
+ final_label = "✅ Foto Asli"
 
 
 
33
  else:
34
+ # Weighted ensemble (Hemg lebih berat)
35
+ weighted_real = (0.25 * score1_real) + (0.25 * score2_real) + (0.5 * score3_real)
36
+ weighted_fake = (0.25 * score1_fake) + (0.25 * score2_fake) + (0.5 * score3_fake)
37
 
38
+ if weighted_real >= weighted_fake:
39
+ final_label = " Foto Asli"
40
+ else:
41
+ final_label = " Foto AI / Hasil Buatan"
42
 
43
+ # Tampilkan hasil detail
44
+ results["Model1"] = res1
45
+ results["Model2"] = res2
46
+ results["Model3"] = res3
47
+ results["Final"] = final_label
48
 
49
+ return results
50
 
51
+ # Interface Gradio
52
+ demo = gr.Interface(
53
  fn=detect_image,
54
  inputs=gr.Image(type="pil"),
55
+ outputs="json",
56
+ title="Deteksi Foto Asli vs AI",
57
+ description="Menggabungkan 3 model Hugging Face dengan hybrid voting + bobot."
58
  )
59
 
60
  if __name__ == "__main__":
61
+ demo.launch()