MetaQu commited on
Commit
f0f1388
·
verified ·
1 Parent(s): f658766

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -10,7 +10,7 @@ model1 = pipeline("image-classification", model="microsoft/resnet-50")
10
  model2 = pipeline("image-classification", model="google/vit-base-patch16-224")
11
 
12
  # ----------------------------
13
- # Fungsi deteksi dengan ensemble
14
  # ----------------------------
15
  def detect_image(image):
16
  results = []
@@ -28,24 +28,29 @@ def detect_image(image):
28
  results.append((label2_final, score2))
29
 
30
  # ----------------------------
31
- # Voting + threshold
32
  # ----------------------------
33
  votes = [r[0] for r in results]
34
- final_label = max(set(votes), key=votes.count)
35
-
36
- # Rata-rata confidence untuk label final
37
- relevant_scores = [s for (l, s) in results if l == final_label]
38
- avg_confidence = np.mean(relevant_scores) * 100
 
 
 
 
 
 
39
 
40
- # Tentukan hasil akhir
41
- if avg_confidence < 80:
42
- final_result = "⚠️ Tidak Pasti (cek manual)"
43
- elif final_label == 'human':
44
- final_result = f"✅ Foto Asli ({avg_confidence:.2f}%)"
45
  else:
46
- final_result = f"🤖 AI Detected ({avg_confidence:.2f}%)"
47
 
48
- # Detail per model
49
  output = f"""
50
  ### Hasil Deteksi:
51
  {final_result}
@@ -63,7 +68,7 @@ iface = gr.Interface(
63
  inputs=gr.Image(type="pil"),
64
  outputs="markdown",
65
  title="AI vs Foto Asli Detector",
66
- description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI menggunakan ensemble 2 model publik + voting."
67
  )
68
 
69
  if __name__ == "__main__":
 
10
  model2 = pipeline("image-classification", model="google/vit-base-patch16-224")
11
 
12
  # ----------------------------
13
+ # Fungsi deteksi dengan ensemble tanpa "Tidak Pasti"
14
  # ----------------------------
15
  def detect_image(image):
16
  results = []
 
28
  results.append((label2_final, score2))
29
 
30
  # ----------------------------
31
+ # Voting atau ambil confidence tertinggi
32
  # ----------------------------
33
  votes = [r[0] for r in results]
34
+ if votes[0] == votes[1]:
35
+ final_label = votes[0] # mayoritas sama
36
+ final_conf = np.mean([r[1] for r in results if r[0]==final_label])*100
37
+ else:
38
+ # ambil model dengan confidence tertinggi
39
+ if results[0][1] > results[1][1]:
40
+ final_label = results[0][0]
41
+ final_conf = results[0][1]*100
42
+ else:
43
+ final_label = results[1][0]
44
+ final_conf = results[1][1]*100
45
 
46
+ # ----------------------------
47
+ # Hasil akhir
48
+ # ----------------------------
49
+ if final_label == 'human':
50
+ final_result = f"✅ Foto Asli ({final_conf:.2f}%)"
51
  else:
52
+ final_result = f"🤖 AI Detected ({final_conf:.2f}%)"
53
 
 
54
  output = f"""
55
  ### Hasil Deteksi:
56
  {final_result}
 
68
  inputs=gr.Image(type="pil"),
69
  outputs="markdown",
70
  title="AI vs Foto Asli Detector",
71
+ description="Unggah gambar, sistem akan mendeteksi apakah gambar AI atau Foto Asli berdasarkan 2 model publik."
72
  )
73
 
74
  if __name__ == "__main__":