MetaQu commited on
Commit
31daa87
·
verified ·
1 Parent(s): 505b320

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -7,15 +7,12 @@ import cv2
7
  # ----------------------------
8
  # MODEL
9
  # ----------------------------
10
-
11
- # Model AI-detector (Hugging Face, public)
12
  try:
13
  hf_detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
14
  except Exception as e:
15
  hf_detector = None
16
  print("HF AI-detector gagal dimuat:", e)
17
 
18
- # Model general classifier (backup)
19
  try:
20
  general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
21
  except Exception as e:
@@ -47,10 +44,12 @@ def has_camera_exif(image):
47
  return False
48
 
49
  # ----------------------------
50
- # DETEKSI HYBRID
51
  # ----------------------------
52
  def detect_image(image):
53
- scores = []
 
 
54
 
55
  # -------- HF AI-detector --------
56
  if hf_detector:
@@ -58,14 +57,9 @@ def detect_image(image):
58
  result = hf_detector(image)
59
  label = result[0]['label'].lower()
60
  conf = result[0]['score'] * 100
61
- if "fake" in label or "ai" in label or "artificial" in label:
62
- scores.append(conf) # skor AI
63
- else:
64
- scores.append(0)
65
  except:
66
- scores.append(0)
67
- else:
68
- scores.append(0)
69
 
70
  # -------- General model --------
71
  if general_model:
@@ -73,32 +67,24 @@ def detect_image(image):
73
  result2 = general_model(image)
74
  label2 = result2[0]['label'].lower()
75
  conf2 = result2[0]['score'] * 100
76
- # Logika sederhana: label aneh / tidak realistis dianggap AI
77
- if any(x in label2 for x in ["abaya", "cartoon", "anime", "illustration"]):
78
- scores.append(conf2)
79
- else:
80
- scores.append(0)
81
  except:
82
- scores.append(0)
83
- else:
84
- scores.append(0)
85
 
86
  # -------- Analisis lokal --------
87
  blur_score = calculate_blur(image)
88
  noise_score = calculate_noise(image)
89
  exif_present = has_camera_exif(image)
90
-
91
- local_ai_score = 0
92
  if blur_score < 100 or noise_score < 10:
93
- local_ai_score += 50
94
  if not exif_present:
95
- local_ai_score += 10
96
- scores.append(local_ai_score)
97
 
98
- # -------- Rata-rata skor & threshold --------
99
- avg_score = sum(scores) / len(scores)
100
 
101
- if avg_score > 60:
102
  final_result = "🤖 AI Detected"
103
  else:
104
  final_result = "✅ Foto Asli"
@@ -107,9 +93,9 @@ def detect_image(image):
107
  ### Hasil Deteksi:
108
  {final_result}
109
 
110
- **Skor rata-rata AI:** {avg_score:.2f}
111
- **Model AI-detector:** {result[0]['label']} ({result[0]['score']*100:.2f}%)
112
- **Model General:** {result2[0]['label']} ({result2[0]['score']*100:.2f}%)
113
  **Blur Score:** {blur_score:.2f}
114
  **Noise Score:** {noise_score:.2f}
115
  **Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
@@ -123,7 +109,7 @@ iface = gr.Interface(
123
  fn=detect_image,
124
  inputs=gr.Image(type="pil"),
125
  outputs="markdown",
126
- title="AI vs Foto Asli Detector (Hybrid + Voting)",
127
  description="Unggah gambar, sistem hybrid akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI."
128
  )
129
 
 
7
  # ----------------------------
8
  # MODEL
9
  # ----------------------------
 
 
10
  try:
11
  hf_detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
12
  except Exception as e:
13
  hf_detector = None
14
  print("HF AI-detector gagal dimuat:", e)
15
 
 
16
  try:
17
  general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
18
  except Exception as e:
 
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:
 
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:
 
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
 
74
  # -------- Analisis lokal --------
75
  blur_score = calculate_blur(image)
76
  noise_score = calculate_noise(image)
77
  exif_present = has_camera_exif(image)
78
+ local_score = 0
 
79
  if blur_score < 100 or noise_score < 10:
80
+ local_score += 50
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"
 
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'}
 
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