MetaQu commited on
Commit
af7913c
·
verified ·
1 Parent(s): bb086d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -62
app.py CHANGED
@@ -2,92 +2,99 @@ import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
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
- # Model tambahan general classifier
10
  general = pipeline("image-classification", model="google/vit-base-patch16-224")
11
 
12
- def variance_of_laplacian(image):
13
- # Menghitung blur menggunakan Laplacian
14
- return cv2.Laplacian(image, cv2.CV_64F).var()
15
-
16
- def estimate_noise(image):
17
- # Menghitung noise sederhana
18
- img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
19
- m = np.mean(img_gray)
20
- noise = np.mean((img_gray - m)**2)
 
 
 
 
21
  return noise
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def detect_image(img):
24
  try:
25
- # Convert PIL ke OpenCV format
26
- img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
27
-
28
- # Blur dan Noise
29
- blur_score = variance_of_laplacian(img_cv)
30
- noise_score = estimate_noise(img_cv)
31
-
32
- # Metadata kamera
33
- metadata_camera = img.info.get("exif") is not None
34
-
35
- # Prediksi AI
36
- result_ai = detector(img)
37
- label_ai = result_ai[0]['label'].lower()
38
- conf_ai = result_ai[0]['score'] * 100
39
-
40
- # Prediksi general
41
- result_gen = general(img)
42
- label_gen = result_gen[0]['label']
43
- conf_gen = result_gen[0]['score'] * 100
44
-
45
- # Hybrid scoring
46
- if "artificial" in label_ai or "fake" in label_ai:
47
- ai_score = max(conf_ai, 70)
48
- elif "human" in label_ai:
49
- ai_score = 100 - conf_ai * 0.7
50
  else:
51
- ai_score = 50
52
 
53
- # Tambah bobot metadata kamera
54
- if metadata_camera:
55
- ai_score -= 10
 
56
 
57
- # Penalti blur rendah untuk AI photorealistic
58
- if blur_score < 100:
59
- ai_score += 10 # foto asli biasanya lebih tajam
60
- # Penalti noise sangat rendah (AI sering terlalu bersih)
61
- if noise_score < 30:
62
- ai_score += 5
63
 
64
- # Clamp nilai
65
- ai_score = max(0, min(100, ai_score))
66
- real_score = 100 - ai_score
67
-
68
- # Output
69
  output = f"""
70
  ### Hasil Deteksi:
71
- 🖼️ Gambar ini {round(ai_score,2)}% AI / {round(real_score,2)}% Asli
72
 
73
- **Model AI-detector:** {result_ai[0]['label']} ({round(conf_ai,2)}%)
74
- **Model General (ViT):** {label_gen} ({round(conf_gen,2)}%)
75
- Blur Score: {round(blur_score,2)}
76
- Noise Score: {round(noise_score,2)}
77
- Metadata Kamera: {'Ada' if metadata_camera else 'Tidak'}
78
  """
79
  return output
80
-
81
  except Exception as e:
82
  return f"Terjadi error: {str(e)}"
83
 
84
- # Gradio interface
85
  iface = gr.Interface(
86
  fn=detect_image,
87
  inputs=gr.Image(type="pil"),
88
  outputs="markdown",
89
- title="AI vs Real Image Detector (Advanced Hybrid)",
90
- description="Gratis, mendeteksi AI photorealistic dengan blur & noise analisis, semua persentase."
91
  )
92
 
93
  if __name__ == "__main__":
 
2
  from transformers import pipeline
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
+ # Model utama
7
  detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
 
8
  general = pipeline("image-classification", model="google/vit-base-patch16-224")
9
 
10
+ # Estimasi blur & noise tanpa OpenCV
11
+ def estimate_blur(img):
12
+ gray = img.convert("L")
13
+ arr = np.array(gray)
14
+ gx, gy = np.gradient(arr)
15
+ laplacian_var = np.var(gx + gy)
16
+ return laplacian_var
17
+
18
+ def estimate_noise(img):
19
+ gray = img.convert("L")
20
+ arr = np.array(gray)
21
+ m = np.mean(arr)
22
+ noise = np.mean((arr - m) ** 2)
23
  return noise
24
 
25
+ # Pengecekan pola photorealistic AI
26
+ def photorealistic_ai_adjustment(img, ai_score):
27
+ # Cek metadata, blur, dan noise
28
+ has_metadata = bool(img.info)
29
+ blur = estimate_blur(img)
30
+ noise = estimate_noise(img)
31
+
32
+ # Logika sensitif:
33
+ # Foto asli biasanya ada metadata kamera, blur & noise tertentu
34
+ # Jika metadata kosong, blur sangat rendah, dan noise aneh => kemungkinan AI lebih tinggi
35
+ adjustment = 0
36
+ if not has_metadata:
37
+ adjustment += 10
38
+ if blur < 100: # sangat tajam
39
+ adjustment += 10
40
+ if noise < 50: # terlalu bersih
41
+ adjustment += 10
42
+
43
+ ai_score += adjustment
44
+ ai_score = min(ai_score, 100) # maksimal 100%
45
+ return ai_score
46
+
47
  def detect_image(img):
48
  try:
49
+ # Prediksi AI detector
50
+ result1 = detector(img)
51
+ label1 = result1[0]['label'].lower()
52
+ conf1 = round(result1[0]['score'] * 100, 2)
53
+
54
+ # Prediksi model general
55
+ result2 = general(img)
56
+ label2 = result2[0]['label']
57
+ conf2 = round(result2[0]['score'] * 100, 2)
58
+
59
+ # Hitung AI % awal
60
+ if "artificial" in label1 or "fake" in label1:
61
+ ai_percent = conf1
62
+ elif "human" in label1:
63
+ ai_percent = 100 - conf1
 
 
 
 
 
 
 
 
 
 
64
  else:
65
+ ai_percent = conf1
66
 
67
+ # Adjust untuk photorealistic AI
68
+ ai_percent = photorealistic_ai_adjustment(img, ai_percent)
69
+ ai_percent = round(ai_percent, 2)
70
+ real_percent = round(100 - ai_percent, 2)
71
 
72
+ # Blur & Noise untuk info
73
+ blur_score = round(estimate_blur(img), 2)
74
+ noise_score = round(estimate_noise(img), 2)
75
+ has_metadata = "Ada" if img.info else "Tidak"
 
 
76
 
 
 
 
 
 
77
  output = f"""
78
  ### Hasil Deteksi:
79
+ 🖼️ Gambar ini {ai_percent}% AI / {real_percent}% Asli
80
 
81
+ **Model AI-detector:** {label1} ({conf1}%)
82
+ **Model General (ViT):** {label2} ({conf2}%)
83
+ **Blur Score:** {blur_score}
84
+ **Noise Score:** {noise_score}
85
+ **Metadata Kamera:** {has_metadata}
86
  """
87
  return output
 
88
  except Exception as e:
89
  return f"Terjadi error: {str(e)}"
90
 
91
+ # UI Gradio
92
  iface = gr.Interface(
93
  fn=detect_image,
94
  inputs=gr.Image(type="pil"),
95
  outputs="markdown",
96
+ title="Hybrid AI vs Real Image Detector (Sensitif Photorealistic)",
97
+ description="Upload foto untuk mendeteksi kemungkinan besar gambar asli atau hasil AI. Lebih sensitif terhadap AI photorealistic. 100% gratis."
98
  )
99
 
100
  if __name__ == "__main__":