Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,91 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from PIL import Image
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
# Model utama
|
| 7 |
detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 8 |
-
|
| 9 |
-
# Model tambahan general classifier (backup)
|
| 10 |
general = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
arr = np.array(gray)
|
| 15 |
-
return np.std(arr) # Standar deviasi → noise
|
| 16 |
-
|
| 17 |
-
def analyze_blur(img):
|
| 18 |
-
gray = img.convert("L")
|
| 19 |
-
arr = np.array(gray)
|
| 20 |
-
lap = cv2.Laplacian(arr, cv2.CV_64F).var()
|
| 21 |
-
return lap # Variansi Laplacian → blur
|
| 22 |
-
|
| 23 |
-
def metadata_score(img):
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
if
|
| 27 |
-
return
|
| 28 |
-
for
|
| 29 |
-
|
| 30 |
-
if
|
| 31 |
-
return
|
| 32 |
-
return
|
| 33 |
except:
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
def detect_image(img
|
| 37 |
try:
|
| 38 |
-
# Prediksi AI-detector
|
| 39 |
result1 = detector(img)
|
| 40 |
label1 = result1[0]['label']
|
| 41 |
-
conf1 = result1[0]['score']
|
| 42 |
|
| 43 |
-
# Prediksi
|
| 44 |
result2 = general(img)
|
| 45 |
label2 = result2[0]['label']
|
| 46 |
-
conf2 = result2[0]['score']
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
try:
|
| 51 |
-
import cv2
|
| 52 |
-
blur = analyze_blur(img)
|
| 53 |
-
except:
|
| 54 |
-
blur = 0
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
ai_score = min(
|
| 62 |
-
human_score =
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
output = f"""
|
| 68 |
### Hasil Deteksi:
|
| 69 |
-
|
| 70 |
|
| 71 |
-
**Model AI-detector:** {label1} ({
|
| 72 |
-
**Model General (ViT):** {label2} ({
|
| 73 |
-
**
|
| 74 |
-
**Noise Score:** {round(noise,2)}
|
| 75 |
-
**Metadata Kamera:** {"Ada" if meta==0 else "Tidak Ada"}
|
| 76 |
"""
|
| 77 |
return output
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Terjadi error: {str(e)}"
|
| 80 |
|
|
|
|
| 81 |
iface = gr.Interface(
|
| 82 |
fn=detect_image,
|
| 83 |
inputs=gr.Image(type="pil"),
|
| 84 |
outputs="markdown",
|
| 85 |
-
title="Hybrid AI vs
|
| 86 |
-
description="Upload
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from PIL.ExifTags import TAGS
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
+
# Model utama deteksi AI
|
| 8 |
detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 9 |
+
# Model general classifier (backup)
|
|
|
|
| 10 |
general = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 11 |
|
| 12 |
+
def get_exif_metadata(img):
|
| 13 |
+
"""Cek metadata kamera untuk membantu deteksi foto asli"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
exif_data = img._getexif()
|
| 16 |
+
if exif_data is None:
|
| 17 |
+
return False
|
| 18 |
+
for tag_id, value in exif_data.items():
|
| 19 |
+
tag = TAGS.get(tag_id, tag_id)
|
| 20 |
+
if tag in ["Make", "Model", "Software"]:
|
| 21 |
+
return True
|
| 22 |
+
return False
|
| 23 |
except:
|
| 24 |
+
return False
|
| 25 |
|
| 26 |
+
def detect_image(img):
|
| 27 |
try:
|
| 28 |
+
# Prediksi dengan AI-detector
|
| 29 |
result1 = detector(img)
|
| 30 |
label1 = result1[0]['label']
|
| 31 |
+
conf1 = result1[0]['score'] * 100
|
| 32 |
|
| 33 |
+
# Prediksi dengan model general
|
| 34 |
result2 = general(img)
|
| 35 |
label2 = result2[0]['label']
|
| 36 |
+
conf2 = result2[0]['score'] * 100
|
| 37 |
|
| 38 |
+
# Cek metadata kamera
|
| 39 |
+
has_metadata = get_exif_metadata(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Weighted hybrid scoring
|
| 42 |
+
# Skema: AI-detector 50%, Metadata 30%, general model 20%
|
| 43 |
+
ai_score = conf1 if "artificial" in label1.lower() or "fake" in label1.lower() else 0
|
| 44 |
+
human_score = conf1 if "human" in label1.lower() or "real" in label1.lower() else 0
|
| 45 |
|
| 46 |
+
if has_metadata:
|
| 47 |
+
human_score += 30 # boost jika metadata ada
|
| 48 |
+
ai_score = min(ai_score, 100)
|
| 49 |
+
human_score = min(human_score, 100)
|
| 50 |
|
| 51 |
+
# Normalisasi agar total = 100%
|
| 52 |
+
total = ai_score + human_score
|
| 53 |
+
if total == 0:
|
| 54 |
+
ai_percent = 50
|
| 55 |
+
human_percent = 50
|
| 56 |
+
else:
|
| 57 |
+
ai_percent = round((ai_score / total) * 100, 2)
|
| 58 |
+
human_percent = round((human_score / total) * 100, 2)
|
| 59 |
|
| 60 |
+
# Tentukan hasil akhir
|
| 61 |
+
if ai_percent == 100:
|
| 62 |
+
final_text = "🖼️ Gambar ini 100% AI"
|
| 63 |
+
elif human_percent == 100:
|
| 64 |
+
final_text = "🖼️ Gambar ini asli 100%"
|
| 65 |
+
else:
|
| 66 |
+
final_text = f"🖼️ Gambar ini {ai_percent}% AI / {human_percent}% Asli"
|
| 67 |
+
|
| 68 |
+
# Tambahkan info model
|
| 69 |
output = f"""
|
| 70 |
### Hasil Deteksi:
|
| 71 |
+
{final_text}
|
| 72 |
|
| 73 |
+
**Model AI-detector:** {label1} ({conf1:.2f}%)
|
| 74 |
+
**Model General (ViT):** {label2} ({conf2:.2f}%)
|
| 75 |
+
**Metadata Kamera Ada:** {"Ya" if has_metadata else "Tidak"}
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
return output
|
| 78 |
+
|
| 79 |
except Exception as e:
|
| 80 |
return f"Terjadi error: {str(e)}"
|
| 81 |
|
| 82 |
+
# UI Gradio
|
| 83 |
iface = gr.Interface(
|
| 84 |
fn=detect_image,
|
| 85 |
inputs=gr.Image(type="pil"),
|
| 86 |
outputs="markdown",
|
| 87 |
+
title="Hybrid AI vs Real Image Detector",
|
| 88 |
+
description="Upload gambar, sistem akan mendeteksi persentase AI vs foto asli, sensitif untuk AI photorealistic tapi tetap akurat untuk foto asli."
|
| 89 |
)
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|