MetaQu commited on
Commit
923a637
·
verified ·
1 Parent(s): 7543c59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -46
app.py CHANGED
@@ -1,54 +1,52 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
- import numpy as np
5
-
6
- # ----------------------------
7
- # Inisialisasi model publik
8
- # ----------------------------
9
- model_b = pipeline("image-classification", model="microsoft/resnet-50")
10
- model_c = pipeline("image-classification", model="google/vit-base-patch16-224")
11
-
12
- # ----------------------------
13
- # Fungsi deteksi dengan ensemble
14
- # ----------------------------
15
- def detect_image(image):
16
- results = []
17
-
18
- # Model B
19
- res_b = model_b(image)[0]
20
- label_b, score_b = res_b['label'].lower(), res_b['score']
21
- label_b_final = 'human' if 'person' in label_b or 'human' in label_b else 'ai'
22
- results.append((label_b_final, score_b))
23
-
24
- # Model C
25
- res_c = model_c(image)[0]
26
- label_c, score_c = res_c['label'].lower(), res_c['score']
27
- label_c_final = 'human' if 'person' in label_c or 'human' in label_c else 'ai'
28
- results.append((label_c_final, score_c))
29
-
30
- # Voting + threshold
31
- votes = [r[0] for r in results]
32
- final_label = max(set(votes), key=votes.count)
33
- relevant_scores = [s for (l, s) in results if l == final_label]
34
- avg_confidence = np.mean(relevant_scores) * 100
35
-
36
- if avg_confidence < 80:
37
- return "⚠️ Tidak Pasti (cek manual)", round(avg_confidence, 2)
38
- elif final_label == 'human':
39
- return f"Foto Asli ✅", round(avg_confidence, 2)
40
- else:
41
- return f"AI Detected 🤖", round(avg_confidence, 2)
42
-
43
- # ----------------------------
44
- # Gradio Interface
45
- # ----------------------------
46
  iface = gr.Interface(
47
  fn=detect_image,
48
  inputs=gr.Image(type="pil"),
49
- outputs=[gr.Textbox(label="Hasil Deteksi"), gr.Number(label="Confidence (%)")],
50
- title="AI vs Foto Asli Detector",
51
- description="Unggah gambar, sistem akan mendeteksi apakah gambar asli atau dihasilkan AI menggunakan ensemble 2 model publik + voting."
52
  )
53
 
54
- iface.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()