Spaces:
Sleeping
Sleeping
| 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() | |