MetaQu commited on
Commit
3898bcc
ยท
verified ยท
1 Parent(s): f8addb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -66
app.py CHANGED
@@ -1,85 +1,65 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
- import cv2
5
- import numpy as np
6
 
7
- # =========================
8
- # Preprocessing function
9
- # =========================
10
- def preprocess_image(img_path):
11
- img = cv2.imread(img_path)
12
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
13
- img = cv2.resize(img, (512, 512))
14
- img = cv2.GaussianBlur(img, (3, 3), 0) # Kurangi noise
15
- return Image.fromarray(img)
16
 
17
- # =========================
18
- # Load models
19
- # =========================
20
- detector_ai = pipeline("image-classification", model="umm-maybe/AI-image-detector")
21
- detector_resnet = pipeline("image-classification", model="microsoft/resnet-50")
22
 
23
- # =========================
24
- # Detection Function
25
- # =========================
26
- def detect_image(img):
27
- # Preprocess
28
- img = preprocess_image(img)
29
 
30
- # Run AI detector
31
- ai_results = detector_ai(img)
32
- ai_dict = {res['label']: res['score'] for res in ai_results}
33
 
34
- human_score = ai_dict.get("human", 0)
35
- artificial_score = ai_dict.get("artificial", 0)
 
36
 
37
- # Run ResNet for natural object cross-check
38
- resnet_results = detector_resnet(img)
39
- top_resnet = resnet_results[0] # ambil label dengan skor tertinggi
 
 
 
 
 
 
 
40
 
41
- # =========================
42
- # Decision Rule with Threshold
43
- # =========================
44
- if artificial_score > 0.75:
45
- verdict = "๐ŸŸฃ AI-generated"
46
- elif human_score > 0.65:
47
- verdict = "๐ŸŸข Foto Asli"
48
- else:
49
- verdict = "โš ๏ธ Tidak Pasti"
50
-
51
- # Extra check: kalau AI-detector bilang AI tapi ResNet yakin objek nyata
52
- if verdict == "๐ŸŸฃ AI-generated" and top_resnet['score'] > 0.70:
53
- verdict = "โš ๏ธ Tidak Pasti (deteksi objek nyata tinggi)"
54
 
55
- # =========================
56
- # Output
57
- # =========================
58
- summary = f"""
59
- ๐Ÿ“Š Ringkasan Deteksi:
60
 
61
- ๐Ÿ”น AI Detector:
62
- - Human: {human_score:.2%}
63
- - Artificial: {artificial_score:.2%}
 
64
 
65
- ๐Ÿ”น ResNet50:
66
- - Top Label: {top_resnet['label']} ({top_resnet['score']:.2%})
67
 
68
- === HASIL AKHIR ===
69
- {verdict}
70
- """
71
- return summary
72
 
73
- # =========================
74
- # Gradio App
75
- # =========================
76
- demo = gr.Interface(
77
  fn=detect_image,
78
- inputs=gr.Image(type="filepath"),
79
- outputs="text",
80
- title="Deteksi AI vs Foto Asli",
81
- description="Upload foto untuk mendeteksi apakah gambar AI-generated atau Foto Asli. Menggunakan threshold + preprocessing."
82
  )
83
 
84
  if __name__ == "__main__":
85
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
 
 
4
 
5
+ # === Load 3 model publik khusus AI vs Real ===
6
+ model1 = pipeline("image-classification", model="prithivMLmods/AI-vs-Deepfake-vs-Real")
7
+ model2 = pipeline("image-classification", model="dima806/ai_vs_real_image_detection")
8
+ model3 = pipeline("image-classification", model="Hemg/AI-VS-REAL-IMAGE-DETECTION")
 
 
 
 
 
9
 
10
+ def detect_image(img: Image.Image):
11
+ results = {}
 
 
 
12
 
13
+ # Model 1
14
+ res1 = model1(img)
15
+ results["Model 1 (prithivMLmods)"] = res1
 
 
 
16
 
17
+ # Model 2
18
+ res2 = model2(img)
19
+ results["Model 2 (dima806)"] = res2
20
 
21
+ # Model 3
22
+ res3 = model3(img)
23
+ results["Model 3 (Hemg)"] = res3
24
 
25
+ # --- Ensemble sederhana ---
26
+ ai_score, real_score = 0, 0
27
+ for res in [res1, res2, res3]:
28
+ top = res[0]
29
+ label = top["label"].lower()
30
+ score = top["score"]
31
+ if "real" in label:
32
+ real_score += score
33
+ else:
34
+ ai_score += score
35
 
36
+ # Normalisasi
37
+ total = ai_score + real_score + 1e-9
38
+ ai_percent = (ai_score / total) * 100
39
+ real_percent = (real_score / total) * 100
 
 
 
 
 
 
 
 
 
40
 
41
+ # Keputusan akhir
42
+ if real_percent > ai_percent:
43
+ verdict = f"โœ… Foto Asli ({real_percent:.2f}%)"
44
+ else:
45
+ verdict = f"โš ๏ธ AI Generated ({ai_percent:.2f}%)"
46
 
47
+ # Format output
48
+ output = f"## ๐Ÿ“Š Ringkasan Deteksi\n"
49
+ for name, res in results.items():
50
+ output += f"\n๐Ÿ”น **{name}**: {res}\n"
51
 
52
+ output += f"\n=== ๐Ÿง  ENSEMBLE HASIL AKHIR ===\n{verdict}\n"
 
53
 
54
+ return output
 
 
 
55
 
56
+ iface = gr.Interface(
 
 
 
57
  fn=detect_image,
58
+ inputs=gr.Image(type="pil"),
59
+ outputs="markdown",
60
+ title="Deteksi AI vs Foto Asli (Ensemble 3 Model Publik)",
61
+ description="Menggunakan 3 model publik Hugging Face (prithivMLmods, dima806, Hemg) untuk mendeteksi apakah gambar AI-generated atau asli."
62
  )
63
 
64
  if __name__ == "__main__":
65
+ iface.launch()