MetaQu commited on
Commit
ad63a0f
·
verified ·
1 Parent(s): 5827e30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,39 +1,43 @@
 
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image
4
 
5
- # Model publik (tidak perlu token)
6
- model1 = pipeline("image-classification", model="umm-maybe/ai-image-detector")
7
- model2 = pipeline("image-classification", model="google/vit-base-patch16-224")
 
8
 
9
- def detect_ai(image):
10
- img = image.convert("RGB").resize((224, 224))
 
11
 
12
- res1 = model1(img)[0]
13
- res2 = model2(img)[0]
 
14
 
15
- label1, conf1 = res1['label'], res1['score']
16
- label2, conf2 = res2['label'], res2['score']
17
-
18
- # Voting sederhana: kalau model1 bilang FAKE, lebih dipercaya
19
- if "fake" in label1.lower() or "ai" in label1.lower():
20
- verdict = "🚨 Kemungkinan besar AI Generated"
21
- elif "real" in label1.lower():
22
- verdict = "✅ Kemungkinan besar Foto Asli"
23
  else:
24
- verdict = "⚠️ Tidak Pasti (cek manual)"
25
-
26
- return f"""{verdict}
27
 
28
- Model AI-detector: {label1} ({conf1*100:.2f}%)
29
- Model General (ViT): {label2} ({conf2*100:.2f}%)"""
 
 
 
30
 
 
31
  demo = gr.Interface(
32
- fn=detect_ai,
33
  inputs=gr.Image(type="pil"),
34
- outputs="text",
35
- title="Deteksi Foto AI vs Asli (Gratis)",
36
- description="Menggunakan model publik gratis Hugging Face."
 
37
  )
38
 
39
- demo.launch()
 
 
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()