MetaQu commited on
Commit
b735378
Β·
verified Β·
1 Parent(s): 31daa87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -26
app.py CHANGED
@@ -44,30 +44,45 @@ def has_camera_exif(image):
44
  return False
45
 
46
  # ----------------------------
47
- # DETEKSI HYBRID WEIGHTED
48
  # ----------------------------
49
  def detect_image(image):
50
- hf_score = 0
51
- general_score = 0
52
- local_score = 0
53
 
54
  # -------- HF AI-detector --------
 
 
 
55
  if hf_detector:
56
  try:
57
  result = hf_detector(image)
58
- label = result[0]['label'].lower()
59
- conf = result[0]['score'] * 100
60
- hf_score = conf if any(x in label for x in ["fake", "ai", "artificial"]) else 0
 
 
61
  except:
62
  hf_score = 0
63
 
 
 
 
 
 
 
 
 
64
  # -------- General model --------
 
 
 
65
  if general_model:
66
  try:
67
  result2 = general_model(image)
68
- label2 = result2[0]['label'].lower()
69
- conf2 = result2[0]['score'] * 100
70
- general_score = conf2 if any(x in label2 for x in ["anime","cartoon","illustration","maya"]) else 0
 
71
  except:
72
  general_score = 0
73
 
@@ -81,26 +96,24 @@ def detect_image(image):
81
  if not exif_present:
82
  local_score += 10
83
 
84
- # -------- Weighted Score --------
85
- final_score = hf_score*0.6 + general_score*0.25 + local_score*0.15
86
 
87
- if final_score > 50:
88
  final_result = "πŸ€– AI Detected"
89
  else:
90
  final_result = "βœ… Foto Asli"
91
 
92
- output = f"""
93
- ### Hasil Deteksi:
94
- {final_result}
 
 
 
 
 
95
 
96
- **Weighted Skor:** {final_score:.2f}
97
- **HF AI-detector:** {result[0]['label']} ({result[0]['score']*100:.2f}%)
98
- **General Model:** {result2[0]['label']} ({result2[0]['score']*100:.2f}%)
99
- **Blur Score:** {blur_score:.2f}
100
- **Noise Score:** {noise_score:.2f}
101
- **Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
102
- """
103
- return output
104
 
105
  # ----------------------------
106
  # Gradio Interface
@@ -109,8 +122,8 @@ iface = gr.Interface(
109
  fn=detect_image,
110
  inputs=gr.Image(type="pil"),
111
  outputs="markdown",
112
- title="AI vs Foto Asli Detector (Weighted Hybrid)",
113
- description="Unggah gambar, sistem hybrid akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI."
114
  )
115
 
116
  if __name__ == "__main__":
 
44
  return False
45
 
46
  # ----------------------------
47
+ # DETEKSI HYBRID HF PRIORITAS
48
  # ----------------------------
49
  def detect_image(image):
50
+ output_lines = []
 
 
51
 
52
  # -------- HF AI-detector --------
53
+ hf_score = 0
54
+ hf_label = "N/A"
55
+ hf_conf = 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
+ # tentukan HF score AI relevan
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
+ # Jika HF confidence tinggi β†’ langsung AI
68
+ if hf_score > 60:
69
+ final_result = "πŸ€– AI Detected"
70
+ weighted_score = hf_score
71
+ output_lines.append(f"### Hasil Deteksi:\n{final_result}")
72
+ output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
73
+ return "\n".join(output_lines)
74
+
75
  # -------- General model --------
76
+ general_score = 0
77
+ general_label = "N/A"
78
+ general_conf = 0
79
  if general_model:
80
  try:
81
  result2 = general_model(image)
82
+ general_label = result2[0]['label']
83
+ general_conf = result2[0]['score'] * 100
84
+ if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya"]):
85
+ general_score = general_conf
86
  except:
87
  general_score = 0
88
 
 
96
  if not exif_present:
97
  local_score += 10
98
 
99
+ # -------- Weighted Score (general + local) --------
100
+ weighted_score = general_score*0.6 + local_score*0.4
101
 
102
+ if weighted_score > 50:
103
  final_result = "πŸ€– AI Detected"
104
  else:
105
  final_result = "βœ… Foto Asli"
106
 
107
+ # -------- Output --------
108
+ output_lines.append(f"### Hasil Deteksi:\n{final_result}")
109
+ output_lines.append(f"Weighted Skor: {weighted_score:.2f}")
110
+ output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
111
+ output_lines.append(f"General Model: {general_label} ({general_conf:.2f}%)")
112
+ output_lines.append(f"Blur Score: {blur_score:.2f}")
113
+ output_lines.append(f"Noise Score: {noise_score:.2f}")
114
+ output_lines.append(f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}")
115
 
116
+ return "\n".join(output_lines)
 
 
 
 
 
 
 
117
 
118
  # ----------------------------
119
  # Gradio Interface
 
122
  fn=detect_image,
123
  inputs=gr.Image(type="pil"),
124
  outputs="markdown",
125
+ title="AI vs Foto Asli Detector (HF Prioritas)",
126
+ description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI."
127
  )
128
 
129
  if __name__ == "__main__":