MetaQu commited on
Commit
c346fbc
·
verified ·
1 Parent(s): 463bdd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
app.py CHANGED
@@ -44,68 +44,63 @@ def has_camera_exif(image):
44
  return False
45
 
46
  # ----------------------------
47
- # DETEKSI HYBRID
48
  # ----------------------------
49
  def detect_image(image):
50
- # Prediksi HF AI-detector
51
  hf_score = 0
52
- hf_label = "N/A"
53
- hf_conf = 0
 
 
 
54
  if hf_detector:
55
  try:
56
  result = hf_detector(image)
57
  hf_label = result[0]['label']
58
  hf_conf = result[0]['score'] * 100
 
59
  if any(x in hf_label.lower() for x in ["fake", "ai", "artificial"]):
60
  hf_score = hf_conf
61
- else:
62
- hf_score = 100 - hf_conf # jika human, kurangi
63
  except:
64
  hf_score = 0
65
 
66
- # Prediksi general model
67
- general_score = 0
68
- general_label = "N/A"
69
- general_conf = 0
70
  if general_model:
71
  try:
72
  result2 = general_model(image)
73
  general_label = result2[0]['label']
74
  general_conf = result2[0]['score'] * 100
75
  if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]):
76
- general_score = general_conf
77
  except:
78
  general_score = 0
79
 
80
- # Analisis lokal
81
  blur_score = calculate_blur(image)
82
  noise_score = calculate_noise(image)
83
  exif_present = has_camera_exif(image)
84
- local_score = 0
85
 
86
- # Blur rendah atau noise rendah → kemungkinan AI
87
  if blur_score < 100 or noise_score < 10:
88
  local_score += 50
89
- # Tidak ada metadata kamera → kemungkinan AI
90
  if not exif_present:
91
- local_score += 40
92
- else:
93
- local_score -= 20 # ada metadata kamera → lebih asli
94
 
95
- # Weighted hybrid
96
- weighted_score = hf_score*0.4 + general_score*0.2 + local_score*0.4
 
 
97
 
98
- # Konversi ke persen AI / Asli
99
- if weighted_score >= 95:
100
- final_result = "🖼️ Gambar ini hasil AI (100%)"
101
- elif weighted_score <= 5:
102
- final_result = "🖼️ Gambar ini asli (100%)"
103
  else:
104
- final_result = f"🖼️ Gambar ini {round(weighted_score,2)}% AI / {round(100-weighted_score,2)}% Asli"
105
 
106
- # Output
107
  output_lines = [
108
- f"### Hasil Deteksi:\n{final_result}",
109
  f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)",
110
  f"General Model: {general_label} ({general_conf:.2f}%)",
111
  f"Blur Score: {blur_score:.2f}",
@@ -122,8 +117,8 @@ iface = gr.Interface(
122
  fn=detect_image,
123
  inputs=gr.Image(type="pil"),
124
  outputs="markdown",
125
- title="AI vs Foto Asli Detector (Hybrid)",
126
- description="Unggah gambar, sistem akan mendeteksi persentase AI vs Asli."
127
  )
128
 
129
  if __name__ == "__main__":
 
44
  return False
45
 
46
  # ----------------------------
47
+ # DETEKSI HYBRID DENGAN PERSENTASE
48
  # ----------------------------
49
  def detect_image(image):
 
50
  hf_score = 0
51
+ general_score = 0
52
+ local_score = 0
53
+
54
+ # -------- HF AI-detector --------
55
+ hf_label, hf_conf = "N/A", 0
56
  if hf_detector:
57
  try:
58
  result = hf_detector(image)
59
  hf_label = result[0]['label']
60
  hf_conf = result[0]['score'] * 100
61
+ # Jika label mengandung tanda AI → skor AI = confidence
62
  if any(x in hf_label.lower() for x in ["fake", "ai", "artificial"]):
63
  hf_score = hf_conf
 
 
64
  except:
65
  hf_score = 0
66
 
67
+ # -------- General model --------
68
+ general_label, general_conf = "N/A", 0
 
 
69
  if general_model:
70
  try:
71
  result2 = general_model(image)
72
  general_label = result2[0]['label']
73
  general_conf = result2[0]['score'] * 100
74
  if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]):
75
+ general_score = general_conf * 0.7 # dikurangi bobot karena cadangan
76
  except:
77
  general_score = 0
78
 
79
+ # -------- Analisis lokal --------
80
  blur_score = calculate_blur(image)
81
  noise_score = calculate_noise(image)
82
  exif_present = has_camera_exif(image)
 
83
 
 
84
  if blur_score < 100 or noise_score < 10:
85
  local_score += 50
 
86
  if not exif_present:
87
+ local_score += 10
 
 
88
 
89
+ # -------- Weighted Score --------
90
+ total_ai_score = hf_score*0.7 + general_score*0.2 + local_score*0.1
91
+ total_ai_score = min(max(total_ai_score, 0), 100) # clamp 0–100
92
+ total_real_score = 100 - total_ai_score
93
 
94
+ # -------- Output --------
95
+ if total_ai_score == 100:
96
+ final_text = "🤖 Gambar ini hasil AI"
97
+ elif total_real_score == 100:
98
+ final_text = " Gambar ini asli"
99
  else:
100
+ final_text = f"🖼️ {total_ai_score:.2f}% AI / {total_real_score:.2f}% Asli"
101
 
 
102
  output_lines = [
103
+ f"### Hasil Deteksi:\n{final_text}",
104
  f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)",
105
  f"General Model: {general_label} ({general_conf:.2f}%)",
106
  f"Blur Score: {blur_score:.2f}",
 
117
  fn=detect_image,
118
  inputs=gr.Image(type="pil"),
119
  outputs="markdown",
120
+ title="Hybrid AI vs Foto Asli Detector (Gratis)",
121
+ description="Unggah gambar, sistem akan mendeteksi persentase AI dan persentase asli. Tidak ada kategori tidak pasti."
122
  )
123
 
124
  if __name__ == "__main__":