MetaQu commited on
Commit
39cfaa0
·
verified ·
1 Parent(s): 7d282ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -13
app.py CHANGED
@@ -1,39 +1,107 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image
 
 
4
 
5
- # Model utama untuk deteksi real vs AI
6
  detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
7
 
8
- # Model tambahan general classifier (backup)
9
  general = pipeline("image-classification", model="google/vit-base-patch16-224")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def detect_image(img):
12
  try:
13
- # Prediksi dengan AI detector
14
  result1 = detector(img)
15
  label1 = result1[0]['label']
16
  conf1 = round(result1[0]['score'] * 100, 2)
17
 
18
- # Prediksi dengan model general (untuk cek ganda)
19
  result2 = general(img)
20
  label2 = result2[0]['label']
21
  conf2 = round(result2[0]['score'] * 100, 2)
22
 
23
- # Logika sederhana untuk memutuskan hasil
 
 
 
 
 
 
 
24
  if "fake" in label1.lower() or "artificial" in label1.lower():
25
- final = f"⚠️ Kemungkinan Besar AI Generated ({conf1}%)"
26
- elif "real" in label1.lower():
27
- final = f"✅ Kemungkinan Besar Foto Asli ({conf1}%)"
 
 
 
 
28
  else:
29
- final = f"⚠️ Tidak Pasti (cek manual)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  output = f"""
32
  ### Hasil Deteksi:
33
  {final}
34
 
35
- **Model AI-detector:** {label1} ({conf1}%)
 
 
 
36
  **Model General (ViT):** {label2} ({conf2}%)
 
 
 
 
 
 
 
37
  """
38
  return output
39
  except Exception as e:
@@ -44,8 +112,8 @@ iface = gr.Interface(
44
  fn=detect_image,
45
  inputs=gr.Image(type="pil"),
46
  outputs="markdown",
47
- title="AI vs Real Image Detector",
48
- description="Upload foto untuk mendeteksi apakah gambar kemungkinan besar asli atau hasil AI."
49
  )
50
 
51
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from PIL import Image, ExifTags
4
+ import numpy as np
5
+ import cv2
6
 
7
+ # Model utama AI-detector
8
  detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
9
 
10
+ # Model backup (general classifier)
11
  general = pipeline("image-classification", model="google/vit-base-patch16-224")
12
 
13
+ def analyze_image(img_pil):
14
+ img = np.array(img_pil)
15
+
16
+ # Blur detection (variance of Laplacian)
17
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
18
+ blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()
19
+
20
+ # Noise estimation (std deviation)
21
+ noise_score = np.std(gray)
22
+
23
+ # Edge consistency
24
+ edges = cv2.Canny(gray, 100, 200)
25
+ edge_score = np.std(edges)
26
+
27
+ # JPEG artifacts (kasar: std dev dari DCT block)
28
+ dct = cv2.dct(np.float32(gray) / 255.0)
29
+ jpeg_artifact = np.std(dct)
30
+
31
+ # Metadata kamera
32
+ metadata = "Tidak Ada"
33
+ try:
34
+ exif_data = img_pil._getexif()
35
+ if exif_data:
36
+ metadata = "Ada"
37
+ except:
38
+ pass
39
+
40
+ return blur_score, noise_score, edge_score, jpeg_artifact, metadata
41
+
42
  def detect_image(img):
43
  try:
44
+ # Prediksi AI-detector
45
  result1 = detector(img)
46
  label1 = result1[0]['label']
47
  conf1 = round(result1[0]['score'] * 100, 2)
48
 
49
+ # Prediksi model general
50
  result2 = general(img)
51
  label2 = result2[0]['label']
52
  conf2 = round(result2[0]['score'] * 100, 2)
53
 
54
+ # Analisis teknis
55
+ blur, noise, edge, jpeg_art, metadata = analyze_image(img)
56
+
57
+ # --- Voting System ---
58
+ ai_score = 0
59
+ real_score = 0
60
+
61
+ # Dari model AI-detector
62
  if "fake" in label1.lower() or "artificial" in label1.lower():
63
+ ai_score += conf1
64
+ elif "real" in label1.lower() or "human" in label1.lower():
65
+ real_score += conf1
66
+
67
+ # Dari metadata
68
+ if metadata == "Ada":
69
+ real_score += 30 # bobot ekstra untuk foto asli
70
  else:
71
+ ai_score += 20
72
+
73
+ # Dari noise & blur
74
+ if noise > 20 and blur > 50:
75
+ real_score += 20
76
+ else:
77
+ ai_score += 10
78
+
79
+ # Hasil akhir
80
+ total = ai_score + real_score + 1e-6
81
+ ai_pct = round((ai_score / total) * 100, 2)
82
+ real_pct = round((real_score / total) * 100, 2)
83
+
84
+ if ai_pct > real_pct:
85
+ final = f"⚠️ Kemungkinan Besar AI Generated ({ai_pct}%)"
86
+ else:
87
+ final = f"✅ Kemungkinan Besar Foto Asli ({real_pct}%)"
88
 
89
  output = f"""
90
  ### Hasil Deteksi:
91
  {final}
92
 
93
+ **Persentase AI:** {ai_pct}%
94
+ **Persentase Asli:** {real_pct}%
95
+
96
+ **Model AI-detector:** {label1} ({conf1}%)
97
  **Model General (ViT):** {label2} ({conf2}%)
98
+
99
+ **Analisis Kamera & Teknis:**
100
+ - Blur Score: {round(blur,2)}
101
+ - Noise Score: {round(noise,2)}
102
+ - Edge Consistency (STD): {round(edge,2)}
103
+ - JPEG Artifact Level: {round(jpeg_art,2)}
104
+ - Metadata Kamera: {metadata}
105
  """
106
  return output
107
  except Exception as e:
 
112
  fn=detect_image,
113
  inputs=gr.Image(type="pil"),
114
  outputs="markdown",
115
+ title="AI vs Real Image Detector (Hybrid)",
116
+ description="Deteksi AI vs Foto Asli dengan kombinasi model + analisis teknis (blur, noise, edge, metadata)."
117
  )
118
 
119
  if __name__ == "__main__":