"""
src/app/streamlit_app.py
App SignalMod — detección de hate speech estilo YouTube.
Ejecutar: streamlit run src/app/streamlit_app.py
"""
import html
import sys
import random
import datetime
from pathlib import Path
import streamlit as st
import pandas as pd
from transformers.utils import logging
logging.set_verbosity_error()
# ── Paths ─────────────────────────────────────────────────────────────────────
PROJECT_ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(PROJECT_ROOT))
try:
from src.service.model_service import ModelService, AVAILABLE_MODELS
except ImportError:
sys.path.insert(0, str(Path(__file__).parent.parent))
from service.model_service import ModelService, AVAILABLE_MODELS
# ── Config ────────────────────────────────────────────────────────────────────
st.set_page_config(
page_title="SignalMod",
page_icon="🎬",
layout="wide",
initial_sidebar_state="expanded",
)
# ── CSS ───────────────────────────────────────────────────────────────────────
# Nota: NO ocultamos el header completo para preservar el botón de toggle del sidebar.
# Solo ocultamos el menú hamburguesa y el footer de Streamlit.
st.markdown("""
""", unsafe_allow_html=True)
# ── Session state init ────────────────────────────────────────────────────────
def _init_state():
defaults = {
"page" : "Home",
"selected_model": list(AVAILABLE_MODELS.keys())[0],
"threshold" : 0.5,
"pending_modal" : None, # dict con el comentario pendiente de decisión
"comments": [
{"user": "user_prime", "initial": "U",
"text": "Excelente video, muy informativo!", "time": "1 h",
"is_toxic": False, "probability": 0.04, "labels": []},
{"user": "troll_master", "initial": "T",
"text": "Esto es una basura completa", "time": "30 min",
"is_toxic": True, "probability": 0.91, "labels": ["Insulto","Agresividad"]},
{"user": "curious_viewer", "initial": "C",
"text": "¿Alguien puede explicar esto mejor?", "time": "15 min",
"is_toxic": False, "probability": 0.07, "labels": []},
],
"hub_history": [
{"Usuario": "@user_992", "Comentario": '"No puedo creer que seas tan..."', "Score": 0.94, "Acción": "🚫 Bloqueado"},
{"Usuario": "@alpha_mod", "Comentario": '"Spam repetitivo de enlaces."', "Score": 0.82, "Acción": "🚩 Revisión"},
{"Usuario": "@anon_404", "Comentario": '"Discurso de odio en contexto."', "Score": 0.98, "Acción": "📋 Archivado"},
{"Usuario": "@user_123", "Comentario": '"¡Gran contenido, sigan!"', "Score": 0.03, "Acción": "✅ Aprobado"},
{"Usuario": "@viewer_x", "Comentario": '"Esta gente debería desaparecer."',"Score": 0.97, "Acción": "🚫 Bloqueado"},
],
}
for k, v in defaults.items():
if k not in st.session_state:
st.session_state[k] = v
_init_state()
# ── Model cache ───────────────────────────────────────────────────────────────
@st.cache_resource(show_spinner="Cargando modelo...")
def get_service(model_name: str) -> ModelService:
return ModelService(model_name, PROJECT_ROOT)
# ══════════════════════════════════════════════════════════════════════════════
# SIDEBAR
# ══════════════════════════════════════════════════════════════════════════════
def render_sidebar():
with st.sidebar:
# Logo
st.markdown(
"
"
"🎬 SignalMod
"
""
"Signal within the Noise
",
unsafe_allow_html=True,
)
nav = {"Home": "🏠", "Moderator Hub": "📊", "Settings": "⚙️"}
for page, icon in nav.items():
label = f"{icon} {page}"
clicked = st.button(label, key=f"nav_{page}", use_container_width=True)
if clicked:
st.session_state.page = page
st.rerun()
st.divider()
# Info modelo activo
model_short = st.session_state.selected_model.split("(")[0].strip()
tox_cnt = sum(1 for c in st.session_state.comments if c["is_toxic"])
total_c = len(st.session_state.comments)
st.markdown(
f"",
unsafe_allow_html=True,
)
# ══════════════════════════════════════════════════════════════════════════════
# MODAL — toxicidad detectada
# ══════════════════════════════════════════════════════════════════════════════
@st.dialog("⚠️ Aviso de Toxicidad Detectada")
def show_toxicity_modal():
"""
@st.dialog crea una ventana modal nativa de Streamlit (1.32+).
Cuando se llama a la función decorada, Streamlit renderiza el contenido
dentro de un overlay modal y pausa la ejecución normal del script.
"""
data = st.session_state.pending_modal
if not data:
st.rerun()
return
text = data["text"]
prob = data["probability"]
lbls = data["labels"]
pct = int(prob * 100)
color = "#cc0000" if pct >= 70 else "#ff6d00" if pct >= 40 else "#f5a623"
st.markdown(
"⚠️
",
unsafe_allow_html=True,
)
st.markdown(
f""
f""{html.escape(text[:140])}{'...' if len(text)>140 else ''}"
",
unsafe_allow_html=True,
)
# Barra de toxicidad
st.markdown(
f""
f"ÍNDICE DE TOXICIDAD"
f"{pct}%
"
f"",
unsafe_allow_html=True,
)
# Etiquetas
if lbls:
tags = " ".join(
f""
f"🚩 {html.escape(l)}"
for l in lbls
)
st.markdown(f"{tags}
", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
if st.button("✏️ Editar comentario", use_container_width=True, type="primary"):
st.session_state.pending_modal = None
st.rerun()
with col2:
if st.button("Publicar de todas maneras", use_container_width=True):
# Publicar aunque sea tóxico
c = st.session_state.pending_modal
st.session_state.comments.append(c)
st.session_state.hub_history.insert(0, {
"Usuario" : "@usuario",
"Comentario": f'"{c["text"][:45]}..."',
"Score" : round(c["probability"], 2),
"Acción" : "⚠️ Override usuario",
})
st.session_state.pending_modal = None
st.rerun()
# ══════════════════════════════════════════════════════════════════════════════
# HOME — interfaz estilo YouTube
# ══════════════════════════════════════════════════════════════════════════════
def render_home():
# Disparar modal si hay comentario pendiente
if st.session_state.pending_modal:
show_toxicity_modal()
col_main, col_right = st.columns([2.8, 1], gap="large")
with col_main:
# Video
st.markdown(
"",
unsafe_allow_html=True,
)
st.markdown(
"AI Moderation Demo — Detección de Hate Speech en tiempo real
"
"15k vistas · 2 horas atrás
",
unsafe_allow_html=True,
)
row_ch, row_sub = st.columns([3, 1])
with row_ch:
st.markdown(
""
"
S
"
"
SignalMod AI
"
"
1.2M suscriptores
",
unsafe_allow_html=True,
)
st.divider()
# ── Comentarios ────────────────────────────────────────────────────
tox_cnt = sum(1 for c in st.session_state.comments if c["is_toxic"])
st.markdown(
f"{len(st.session_state.comments)} Comentarios "
f"· {tox_cnt} detectados
",
unsafe_allow_html=True,
)
# Input de nuevo comentario
new_text = st.text_area(
"Escribe un comentario...",
height=80, label_visibility="collapsed",
key="comment_input",
placeholder="Escribe un comentario...",
)
# Análisis en tiempo real (solo cuando hay texto)
analysis = None
if new_text.strip():
svc = get_service(st.session_state.selected_model)
analysis = svc.predict(new_text)
pct = int(analysis["probability"] * 100)
color = "#cc0000" if pct >= 70 else "#f5a623" if pct >= 40 else "#00c853"
verdict = "TÓXICO" if analysis["is_toxic"] else "SEGURO"
v_color = "#cc0000" if analysis["is_toxic"] else "#00c853"
st.markdown(
f""
f"
🔍 Analizando..."
f"
{verdict}"
f"
Toxicidad: {pct}%"
f"
",
unsafe_allow_html=True,
)
col_c, col_p = st.columns([1, 1])
with col_c:
if st.button("Cancelar", use_container_width=True):
st.rerun()
with col_p:
post = st.button("Comentar", type="primary", use_container_width=True)
# Procesar envío
if post and new_text.strip():
if analysis is None:
svc = get_service(st.session_state.selected_model)
analysis = svc.predict(new_text)
comment_obj = {
"user" : "usuario",
"initial" : "U",
"text" : new_text.strip(),
"time" : "ahora",
"is_toxic" : analysis["is_toxic"],
"probability": analysis["probability"],
"labels" : analysis["labels"],
}
if analysis["is_toxic"]:
# Guardar en pendiente y mostrar modal en el próximo render
st.session_state.pending_modal = comment_obj
st.rerun()
else:
# Publicar directamente
st.session_state.comments.append(comment_obj)
st.session_state.hub_history.insert(0, {
"Usuario" : "@usuario",
"Comentario": f'"{new_text.strip()[:45]}{"..." if len(new_text)>45 else ""}"',
"Score" : round(analysis["probability"], 2),
"Acción" : "✅ Aprobado",
})
st.rerun()
# ── Lista de comentarios ───────────────────────────────────────────
for c in reversed(st.session_state.comments):
is_tox = c["is_toxic"]
pct = int(c["probability"] * 100)
av_class = "c-avatar" if is_tox else "c-avatar safe"
badge = (
"TÓXICO" if is_tox
else "SEGURO"
)
text_class = "c-text toxic" if is_tox else "c-text"
flagged = "🚩 Flagged for review
" if is_tox else ""
# html.escape() protege contra caracteres que rompen el HTML
safe_text = html.escape(c["text"])
safe_user = html.escape(c["user"])
initial = html.escape(c.get("initial", c["user"][0].upper()))
st.markdown(
f"",
unsafe_allow_html=True,
)
# ── Columna derecha ────────────────────────────────────────────────────
with col_right:
st.markdown("**Sugeridos**")
suggested = [
("🤖", "Understanding Transformer Models...", "Neural Systems", "89k · 1 día"),
("🎓", "The Future of Content Moderation", "Tech Ethics Pro", "1.4M · 2 sem"),
("📡", "Signal vs Noise: SignalMod Deep Dive","SignalMod AI", "250k · 3 días"),
("💡", "Why AI Moderation is Harder Than...", "Ethics in Code", "45k · 5 h"),
("🔬", "Hate Speech Detection 2024", "AI Research Lab", "12k · 1 sem"),
]
for emoji, title, ch, meta in suggested:
st.markdown(
f""
f"
{emoji}
"
f"
"
f"
{html.escape(title)}
"
f"
{html.escape(ch)}
"
f"
{html.escape(meta)}
"
f"
"
f"
",
unsafe_allow_html=True,
)
# ══════════════════════════════════════════════════════════════════════════════
# MODERATOR HUB
# ══════════════════════════════════════════════════════════════════════════════
def render_hub():
try:
import plotly.graph_objects as go
except ImportError:
st.error("Instala plotly: pip install plotly")
return
st.markdown("## 📊 Panel de Estadísticas")
# ── Cards de configuración ──────────────────────────────────────────────
model_short = st.session_state.selected_model.split("(")[0].strip()
c1, c2, c3 = st.columns(3)
for col, label, val in [
(c1, "MODEL ARCHITECTURE", model_short),
(c2, "CONFIDENCE THRESHOLD", f"{st.session_state.threshold:.2f} Alpha"),
(c3, "LANGUAGE COVERAGE", "English"),
]:
with col:
st.markdown(
f""
f"
{label}
"
f"
"
f"{html.escape(str(val))}
",
unsafe_allow_html=True,
)
st.write("")
# ── KPIs ───────────────────────────────────────────────────────────────
total = len(st.session_state.comments) + 100
tox_cnt = sum(1 for c in st.session_state.comments if c["is_toxic"]) + 5
tox_rate = tox_cnt / total * 100
m1, m2, m3 = st.columns(3)
m1.metric("💬 Total comentarios", f"{total:,}", "+12%")
m2.metric("☠️ Tasa de toxicidad", f"{tox_rate:.1f}%",
f"+0.8%", delta_color="inverse")
m3.metric("🎯 F1 Score", "0.7579", "Stable")
st.divider()
# ── Gráficos ───────────────────────────────────────────────────────────
gcol, pcol = st.columns([2.2, 1])
with gcol:
days = ["Lun","Mar","Mié","Jue","Vie","Sáb","Dom"]
vals = [random.randint(30, 80) for _ in days]
vals[3] = max(vals) + 25
colors = ["#cc0000" if i == 3 else "#b3c6ff" for i in range(7)]
fig = go.Figure(go.Bar(x=days, y=vals, marker_color=colors, width=0.55))
fig.update_layout(
title="Tendencias de Toxicidad (7D)",
paper_bgcolor="#ffffff", plot_bgcolor="#ffffff",
margin=dict(l=20, r=20, t=40, b=20), height=260,
font=dict(size=11, color="#0f0f0f"),
)
fig.update_yaxes(showgrid=True, gridcolor="#f0f0f0", zeroline=False)
fig.update_xaxes(showgrid=False)
st.plotly_chart(fig, use_container_width=True)
with pcol:
fig2 = go.Figure(go.Pie(
labels=["Hate Speech","Insulto","Agresividad"],
values=[45, 35, 20],
hole=0.58,
marker_colors=["#cc0000","#0f0f0f","#909090"],
textfont_size=11,
))
fig2.update_layout(
title="Categorías",
paper_bgcolor="#ffffff",
margin=dict(l=10, r=10, t=40, b=10), height=260,
legend=dict(font=dict(size=10), orientation="v"),
font=dict(size=11, color="#0f0f0f"),
)
st.plotly_chart(fig2, use_container_width=True)
# ── Historial ──────────────────────────────────────────────────────────
st.markdown("### Historial Reciente")
df = pd.DataFrame(st.session_state.hub_history)
if not df.empty:
st.dataframe(
df, use_container_width=True, hide_index=True,
column_config={
"Score": st.column_config.ProgressColumn(
"Score", min_value=0, max_value=1, format="%.2f"
)
},
)
# ══════════════════════════════════════════════════════════════════════════════
# SETTINGS
# ══════════════════════════════════════════════════════════════════════════════
def render_settings():
st.markdown("## ⚙️ Ajustes")
# ── Selección de modelo ─────────────────────────────────────────────────
st.markdown("### 🤖 Modelo de detección",)
st.caption(
"Los modelos HuggingFace se descargan la primera vez (~300–600 MB). "
"Requieren: `pip install transformers torch sentencepiece`"
)
st.write("")
# Usamos st.radio para la selección — sin bugs de HTML
model_names = list(AVAILABLE_MODELS.keys())
current_idx = model_names.index(st.session_state.selected_model) \
if st.session_state.selected_model in model_names else 0
chosen = st.radio(
"Seleccionar modelo",
model_names,
index=current_idx,
label_visibility="collapsed",
)
if chosen != st.session_state.selected_model:
st.session_state.selected_model = chosen
st.rerun()
# Ficha del modelo seleccionado
info = AVAILABLE_MODELS[st.session_state.selected_model]
st.markdown(
f""
f"
{info['icon']} {html.escape(st.session_state.selected_model)}
"
f"
{html.escape(info['description'])}
"
f"
"
f"⚡ {html.escape(info['speed'])}"
f"🎯 {html.escape(info['accuracy'])}"
f"📦 {html.escape(info['requires'])}"
f"
",
unsafe_allow_html=True,
)
# Info sobre modelo fine-tuneado
if st.session_state.selected_model == "Modelo fine-tuneado (local)":
path = PROJECT_ROOT / "models" / "finetuned_hf"
if path.exists():
st.success(f"✅ Modelo encontrado en `{path}`")
else:
st.warning(
f"⚠️ No se encontró el modelo en `{path}`. "
f"Ejecuta el **notebook 08** para generar el modelo fine-tuneado."
)
st.divider()
# ── Umbral de confianza ─────────────────────────────────────────────────
st.markdown("### 🎚️ Umbral de confianza")
st.caption("Probabilidad mínima para marcar un comentario como tóxico.")
new_thr = st.slider(
"Umbral",
min_value=0.3, max_value=0.9, step=0.05,
value=st.session_state.threshold,
label_visibility="collapsed",
format="%.2f",
)
if new_thr != st.session_state.threshold:
st.session_state.threshold = new_thr
st.info(f"Umbral actualizado: **{new_thr:.2f}**")
ta, tb = st.columns(2)
ta.info(f"⬇️ **{new_thr:.2f}** bajo → más FP (más censura)", icon="⚠️")
tb.info(f"⬆️ **{new_thr:.2f}** alto → más FN (más escapes)", icon="⚠️")
st.divider()
# ── Test rápido ─────────────────────────────────────────────────────────
st.markdown("### 🧪 Probar modelo")
test_txt = st.text_input(
"Texto a analizar",
placeholder="Ej: This is absolutely stupid and racist...",
label_visibility="collapsed",
)
if st.button("Analizar", type="primary") and test_txt.strip():
with st.spinner("Analizando..."):
svc = get_service(st.session_state.selected_model)
res = svc.predict(test_txt)
pct = int(res["probability"] * 100)
verdict = "🔴 TÓXICO" if res["is_toxic"] else "🟢 SEGURO"
st.markdown(f"**{verdict}** — {pct}% de toxicidad")
st.progress(res["probability"])
if res["labels"]:
st.markdown(f"**Categorías:** {', '.join(res['labels'])}")
if "error" in res:
st.error(f"Error: {res['error']}")
st.caption(f"Modelo: {res['model_used']}")
# ══════════════════════════════════════════════════════════════════════════════
# MAIN
# ══════════════════════════════════════════════════════════════════════════════
def main():
render_sidebar()
page = st.session_state.page
if page == "Home":
render_home()
elif page == "Moderator Hub":
render_hub()
elif page == "Settings":
render_settings()
if __name__ == "__main__":
main()