MetaQu commited on
Commit
c03cdfb
·
verified ·
1 Parent(s): 469f532

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
app.py CHANGED
@@ -1,43 +1,52 @@
1
- # app.py
2
  import gradio as gr
3
  from transformers import pipeline
 
4
 
5
- # Muat pipeline deteksi AI-generated image (bisa ganti model sesuai kebutuhan)
6
- # Model gratis: "microsoft/resnet-50" hanya sebagai contoh,
7
- # lebih baik gunakan model yang memang fine-tuned untuk AI vs Real
8
- detector = pipeline("image-classification", model="falconsai/nsfw_image_detection")
9
-
10
- # Fungsi untuk memproses input gambar
11
- def detect_ai_image(image):
12
- results = detector(image)
13
-
14
- # Ambil prediksi dengan skor tertinggi
15
- label = results[0]['label']
16
- score = results[0]['score']
17
-
18
- # Tentukan kategori berdasarkan threshold
19
- if score > 0.85 and label.lower() in ["artificial", "ai", "fake"]:
20
- verdict = "⚠️ Pasti AI-Generated"
21
- elif score < 0.15 and label.lower() in ["real", "photo", "natural"]:
22
- verdict = "✅ Pasti Foto Asli"
23
- else:
24
- verdict = " Tidak Pasti (perlu cek manual)"
25
-
26
- return {
27
- "Prediksi Label": label,
28
- "Tingkat Keyakinan": f"{score*100:.2f}%",
29
- "Kesimpulan": verdict
30
- }
 
 
 
 
 
 
 
 
 
 
31
 
32
  # UI Gradio
33
- demo = gr.Interface(
34
- fn=detect_ai_image,
35
  inputs=gr.Image(type="pil"),
36
- outputs="json",
37
  title="AI vs Real Image Detector",
38
- description="Unggah foto untuk mendeteksi apakah itu buatan AI atau foto asli. "
39
- "Gunakan threshold >85% untuk 'AI pasti' dan <15% untuk 'Asli pasti'."
40
  )
41
 
42
  if __name__ == "__main__":
43
- 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()