Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +57 -124
src/streamlit_app.py
CHANGED
|
@@ -1,190 +1,123 @@
|
|
| 1 |
-
# streamlit_app.py
|
| 2 |
-
|
| 3 |
-
# Voice Guard (Streamlit) - env-only config (no st.secrets required)
|
| 4 |
-
# - Tries app/ then src/ for the Detector
|
| 5 |
-
# - Accepts mic (best-effort) or upload
|
| 6 |
-
# - Shows probabilities, decision details, and CAM heatmap
|
| 7 |
-
# - If MODEL_WEIGHTS_URL is set, downloads weights on boot when missing
|
| 8 |
-
# ------------------------------------------------------------
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
import io
|
| 12 |
-
import pathlib
|
| 13 |
-
import urllib.request
|
| 14 |
import numpy as np
|
| 15 |
import streamlit as st
|
| 16 |
from PIL import Image
|
| 17 |
from matplotlib import cm
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
Detector = None
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
"app.inference_wav2vec",
|
| 24 |
-
"app.inference",
|
| 25 |
-
"src.inference_wav2vec",
|
| 26 |
-
"src.inference",
|
| 27 |
-
]:
|
| 28 |
try:
|
| 29 |
Detector = __import__(mod, fromlist=["Detector"]).Detector
|
| 30 |
break
|
| 31 |
except Exception as e:
|
| 32 |
_last_err = e
|
| 33 |
-
|
| 34 |
if Detector is None:
|
| 35 |
-
st.error(
|
| 36 |
-
"Could not import Detector from app/ or src/. "
|
| 37 |
-
"Please include app/inference_wav2vec.py (preferred) or app/inference.py. "
|
| 38 |
-
f"Last import error: {_last_err}"
|
| 39 |
-
)
|
| 40 |
st.stop()
|
| 41 |
|
| 42 |
-
#
|
| 43 |
def cfg(name: str, default: str = "") -> str:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return val if val not in (None, "") else default
|
| 47 |
|
| 48 |
def ensure_weights() -> str:
|
| 49 |
-
"""
|
| 50 |
-
Ensure model weights exist at MODEL_WEIGHTS_PATH.
|
| 51 |
-
If missing and MODEL_WEIGHTS_URL is set, download them.
|
| 52 |
-
"""
|
| 53 |
-
default_path = "app/models/weights/wav2vec2_classifier.pth"
|
| 54 |
-
wp = cfg("MODEL_WEIGHTS_PATH", default_path)
|
| 55 |
url = cfg("MODEL_WEIGHTS_URL", "")
|
| 56 |
-
|
| 57 |
dest = pathlib.Path(wp)
|
| 58 |
-
if not dest.exists():
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
"Settings → Variables & secrets so the app can download them."
|
| 69 |
-
)
|
| 70 |
return str(dest)
|
| 71 |
|
| 72 |
@st.cache_resource(show_spinner=True)
|
| 73 |
-
def load_detector()
|
| 74 |
-
weights_path
|
| 75 |
-
det = Detector(weights_path=weights_path)
|
| 76 |
-
return det
|
| 77 |
|
| 78 |
det = load_detector()
|
| 79 |
|
| 80 |
-
#
|
| 81 |
def cam_to_png_bytes(cam: np.ndarray) -> bytes:
|
| 82 |
-
"""Map [H,W] float array (0..1) to magma RGB PNG bytes."""
|
| 83 |
cam = np.asarray(cam, dtype=np.float32)
|
| 84 |
-
cam = np.nan_to_num(cam, nan=0.0)
|
| 85 |
-
cam = np.clip(cam, 0.0, 1.0)
|
| 86 |
rgb = (cm.magma(cam)[..., :3] * 255).astype(np.uint8)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
img.save(bio, format="PNG")
|
| 90 |
-
return bio.getvalue()
|
| 91 |
|
| 92 |
def analyze(wav_bytes: bytes, source_hint: str):
|
| 93 |
-
"""Call detector predict + explain; returns (proba_dict, explain_dict)."""
|
| 94 |
proba = det.predict_proba(wav_bytes, source_hint=source_hint)
|
| 95 |
-
exp
|
| 96 |
return proba, exp
|
| 97 |
|
| 98 |
-
#
|
| 99 |
st.set_page_config(page_title="Voice Guard", page_icon="🛡️", layout="wide")
|
| 100 |
st.title("🛡️ Voice Guard — Human vs AI Speech")
|
| 101 |
|
| 102 |
-
left, right = st.columns([1,
|
| 103 |
-
|
| 104 |
with left:
|
| 105 |
st.subheader("Input")
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
wav_bytes = None
|
| 109 |
-
source_hint = None
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
st.caption("Record ~3–7 seconds. If mic fails in your browser, use Upload.")
|
| 114 |
try:
|
| 115 |
from audio_recorder_streamlit import audio_recorder
|
| 116 |
-
audio = audio_recorder(
|
| 117 |
-
|
| 118 |
-
recording_color="#ff6a00",
|
| 119 |
-
neutral_color="#2b2b2b",
|
| 120 |
-
icon_size="2x",
|
| 121 |
-
)
|
| 122 |
if audio:
|
| 123 |
-
wav_bytes = audio
|
| 124 |
-
source_hint = "microphone"
|
| 125 |
st.audio(wav_bytes, format="audio/wav")
|
| 126 |
except Exception:
|
| 127 |
-
st.info("Recorder
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
f
|
| 132 |
-
|
| 133 |
-
type=["wav", "mp3", "m4a", "aac"],
|
| 134 |
-
)
|
| 135 |
-
if f is not None:
|
| 136 |
-
wav_bytes = f.read()
|
| 137 |
-
source_hint = "upload"
|
| 138 |
st.audio(wav_bytes)
|
| 139 |
|
| 140 |
st.markdown("---")
|
| 141 |
-
run = st.button(
|
| 142 |
-
|
| 143 |
-
)
|
| 144 |
|
| 145 |
with right:
|
| 146 |
st.subheader("Results")
|
| 147 |
-
|
| 148 |
if run and wav_bytes:
|
| 149 |
try:
|
| 150 |
with st.spinner("Analyzing…"):
|
| 151 |
proba, exp = analyze(wav_bytes, source_hint or "auto")
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
rule = proba.get("decision", "threshold")
|
| 158 |
-
thr_src = proba.get("threshold_source", "—")
|
| 159 |
rscore = proba.get("replay_score", None)
|
| 160 |
|
| 161 |
-
c1,
|
| 162 |
-
with c1:
|
| 163 |
-
|
| 164 |
-
with c2:
|
| 165 |
-
st.metric("AI", f"{pa*100:.1f}%")
|
| 166 |
with c3:
|
| 167 |
-
color = "#22c55e" if label
|
| 168 |
-
st.markdown(
|
| 169 |
-
|
| 170 |
-
unsafe_allow_html=True,
|
| 171 |
-
)
|
| 172 |
-
st.caption(
|
| 173 |
-
f"thr({thr_src})={thr:.2f} • rule={rule} • replay={'—' if rscore is None else f'{float(rscore):.2f}'}"
|
| 174 |
-
)
|
| 175 |
|
| 176 |
st.markdown("##### Explanation Heatmap")
|
| 177 |
cam = np.asarray(exp.get("cam"), dtype=np.float32)
|
| 178 |
-
st.image(
|
| 179 |
-
cam_to_png_bytes(cam),
|
| 180 |
-
caption="Spectrogram importance",
|
| 181 |
-
use_column_width=True,
|
| 182 |
-
)
|
| 183 |
|
| 184 |
with st.expander("Raw JSON (debug)"):
|
| 185 |
st.json({"proba": proba, "explain": {"cam_shape": list(cam.shape)}})
|
| 186 |
-
|
| 187 |
except Exception as e:
|
| 188 |
st.error(f"Analyze failed: {e}")
|
| 189 |
|
| 190 |
-
st.caption("
|
|
|
|
| 1 |
+
# streamlit_app.py — ENV-ONLY CONFIG (no st.secrets)
|
| 2 |
+
import os, io, pathlib, urllib.request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import streamlit as st
|
| 5 |
from PIL import Image
|
| 6 |
from matplotlib import cm
|
| 7 |
|
| 8 |
+
# ---- import Detector from app/ or src/ ----
|
| 9 |
+
Detector, _last_err = None, None
|
| 10 |
+
for mod in ["app.inference_wav2vec", "app.inference",
|
| 11 |
+
"src.inference_wav2vec", "src.inference"]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
Detector = __import__(mod, fromlist=["Detector"]).Detector
|
| 14 |
break
|
| 15 |
except Exception as e:
|
| 16 |
_last_err = e
|
|
|
|
| 17 |
if Detector is None:
|
| 18 |
+
st.error(f"Could not import Detector from app/ or src/. Last error: {_last_err}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
st.stop()
|
| 20 |
|
| 21 |
+
# ---- weights handling (ENV ONLY) ----
|
| 22 |
def cfg(name: str, default: str = "") -> str:
|
| 23 |
+
v = os.getenv(name)
|
| 24 |
+
return v if v not in (None, "") else default
|
|
|
|
| 25 |
|
| 26 |
def ensure_weights() -> str:
|
| 27 |
+
wp = cfg("MODEL_WEIGHTS_PATH", "app/models/weights/wav2vec2_classifier.pth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
url = cfg("MODEL_WEIGHTS_URL", "")
|
|
|
|
| 29 |
dest = pathlib.Path(wp)
|
| 30 |
+
if not dest.exists() and url:
|
| 31 |
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
| 32 |
+
with st.spinner(f"Downloading model weights to {dest} …"):
|
| 33 |
+
urllib.request.urlretrieve(url, str(dest))
|
| 34 |
+
st.toast("Weights downloaded", icon="✅")
|
| 35 |
+
if not dest.exists() and not url:
|
| 36 |
+
st.warning(
|
| 37 |
+
f"Model weights not found at '{wp}'. "
|
| 38 |
+
"Upload the .pth there OR set MODEL_WEIGHTS_URL in Settings → Variables & secrets."
|
| 39 |
+
)
|
|
|
|
|
|
|
| 40 |
return str(dest)
|
| 41 |
|
| 42 |
@st.cache_resource(show_spinner=True)
|
| 43 |
+
def load_detector():
|
| 44 |
+
return Detector(weights_path=ensure_weights())
|
|
|
|
|
|
|
| 45 |
|
| 46 |
det = load_detector()
|
| 47 |
|
| 48 |
+
# ---- helpers ----
|
| 49 |
def cam_to_png_bytes(cam: np.ndarray) -> bytes:
|
|
|
|
| 50 |
cam = np.asarray(cam, dtype=np.float32)
|
| 51 |
+
cam = np.nan_to_num(cam, nan=0.0); cam = np.clip(cam, 0.0, 1.0)
|
|
|
|
| 52 |
rgb = (cm.magma(cam)[..., :3] * 255).astype(np.uint8)
|
| 53 |
+
buf = io.BytesIO(); Image.fromarray(rgb).save(buf, "PNG")
|
| 54 |
+
return buf.getvalue()
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def analyze(wav_bytes: bytes, source_hint: str):
|
|
|
|
| 57 |
proba = det.predict_proba(wav_bytes, source_hint=source_hint)
|
| 58 |
+
exp = det.explain(wav_bytes, source_hint=source_hint)
|
| 59 |
return proba, exp
|
| 60 |
|
| 61 |
+
# ---- UI ----
|
| 62 |
st.set_page_config(page_title="Voice Guard", page_icon="🛡️", layout="wide")
|
| 63 |
st.title("🛡️ Voice Guard — Human vs AI Speech")
|
| 64 |
|
| 65 |
+
left, right = st.columns([1,2], gap="large")
|
|
|
|
| 66 |
with left:
|
| 67 |
st.subheader("Input")
|
| 68 |
+
tab_rec, tab_up = st.tabs(["🎙️ Microphone", "📁 Upload"])
|
| 69 |
+
wav_bytes, source_hint = None, None
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
with tab_rec:
|
| 72 |
+
st.caption("Record ~3–7 s. If mic fails, use Upload.")
|
|
|
|
| 73 |
try:
|
| 74 |
from audio_recorder_streamlit import audio_recorder
|
| 75 |
+
audio = audio_recorder(text="Record", recording_color="#ff6a00",
|
| 76 |
+
neutral_color="#2b2b2b", icon_size="2x")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if audio:
|
| 78 |
+
wav_bytes, source_hint = audio, "microphone"
|
|
|
|
| 79 |
st.audio(wav_bytes, format="audio/wav")
|
| 80 |
except Exception:
|
| 81 |
+
st.info("Recorder not available—use Upload tab.")
|
| 82 |
|
| 83 |
+
with tab_up:
|
| 84 |
+
f = st.file_uploader("Upload wav/mp3/m4a/aac", type=["wav","mp3","m4a","aac"])
|
| 85 |
+
if f:
|
| 86 |
+
wav_bytes, source_hint = f.read(), "upload"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
st.audio(wav_bytes)
|
| 88 |
|
| 89 |
st.markdown("---")
|
| 90 |
+
run = st.button("🔍 Analyze", type="primary", use_container_width=True,
|
| 91 |
+
disabled=wav_bytes is None)
|
|
|
|
| 92 |
|
| 93 |
with right:
|
| 94 |
st.subheader("Results")
|
|
|
|
| 95 |
if run and wav_bytes:
|
| 96 |
try:
|
| 97 |
with st.spinner("Analyzing…"):
|
| 98 |
proba, exp = analyze(wav_bytes, source_hint or "auto")
|
| 99 |
+
ph = float(proba.get("human",0.0)); pa = float(proba.get("ai",0.0))
|
| 100 |
+
label = (proba.get("label","human") or "human").upper()
|
| 101 |
+
thr = float(proba.get("threshold",0.5))
|
| 102 |
+
rule = proba.get("decision","threshold")
|
| 103 |
+
thr_src = proba.get("threshold_source","—")
|
|
|
|
|
|
|
| 104 |
rscore = proba.get("replay_score", None)
|
| 105 |
|
| 106 |
+
c1,c2,c3 = st.columns(3)
|
| 107 |
+
with c1: st.metric("Human", f"{ph*100:.1f}%")
|
| 108 |
+
with c2: st.metric("AI", f"{pa*100:.1f}%")
|
|
|
|
|
|
|
| 109 |
with c3:
|
| 110 |
+
color = "#22c55e" if label=="HUMAN" else "#fb7185"
|
| 111 |
+
st.markdown(f"**Final Label:** <span style='color:{color}'>{label}</span>", unsafe_allow_html=True)
|
| 112 |
+
st.caption(f"thr({thr_src})={thr:.2f} • rule={rule} • replay={'—' if rscore is None else f'{float(rscore):.2f}'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
st.markdown("##### Explanation Heatmap")
|
| 115 |
cam = np.asarray(exp.get("cam"), dtype=np.float32)
|
| 116 |
+
st.image(cam_to_png_bytes(cam), caption="Spectrogram importance", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
with st.expander("Raw JSON (debug)"):
|
| 119 |
st.json({"proba": proba, "explain": {"cam_shape": list(cam.shape)}})
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
st.error(f"Analyze failed: {e}")
|
| 122 |
|
| 123 |
+
st.caption("Upload 3–7s clips for the most reliable experience across browsers.")
|