Spaces:
Sleeping
Sleeping
| # modules/studentact/current_situation_interface.py | |
| import streamlit as st | |
| import logging | |
| from ..utils.widget_utils import generate_unique_key | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| from ..database.current_situation_mongo_db import store_current_situation_result | |
| # Importaciones locales | |
| from translations import get_translations | |
| # Importamos la función de recomendaciones personalizadas si existe | |
| try: | |
| from .claude_recommendations import display_personalized_recommendations | |
| except ImportError: | |
| # Si no existe el módulo, definimos una función placeholder | |
| def display_personalized_recommendations(text, metrics, text_type, lang_code, t): | |
| st.warning("Módulo de recomendaciones personalizadas no disponible. Por favor, contacte al administrador.") | |
| from .current_situation_analysis import ( | |
| analyze_text_dimensions, | |
| analyze_clarity, | |
| analyze_vocabulary_diversity, | |
| analyze_cohesion, | |
| analyze_structure, | |
| get_dependency_depths, | |
| normalize_score, | |
| generate_sentence_graphs, | |
| generate_word_connections, | |
| generate_connection_paths, | |
| create_vocabulary_network, | |
| create_syntax_complexity_graph, | |
| create_cohesion_heatmap | |
| ) | |
| # Configuración del estilo de matplotlib para el gráfico de radar | |
| plt.rcParams['font.family'] = 'sans-serif' | |
| plt.rcParams['axes.grid'] = True | |
| plt.rcParams['axes.spines.top'] = False | |
| plt.rcParams['axes.spines.right'] = False | |
| logger = logging.getLogger(__name__) | |
| #################################### | |
| # Definición global de los tipos de texto y sus umbrales | |
| TEXT_TYPES = { | |
| 'academic_article': { | |
| 'name': 'Artículo Académico', | |
| 'thresholds': { | |
| 'vocabulary': {'min': 0.70, 'target': 0.85}, | |
| 'structure': {'min': 0.75, 'target': 0.90}, | |
| 'cohesion': {'min': 0.65, 'target': 0.80}, | |
| 'clarity': {'min': 0.70, 'target': 0.85} | |
| } | |
| }, | |
| 'student_essay': { | |
| 'name': 'Trabajo Universitario', | |
| 'thresholds': { | |
| 'vocabulary': {'min': 0.60, 'target': 0.75}, | |
| 'structure': {'min': 0.65, 'target': 0.80}, | |
| 'cohesion': {'min': 0.55, 'target': 0.70}, | |
| 'clarity': {'min': 0.60, 'target': 0.75} | |
| } | |
| }, | |
| 'general_communication': { | |
| 'name': 'Comunicación General', | |
| 'thresholds': { | |
| 'vocabulary': {'min': 0.50, 'target': 0.65}, | |
| 'structure': {'min': 0.55, 'target': 0.70}, | |
| 'cohesion': {'min': 0.45, 'target': 0.60}, | |
| 'clarity': {'min': 0.50, 'target': 0.65} | |
| } | |
| } | |
| } | |
| #################################### | |
| # Función para generar recomendaciones basadas en las métricas | |
| def generate_recommendations(metrics, text_type, lang_code): | |
| """ | |
| Genera recomendaciones básicas basadas en las métricas y el tipo de texto. | |
| Este es un generador de recomendaciones básico que puede ser reemplazado | |
| por una versión más avanzada que use la API de Claude. | |
| """ | |
| try: | |
| # Obtenemos los umbrales para el tipo de texto seleccionado | |
| thresholds = TEXT_TYPES[text_type]['thresholds'] | |
| # Identificamos las áreas más débiles | |
| areas = ['vocabulary', 'structure', 'cohesion', 'clarity'] | |
| scores = {area: metrics[area]['normalized_score'] for area in areas} | |
| weak_areas = sorted(areas, key=lambda x: scores[x]) | |
| # La primera área es la más débil | |
| priority_area = weak_areas[0] | |
| # Recomendaciones básicas para cada área | |
| recommendations = { | |
| 'priority': { | |
| 'area': priority_area, | |
| 'tips': ["Despliega el asistente virtual (potenciado por Claude.AI) que se ubica en la parte superior izquierda, presiona la flecha del lado del logo."] | |
| }, | |
| 'vocabulary': [ | |
| "Utiliza un vocabulario más variado y específico.", | |
| "Evita repetir palabras, usa sinónimos.", | |
| "Incorpora términos técnicos apropiados para tu disciplina." | |
| ], | |
| 'structure': [ | |
| "Varía la estructura de tus oraciones.", | |
| "Usa tanto oraciones simples como complejas.", | |
| "Organiza tus ideas en párrafos con una estructura clara." | |
| ], | |
| 'cohesion': [ | |
| "Utiliza conectores y marcadores textuales.", | |
| "Asegura la progresión lógica entre ideas.", | |
| "Mantén la coherencia temática a lo largo del texto." | |
| ], | |
| 'clarity': [ | |
| "Evita frases excesivamente largas o complejas.", | |
| "Define términos técnicos cuando sea necesario.", | |
| "Revisa que cada párrafo desarrolle una idea principal." | |
| ], | |
| 'text_type': text_type # Guardamos el tipo de texto | |
| } | |
| return recommendations | |
| except Exception as e: | |
| logger.error(f"Error en generate_recommendations: {str(e)}") | |
| # Recomendaciones por defecto en caso de error | |
| return { | |
| 'priority': { | |
| 'area': 'clarity', | |
| 'tips': ["Revisa tu texto para mejorar su claridad y estructura."] | |
| }, | |
| 'text_type': text_type | |
| } | |
| # Función para mostrar recomendaciones | |
| def display_recommendations_with_actions(recommendations, lang_code, t): | |
| """ | |
| Muestra recomendaciones personalizadas para mejorar el texto. | |
| Esta función puede ser reemplazada por display_personalized_recommendations | |
| cuando se implemente la integración con Claude API. | |
| """ | |
| try: | |
| # Definir colores para cada categoría | |
| colors = { | |
| 'vocabulary': '#2E86C1', # Azul | |
| 'structure': '#28B463', # Verde | |
| 'cohesion': '#F39C12', # Naranja | |
| 'clarity': '#9B59B6', # Púrpura | |
| 'priority': '#E74C3C' # Rojo para la categoría prioritaria | |
| } | |
| # Iconos para cada categoría | |
| icons = { | |
| 'vocabulary': '📚', | |
| 'structure': '🏗️', | |
| 'cohesion': '🔄', | |
| 'clarity': '💡', | |
| 'priority': '⭐' | |
| } | |
| # Obtener traducciones para cada dimensión | |
| dimension_names = { | |
| 'vocabulary': t.get('SITUATION_ANALYSIS', {}).get('vocabulary', "Vocabulario"), | |
| 'structure': t.get('SITUATION_ANALYSIS', {}).get('structure', "Estructura"), | |
| 'cohesion': t.get('SITUATION_ANALYSIS', {}).get('cohesion', "Cohesión"), | |
| 'clarity': t.get('SITUATION_ANALYSIS', {}).get('clarity', "Claridad"), | |
| 'priority': t.get('SITUATION_ANALYSIS', {}).get('priority', "Prioridad") | |
| } | |
| # Título de la sección prioritaria | |
| priority_focus = t.get('SITUATION_ANALYSIS', {}).get('priority_focus', 'Área prioritaria para mejorar') | |
| st.markdown(f"### {icons['priority']} {priority_focus}") | |
| # Determinar área prioritaria | |
| priority_area = recommendations.get('priority', {}).get('area', 'vocabulary') | |
| priority_title = dimension_names.get(priority_area, "Área prioritaria") | |
| # Determinar el contenido para mostrar | |
| priority_content = recommendations.get('priority', {}).get('tips', []) | |
| if isinstance(priority_content, list): | |
| priority_content = "<br>".join([f"• {tip}" for tip in priority_content]) | |
| # Mostrar la recomendación prioritaria | |
| with st.container(): | |
| st.markdown( | |
| f""" | |
| <div style="border:2px solid {colors['priority']}; border-radius:5px; padding:15px; margin-bottom:20px;"> | |
| <h4 style="color:{colors['priority']};">{priority_title}</h4> | |
| <p>{priority_content}</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Crear dos columnas para las tarjetas de recomendaciones restantes | |
| col1, col2 = st.columns(2) | |
| # Distribuir las recomendaciones en las columnas | |
| categories = ['vocabulary', 'structure', 'cohesion', 'clarity'] | |
| for i, category in enumerate(categories): | |
| # Saltar si esta categoría ya es la prioritaria | |
| if category == priority_area: | |
| continue | |
| # Obtener las recomendaciones para esta categoría | |
| category_content = recommendations.get(category, []) | |
| if isinstance(category_content, list): | |
| category_content = "<br>".join([f"• {tip}" for tip in category_content]) | |
| category_title = dimension_names.get(category, category) | |
| # Alternar entre columnas | |
| with col1 if i % 2 == 0 else col2: | |
| # Crear tarjeta para cada recomendación | |
| st.markdown( | |
| f""" | |
| <div style="border:1px solid {colors[category]}; border-radius:5px; padding:10px; margin-bottom:15px;"> | |
| <h4 style="color:{colors[category]};">{icons[category]} {category_title}</h4> | |
| <p>{category_content}</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Agregar una sección para recursos adicionales | |
| st.markdown("---") | |
| st.markdown("### 📖 Recursos adicionales") | |
| with st.expander("Ver recursos de aprendizaje"): | |
| st.markdown(""" | |
| ### Recursos por área | |
| #### Vocabulario | |
| - **Diccionario de la Real Academia Española**: [www.rae.es](https://www.rae.es) | |
| - **Fundación del Español Urgente**: [www.fundeu.es](https://www.fundeu.es) | |
| #### Estructura | |
| - **Manual de gramática**: [Gramática y ortografía para dummies](https://www.planetadelibros.com/libro-gramatica-y-ortografia-para-dummies/248265) | |
| - **Ortografía de la RAE**: [Ortografía básica de la lengua española](https://www.rae.es/obras-academicas/ortografia/ortografia-basica-de-la-lengua-espanola) | |
| #### Cohesión | |
| - **Centro Virtual Cervantes**: [Diccionario de términos clave de ELE](https://cvc.cervantes.es/ensenanza/biblioteca_ele/diccio_ele/indice.htm) | |
| - **Curso de cohesión textual**: [Centro de Escritura Javeriano](https://www2.javerianacali.edu.co/sites/ujc/files/normas_apa_revisada_y_actualizada_mayo_2019.pdf) | |
| #### Claridad | |
| - **Curso de escritura científica**: [Cómo escribir y publicar trabajos científicos](https://www.conacyt.gov.py/sites/default/files/upload_editores/u38/CONI-NOR-113.pdf) | |
| - **Manual de estilo**: [Manual de estilo de la lengua española](https://www.planetadelibros.com/libro-manual-de-estilo-de-la-lengua-espanola/17811) | |
| """) | |
| # Boletines o actualizaciones del sistema | |
| with st.expander("📬 Actualizaciones de AIdeaText"): | |
| st.markdown(""" | |
| ## Próximas actualizaciones | |
| - **Nueva funcionalidad**: Análisis comparativo entre textos propios | |
| - **Mejora**: Recomendaciones más detalladas y personalizadas | |
| - **Próximamente**: Tutorial interactivo para mejorar la escritura | |
| > Estamos trabajando continuamente para mejorar tus herramientas de escritura. | |
| """) | |
| except Exception as e: | |
| logger.error(f"Error mostrando recomendaciones: {str(e)}") | |
| st.error("Error al mostrar las recomendaciones") | |
| def display_current_situation_interface(lang_code, nlp_models, t): | |
| """ | |
| Interfaz simplificada con gráfico de radar para visualizar métricas. | |
| """ | |
| # Inicializar estados si no existen | |
| if 'text_input' not in st.session_state: | |
| st.session_state.text_input = "" | |
| if 'text_area' not in st.session_state: # Añadir inicialización de text_area | |
| st.session_state.text_area = "" | |
| if 'show_results' not in st.session_state: | |
| st.session_state.show_results = False | |
| if 'current_doc' not in st.session_state: | |
| st.session_state.current_doc = None | |
| if 'current_metrics' not in st.session_state: | |
| st.session_state.current_metrics = None | |
| if 'current_recommendations' not in st.session_state: | |
| st.session_state.current_recommendations = None | |
| try: | |
| # Container principal con dos columnas | |
| with st.container(): | |
| input_col, results_col = st.columns([1,2]) | |
| with input_col: | |
| # Text area con manejo de estado | |
| text_input = st.text_area( | |
| t.get('input_prompt', "Escribe o pega tu texto aquí:"), | |
| height=400, | |
| key="text_area", | |
| value=st.session_state.text_input, | |
| help="Este texto será analizado para darte recomendaciones personalizadas" | |
| ) | |
| # Función para manejar cambios de texto | |
| if text_input != st.session_state.text_input: | |
| st.session_state.text_input = text_input | |
| st.session_state.show_results = False | |
| if st.button( | |
| t.get('analyze_button', "Analizar mi escritura"), | |
| type="primary", | |
| disabled=not text_input.strip(), | |
| use_container_width=True, | |
| ): | |
| try: | |
| with st.spinner(t.get('processing', "Analizando...")): | |
| doc = nlp_models[lang_code](text_input) | |
| metrics = analyze_text_dimensions(doc) | |
| storage_success = store_current_situation_result( | |
| username=st.session_state.username, | |
| text=text_input, | |
| metrics=metrics, | |
| feedback=None | |
| ) | |
| if not storage_success: | |
| logger.warning("No se pudo guardar el análisis en la base de datos") | |
| st.session_state.current_doc = doc | |
| st.session_state.current_metrics = metrics | |
| st.session_state.show_results = True | |
| except Exception as e: | |
| logger.error(f"Error en análisis: {str(e)}") | |
| st.error(t.get('analysis_error', "Error al analizar el texto")) | |
| # Mostrar resultados en la columna derecha | |
| with results_col: | |
| if st.session_state.show_results and st.session_state.current_metrics is not None: | |
| # Primero los radio buttons para tipo de texto | |
| st.markdown("### Tipo de texto") | |
| text_type = st.radio( | |
| label="Tipo de texto", | |
| options=list(TEXT_TYPES.keys()), | |
| format_func=lambda x: TEXT_TYPES[x]['name'], | |
| horizontal=True, | |
| key="text_type_radio", | |
| label_visibility="collapsed", | |
| help="Selecciona el tipo de texto para ajustar los criterios de evaluación" | |
| ) | |
| st.session_state.current_text_type = text_type | |
| # Crear subtabs | |
| subtab1, subtab2 = st.tabs(["Diagnóstico", "Recomendaciones"]) | |
| # Mostrar resultados en el primer subtab | |
| with subtab1: | |
| display_diagnosis( | |
| metrics=st.session_state.current_metrics, | |
| text_type=text_type | |
| ) | |
| # Mostrar recomendaciones en el segundo subtab | |
| with subtab2: | |
| # Generar recomendaciones si no existen o si cambió el tipo de texto | |
| if (st.session_state.current_recommendations is None or | |
| st.session_state.current_recommendations.get('text_type') != text_type): | |
| recommendations = generate_recommendations( | |
| metrics=st.session_state.current_metrics, | |
| text_type=text_type, | |
| lang_code=lang_code | |
| ) | |
| recommendations['text_type'] = text_type | |
| st.session_state.current_recommendations = recommendations | |
| # Intentar usar recomendaciones avanzadas si están disponibles | |
| try: | |
| # Intenta usar la función avanzada si está disponible | |
| display_personalized_recommendations( | |
| text=text_input, | |
| metrics=st.session_state.current_metrics, | |
| text_type=text_type, | |
| lang_code=lang_code, | |
| t=t | |
| ) | |
| except (NameError, ImportError): | |
| # Si no está disponible, usa la función básica | |
| display_recommendations_with_actions( | |
| st.session_state.current_recommendations, | |
| lang_code, | |
| t | |
| ) | |
| except Exception as e: | |
| logger.error(f"Error en interfaz principal: {str(e)}") | |
| st.error("Ocurrió un error al cargar la interfaz") | |
| def display_diagnosis(metrics, text_type=None): | |
| """ | |
| Muestra los resultados del análisis: métricas verticalmente y gráfico radar. | |
| """ | |
| try: | |
| # Usar valor por defecto si no se especifica tipo | |
| text_type = text_type or 'student_essay' | |
| # Obtener umbrales según el tipo de texto | |
| thresholds = TEXT_TYPES[text_type]['thresholds'] | |
| # Crear dos columnas para las métricas y el gráfico | |
| metrics_col, graph_col = st.columns([1, 1.5]) | |
| # Columna de métricas | |
| with metrics_col: | |
| metrics_config = [ | |
| { | |
| 'label': "Vocabulario", | |
| 'key': 'vocabulary', | |
| 'value': metrics['vocabulary']['normalized_score'], | |
| 'help': "Riqueza y variedad del vocabulario", | |
| 'thresholds': thresholds['vocabulary'] | |
| }, | |
| { | |
| 'label': "Estructura", | |
| 'key': 'structure', | |
| 'value': metrics['structure']['normalized_score'], | |
| 'help': "Organización y complejidad de oraciones", | |
| 'thresholds': thresholds['structure'] | |
| }, | |
| { | |
| 'label': "Cohesión", | |
| 'key': 'cohesion', | |
| 'value': metrics['cohesion']['normalized_score'], | |
| 'help': "Conexión y fluidez entre ideas", | |
| 'thresholds': thresholds['cohesion'] | |
| }, | |
| { | |
| 'label': "Claridad", | |
| 'key': 'clarity', | |
| 'value': metrics['clarity']['normalized_score'], | |
| 'help': "Facilidad de comprensión del texto", | |
| 'thresholds': thresholds['clarity'] | |
| } | |
| ] | |
| # Mostrar métricas | |
| for metric in metrics_config: | |
| value = metric['value'] | |
| if value < metric['thresholds']['min']: | |
| status = "⚠️ Por mejorar" | |
| color = "inverse" | |
| elif value < metric['thresholds']['target']: | |
| status = "📈 Aceptable" | |
| color = "off" | |
| else: | |
| status = "✅ Óptimo" | |
| color = "normal" | |
| st.metric( | |
| metric['label'], | |
| f"{value:.2f}", | |
| f"{status} (Meta: {metric['thresholds']['target']:.2f})", | |
| delta_color=color, | |
| help=metric['help'] | |
| ) | |
| st.markdown("<div style='margin-bottom: 0.5rem;'></div>", unsafe_allow_html=True) | |
| # Gráfico radar en la columna derecha | |
| with graph_col: | |
| display_radar_chart(metrics_config, thresholds) | |
| except Exception as e: | |
| logger.error(f"Error mostrando resultados: {str(e)}") | |
| st.error("Error al mostrar los resultados") | |
| def display_radar_chart(metrics_config, thresholds): | |
| """ | |
| Muestra el gráfico radar con los resultados. | |
| """ | |
| try: | |
| # Preparar datos para el gráfico | |
| categories = [m['label'] for m in metrics_config] | |
| values_user = [m['value'] for m in metrics_config] | |
| min_values = [m['thresholds']['min'] for m in metrics_config] | |
| target_values = [m['thresholds']['target'] for m in metrics_config] | |
| # Crear y configurar gráfico | |
| fig = plt.figure(figsize=(8, 8)) | |
| ax = fig.add_subplot(111, projection='polar') | |
| # Configurar radar | |
| angles = [n / float(len(categories)) * 2 * np.pi for n in range(len(categories))] | |
| angles += angles[:1] | |
| values_user += values_user[:1] | |
| min_values += min_values[:1] | |
| target_values += target_values[:1] | |
| # Configurar ejes | |
| ax.set_xticks(angles[:-1]) | |
| ax.set_xticklabels(categories, fontsize=10) | |
| circle_ticks = np.arange(0, 1.1, 0.2) | |
| ax.set_yticks(circle_ticks) | |
| ax.set_yticklabels([f'{tick:.1f}' for tick in circle_ticks], fontsize=8) | |
| ax.set_ylim(0, 1) | |
| # Dibujar áreas de umbrales | |
| ax.plot(angles, min_values, '#e74c3c', linestyle='--', linewidth=1, label='Mínimo', alpha=0.5) | |
| ax.plot(angles, target_values, '#2ecc71', linestyle='--', linewidth=1, label='Meta', alpha=0.5) | |
| ax.fill_between(angles, target_values, [1]*len(angles), color='#2ecc71', alpha=0.1) | |
| ax.fill_between(angles, [0]*len(angles), min_values, color='#e74c3c', alpha=0.1) | |
| # Dibujar valores del usuario | |
| ax.plot(angles, values_user, '#3498db', linewidth=2, label='Tu escritura') | |
| ax.fill(angles, values_user, '#3498db', alpha=0.2) | |
| # Ajustar leyenda | |
| ax.legend( | |
| loc='upper right', | |
| bbox_to_anchor=(1.3, 1.1), | |
| fontsize=10, | |
| frameon=True, | |
| facecolor='white', | |
| edgecolor='none', | |
| shadow=True | |
| ) | |
| plt.tight_layout() | |
| st.pyplot(fig) | |
| plt.close() | |
| except Exception as e: | |
| logger.error(f"Error mostrando gráfico radar: {str(e)}") | |
| st.error("Error al mostrar el gráfico") |