MetaQu commited on
Commit
7ad75f4
·
verified ·
1 Parent(s): f924686

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -40
app.py CHANGED
@@ -1,62 +1,80 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image
 
4
 
5
  # Model utama untuk deteksi AI
6
- detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
7
 
8
- # Model tambahan general (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'].lower()
16
- conf1 = result1[0]['score'] * 100
17
 
18
- # Prediksi dengan model general
19
  result2 = general(img)
20
- conf2 = result2[0]['score'] * 100
 
21
 
22
- # Kombinasi skor untuk hybrid
23
- # Logika: AI detector > 50% dianggap AI, <50% dianggap asli
24
- if "fake" in label1 or "artificial" in label1:
25
- ai_score = conf1
26
- real_score = 100 - ai_score
27
- elif "human" in label1 or "real" in label1:
28
- real_score = conf1
29
- ai_score = 100 - real_score
30
- else:
31
- # fallback jika label tidak spesifik
32
- ai_score = conf1 / 2
33
- real_score = 100 - ai_score
34
 
35
- # Sensitivitas tambahan: jika AI photorealistic tapi AI score rendah
36
- # gunakan hasil general model sebagai penyesuaian
37
- ai_score += conf2 * 0.1
38
- real_score = 100 - ai_score
39
 
40
- # Batasi 0-100
41
- ai_score = max(0, min(100, round(ai_score, 2)))
42
- real_score = max(0, min(100, round(real_score, 2)))
 
43
 
44
- if ai_score == 100:
45
- final = "🖼️ Gambar ini 100% Hasil AI"
46
- elif real_score == 100:
47
- final = "🖼️ Gambar ini 100% Asli"
48
- else:
49
- final = f"🖼️ Gambar ini {ai_score}% AI / {real_score}% Asli"
50
 
51
  output = f"""
52
  ### Hasil Deteksi:
53
- {final}
54
 
55
- **Model AI-detector:** {label1} ({round(conf1,2)}%)
56
- **Model General (ViT):** {result2[0]['label']} ({round(conf2,2)}%)
 
 
 
57
  """
58
  return output
59
-
60
  except Exception as e:
61
  return f"Terjadi error: {str(e)}"
62
 
@@ -64,8 +82,8 @@ iface = gr.Interface(
64
  fn=detect_image,
65
  inputs=gr.Image(type="pil"),
66
  outputs="markdown",
67
- title="AI vs Real Image Detector Hybrid",
68
- description="Upload foto untuk mendeteksi persentase kemungkinan AI vs Asli."
69
  )
70
 
71
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from PIL import Image, ExifTags, ImageStat, ImageFilter
4
+ import numpy as np
5
 
6
  # Model utama untuk deteksi AI
7
+ detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
8
 
9
+ # Model tambahan general classifier (backup)
10
  general = pipeline("image-classification", model="google/vit-base-patch16-224")
11
 
12
+ def analyze_noise(img):
13
+ gray = img.convert("L")
14
+ arr = np.array(gray)
15
+ return np.std(arr) # Standar deviasi → noise
16
+
17
+ def analyze_blur(img):
18
+ gray = img.convert("L")
19
+ arr = np.array(gray)
20
+ lap = cv2.Laplacian(arr, cv2.CV_64F).var()
21
+ return lap # Variansi Laplacian → blur
22
+
23
+ def metadata_score(img):
24
  try:
25
+ exif = img._getexif()
26
+ if exif is None:
27
+ return 1 # Tidak ada metadata → kemungkinan AI
28
+ for tag, value in exif.items():
29
+ decoded = ExifTags.TAGS.get(tag, tag)
30
+ if decoded == "Make" or decoded == "Model":
31
+ return 0 # Ada kamera → kemungkinan asli
32
+ return 1
33
+ except:
34
+ return 1
35
+
36
+ def detect_image(img: Image.Image):
37
+ try:
38
+ # Prediksi AI-detector
39
  result1 = detector(img)
40
+ label1 = result1[0]['label']
41
+ conf1 = result1[0]['score'] # 0-1
42
 
43
+ # Prediksi general model
44
  result2 = general(img)
45
+ label2 = result2[0]['label']
46
+ conf2 = result2[0]['score']
47
 
48
+ # Analisis blur & noise
49
+ noise = analyze_noise(img)
50
+ try:
51
+ import cv2
52
+ blur = analyze_blur(img)
53
+ except:
54
+ blur = 0
 
 
 
 
 
55
 
56
+ # Metadata
57
+ meta = metadata_score(img)
 
 
58
 
59
+ # Weighted skor akhir (lebih sensitif terhadap AI photorealistic)
60
+ ai_score = conf1 * 0.6 + meta * 0.2 + (1 - min(noise/100,1)) * 0.1 + (1 - min(blur/1000,1)) * 0.1
61
+ ai_score = min(max(ai_score, 0), 1) # Clamp 0-1
62
+ human_score = 1 - ai_score
63
 
64
+ ai_percent = round(ai_score * 100, 2)
65
+ human_percent = round(human_score * 100, 2)
 
 
 
 
66
 
67
  output = f"""
68
  ### Hasil Deteksi:
69
+ 🖼️ Gambar ini {ai_percent}% AI / {human_percent}% Asli
70
 
71
+ **Model AI-detector:** {label1} ({round(conf1*100,2)}%)
72
+ **Model General (ViT):** {label2} ({round(conf2*100,2)}%)
73
+ **Blur Score:** {round(blur,2)}
74
+ **Noise Score:** {round(noise,2)}
75
+ **Metadata Kamera:** {"Ada" if meta==0 else "Tidak Ada"}
76
  """
77
  return output
 
78
  except Exception as e:
79
  return f"Terjadi error: {str(e)}"
80
 
 
82
  fn=detect_image,
83
  inputs=gr.Image(type="pil"),
84
  outputs="markdown",
85
+ title="Hybrid AI vs Foto Asli Detector",
86
+ description="Upload foto untuk mendeteksi persentase AI vs foto asli. Lebih sensitif terhadap AI photorealistic."
87
  )
88
 
89
  if __name__ == "__main__":