Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from transformers import pipeline
|
| 4 |
-
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
|
| 7 |
# Muat model deteksi AI
|
| 8 |
classifier = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 9 |
|
| 10 |
-
def calculate_blur(image):
|
| 11 |
-
gray = np.array(image.convert("L"))
|
| 12 |
-
return cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 13 |
-
|
| 14 |
-
def calculate_noise(image):
|
| 15 |
-
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 16 |
-
return np.std(gray - np.mean(gray))
|
| 17 |
-
|
| 18 |
-
def high_freq_artifacts(image):
|
| 19 |
-
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 20 |
-
f = np.fft.fft2(gray)
|
| 21 |
-
fshift = np.fft.fftshift(f)
|
| 22 |
-
magnitude_spectrum = np.abs(fshift)
|
| 23 |
-
rows, cols = gray.shape
|
| 24 |
-
crow, ccol = rows // 2 , cols // 2
|
| 25 |
-
r = min(crow, ccol) // 4
|
| 26 |
-
mask = np.zeros((rows, cols))
|
| 27 |
-
mask[crow-r:crow+r, ccol-r:ccol+r] = 1
|
| 28 |
-
low_energy = np.sum(magnitude_spectrum * mask)
|
| 29 |
-
total_energy = np.sum(magnitude_spectrum)
|
| 30 |
-
high_ratio = (total_energy - low_energy) / total_energy * 100
|
| 31 |
-
return high_ratio
|
| 32 |
-
|
| 33 |
def detect_image(image: Image.Image):
|
| 34 |
-
blur_score = calculate_blur(image)
|
| 35 |
-
noise_score = calculate_noise(image)
|
| 36 |
-
high_freq_score = high_freq_artifacts(image)
|
| 37 |
-
|
| 38 |
-
# Prediksi menggunakan model deteksi AI
|
| 39 |
result = classifier(image)
|
| 40 |
ai_score = result[0]['score'] if result[0]['label'] == 'AI-generated' else 0
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if final_score > 0.5:
|
| 46 |
-
return "🤖 Gambar ini kemungkinan besar dihasilkan oleh AI."
|
| 47 |
-
else:
|
| 48 |
-
return "✅ Gambar ini kemungkinan besar asli."
|
| 49 |
-
|
| 50 |
-
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("### Deteksi Gambar AI vs Asli")
|
| 52 |
-
with gr.Row():
|
| 53 |
-
img_input = gr.Image(type="pil", label="Unggah Gambar")
|
| 54 |
-
output = gr.Textbox(label="Hasil Deteksi")
|
| 55 |
-
btn = gr.Button("Deteksi")
|
| 56 |
-
btn.click(detect_image, inputs=img_input, outputs=output)
|
| 57 |
-
|
| 58 |
-
demo.launch()
|
|
|
|
| 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 |
+
return f"Skor AI: {ai_score:.2f}"
|
| 12 |
|
| 13 |
+
# Antarmuka Gradio
|
| 14 |
+
iface = gr.Interface(fn=detect_image, inputs=gr.Image(type="pil"), outputs="text")
|
| 15 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|