Spaces:
Sleeping
Sleeping
File size: 3,526 Bytes
b54fd41 2e44d5d 75c0195 b54fd41 13337b8 1ece950 13337b8 3964bb8 1ece950 3964bb8 b54fd41 13337b8 3964bb8 1ece950 2e44d5d 1ece950 b54fd41 13337b8 3964bb8 13337b8 b54fd41 3964bb8 13337b8 3964bb8 13337b8 2e44d5d 13337b8 b54fd41 13337b8 2e44d5d 13337b8 75c0195 13337b8 2e44d5d 13337b8 b54fd41 13337b8 b54fd41 13337b8 | 1 2 3 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import gradio as gr
from PIL import Image, ImageStat, ImageDraw
import io
# ===========================
# EURISTIC AI DETECTOR
# ===========================
def heuristic_ai_detector(image):
stat = ImageStat.Stat(image.convert("L"))
variance = stat.var[0]
width, height = image.size
exif = image.getexif()
exif_score = 0 if len(exif) == 0 else 50
score = 50
if variance < 500:
score += 30
if width > 2000 or height > 2000:
score += 20
score -= exif_score / 2
score = max(0, min(100, score))
label = "AI Generated" if score >= 50 else "Realistic"
return label, score
# ===========================
# GENERA HEATMAP
# ===========================
def generate_heatmap(image, score):
base = image.convert("RGBA")
overlay = Image.new("RGBA", base.size, (255, 0, 0, int(score*2.5)))
return Image.alpha_composite(base, overlay)
# ===========================
# FUNZIONE PRINCIPALE
# ===========================
def check_image(image):
label, score = heuristic_ai_detector(image)
heatmap = generate_heatmap(image, score)
if score < 40:
color = "#4CAF50" # verde
elif score < 70:
color = "#FFC107" # giallo
else:
color = "#F44336" # rosso
if label == "AI Generated":
explanation = f"""
<div class="result-card" style="border-left: 5px solid {color};">
<h2>⚠️ AI Generated Image</h2>
<p><b>Score AI stimato:</b> {score:.1f}%</p>
<p>Rumore basso / texture uniforme</p>
<p>Dimensioni insolite</p>
<p>Metadata EXIF assente</p>
</div>
"""
else:
explanation = f"""
<div class="result-card" style="border-left: 5px solid {color};">
<h2>✅ Realistic Image</h2>
<p><b>Score AI stimato:</b> {score:.1f}%</p>
<p>Rumore naturale</p>
<p>Dimensioni coerenti</p>
<p>Metadata EXIF presente</p>
</div>
"""
return heatmap, explanation
# ===========================
# GRADIO BLOCKS MODERNO
# ===========================
with gr.Blocks(css="""
body { background-color: #0d1117; font-family: 'Montserrat', sans-serif; color: #EEE; margin:0; }
h1 { text-align:center; font-size: 48px; color: #FFD700; margin-bottom: 5px;}
h3 { text-align:center; color: #CCCCCC; margin-top:0px; font-weight:normal; }
.gr-button { background: linear-gradient(90deg,#FFD700,#FFAA00); color:black; font-weight:bold; border-radius:10px; height:50px; font-size:18px; transition: all 0.3s ease;}
.gr-button:hover { transform: scale(1.05); box-shadow:0px 5px 15px rgba(255,170,0,0.6);}
.result-card { background-color:#161B22; border-radius:15px; padding:20px; margin-top:20px; box-shadow:0 4px 15px rgba(0,0,0,0.4); transition: transform 0.3s ease;}
.result-card:hover { transform: scale(1.02); }
.gr-box { background-color:transparent; border-radius:15px; padding:15px; }
""") as demo:
gr.HTML("<h1>💎 Image Trust Checker 2026</h1><h3>Verifica se le immagini sono generate da AI o realistiche</h3>")
with gr.Row():
with gr.Column(scale=1):
img_input = gr.Image(label="Carica immagine", type="pil")
analyze_btn = gr.Button("Analizza immagine")
with gr.Column(scale=1):
img_output = gr.Image(label="Heatmap sospetta", type="pil")
txt_output = gr.HTML(label="Risultato")
analyze_btn.click(fn=check_image, inputs=img_input, outputs=[img_output, txt_output])
demo.launch()
|