Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,12 +4,10 @@ from PIL import Image
|
|
| 4 |
import sys
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Agregamos 'src' al path de Python para que pueda encontrar los módulos
|
| 12 |
-
sys.path.append(src_path)
|
| 13 |
|
| 14 |
try:
|
| 15 |
from inference import MemePredictor
|
|
@@ -27,31 +25,31 @@ st.set_page_config(
|
|
| 27 |
# --- 2. ESTILOS CSS "APPLE STYLE" ---
|
| 28 |
st.markdown("""
|
| 29 |
<style>
|
| 30 |
-
/* Tipografía limpia
|
| 31 |
html, body, [class*="css"] {
|
| 32 |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 33 |
color: #1d1d1f;
|
| 34 |
}
|
| 35 |
|
| 36 |
-
/* Ocultar elementos nativos
|
| 37 |
#MainMenu {visibility: hidden;}
|
| 38 |
footer {visibility: hidden;}
|
| 39 |
header {visibility: hidden;}
|
| 40 |
|
| 41 |
-
/* Contenedor principal con más aire */
|
| 42 |
.block-container {
|
| 43 |
padding-top: 3rem;
|
| 44 |
padding-bottom: 3rem;
|
| 45 |
max-width: 700px;
|
| 46 |
}
|
| 47 |
-
|
|
|
|
| 48 |
div.stButton > button:first-child {
|
| 49 |
-
background-color: #0071e3;
|
| 50 |
color: white;
|
| 51 |
border: none;
|
| 52 |
border-radius: 12px;
|
| 53 |
-
padding: 15px 30px;
|
| 54 |
-
font-size: 18px;
|
| 55 |
font-weight: 600;
|
| 56 |
width: 100%;
|
| 57 |
transition: all 0.2s ease;
|
|
@@ -63,12 +61,13 @@ st.markdown("""
|
|
| 63 |
transform: scale(1.01);
|
| 64 |
}
|
| 65 |
|
| 66 |
-
/*
|
| 67 |
div[role="radiogroup"] label > div:first-child {
|
| 68 |
background-color: #0071e3 !important;
|
| 69 |
border-color: #0071e3 !important;
|
| 70 |
}
|
| 71 |
-
|
|
|
|
| 72 |
div[data-testid="stMetric"] {
|
| 73 |
background-color: #f5f5f7;
|
| 74 |
border-radius: 18px;
|
|
@@ -88,19 +87,17 @@ st.markdown("""
|
|
| 88 |
color: #1d1d1f;
|
| 89 |
}
|
| 90 |
|
| 91 |
-
/* Imágenes
|
| 92 |
img {
|
| 93 |
border-radius: 18px;
|
| 94 |
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
|
| 95 |
}
|
| 96 |
|
| 97 |
-
/* File Uploader más limpio */
|
| 98 |
div[data-testid="stFileUploader"] {
|
| 99 |
margin-top: 20px;
|
| 100 |
margin-bottom: 20px;
|
| 101 |
}
|
| 102 |
|
| 103 |
-
/* Títulos */
|
| 104 |
h1, h2, h3 {
|
| 105 |
font-weight: 600;
|
| 106 |
letter-spacing: -0.02em;
|
|
@@ -116,36 +113,33 @@ def get_engine():
|
|
| 116 |
try:
|
| 117 |
predictor = get_engine()
|
| 118 |
except Exception as e:
|
| 119 |
-
st.error("
|
| 120 |
st.stop()
|
| 121 |
|
| 122 |
# --- 4. INTERFAZ DE USUARIO ---
|
| 123 |
|
| 124 |
-
# Encabezado Minimalista
|
| 125 |
st.markdown("<h1 style='text-align: center; margin-bottom: 10px;'>DIME-MEX</h1>", unsafe_allow_html=True)
|
| 126 |
st.markdown("<p style='text-align: center; color: #86868b; font-size: 18px;'>Análisis de contenido mediante IA.</p>", unsafe_allow_html=True)
|
| 127 |
|
| 128 |
-
st.write("")
|
| 129 |
|
| 130 |
-
# Configuración
|
| 131 |
with st.expander("Elegir modelo"):
|
| 132 |
task_mode = st.radio(
|
| 133 |
"Sensibilidad del modelo",
|
| 134 |
options=["simple", "complex"],
|
| 135 |
-
format_func=lambda x: "Estándar ( none, inappropriate, hate-speech )" if x == "simple" else "Detallado (
|
| 136 |
label_visibility="collapsed"
|
| 137 |
)
|
| 138 |
|
| 139 |
-
#
|
| 140 |
uploaded_file = st.file_uploader("Subir imagen", type=["jpg", "png", "jpeg"], label_visibility="collapsed")
|
| 141 |
|
| 142 |
if uploaded_file is not None:
|
| 143 |
-
# Mostrar imagen
|
| 144 |
image = Image.open(uploaded_file)
|
| 145 |
-
# CORRECCIÓN: Usamos use_container_width en lugar de use_column_width
|
| 146 |
st.image(image, use_container_width=True)
|
| 147 |
|
| 148 |
-
st.write("")
|
| 149 |
|
| 150 |
# Botón de Acción
|
| 151 |
if st.button("Analizar"):
|
|
@@ -154,11 +148,12 @@ if uploaded_file is not None:
|
|
| 154 |
result = predictor.predict(uploaded_file, task=task_mode)
|
| 155 |
|
| 156 |
if "error" in result:
|
| 157 |
-
|
|
|
|
| 158 |
else:
|
| 159 |
-
st.write("")
|
| 160 |
|
| 161 |
-
# --- RESULTADOS
|
| 162 |
col1, col2 = st.columns(2)
|
| 163 |
|
| 164 |
with col1:
|
|
@@ -167,20 +162,37 @@ if uploaded_file is not None:
|
|
| 167 |
with col2:
|
| 168 |
st.metric(label="Certeza", value=f"{result['confidence']:.1%}")
|
| 169 |
|
| 170 |
-
|
|
|
|
|
|
|
| 171 |
st.write("")
|
| 172 |
with st.expander("Detalles del análisis"):
|
| 173 |
st.caption("TEXTO DETECTADO")
|
| 174 |
-
|
|
|
|
| 175 |
|
| 176 |
st.write("")
|
| 177 |
st.caption("PROBABILIDADES")
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import sys
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# --- CORRECCIÓN DE RUTAS ---
|
| 8 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
+
src_path = os.path.join(current_dir, 'src') # Quitamos la barra inicial '/' que causaba error en Linux
|
| 10 |
+
sys.path.append(os.path.abspath(src_path))
|
|
|
|
|
|
|
| 11 |
|
| 12 |
try:
|
| 13 |
from inference import MemePredictor
|
|
|
|
| 25 |
# --- 2. ESTILOS CSS "APPLE STYLE" ---
|
| 26 |
st.markdown("""
|
| 27 |
<style>
|
| 28 |
+
/* Tipografía limpia */
|
| 29 |
html, body, [class*="css"] {
|
| 30 |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 31 |
color: #1d1d1f;
|
| 32 |
}
|
| 33 |
|
| 34 |
+
/* Ocultar elementos nativos */
|
| 35 |
#MainMenu {visibility: hidden;}
|
| 36 |
footer {visibility: hidden;}
|
| 37 |
header {visibility: hidden;}
|
| 38 |
|
|
|
|
| 39 |
.block-container {
|
| 40 |
padding-top: 3rem;
|
| 41 |
padding-bottom: 3rem;
|
| 42 |
max-width: 700px;
|
| 43 |
}
|
| 44 |
+
|
| 45 |
+
/* Botón Principal */
|
| 46 |
div.stButton > button:first-child {
|
| 47 |
+
background-color: #0071e3;
|
| 48 |
color: white;
|
| 49 |
border: none;
|
| 50 |
border-radius: 12px;
|
| 51 |
+
padding: 15px 30px;
|
| 52 |
+
font-size: 18px;
|
| 53 |
font-weight: 600;
|
| 54 |
width: 100%;
|
| 55 |
transition: all 0.2s ease;
|
|
|
|
| 61 |
transform: scale(1.01);
|
| 62 |
}
|
| 63 |
|
| 64 |
+
/* Radio Button */
|
| 65 |
div[role="radiogroup"] label > div:first-child {
|
| 66 |
background-color: #0071e3 !important;
|
| 67 |
border-color: #0071e3 !important;
|
| 68 |
}
|
| 69 |
+
|
| 70 |
+
/* Tarjetas de Métricas */
|
| 71 |
div[data-testid="stMetric"] {
|
| 72 |
background-color: #f5f5f7;
|
| 73 |
border-radius: 18px;
|
|
|
|
| 87 |
color: #1d1d1f;
|
| 88 |
}
|
| 89 |
|
| 90 |
+
/* Imágenes */
|
| 91 |
img {
|
| 92 |
border-radius: 18px;
|
| 93 |
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
|
| 94 |
}
|
| 95 |
|
|
|
|
| 96 |
div[data-testid="stFileUploader"] {
|
| 97 |
margin-top: 20px;
|
| 98 |
margin-bottom: 20px;
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
h1, h2, h3 {
|
| 102 |
font-weight: 600;
|
| 103 |
letter-spacing: -0.02em;
|
|
|
|
| 113 |
try:
|
| 114 |
predictor = get_engine()
|
| 115 |
except Exception as e:
|
| 116 |
+
st.error(f"Error iniciando el motor: {e}")
|
| 117 |
st.stop()
|
| 118 |
|
| 119 |
# --- 4. INTERFAZ DE USUARIO ---
|
| 120 |
|
|
|
|
| 121 |
st.markdown("<h1 style='text-align: center; margin-bottom: 10px;'>DIME-MEX</h1>", unsafe_allow_html=True)
|
| 122 |
st.markdown("<p style='text-align: center; color: #86868b; font-size: 18px;'>Análisis de contenido mediante IA.</p>", unsafe_allow_html=True)
|
| 123 |
|
| 124 |
+
st.write("")
|
| 125 |
|
| 126 |
+
# Configuración
|
| 127 |
with st.expander("Elegir modelo"):
|
| 128 |
task_mode = st.radio(
|
| 129 |
"Sensibilidad del modelo",
|
| 130 |
options=["simple", "complex"],
|
| 131 |
+
format_func=lambda x: "Estándar ( none, inappropriate, hate-speech )" if x == "simple" else "Detallado ( 6 clases )",
|
| 132 |
label_visibility="collapsed"
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Carga
|
| 136 |
uploaded_file = st.file_uploader("Subir imagen", type=["jpg", "png", "jpeg"], label_visibility="collapsed")
|
| 137 |
|
| 138 |
if uploaded_file is not None:
|
|
|
|
| 139 |
image = Image.open(uploaded_file)
|
|
|
|
| 140 |
st.image(image, use_container_width=True)
|
| 141 |
|
| 142 |
+
st.write("")
|
| 143 |
|
| 144 |
# Botón de Acción
|
| 145 |
if st.button("Analizar"):
|
|
|
|
| 148 |
result = predictor.predict(uploaded_file, task=task_mode)
|
| 149 |
|
| 150 |
if "error" in result:
|
| 151 |
+
# Mostrar error lógico (del backend)
|
| 152 |
+
st.error(f"Error de procesamiento: {result['error']}")
|
| 153 |
else:
|
| 154 |
+
st.write("")
|
| 155 |
|
| 156 |
+
# --- RESULTADOS ---
|
| 157 |
col1, col2 = st.columns(2)
|
| 158 |
|
| 159 |
with col1:
|
|
|
|
| 162 |
with col2:
|
| 163 |
st.metric(label="Certeza", value=f"{result['confidence']:.1%}")
|
| 164 |
|
| 165 |
+
st.progress(result['confidence'])
|
| 166 |
+
|
| 167 |
+
# --- DETALLES ---
|
| 168 |
st.write("")
|
| 169 |
with st.expander("Detalles del análisis"):
|
| 170 |
st.caption("TEXTO DETECTADO")
|
| 171 |
+
# Usamos .get() para evitar errores si la clave no existe
|
| 172 |
+
st.markdown(f"_{result.get('ocr_text', 'No disponible')}_")
|
| 173 |
|
| 174 |
st.write("")
|
| 175 |
st.caption("PROBABILIDADES")
|
| 176 |
+
|
| 177 |
+
if 'all_labels' in result and 'probabilities' in result:
|
| 178 |
+
chart_data = pd.DataFrame({
|
| 179 |
+
"Categoría": result['all_labels'],
|
| 180 |
+
"Probabilidad": result['probabilities']
|
| 181 |
+
})
|
| 182 |
+
st.bar_chart(chart_data.set_index("Categoría"), color="#86868b")
|
| 183 |
+
|
| 184 |
+
# --- CAPTURA DE ERROR TÉCNICO (AQUÍ VERÁS QUÉ PASA) ---
|
| 185 |
+
except Exception as e:
|
| 186 |
+
st.error(f"Error técnico crítico: {str(e)}")
|
| 187 |
+
# Pista probable
|
| 188 |
+
if "libGL" in str(e):
|
| 189 |
+
st.warning("Falta el archivo 'packages.txt' con libgl1 en el repositorio.")
|
| 190 |
+
else:
|
| 191 |
+
st.markdown(
|
| 192 |
+
"""
|
| 193 |
+
<div style='text-align: center; color: #86868b; padding: 40px; border: 2px dashed #e5e5e5; border-radius: 18px; margin-top: 20px;'>
|
| 194 |
+
Arrastra una imagen aquí o haz clic para explorar
|
| 195 |
+
</div>
|
| 196 |
+
""",
|
| 197 |
+
unsafe_allow_html=True
|
| 198 |
+
)
|