Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,85 @@
|
|
| 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 |
-
results["Model 3 (Hemg)"] = res3
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
top = res[0]
|
| 29 |
-
label = top["label"].lower()
|
| 30 |
-
score = top["score"]
|
| 31 |
-
if "real" in label:
|
| 32 |
-
real_score += score
|
| 33 |
-
else:
|
| 34 |
-
ai_score += score
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
verdict = f"โ
Foto Asli ({real_percent:.2f}%)"
|
| 44 |
else:
|
| 45 |
-
verdict =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
output += f"\n๐น **{name}**: {res}\n"
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
fn=detect_image,
|
| 58 |
-
inputs=gr.Image(type="
|
| 59 |
-
outputs="
|
| 60 |
-
title="Deteksi AI vs Foto Asli
|
| 61 |
-
description="
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
# =========================
|
| 8 |
+
# Preprocessing function
|
| 9 |
+
# =========================
|
| 10 |
+
def preprocess_image(img_path):
|
| 11 |
+
img = cv2.imread(img_path)
|
| 12 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 13 |
+
img = cv2.resize(img, (512, 512))
|
| 14 |
+
img = cv2.GaussianBlur(img, (3, 3), 0) # Kurangi noise
|
| 15 |
+
return Image.fromarray(img)
|
| 16 |
|
| 17 |
+
# =========================
|
| 18 |
+
# Load models
|
| 19 |
+
# =========================
|
| 20 |
+
detector_ai = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 21 |
+
detector_resnet = pipeline("image-classification", model="microsoft/resnet-50")
|
| 22 |
|
| 23 |
+
# =========================
|
| 24 |
+
# Detection Function
|
| 25 |
+
# =========================
|
| 26 |
+
def detect_image(img):
|
| 27 |
+
# Preprocess
|
| 28 |
+
img = preprocess_image(img)
|
| 29 |
|
| 30 |
+
# Run AI detector
|
| 31 |
+
ai_results = detector_ai(img)
|
| 32 |
+
ai_dict = {res['label']: res['score'] for res in ai_results}
|
| 33 |
|
| 34 |
+
human_score = ai_dict.get("human", 0)
|
| 35 |
+
artificial_score = ai_dict.get("artificial", 0)
|
|
|
|
| 36 |
|
| 37 |
+
# Run ResNet for natural object cross-check
|
| 38 |
+
resnet_results = detector_resnet(img)
|
| 39 |
+
top_resnet = resnet_results[0] # ambil label dengan skor tertinggi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# =========================
|
| 42 |
+
# Decision Rule with Threshold
|
| 43 |
+
# =========================
|
| 44 |
+
if artificial_score > 0.75:
|
| 45 |
+
verdict = "๐ฃ AI-generated"
|
| 46 |
+
elif human_score > 0.65:
|
| 47 |
+
verdict = "๐ข Foto Asli"
|
|
|
|
| 48 |
else:
|
| 49 |
+
verdict = "โ ๏ธ Tidak Pasti"
|
| 50 |
+
|
| 51 |
+
# Extra check: kalau AI-detector bilang AI tapi ResNet yakin objek nyata
|
| 52 |
+
if verdict == "๐ฃ AI-generated" and top_resnet['score'] > 0.70:
|
| 53 |
+
verdict = "โ ๏ธ Tidak Pasti (deteksi objek nyata tinggi)"
|
| 54 |
+
|
| 55 |
+
# =========================
|
| 56 |
+
# Output
|
| 57 |
+
# =========================
|
| 58 |
+
summary = f"""
|
| 59 |
+
๐ Ringkasan Deteksi:
|
| 60 |
|
| 61 |
+
๐น AI Detector:
|
| 62 |
+
- Human: {human_score:.2%}
|
| 63 |
+
- Artificial: {artificial_score:.2%}
|
|
|
|
| 64 |
|
| 65 |
+
๐น ResNet50:
|
| 66 |
+
- Top Label: {top_resnet['label']} ({top_resnet['score']:.2%})
|
| 67 |
|
| 68 |
+
=== HASIL AKHIR ===
|
| 69 |
+
{verdict}
|
| 70 |
+
"""
|
| 71 |
+
return summary
|
| 72 |
|
| 73 |
+
# =========================
|
| 74 |
+
# Gradio App
|
| 75 |
+
# =========================
|
| 76 |
+
demo = gr.Interface(
|
| 77 |
fn=detect_image,
|
| 78 |
+
inputs=gr.Image(type="filepath"),
|
| 79 |
+
outputs="text",
|
| 80 |
+
title="Deteksi AI vs Foto Asli",
|
| 81 |
+
description="Upload foto untuk mendeteksi apakah gambar AI-generated atau Foto Asli. Menggunakan threshold + preprocessing."
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|