MetaQu commited on
Commit
19e6f0c
·
verified ·
1 Parent(s): de685c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -1,21 +1,52 @@
1
  import gradio as gr
2
- from PIL import Image
3
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Muat model deteksi AI
6
- classifier = pipeline("image-classification", model="umm-maybe/AI-image-detector")
7
-
8
- def detect_image(image: Image.Image):
9
- result = classifier(image)
10
- ai_score = result[0]['score'] if result[0]['label'] == 'AI-generated' else 0
11
- human_score = 1 - ai_score
12
- if ai_score == 1:
13
- return f"Gambar ini 100% dihasilkan oleh AI."
14
- elif human_score == 1:
15
- return f"Gambar ini 100% asli."
16
- else:
17
- return f"Gambar ini {ai_score*100:.2f}% dihasilkan oleh AI dan {human_score*100:.2f}% asli."
18
-
19
- # Antarmuka Gradio
20
- iface = gr.Interface(fn=detect_image, inputs=gr.Image(type="pil"), outputs="text")
21
- 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()