Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# ----------------------------
|
| 44 |
-
# Gradio Interface
|
| 45 |
-
# ----------------------------
|
| 46 |
iface = gr.Interface(
|
| 47 |
fn=detect_image,
|
| 48 |
inputs=gr.Image(type="pil"),
|
| 49 |
-
outputs=
|
| 50 |
-
title="AI vs
|
| 51 |
-
description="
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
| 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()
|