Danielamn026's picture
Add: deteccion automatica de casos con etiqueta dudosa
6396ba1 verified
Raw History Blame Contribute Delete
12.5 kB
# Clasificador de mensajes en emergencias - CrisisBench
# Proyecto final Tecnicas de Aprendizaje de Maquina - PUJ 2026-2
import re
import html
import ftfy
import pickle
import torch
import gradio as gr
import spaces
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# ============================================================
# 1. PREPROCESAMIENTO
# ============================================================
RT_RE = re.compile(r'^(rt\s+@\w+:?\s*)+')
URL_RE = re.compile(r'https?://\S+|www\.\S+')
USER_RE = re.compile(r'@\w+')
HASH_RE = re.compile(r'#(\w+)')
WS_RE = re.compile(r'\s+')
def limpiar(t: str) -> str:
t = ftfy.fix_text(t)
t = html.unescape(t)
t = RT_RE.sub("", t)
t = URL_RE.sub(" <URL> ", t)
t = USER_RE.sub(" <USER> ", t)
t = HASH_RE.sub(lambda m: " " + re.sub(r"(?<=[a-z])(?=[A-Z])", " ", m.group(1)) + " ", t)
return WS_RE.sub(" ", t).strip()
# ============================================================
# 2. CARGAR MODELOS
# ============================================================
print("Cargando XLM-R...")
XLM_PATH = "Danielamn026/crisisbench-xlmr"
tok = AutoTokenizer.from_pretrained(XLM_PATH)
model_xlm = AutoModelForSequenceClassification.from_pretrained(XLM_PATH)
model_xlm.eval()
print("XLM-R listo")
print("Cargando LogReg + TF-IDF...")
with open("logreg.pkl", "rb") as f:
logreg = pickle.load(f)
with open("tfidf_vectorizer.pkl", "rb") as f:
vec = pickle.load(f)
print("LogReg listo")
MAX_LEN = 64
# ============================================================
# 3. HEURISTICA: detectar mensajes propensos a etiqueta dudosa
# ============================================================
# Patrones observados en la revision manual de 100 mensajes (seccion 4.8 del cuaderno)
# donde el dataset marca "informativo" pero son condolencias/reacciones emocionales
PATRONES_CONDOLENCIA = [
r'\b(pray|prayers|praying|rezar|rezando|oraciones?)\b',
r'\b(fuerza|animo|anim[oó])\b',
r'\b(thoughts?|pensamientos?)\s+(and|y)\s+(prayers?|oraciones?)\b',
r'\b(condolencias?|condolences?)\b',
r'\b(tristeza|triste|sad|sorry|lo\s+siento)\b',
r'\b(que\s+(horror|tragedia|pena|lastima|desgracia))\b',
r'\b(rest\s+in\s+peace|rip|descanse(n)?\s+en\s+paz|dep)\b',
r'#(pray|fuerza|prayfor|rip\w*|descansa)',
]
RE_CONDOLENCIA = re.compile("|".join(PATRONES_CONDOLENCIA), re.IGNORECASE)
# Palabras tipicas de mensajes genuinamente operativos (si aparecen, es menos probable que sea solo condolencia)
PATRONES_OPERATIVO = [
r'\b\d+\s*(muertos?|heridos?|desaparecidos?|dead|injured|missing|casualties?|victims?)\b',
r'\b(evacuar|evacuation|rescate|rescue|emergencia|emergency|911|112|hospital)\b',
r'\b(magnitud|magnitude|epicentro|epicenter|richter|escala)\b',
r'\b(colapso|collapsed|destruyo|destroyed|derrumbe|hundio|sank)\b',
r'\b(alert|alerta|aviso|warning|tsunami|replicas?|aftershock)\b',
]
RE_OPERATIVO = re.compile("|".join(PATRONES_OPERATIVO), re.IGNORECASE)
def detectar_caso_dudoso(texto: str, p_xlm: float, p_lr: float) -> str:
"""Devuelve HTML de aviso si el caso puede ser una etiqueta dudosa del dataset."""
tiene_condolencia = bool(RE_CONDOLENCIA.search(texto))
tiene_operativo = bool(RE_OPERATIVO.search(texto))
ambos_altos = p_xlm >= 0.5 and p_lr >= 0.5
# Caso tipico: parece condolencia, NO tiene datos operativos, y los modelos lo marcan como informativo
if tiene_condolencia and not tiene_operativo and ambos_altos:
return """
<div style="padding: 14px 18px; background: rgba(239, 68, 68, 0.08); border-left: 3px solid #ef4444; border-radius: 8px; color: #fca5a5; font-size: 13.5px; margin-top: 12px; line-height: 1.5;">
<strong>⚠ Atencion: este es probablemente un caso de etiqueta dudosa del dataset.</strong><br>
El mensaje contiene expresiones de condolencia o reaccion emocional (sin datos operativos concretos).
La revision manual mostro que el 32% de los mensajes en espanol marcados como "informativo" en CrisisBench
son en realidad de este tipo. Los modelos aprendieron ese sesgo. <strong>Para uso real, un humano deberia descartarlo.</strong>
</div>
"""
return ""
# ============================================================
# 4. RENDER DE RESULTADOS
# ============================================================
def render_resultado(p_inf: float) -> str:
es_inf = p_inf >= 0.5
pct = p_inf * 100
chip_bg = "#10b981" if es_inf else "#6b7280"
chip_label = "INFORMATIVO" if es_inf else "NO INFORMATIVO"
chip_icon = "✓" if es_inf else "✕"
return f"""
<div style="padding: 16px; border-radius: 12px; background: rgba(255,255,255,0.03); border: 1px solid rgba(255,255,255,0.08);">
<div style="display: inline-block; padding: 6px 14px; background: {chip_bg}; color: white; border-radius: 999px; font-weight: 700; font-size: 13px; letter-spacing: 0.5px;">
{chip_icon} {chip_label}
</div>
<div style="margin-top: 14px; font-size: 13px; color: #9ca3af;">Probabilidad de ser informativo</div>
<div style="display: flex; align-items: center; gap: 12px; margin-top: 6px;">
<div style="flex: 1; background: rgba(255,255,255,0.08); height: 10px; border-radius: 999px; overflow: hidden;">
<div style="width: {pct:.1f}%; height: 100%; background: linear-gradient(90deg, {chip_bg}dd, {chip_bg}); border-radius: 999px;"></div>
</div>
<div style="font-size: 20px; font-weight: 700; color: white; min-width: 70px; text-align: right;">{pct:.1f}%</div>
</div>
</div>
"""
def render_acuerdo(pred_xlm_inf, pred_lr_inf) -> str:
if pred_xlm_inf == pred_lr_inf:
return """
<div style="padding: 12px 16px; background: rgba(16, 185, 129, 0.1); border-left: 3px solid #10b981; border-radius: 8px; color: #6ee7b7; font-size: 14px;">
✓ <strong>Los dos modelos coinciden.</strong>
</div>
"""
return """
<div style="padding: 12px 16px; background: rgba(245, 158, 11, 0.1); border-left: 3px solid #f59e0b; border-radius: 8px; color: #fcd34d; font-size: 14px;">
⚠ <strong>Los modelos discrepan.</strong> Suele indicar un mensaje ambiguo o un vocabulario poco conocido para el modelo clasico.
</div>
"""
# ============================================================
# 5. INFERENCIA
# ============================================================
@spaces.GPU(duration=30)
def clasificar(mensaje: str):
if not mensaje or not mensaje.strip():
return "", "<div style='color:#9ca3af'>Esperando mensaje...</div>", "<div style='color:#9ca3af'>Esperando mensaje...</div>", ""
texto_limpio = limpiar(mensaje)
device = "cuda" if torch.cuda.is_available() else "cpu"
model_xlm.to(device)
with torch.no_grad():
enc = tok(texto_limpio, padding="max_length", truncation=True,
max_length=MAX_LEN, return_tensors="pt").to(device)
logits = model_xlm(**enc).logits
probs_xlm = torch.softmax(logits, dim=-1).cpu().numpy()[0]
p_xlm = float(probs_xlm[1])
X = vec.transform([texto_limpio])
p_lr = float(logreg.predict_proba(X)[0, 1])
aviso_html = render_acuerdo(p_xlm >= 0.5, p_lr >= 0.5) + detectar_caso_dudoso(mensaje, p_xlm, p_lr)
return (
texto_limpio,
render_resultado(p_xlm),
render_resultado(p_lr),
aviso_html,
)
# ============================================================
# 6. INTERFAZ
# ============================================================
CUSTOM_CSS = """
.gradio-container { font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important; }
#header-banner {
background: linear-gradient(135deg, #dc2626 0%, #f59e0b 100%);
padding: 32px 28px;
border-radius: 16px;
margin-bottom: 24px;
color: white;
box-shadow: 0 10px 30px rgba(220, 38, 38, 0.25);
}
#header-banner h1 { margin: 0; font-size: 28px; font-weight: 800; color: white !important; }
#header-banner p { margin: 8px 0 0 0; opacity: 0.95; font-size: 15px; color: white !important; }
.model-title { font-size: 15px; font-weight: 700; margin-bottom: 4px; color: #f3f4f6; }
.model-subtitle { font-size: 12px; color: #9ca3af; margin-bottom: 12px; }
#footer-info { background: rgba(255,255,255,0.02); border-radius: 12px; padding: 20px 24px; margin-top: 24px; border: 1px solid rgba(255,255,255,0.05); }
#footer-info h3 { margin-top: 0; font-size: 16px; color: #f3f4f6; }
#footer-info ul { padding-left: 20px; color: #d1d5db; font-size: 13.5px; line-height: 1.6; }
"""
EJEMPLOS = [
["BREAKING: 7.2 magnitude earthquake hits coastal region, buildings collapsed in downtown area"],
["Our thoughts and prayers go out to everyone affected by the hurricane"],
["Alerta sanitaria oficial en Iquique tras sismo: no consumir agua de la red"],
["Que tristeza lo que pasa en Venezuela. #FuerzaVenezuela"],
["Rescue teams working to free 20 people trapped in collapsed hotel in downtown Port-au-Prince"],
["Peanut butter jelly sandwiches are the best"],
["RT @24HorasTVN: Imagen del conductor del tren accidentado en Espana da la vuelta al mundo"],
["Happy birthday bro @Hurricane_Dolce"],
]
with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="orange", neutral_hue="slate"),
css=CUSTOM_CSS,
title="CrisisBench Classifier") as app:
gr.HTML("""
<div id="header-banner">
<h1>🚨 Filtro de mensajes en emergencias</h1>
<p>Clasifica tuits de desastres como <strong>informativos</strong> (datos operativos) o <strong>no informativos</strong> (opiniones, ruido). Compara dos modelos en vivo.</p>
</div>
""")
with gr.Row():
with gr.Column(scale=5):
entrada = gr.Textbox(
label="📝 Mensaje a clasificar",
placeholder="Pega aqui un tuit, un mensaje de Facebook o un WhatsApp sobre un desastre...",
lines=5,
)
boton = gr.Button("Clasificar mensaje", variant="primary", size="lg")
gr.Examples(
examples=EJEMPLOS,
inputs=entrada,
label="Prueba con estos ejemplos:",
examples_per_page=8,
)
with gr.Column(scale=6):
texto_limpio = gr.Textbox(
label="🧼 Texto despues del preprocesamiento",
interactive=False,
lines=2,
)
with gr.Row():
with gr.Column():
gr.HTML('<div class="model-title">XLM-RoBERTa (fine-tuned)</div><div class="model-subtitle">Transformer multilingue preentrenado · 94 idiomas · F1 macro test: 0.873</div>')
out_xlm = gr.HTML("<div style='color:#9ca3af; padding:16px;'>Esperando mensaje...</div>")
with gr.Column():
gr.HTML('<div class="model-title">Regresion Logistica + TF-IDF</div><div class="model-subtitle">Modelo clasico rapido · F1 macro test: 0.856 · Costo cero</div>')
out_lr = gr.HTML("<div style='color:#9ca3af; padding:16px;'>Esperando mensaje...</div>")
acuerdo = gr.HTML("")
gr.HTML("""
<div id="footer-info">
<h3>Sobre esta aplicacion</h3>
<p style="color:#d1d5db; font-size:13.5px; line-height:1.6;">
Proyecto final de <strong>Tecnicas de Aprendizaje de Maquina</strong> · Pontificia Universidad Javeriana 2026-2 · Dataset <a href="https://huggingface.co/datasets/timhigins/crisisbench" style="color:#fca5a5;">timhigins/crisisbench</a> (166k mensajes, 61 eventos de desastre, 7 idiomas).
</p>
<h3>Limitaciones conocidas</h3>
<ul>
<li><strong>Calidad de etiquetas</strong>: una revision manual de 100 mensajes mostro 18% de desacuerdo con la etiqueta oficial. Los modelos aprenden ese ruido. Por eso la app muestra un aviso cuando detecta un caso tipico de etiqueta dudosa.</li>
<li><strong>Espanol</strong>: solo 3% del corpus, con 32% de etiquetas discutibles en la clase "informativo". Precision menor que en ingles.</li>
<li><strong>Eventos nuevos</strong>: XLM-R generaliza mejor (caida de 9 puntos F1 en evento Chile dejado fuera) que LogReg (caida de 22 puntos).</li>
<li><strong>No reemplaza a un humano</strong>: es un filtro de primer paso para reducir el volumen a revisar en una emergencia.</li>
</ul>
</div>
""")
boton.click(clasificar, inputs=entrada,
outputs=[texto_limpio, out_xlm, out_lr, acuerdo])
if __name__ == "__main__":
app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)