MetaQu commited on
Commit
958932b
·
verified ·
1 Parent(s): 72c9a0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -68
app.py CHANGED
@@ -1,75 +1,52 @@
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
- import io
7
-
8
- # Load HuggingFace pipeline
9
- detector = pipeline("image-classification", model="falconsai/nsfw_image_detection")
10
- # ganti model ke yang support binary classification real vs ai kalau mau training
11
-
12
- def analyze_image(image):
13
- # Konversi ke format OpenCV
14
- img_bytes = io.BytesIO()
15
- image.save(img_bytes, format="PNG")
16
- img_bytes = np.frombuffer(img_bytes.getvalue(), dtype=np.uint8)
17
- cv_img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
18
-
19
- # Blur score (laplacian variance)
20
- gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
21
- blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()
22
-
23
- # Noise score (std dev)
24
- noise_score = np.std(gray)
25
-
26
- # Metadata Kamera
27
- meta = image.getexif()
28
- meta_info = "Ada" if meta else "Tidak Ada"
29
-
30
- # Prediksi AI vs Asli (pakai HF pipeline dummy dulu)
31
- preds = detector(image)
32
- label = preds[0]["label"].lower()
33
- score = preds[0]["score"]
34
-
35
- # Normalisasi ke persentase AI vs Asli
36
- if "artificial" in label or "fake" in label or "ai" in label:
37
- ai_prob = score * 100
38
- else:
39
- ai_prob = (1 - score) * 100
40
- real_prob = 100 - ai_prob
41
-
42
- # Koreksi dengan metadata → jika metadata kamera ada, naikkan skor real
43
- if meta_info == "Ada":
44
- real_prob += 15
45
- ai_prob -= 15
46
-
47
- # Koreksi blur/noise → foto asli biasanya lebih natural
48
- if blur_score > 500 and noise_score > 20:
49
- real_prob += 5
50
-
51
- # Clamp ke 0-100
52
- ai_prob = max(0, min(100, ai_prob))
53
- real_prob = max(0, min(100, real_prob))
54
-
55
- # Output
56
- hasil = f"""🖼️ Hasil Deteksi:
57
- {ai_prob:.2f}% AI / {real_prob:.2f}% Asli
58
-
59
- Blur Score: {blur_score:.2f}
60
- Noise Score: {noise_score:.2f}
61
- Metadata Kamera: {meta_info}
62
- """
63
-
64
- return hasil
65
-
66
- demo = gr.Interface(
67
- fn=analyze_image,
68
  inputs=gr.Image(type="pil"),
69
- outputs="text",
70
  title="AI vs Real Image Detector",
71
- description="Deteksi apakah gambar AI-generated atau asli. Gratis, tanpa API berbayar."
72
  )
73
 
74
  if __name__ == "__main__":
75
- demo.launch()
 
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:
40
+ return f"Terjadi error: {str(e)}"
41
+
42
+ # UI Gradio
43
+ 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__":
52
+ iface.launch()