File size: 5,787 Bytes
dd652d7
 
 
 
a07ace5
dd652d7
 
 
 
 
 
 
928bdac
dd652d7
 
bee63f3
928bdac
dd652d7
 
2a9c2e2
bee63f3
dd652d7
2a9c2e2
bee63f3
dd652d7
2a9c2e2
bee63f3
dd652d7
 
2a9c2e2
bee63f3
dd652d7
2a9c2e2
bee63f3
dd652d7
2a9c2e2
bee63f3
dd652d7
bee63f3
2a9c2e2
bee63f3
 
 
 
dd652d7
928bdac
 
 
 
 
 
 
2a9c2e2
 
 
a07ace5
2a9c2e2
 
 
 
 
 
928bdac
 
 
2a9c2e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
928bdac
 
 
 
 
 
 
 
 
 
2a9c2e2
 
 
 
 
 
 
 
 
 
 
 
a07ace5
 
2a9c2e2
bee63f3
 
 
 
 
2a9c2e2
bee63f3
 
a07ace5
bee63f3
4622f70
bee63f3
4622f70
 
bb6f198
bee63f3
 
2a9c2e2
4622f70
 
a07ace5
bb6f198
 
4622f70
 
 
bee63f3
 
2a9c2e2
bee63f3
 
4622f70
bee63f3
4622f70
2a9c2e2
dd652d7
bee63f3
 
 
4622f70
bee63f3
4622f70
bee63f3
 
 
bb6f198
bee63f3
 
 
4622f70
a07ace5
 
dd652d7
 
a07ace5
bb6f198
 
 
a07ace5
4622f70
 
a07ace5
 
 
 
 
 
 
 
4622f70
 
 
 
 
a07ace5
 
 
 
 
 
 
 
 
 
 
 
4622f70
 
 
bb6f198
4622f70
bb6f198
a07ace5
bee63f3
928bdac
 
 
 
4622f70
bee63f3
 
 
 
bb6f198
bee63f3
a07ace5
bee63f3
a07ace5
bee63f3
 
a07ace5
bb6f198
 
 
 
 
 
bee63f3
2a9c2e2
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import gradio as gr

# ---- IMPORT BACKENDS ----
from image_backend import predict_image_pil
from report_generator import generate_report


# =========================
# IMAGE LOGIC
# =========================
def analyze_image(image):
    if image is None:
        return "", "", "", None, '<div class="status">Status: Idle</div>'

    label, confidence, heatmap = predict_image_pil(image)

    # Risk classification
    if label == "Fake":
        if confidence >= 90:
            risk = "high"
            message = "High likelihood of deepfake"
        elif confidence >= 60:
            risk = "warning"
            message = "Possibly deepfake"
        else:
            risk = "neutral"
            message = "Uncertain deepfake"
    else:
        if confidence >= 90:
            risk = "real"
            message = "Likely real"
        elif confidence >= 60:
            risk = "warning"
            message = "Possibly real"
        else:
            risk = "neutral"
            message = "Uncertain - review needed"

    risk_html = f"""
    <div class="risk-card {risk}">
        <div class="risk-title">{label}</div>
        <div class="risk-msg">{message}</div>
    </div>
    """

    return (
        label,
        f"{confidence} %",
        risk_html,
        heatmap,
        '<div class="status">Status: Completed</div>'
    )


# =========================
# CSS (UPDATED FOR DOWNLOAD FIX)
# =========================
css = """
body { background-color: #0f172a; }

.header {
    text-align: center;
    padding: 12px;
    font-size: 28px;
    font-weight: 600;
    color: white;
}

.section {
    color: #cbd5f5;
    margin-bottom: 10px;
}

.gr-box {
    border-radius: 12px !important;
    background: #1e293b !important;
    padding: 15px !important;
}

.risk-card {
    padding: 15px;
    border-radius: 10px;
    color: white;
    font-weight: bold;
}

.risk-title {
    font-size: 18px;
    margin-bottom: 5px;
}

.risk-msg {
    font-size: 14px;
    opacity: 0.9;
}

.risk-card.real { background: #16a34a; }
.risk-card.high { background: #dc2626; }
.risk-card.warning { background: #f59e0b; }
.risk-card.neutral { background: #64748b; }

.status {
    padding: 6px 12px;
    border-radius: 20px;
    background: #334155;
    color: white;
    display: inline-block;
}


"""


