Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoImageProcessor, AutoModelForImageClassification | |
| from PIL import Image | |
| import torch | |
| import torch.nn.functional as F | |
| # ===================== Startup Info ===================== | |
| print("\n" + "="*80) | |
| print("π BEST FREE AI IMAGE DETECTOR 2025 - ATEEQQ MODEL ONLY") | |
| print("="*80) | |
| print("\nBased on Ateeqq 2025 benchmarks:") | |
| print("β Diffusion detection (Midjourney, DALL-E, Stable Diffusion): 88-94% accuracy") | |
| print("β CNN + Semantic Analysis approach") | |
| print("="*80 + "\n") | |
| # ===================== Load Model ===================== | |
| MODEL_NAME = "Ateeqq/ai-vs-human-image-detector" | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| print(f"π₯οΈ Device: {str(device).upper()}\n") | |
| try: | |
| processor = AutoImageProcessor.from_pretrained(MODEL_NAME) | |
| model = AutoModelForImageClassification.from_pretrained(MODEL_NAME).to(device) | |
| model.eval() | |
| print(f"β Successfully loaded model: {MODEL_NAME}") | |
| except Exception as e: | |
| raise RuntimeError(f"β Failed to load model: {str(e)}") | |
| # ===================== Prediction Function ===================== | |
| def predict(image: Image.Image): | |
| if image is None: | |
| return "β No image uploaded", 0.0, "Upload an image to analyze" | |
| if image.mode != "RGB": | |
| image = image.convert("RGB") | |
| try: | |
| inputs = processor(images=image, return_tensors="pt").to(device) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| probs = F.softmax(logits, dim=1)[0].cpu().numpy() | |
| real_prob, ai_prob = float(probs[0]), float(probs[1]) | |
| pred = "π¨ AI-GENERATED" if ai_prob > real_prob else "β REAL PHOTO" | |
| confidence = max(ai_prob, real_prob) | |
| # Build simple report | |
| report = f""" | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β π¬ Ateeqq AI Image Detection Report β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| π― PREDICTION: {pred} | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| AI Probability: {ai_prob:.4f} | |
| Real Probability: {real_prob:.4f} | |
| Detection Confidence: {confidence:.4f} | |
| β Detected by: Ateeqq/ai-vs-human-image-detector | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β’ High accuracy on DALL-E 3, Midjourney v6+, Stable Diffusion | |
| β’ CNN + Semantic Analysis approach | |
| β’ Robust for post-processed AI images | |
| β’ Free to use for research or analysis | |
| """ | |
| return pred, round(ai_prob, 4), report | |
| except Exception as e: | |
| return f"β Error: {str(e)}", 0.0, f"Processing failed: {str(e)}" | |
| # ===================== Gradio Interface ===================== | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="πΈ Upload Image"), | |
| outputs=[ | |
| gr.Textbox(label="π― Detection Result"), | |
| gr.Number(label="π AI Score (0.0-1.0)"), | |
| gr.Textbox(label="π Detection Report", lines=25) | |
| ], | |
| title="π Ateeqq AI Image Detector (2025)", | |
| description="Detect AI-generated images using the official Ateeqq model from Hugging Face. Works best for DALL-E 3, Midjourney v6+, Stable Diffusion." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |