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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -22
app.py CHANGED
@@ -5,26 +5,33 @@ import numpy as np
5
  import cv2
6
 
7
  # ----------------------------
8
- # Model Hugging Face AI detector (public)
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("Hugging Face model gagal dimuat, hanya analisis lokal yang dipakai:", e)
 
 
 
 
 
 
 
15
 
16
  # ----------------------------
17
- # Analisis gambar lokal
18
  # ----------------------------
19
  def calculate_blur(image):
20
  gray = np.array(image.convert("L"))
21
  return cv2.Laplacian(gray, cv2.CV_64F).var()
22
 
23
  def calculate_noise(image):
24
- img_gray = np.array(image.convert("L"), dtype=np.float32)
25
- h, w = img_gray.shape
26
- mean = np.mean(img_gray)
27
- noise_std = np.std(img_gray - mean)
28
  return noise_std
29
 
30
  def has_camera_exif(image):
@@ -40,47 +47,58 @@ def has_camera_exif(image):
40
  return False
41
 
42
  # ----------------------------
43
- # Deteksi hybrid AI vs Foto Asli
44
  # ----------------------------
45
  def detect_image(image):
46
  scores = []
47
 
48
- # ---- HUGGING FACE ----
49
  if hf_detector:
50
  try:
51
  result = hf_detector(image)
52
  label = result[0]['label'].lower()
53
  conf = result[0]['score'] * 100
54
  if "fake" in label or "ai" in label or "artificial" in label:
55
- scores.append(conf) # AI score
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  else:
57
- scores.append(0) # Foto asli = 0 AI score
58
  except:
59
  scores.append(0)
60
  else:
61
  scores.append(0)
62
 
63
- # ---- ANALISIS LOKAL ----
64
  blur_score = calculate_blur(image)
65
  noise_score = calculate_noise(image)
66
  exif_present = has_camera_exif(image)
67
 
68
- # Blur & noise heuristics
69
- # Foto asli: blur tinggi (>100) atau noise > threshold
70
- # AI: blur rendah atau noise sangat rendah
71
  local_ai_score = 0
72
  if blur_score < 100 or noise_score < 10:
73
- local_ai_score += 50 # menambah skor AI
74
  if not exif_present:
75
- local_ai_score += 10 # sedikit penalti jika metadata hilang
76
-
77
  scores.append(local_ai_score)
78
 
79
- # ---- Gabungkan skor ----
80
  avg_score = sum(scores) / len(scores)
81
 
82
- # ---- Tentukan hasil final ----
83
- if avg_score > 50:
84
  final_result = "🤖 AI Detected"
85
  else:
86
  final_result = "✅ Foto Asli"
@@ -90,6 +108,8 @@ def detect_image(image):
90
  {final_result}
91
 
92
  **Skor rata-rata AI:** {avg_score:.2f}
 
 
93
  **Blur Score:** {blur_score:.2f}
94
  **Noise Score:** {noise_score:.2f}
95
  **Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
@@ -103,7 +123,7 @@ iface = gr.Interface(
103
  fn=detect_image,
104
  inputs=gr.Image(type="pil"),
105
  outputs="markdown",
106
- title="AI vs Foto Asli Detector (Hybrid, Gratis)",
107
  description="Unggah gambar, sistem hybrid akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI."
108
  )
109
 
 
5
  import cv2
6
 
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:
22
+ general_model = None
23
+ print("General classifier gagal dimuat:", e)
24
 
25
  # ----------------------------
26
+ # ANALISIS LOKAL
27
  # ----------------------------
28
  def calculate_blur(image):
29
  gray = np.array(image.convert("L"))
30
  return cv2.Laplacian(gray, cv2.CV_64F).var()
31
 
32
  def calculate_noise(image):
33
+ gray = np.array(image.convert("L"), dtype=np.float32)
34
+ noise_std = np.std(gray - np.mean(gray))
 
 
35
  return noise_std
36
 
37
  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:
57
  try:
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:
72
+ try:
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"
 
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
  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