# =========================
# UI
# =========================
with gr.Blocks(css=css) as demo:

    # HEADER
    gr.Markdown('<div class="header">AI Driven Deepfake Detection Dashboard</div>')

    # SYSTEM OVERVIEW
    gr.Markdown("""
    ### πŸ” System Overview
    This system detects whether an uploaded image is **real or AI-generated (deepfake)** 
    using deep learning-based image forensics techniques.
    """)

    # MODEL INFO
    gr.Markdown("""
    ### 🧠 Model Details
    - Vision Transformer based architecture  
    - Learns fine-grained facial artifacts  
    - Uses attention for explainability  
    """)

    # STATUS
    status = gr.HTML('<div class="status">Status: Idle</div>')

    # MAIN UI
    with gr.Row():

        # INPUT PANEL
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“€ Upload Image")
            image_input = gr.Image(type="pil", height=300)

            img_submit = gr.Button("Analyze", variant="primary")
            img_clear = gr.Button("Reset")

        # OUTPUT PANEL
        with gr.Column(scale=2):
            gr.Markdown("### πŸ“Š Analysis Results")

            img_pred = gr.Text(label="Prediction")
            img_conf = gr.Text(label="Confidence")

            img_risk = gr.HTML()

            img_heatmap = gr.Image(
                label="Explainability Heatmap",
                height=300,
                interactive=False
            )

            # REPORT FEATURE
            generate_btn = gr.Button("Generate Report")
            report_file = gr.File(label="Download Report")

    # INTERPRETATION GUIDE
    gr.Markdown("""
    ### πŸ“– How to Interpret Results
    - **Prediction** β†’ Final classification (Real / Fake)  
    - **Confidence** β†’ Model certainty score  
    - **Heatmap** β†’ Highlights regions influencing the decision  
    - **Risk Level**:
      - πŸ”΄ High β†’ Strong deepfake indication  
      - 🟑 Warning β†’ Possible manipulation  
      - βšͺ Neutral β†’ Uncertain (manual review required)  
      - 🟒 Real β†’ Likely authentic image  
    """)

    # LIMITATIONS
    gr.Markdown("""
    ### ⚠️ Limitations
    - Performance may drop on **low-resolution or heavily compressed images**
    - May struggle with **high-quality GAN-generated content**
    - Works best on **face-centric images**
    - Not a replacement for human forensic analysis
    """)

    # PRIVACY
    gr.Markdown("""
    ### πŸ” Privacy & Usage
    - Images are processed temporarily and not stored
    - Intended for **educational and research purposes**
    - Should be used as a **decision-support tool only**
    """)

    # =========================
    # EVENTS
    # =========================

    # Analyze
    img_submit.click(
        lambda img: ("", "", "", None, '<div class="status">Status: Processing...</div>'),
        inputs=image_input,
        outputs=[img_pred, img_conf, img_risk, img_heatmap, status]
    ).then(
        analyze_image,
        inputs=image_input,
        outputs=[img_pred, img_conf, img_risk, img_heatmap, status]
    )

    # Reset
    img_clear.click(
        lambda: (None, "", "", "", None, '<div class="status">Status: Idle</div>', None),
        None,
        [image_input, img_pred, img_conf, img_risk, img_heatmap, status, report_file]
    )

        # βœ… Generate Report
    generate_btn.click(
        generate_report,
        inputs=[img_pred, img_conf, image_input, img_heatmap],
        outputs=report_file
    )


demo.launch()