MetaQu commited on
Commit
f034887
Β·
verified Β·
1 Parent(s): 992825b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -18
app.py CHANGED
@@ -1,19 +1,32 @@
1
  import gradio as gr
2
- from PIL import Image, ImageStat, ExifTags
 
3
  import numpy as np
4
  import cv2
5
 
6
  # ----------------------------
7
- # Fungsi hitung sharpness / blur
8
  # ----------------------------
9
- def calculate_blur(image):
10
- image_cv = np.array(image.convert("L")) # konversi ke grayscale
11
- laplacian_var = cv2.Laplacian(image_cv, cv2.CV_64F).var()
12
- return laplacian_var
 
13
 
14
  # ----------------------------
15
- # Fungsi deteksi metadata
16
  # ----------------------------
 
 
 
 
 
 
 
 
 
 
 
17
  def has_camera_exif(image):
18
  try:
19
  exif = image._getexif()
@@ -27,25 +40,58 @@ def has_camera_exif(image):
27
  return False
28
 
29
  # ----------------------------
30
- # Fungsi prediksi AI vs Real
31
  # ----------------------------
32
  def detect_image(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  blur_score = calculate_blur(image)
 
34
  exif_present = has_camera_exif(image)
35
-
36
- # Threshold empiris (bisa disesuaikan)
37
- # blur rendah + metadata β†’ Foto Asli
38
- # blur tinggi + tidak ada metadata β†’ AI
39
- if exif_present or blur_score > 100:
40
- final_result = f"βœ… Foto Asli"
 
 
 
 
 
 
 
 
 
 
 
 
41
  else:
42
- final_result = f"πŸ€– AI Detected"
43
-
44
  output = f"""
45
  ### Hasil Deteksi:
46
  {final_result}
47
 
 
48
  **Blur Score:** {blur_score:.2f}
 
49
  **Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
50
  """
51
  return output
@@ -57,8 +103,8 @@ iface = gr.Interface(
57
  fn=detect_image,
58
  inputs=gr.Image(type="pil"),
59
  outputs="markdown",
60
- title="AI vs Foto Asli Detector (Gratis)",
61
- description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI berdasarkan analisis blur dan metadata."
62
  )
63
 
64
  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
  # ----------------------------
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):
31
  try:
32
  exif = image._getexif()
 
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"
87
+
88
  output = f"""
89
  ### Hasil Deteksi:
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'}
96
  """
97
  return output
 
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
 
110
  if __name__ == "__main__":