Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,43 @@
|
|
|
|
|
| 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 |
-
verdict = "🚨 Kemungkinan besar AI Generated"
|
| 21 |
-
elif "real" in label1.lower():
|
| 22 |
-
verdict = "✅ Kemungkinan besar Foto Asli"
|
| 23 |
else:
|
| 24 |
-
verdict = "
|
| 25 |
-
|
| 26 |
-
return f"""{verdict}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
| 31 |
demo = gr.Interface(
|
| 32 |
-
fn=
|
| 33 |
inputs=gr.Image(type="pil"),
|
| 34 |
-
outputs="
|
| 35 |
-
title="
|
| 36 |
-
description="
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
| 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()
|