Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,150 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from image_backend import predict_image_pil
|
| 3 |
from audio_backend import predict_audio
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
-
# IMAGE LOGIC
|
| 7 |
# =========================
|
| 8 |
def analyze_image(image):
|
| 9 |
label, confidence, heatmap = predict_image_pil(image)
|
|
|
|
| 10 |
if label == "Fake":
|
| 11 |
if confidence >= 90:
|
| 12 |
-
risk = "High likelihood of
|
| 13 |
elif confidence >= 60:
|
| 14 |
-
risk = "Possibly
|
| 15 |
else:
|
| 16 |
-
risk = "Uncertain
|
| 17 |
else:
|
| 18 |
if confidence >= 90:
|
| 19 |
-
risk = "Likely
|
| 20 |
elif confidence >= 60:
|
| 21 |
-
risk = "Possibly
|
| 22 |
else:
|
| 23 |
-
risk = "Uncertain –
|
|
|
|
| 24 |
return label, f"{confidence} %", risk, heatmap
|
| 25 |
|
|
|
|
| 26 |
# =========================
|
| 27 |
-
# AUDIO LOGIC
|
| 28 |
# =========================
|
| 29 |
def analyze_audio(audio_path):
|
| 30 |
label, confidence = predict_audio(audio_path)
|
|
|
|
| 31 |
if label == "fake":
|
| 32 |
if confidence >= 90:
|
| 33 |
-
risk = "High likelihood of
|
| 34 |
elif confidence >= 60:
|
| 35 |
-
risk = "Possibly
|
| 36 |
else:
|
| 37 |
-
risk = "Uncertain –
|
| 38 |
else:
|
| 39 |
if confidence >= 90:
|
| 40 |
-
risk = "Likely
|
| 41 |
elif confidence >= 60:
|
| 42 |
-
risk = "Possibly
|
| 43 |
else:
|
| 44 |
-
risk = "Uncertain –
|
|
|
|
| 45 |
return label.capitalize(), f"{confidence} %", risk
|
| 46 |
|
|
|
|
| 47 |
# =========================
|
| 48 |
# UI
|
| 49 |
# =========================
|
| 50 |
with gr.Blocks(css="style.css") as demo:
|
| 51 |
-
# Load Material Icons stylesheet
|
| 52 |
-
gr.HTML('<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">')
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
with gr.Tabs():
|
|
|
|
| 57 |
# HOME TAB
|
| 58 |
-
with gr.Tab(
|
| 59 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# IMAGE TAB
|
| 62 |
-
with gr.Tab(
|
| 63 |
-
gr.Markdown("## Deepfake Image Detection
|
|
|
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column(scale=1):
|
| 66 |
-
image_input = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
img_submit = gr.Button("Submit")
|
| 68 |
img_clear = gr.Button("Clear")
|
|
|
|
| 69 |
with gr.Column(scale=2):
|
| 70 |
img_pred = gr.Text(label="Prediction")
|
| 71 |
img_conf = gr.Text(label="Confidence")
|
| 72 |
-
img_risk = gr.
|
| 73 |
-
img_heatmap = gr.Image(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# AUDIO TAB
|
| 80 |
-
with gr.Tab(
|
| 81 |
-
gr.Markdown("## Deepfake Audio Detection
|
|
|
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column(scale=1):
|
| 84 |
-
audio_input = gr.Audio(
|
|
|
|
|
|
|
|
|
|
| 85 |
aud_submit = gr.Button("Submit")
|
| 86 |
aud_clear = gr.Button("Clear")
|
|
|
|
| 87 |
with gr.Column(scale=2):
|
| 88 |
aud_pred = gr.Text(label="Prediction")
|
| 89 |
aud_conf = gr.Text(label="Confidence")
|
| 90 |
-
aud_risk = gr.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# ---- IMPORT BACKENDS ----
|
| 4 |
from image_backend import predict_image_pil
|
| 5 |
from audio_backend import predict_audio
|
| 6 |
+
# =========================
|
| 7 |
+
|
| 8 |
|
| 9 |
# =========================
|
| 10 |
+
# IMAGE LOGIC (UNCHANGED)
|
| 11 |
# =========================
|
| 12 |
def analyze_image(image):
|
| 13 |
label, confidence, heatmap = predict_image_pil(image)
|
| 14 |
+
|
| 15 |
if label == "Fake":
|
| 16 |
if confidence >= 90:
|
| 17 |
+
risk = '<span class="material-icons">error</span> High likelihood of deepfake'
|
| 18 |
elif confidence >= 60:
|
| 19 |
+
risk = '<span class="material-icons">warning</span> Possibly deepfake'
|
| 20 |
else:
|
| 21 |
+
risk = '<span class="material-icons">help_outline</span> Uncertain deepfake'
|
| 22 |
else:
|
| 23 |
if confidence >= 90:
|
| 24 |
+
risk = '<span class="material-icons">check_circle</span> Likely real'
|
| 25 |
elif confidence >= 60:
|
| 26 |
+
risk = '<span class="material-icons">warning</span> Possibly real'
|
| 27 |
else:
|
| 28 |
+
risk = '<span class="material-icons">help_outline</span> Uncertain – needs review'
|
| 29 |
+
|
| 30 |
return label, f"{confidence} %", risk, heatmap
|
| 31 |
|
| 32 |
+
|
| 33 |
# =========================
|
| 34 |
+
# AUDIO LOGIC (UNCHANGED)
|
| 35 |
# =========================
|
| 36 |
def analyze_audio(audio_path):
|
| 37 |
label, confidence = predict_audio(audio_path)
|
| 38 |
+
|
| 39 |
if label == "fake":
|
| 40 |
if confidence >= 90:
|
| 41 |
+
risk = '<span class="material-icons">error</span> High likelihood of deepfake'
|
| 42 |
elif confidence >= 60:
|
| 43 |
+
risk = '<span class="material-icons">warning</span> Possibly deepfake'
|
| 44 |
else:
|
| 45 |
+
risk = '<span class="material-icons">help_outline</span> Uncertain – needs review'
|
| 46 |
else:
|
| 47 |
if confidence >= 90:
|
| 48 |
+
risk = '<span class="material-icons">check_circle</span> Likely real'
|
| 49 |
elif confidence >= 60:
|
| 50 |
+
risk = '<span class="material-icons">warning</span> Possibly real'
|
| 51 |
else:
|
| 52 |
+
risk = '<span class="material-icons">help_outline</span> Uncertain – needs review'
|
| 53 |
+
|
| 54 |
return label.capitalize(), f"{confidence} %", risk
|
| 55 |
|
| 56 |
+
|
| 57 |
# =========================
|
| 58 |
# UI
|
| 59 |
# =========================
|
| 60 |
with gr.Blocks(css="style.css") as demo:
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Load Material Icons
|
| 63 |
+
gr.Markdown("""
|
| 64 |
+
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
| 65 |
+
""")
|
| 66 |
+
|
| 67 |
+
gr.Markdown("# Unified Deepfake Detection System")
|
| 68 |
|
| 69 |
with gr.Tabs():
|
| 70 |
+
|
| 71 |
# HOME TAB
|
| 72 |
+
with gr.Tab("Home"):
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
## Welcome
|
| 75 |
+
Select the type of media you want to analyze.
|
| 76 |
+
""")
|
| 77 |
+
gr.Markdown("""
|
| 78 |
+
**Detection Modes Available**
|
| 79 |
+
- Image Deepfake Detection
|
| 80 |
+
- Audio Deepfake Detection
|
| 81 |
+
""")
|
| 82 |
+
gr.Markdown("Use the tabs above to proceed.")
|
| 83 |
|
| 84 |
# IMAGE TAB
|
| 85 |
+
with gr.Tab("Image Deepfake"):
|
| 86 |
+
gr.Markdown("## Deepfake Image Detection")
|
| 87 |
+
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
+
image_input = gr.Image(
|
| 91 |
+
label="Upload Image",
|
| 92 |
+
type="pil",
|
| 93 |
+
height=280
|
| 94 |
+
)
|
| 95 |
img_submit = gr.Button("Submit")
|
| 96 |
img_clear = gr.Button("Clear")
|
| 97 |
+
|
| 98 |
with gr.Column(scale=2):
|
| 99 |
img_pred = gr.Text(label="Prediction")
|
| 100 |
img_conf = gr.Text(label="Confidence")
|
| 101 |
+
img_risk = gr.HTML(label="Risk Assessment")
|
| 102 |
+
img_heatmap = gr.Image(
|
| 103 |
+
label="Explainability Heatmap",
|
| 104 |
+
height=280
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
img_submit.click(
|
| 108 |
+
fn=analyze_image,
|
| 109 |
+
inputs=image_input,
|
| 110 |
+
outputs=[img_pred, img_conf, img_risk, img_heatmap]
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
img_clear.click(
|
| 114 |
+
fn=lambda: (None, "", "", None),
|
| 115 |
+
inputs=None,
|
| 116 |
+
outputs=[image_input, img_pred, img_conf, img_risk]
|
| 117 |
+
)
|
| 118 |
|
| 119 |
# AUDIO TAB
|
| 120 |
+
with gr.Tab("Audio Deepfake"):
|
| 121 |
+
gr.Markdown("## Deepfake Audio Detection")
|
| 122 |
+
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column(scale=1):
|
| 125 |
+
audio_input = gr.Audio(
|
| 126 |
+
label="Upload Audio (.wav)",
|
| 127 |
+
type="filepath"
|
| 128 |
+
)
|
| 129 |
aud_submit = gr.Button("Submit")
|
| 130 |
aud_clear = gr.Button("Clear")
|
| 131 |
+
|
| 132 |
with gr.Column(scale=2):
|
| 133 |
aud_pred = gr.Text(label="Prediction")
|
| 134 |
aud_conf = gr.Text(label="Confidence")
|
| 135 |
+
aud_risk = gr.HTML(label="Risk Assessment")
|
| 136 |
+
|
| 137 |
+
aud_submit.click(
|
| 138 |
+
fn=analyze_audio,
|
| 139 |
+
inputs=audio_input,
|
| 140 |
+
outputs=[aud_pred, aud_conf, aud_risk]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
aud_clear.click(
|
| 144 |
+
fn=lambda: (None, "", ""),
|
| 145 |
+
inputs=None,
|
| 146 |
+
outputs=[audio_input, aud_pred, aud_conf]
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
|
| 150 |
+
demo.launch()
|