File size: 3,828 Bytes
810b71d
78e86b6
c76be84
4c40d3d
f32bad1
 
e2caa40
f32bad1
4c40d3d
 
 
 
 
78e86b6
 
4c40d3d
e2caa40
4c40d3d
 
 
 
 
 
 
 
 
c76be84
 
4c40d3d
 
c76be84
 
4c40d3d
403b075
 
 
 
 
 
4c40d3d
403b075
4c40d3d
f32bad1
4c40d3d
 
 
 
 
 
 
 
 
 
 
403b075
 
c76be84
8a74a2a
4c40d3d
c76be84
cc95494
f32bad1
403b075
f32bad1
4c40d3d
 
 
f32bad1
 
 
 
 
4c40d3d
f32bad1
 
 
 
 
 
4c40d3d
f32bad1
 
 
 
 
4c40d3d
 
 
 
f32bad1
4c40d3d
 
f32bad1
4c40d3d
 
 
 
 
 
 
 
 
 
403b075
c1504a1
f32bad1
4c40d3d
 
c1504a1
4c40d3d
 
 
 
 
 
 
 
 
 
f32bad1
4c40d3d
c76be84
4c40d3d
f32bad1
c76be84
 
4c40d3d
c76be84
 
4c40d3d
cc95494
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import gradio as gr
from transformers import pipeline
from PIL import Image
import traceback
import time
import threading

# Models
models = [
    ("Ateeqq/ai-vs-human-image-detector", "ateeq"),
    ("umm-maybe/AI-image-detector", "umm_maybe"),
    ("dima806/ai_vs_human_generated_image_detection", "dimma"),
]

pipes = []
for model_id, _ in models:
    try:
        pipes.append((model_id, pipeline("image-classification", model=model_id)))
        print(f"Loaded {model_id}")
    except Exception as e:
        print(f"Error loading {model_id}: {e}")

def predict_image(image: Image.Image):
    try:
        results = []
        for _, pipe in pipes:
            res = pipe(image)[0]
            results.append(res)

        final_result = results[0]
        label = final_result["label"].lower()
        score = final_result["score"] * 100

        if "ai" in label or "fake" in label:
            verdict = f"🧠 AI-Generated ({score:.1f}% confidence)"
            color = "#007BFF"
        else:
            verdict = f"🧍 Human-Made ({score:.1f}% confidence)"
            color = "#4CAF50"

        html = f"""
        <div class='result-box' style="
            background: linear-gradient(135deg, {color}33, #1a1a1a);
            border: 2px solid {color};
            border-radius: 15px;
            padding: 25px;
            text-align: center;
            color: white;
            font-size: 20px;
            font-weight: 600;
            box-shadow: 0 0 20px {color}55;
            animation: fadeIn 0.6s ease-in-out;
        ">
            {verdict}
        </div>
        """
        return html
    except Exception as e:
        traceback.print_exc()
        return f"<div style='color:red;'>Error analyzing image: {str(e)}</div>"

# CSS for sleek glowing pulse
css = """
body, .gradio-container {
    font-family: 'Poppins', sans-serif !important;
    background: transparent !important;
}
h1 {
    text-align: center;
    font-weight: 700;
    color: #007BFF;
    margin-bottom: 10px;
}
.gr-button-primary {
    background-color: #007BFF !important;
    color: white !important;
    font-weight: 600;
    border-radius: 10px;
    height: 45px;
}
.gr-button-secondary {
    background-color: #dc3545 !important;
    color: white !important;
    border-radius: 10px;
    height: 45px;
}
#pulse-loader {
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, #007BFF, #00C3FF);
    animation: pulse 1.2s infinite ease-in-out;
    border-radius: 2px;
    box-shadow: 0 0 10px #007BFF;
}
@keyframes pulse {
    0% { transform: scaleX(0.1); opacity: 0.6; }
    50% { transform: scaleX(1); opacity: 1; }
    100% { transform: scaleX(0.1); opacity: 0.6; }
}
@keyframes fadeIn {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}
"""

with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
    gr.Markdown("<h1>🔍 AI Image Detector</h1>")

    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(type="pil", label="Upload an image")
            analyze_button = gr.Button("Analyze", variant="primary")
            clear_button = gr.Button("Clear", variant="secondary")
            loader = gr.HTML("")
        with gr.Column(scale=1):
            output = gr.HTML(label="Result")

    def analyze(img):
        if img is None:
            return ("", "<div style='color:red;'>Please upload an image first!</div>")
        loader_html = "<div id='pulse-loader'></div>"
        yield (loader_html, "")  # instantly show loader

        # do analysis in background
        result = predict_image(img)
        yield ("", result)  # hide loader, show result

    analyze_button.click(analyze, inputs=image_input, outputs=[loader, output])
    clear_button.click(lambda: ("", ""), outputs=[loader, output])

demo.launch()