Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -143,9 +143,34 @@ def combine_scores(ela_mean, hf_ratio, noise_incons_score, texture_corr=1.0):
|
|
| 143 |
|
| 144 |
# ====================== Gradio handler ======================
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
def analyze_simple(pil_img: Image.Image):
|
| 147 |
if pil_img is None:
|
| 148 |
-
return "
|
| 149 |
|
| 150 |
# 1) Face crop + normalize
|
| 151 |
pil_img = crop_face(pil_img)
|
|
@@ -165,16 +190,43 @@ def analyze_simple(pil_img: Image.Image):
|
|
| 165 |
texture_corr = natural_texture_correction(pil_img)
|
| 166 |
label, conf = combine_scores(ela_mean, hf_ratio, noi_score, texture_corr)
|
| 167 |
|
| 168 |
-
return
|
| 169 |
|
| 170 |
# ====================== UI ======================
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
btn.click(analyze_simple, inputs=inp, outputs=out)
|
|
|
|
|
|
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
demo.launch()
|
|
|
|
| 143 |
|
| 144 |
# ====================== Gradio handler ======================
|
| 145 |
|
| 146 |
+
def _result_card(label: str, conf: float) -> str:
|
| 147 |
+
"""
|
| 148 |
+
Build a clean, single-line verdict card with a subtle progress bar.
|
| 149 |
+
"""
|
| 150 |
+
pct = max(0.0, min(1.0, conf)) * 100.0
|
| 151 |
+
color = "#d84a4a" if label.startswith("Likely Manipulated") else "#2e7d32"
|
| 152 |
+
bar_bg = "#e9ecef"
|
| 153 |
+
return f"""
|
| 154 |
+
<div style="max-width:860px;margin:0 auto;">
|
| 155 |
+
<div style="border:1px solid #e5e7eb;border-radius:14px;padding:18px 20px;background:#fff;
|
| 156 |
+
box-shadow: 0 2px 10px rgba(16,24,40,.04);">
|
| 157 |
+
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px;">
|
| 158 |
+
<div style="font-size:18px;color:#111827;font-weight:600;">Deepfake likelihood</div>
|
| 159 |
+
<div style="font-weight:700;color:{color};">{pct:.1f}% — {label}</div>
|
| 160 |
+
</div>
|
| 161 |
+
<div style="width:100%;height:10px;background:{bar_bg};border-radius:999px;overflow:hidden;">
|
| 162 |
+
<div style="height:100%;width:{pct:.4f}%;background:{color};"></div>
|
| 163 |
+
</div>
|
| 164 |
+
</div>
|
| 165 |
+
<div style="color:#6b7280;font-size:12px;margin-top:10px;text-align:center;">
|
| 166 |
+
Heuristic detector (no training). Treat as an estimate, not forensic proof.
|
| 167 |
+
</div>
|
| 168 |
+
</div>
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
def analyze_simple(pil_img: Image.Image):
|
| 172 |
if pil_img is None:
|
| 173 |
+
return _result_card("Likely Authentic", 0.0)
|
| 174 |
|
| 175 |
# 1) Face crop + normalize
|
| 176 |
pil_img = crop_face(pil_img)
|
|
|
|
| 190 |
texture_corr = natural_texture_correction(pil_img)
|
| 191 |
label, conf = combine_scores(ela_mean, hf_ratio, noi_score, texture_corr)
|
| 192 |
|
| 193 |
+
return _result_card(label, conf)
|
| 194 |
|
| 195 |
# ====================== UI ======================
|
| 196 |
|
| 197 |
+
CUSTOM_CSS = """
|
| 198 |
+
.gradio-container {max-width: 980px !important;}
|
| 199 |
+
/* Card-like uploader */
|
| 200 |
+
.sleek-card {
|
| 201 |
+
border: 1px solid #e5e7eb; border-radius: 16px; background: #fff;
|
| 202 |
+
box-shadow: 0 2px 10px rgba(16,24,40,.04); padding: 18px;
|
| 203 |
+
}
|
| 204 |
+
"""
|
| 205 |
+
|
| 206 |
+
with gr.Blocks(title="Deepfake Detector", css=CUSTOM_CSS, theme=gr.themes.Soft()) as demo:
|
| 207 |
+
gr.Markdown(
|
| 208 |
+
"<h2 style='text-align:center;margin-bottom:6px;'>Deepfake Detector</h2>"
|
| 209 |
+
"<p style='text-align:center;color:#6b7280;'>Upload an image and get a single, clean likelihood estimate.</p>"
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
with gr.Row():
|
| 213 |
+
with gr.Column(scale=6, elem_classes=["sleek-card"]):
|
| 214 |
+
inp = gr.Image(
|
| 215 |
+
type="pil",
|
| 216 |
+
label="Upload / Paste Image",
|
| 217 |
+
sources=["upload", "clipboard", "webcam", "url"],
|
| 218 |
+
height=420,
|
| 219 |
+
show_label=True,
|
| 220 |
+
interactive=True,
|
| 221 |
+
)
|
| 222 |
+
btn = gr.Button("Analyze", variant="primary", size="lg")
|
| 223 |
+
|
| 224 |
+
with gr.Column(scale=6):
|
| 225 |
+
out = gr.HTML()
|
| 226 |
+
|
| 227 |
btn.click(analyze_simple, inputs=inp, outputs=out)
|
| 228 |
+
# Also run immediately when an image is provided (nice UX)
|
| 229 |
+
inp.change(analyze_simple, inputs=inp, outputs=out)
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|
| 232 |
demo.launch()
|