Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,149 +5,86 @@ import torch
|
|
| 5 |
import torch.nn.functional as F
|
| 6 |
|
| 7 |
print("\n" + "="*80)
|
| 8 |
-
print("
|
| 9 |
print("="*80)
|
| 10 |
-
print("\nBased on
|
| 11 |
-
print("β
|
| 12 |
-
print("β
|
| 13 |
print("="*80 + "\n")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"name": "bcmi/SSP-AI-Generated-Image-Detection",
|
| 19 |
-
"weight": 1.0,
|
| 20 |
-
"type": "SSP-AI Detection",
|
| 21 |
-
"proven_accuracy": "SOTA (claimed >90% on modern generators)",
|
| 22 |
-
"best_for": "Midjourney v6+, DALL-E 3, Stable Diffusion, GANs, and Hybrid generators"
|
| 23 |
-
},
|
| 24 |
-
]
|
| 25 |
|
| 26 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 27 |
print(f"π₯οΈ Device: {str(device).upper()}\n")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
try:
|
| 38 |
-
print(f"[{i+1}/{len(MODELS_CONFIG)}] {model_name}")
|
| 39 |
-
print(f" β’ Type: {config['type']}")
|
| 40 |
-
print(f" β’ Weight: {int(config['weight']*100)}%")
|
| 41 |
-
print(f" β’ Accuracy: {config['proven_accuracy']}")
|
| 42 |
-
print(f" β’ Best for: {config['best_for']}\n")
|
| 43 |
-
|
| 44 |
-
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 45 |
-
model = AutoModelForImageClassification.from_pretrained(model_name).to(device)
|
| 46 |
-
model.eval()
|
| 47 |
-
|
| 48 |
-
models_list.append(model)
|
| 49 |
-
processors_list.append(processor)
|
| 50 |
-
model_metadata.append(config)
|
| 51 |
-
loaded_count += 1
|
| 52 |
-
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f" β οΈ Warning: Failed to load - {str(e)[:50]}\n")
|
| 55 |
-
|
| 56 |
-
if loaded_count == 0:
|
| 57 |
-
raise RuntimeError("β Failed to load SSP-AI model. Check Hugging Face access and model name.")
|
| 58 |
-
|
| 59 |
-
print("="*80)
|
| 60 |
-
print(f"β
Successfully loaded {loaded_count} model(s)")
|
| 61 |
-
print("="*80 + "\n")
|
| 62 |
-
|
| 63 |
|
| 64 |
def predict(image):
|
| 65 |
if image is None:
|
| 66 |
return "β No image uploaded", 0.0, "Upload an image to analyze"
|
| 67 |
-
|
| 68 |
try:
|
| 69 |
if image.mode != 'RGB':
|
| 70 |
image = image.convert('RGB')
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
real_prob = float(probs[0])
|
| 84 |
-
ai_prob = float(probs[1])
|
| 85 |
-
all_scores.append(ai_prob)
|
| 86 |
-
|
| 87 |
-
pred = "π€ AI-Generated" if ai_prob > real_prob else "β Real Photo"
|
| 88 |
-
conf = max(ai_prob, real_prob)
|
| 89 |
-
meta = model_metadata[idx]
|
| 90 |
-
|
| 91 |
-
model_results.append({
|
| 92 |
-
'name': meta["name"].split('/')[-1],
|
| 93 |
-
'type': meta['type'],
|
| 94 |
-
'weight': meta['weight'],
|
| 95 |
-
'prediction': pred,
|
| 96 |
-
'ai_score': ai_prob,
|
| 97 |
-
'real_score': real_prob,
|
| 98 |
-
'confidence': conf,
|
| 99 |
-
'accuracy': meta['proven_accuracy']
|
| 100 |
-
})
|
| 101 |
-
|
| 102 |
-
# Weighted score
|
| 103 |
-
weights = [m['weight'] for m in model_metadata[:len(all_scores)]]
|
| 104 |
-
total_weight = sum(weights)
|
| 105 |
-
normalized_weights = [w/total_weight for w in weights]
|
| 106 |
-
weighted_ai_score = sum(s * w for s, w in zip(all_scores, normalized_weights))
|
| 107 |
-
|
| 108 |
threshold = 0.50
|
| 109 |
is_ai = weighted_ai_score > threshold
|
| 110 |
final_pred = "π¨ AI-GENERATED" if is_ai else "β
REAL PHOTO"
|
| 111 |
confidence = max(weighted_ai_score, 1 - weighted_ai_score)
|
| 112 |
-
|
| 113 |
-
# Individual votes
|
| 114 |
-
ai_votes = sum(1 for r in model_results if "AI" in r['prediction'])
|
| 115 |
-
total_votes = len(model_results)
|
| 116 |
-
|
| 117 |
# Build report
|
| 118 |
report = f"""
|
| 119 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 120 |
-
β
|
| 121 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 122 |
|
| 123 |
π― PREDICTION: {final_pred}
|
| 124 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 125 |
Weighted AI Probability: {weighted_ai_score:.4f}
|
| 126 |
Detection Confidence: {confidence:.4f}
|
| 127 |
-
Ensemble Consensus: {ai_votes}/{total_votes} models vote AI-Generated
|
| 128 |
-
|
| 129 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 130 |
-
π
|
| 131 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
"""
|
| 133 |
-
|
| 134 |
-
for i, result in enumerate(model_results, 1):
|
| 135 |
-
weight_pct = int(result['weight'] * 100)
|
| 136 |
-
report += f"""
|
| 137 |
-
Model {i}: {result['name']} ({result['type']})
|
| 138 |
-
ββ Ensemble Weight: {weight_pct}%
|
| 139 |
-
ββ Vote: {result['prediction']}
|
| 140 |
-
ββ AI Score: {result['ai_score']:.4f} | Real Score: {result['real_score']:.4f}
|
| 141 |
-
ββ Model Confidence: {result['confidence']:.4f}
|
| 142 |
-
ββ Accuracy: {result['accuracy']}
|
| 143 |
-
"""
|
| 144 |
-
|
| 145 |
return final_pred, round(weighted_ai_score, 4), report
|
| 146 |
-
|
| 147 |
except Exception as e:
|
| 148 |
return f"β Error: {str(e)}", 0.0, f"Processing failed: {str(e)}"
|
| 149 |
|
| 150 |
-
|
| 151 |
# Gradio Interface
|
| 152 |
demo = gr.Interface(
|
| 153 |
fn=predict,
|
|
@@ -155,10 +92,10 @@ demo = gr.Interface(
|
|
| 155 |
outputs=[
|
| 156 |
gr.Textbox(label="π― Detection Result"),
|
| 157 |
gr.Number(label="π AI Score (0.0-1.0)"),
|
| 158 |
-
gr.Textbox(label="π
|
| 159 |
],
|
| 160 |
-
title="
|
| 161 |
-
description="
|
| 162 |
)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import torch.nn.functional as F
|
| 6 |
|
| 7 |
print("\n" + "="*80)
|
| 8 |
+
print("π BEST FREE AI IMAGE DETECTOR 2025 - ATEEQQ MODEL ONLY")
|
| 9 |
print("="*80)
|
| 10 |
+
print("\nBased on Ateeqq 2025 benchmarks:")
|
| 11 |
+
print("β Diffusion detection (Midjourney, DALL-E, Stable Diffusion): 88-94% accuracy")
|
| 12 |
+
print("β CNN + Semantic Analysis approach")
|
| 13 |
print("="*80 + "\n")
|
| 14 |
|
| 15 |
+
# Use only Ateeqq model
|
| 16 |
+
MODEL_NAME = "Ateeqq/ai-vs-human-image-detector"
|
| 17 |
+
MODEL_WEIGHT = 1.0 # 100% weight
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 20 |
print(f"π₯οΈ Device: {str(device).upper()}\n")
|
| 21 |
|
| 22 |
+
# Load Ateeqq model
|
| 23 |
+
try:
|
| 24 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
| 25 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_NAME).to(device)
|
| 26 |
+
model.eval()
|
| 27 |
+
print(f"β
Successfully loaded model: {MODEL_NAME}")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise RuntimeError(f"β Failed to load model {MODEL_NAME}. Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def predict(image):
|
| 32 |
if image is None:
|
| 33 |
return "β No image uploaded", 0.0, "Upload an image to analyze"
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
if image.mode != 'RGB':
|
| 37 |
image = image.convert('RGB')
|
| 38 |
+
|
| 39 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = model(**inputs)
|
| 42 |
+
logits = outputs.logits
|
| 43 |
+
probs = F.softmax(logits, dim=1)[0].cpu().numpy()
|
| 44 |
+
|
| 45 |
+
real_prob = float(probs[0])
|
| 46 |
+
ai_prob = float(probs[1])
|
| 47 |
+
|
| 48 |
+
weighted_ai_score = ai_prob * MODEL_WEIGHT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
threshold = 0.50
|
| 50 |
is_ai = weighted_ai_score > threshold
|
| 51 |
final_pred = "π¨ AI-GENERATED" if is_ai else "β
REAL PHOTO"
|
| 52 |
confidence = max(weighted_ai_score, 1 - weighted_ai_score)
|
| 53 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Build report
|
| 55 |
report = f"""
|
| 56 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
β π¬ AI IMAGE DETECTION ANALYSIS - ATEEQQ MODEL ONLY β
|
| 58 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
|
| 60 |
π― PREDICTION: {final_pred}
|
| 61 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 62 |
Weighted AI Probability: {weighted_ai_score:.4f}
|
| 63 |
Detection Confidence: {confidence:.4f}
|
|
|
|
|
|
|
| 64 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 65 |
+
π MODEL ANALYSIS:
|
| 66 |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
Model: Ateeqq/ai-vs-human-image-detector
|
| 68 |
+
ββ Detection Type: SigLIP + Semantic Analysis
|
| 69 |
+
ββ Vote: {"π€ AI-Generated" if ai_prob > real_prob else "β Real Photo"}
|
| 70 |
+
ββ AI Score: {ai_prob:.4f} | Real Score: {real_prob:.4f}
|
| 71 |
+
ββ Confidence: {confidence:.4f}
|
| 72 |
+
ββ Proven Accuracy: 88-94% on diffusion models
|
| 73 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 74 |
+
β
WHAT IT DETECTS:
|
| 75 |
+
β DALL-E 3, Midjourney v6+, Stable Diffusion
|
| 76 |
+
β Flux, GANs, Hybrid AI generators
|
| 77 |
+
β Post-processed AI images
|
| 78 |
+
β οΈ LIMITATIONS:
|
| 79 |
+
β May struggle with heavily edited/compressed images
|
| 80 |
+
β Hybrid real+AI images are challenging
|
| 81 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 82 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return final_pred, round(weighted_ai_score, 4), report
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
return f"β Error: {str(e)}", 0.0, f"Processing failed: {str(e)}"
|
| 87 |
|
|
|
|
| 88 |
# Gradio Interface
|
| 89 |
demo = gr.Interface(
|
| 90 |
fn=predict,
|
|
|
|
| 92 |
outputs=[
|
| 93 |
gr.Textbox(label="π― Detection Result"),
|
| 94 |
gr.Number(label="π AI Score (0.0-1.0)"),
|
| 95 |
+
gr.Textbox(label="π Research-Based Analysis", lines=25)
|
| 96 |
],
|
| 97 |
+
title="π Advanced AI Image Detector 2025 - Ateeqq Only",
|
| 98 |
+
description="Detection using the proven Ateeqq model (88-94% accuracy on modern AI generators)."
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|