Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update modules/morphosyntax/morphosyntax_interface.py
Browse files
    	
        modules/morphosyntax/morphosyntax_interface.py
    CHANGED
    
    | 
         @@ -1,184 +1,186 @@ 
     | 
|
| 1 | 
         
            -
            #modules/morphosyntax/morphosyntax_interface.py
         
     | 
| 2 | 
         
            -
            import streamlit as st
         
     | 
| 3 | 
         
            -
            from streamlit_float import *
         
     | 
| 4 | 
         
            -
            from streamlit_antd_components import *
         
     | 
| 5 | 
         
            -
            from streamlit.components.v1 import html
         
     | 
| 6 | 
         
            -
            import base64
         
     | 
| 7 | 
         
            -
            from .morphosyntax_process import process_morphosyntactic_input
         
     | 
| 8 | 
         
            -
            from ..chatbot.chatbot import initialize_chatbot
         
     | 
| 9 | 
         
            -
            from ..utils.widget_utils import generate_unique_key
         
     | 
| 10 | 
         
            -
            from ..database.morphosintax_mongo_db import store_student_morphosyntax_result
         
     | 
| 11 | 
         
            -
            from ..database.chat_db import store_chat_history
         
     | 
| 12 | 
         
            -
            from ..database.morphosintaxis_export import export_user_interactions
         
     | 
| 13 | 
         
            -
             
     | 
| 14 | 
         
            -
            import logging
         
     | 
| 15 | 
         
            -
            logger = logging.getLogger(__name__)
         
     | 
| 16 | 
         
            -
             
     | 
| 17 | 
         
            -
            def display_morphosyntax_interface(lang_code, nlp_models, t):
         
     | 
| 18 | 
         
            -
                st.title("Análisis Morfosintáctico")
         
     | 
| 19 | 
         
            -
             
     | 
| 20 | 
         
            -
                # Contenedor para el historial del chat
         
     | 
| 21 | 
         
            -
                chat_container = st.container()
         
     | 
| 22 | 
         
            -
             
     | 
| 23 | 
         
            -
                # Input del usuario (siempre visible en la parte inferior)
         
     | 
| 24 | 
         
            -
                user_input = st.chat_input(t['morpho_input_label'])
         
     | 
| 25 | 
         
            -
             
     | 
| 26 | 
         
            -
                #  
     | 
| 27 | 
         
            -
                if  
     | 
| 28 | 
         
            -
                     
     | 
| 29 | 
         
            -
             
     | 
| 30 | 
         
            -
             
     | 
| 31 | 
         
            -
             
     | 
| 32 | 
         
            -
                     
     | 
| 33 | 
         
            -
             
     | 
| 34 | 
         
            -
                     
     | 
| 35 | 
         
            -
             
     | 
| 36 | 
         
            -
             
     | 
| 37 | 
         
            -
             
     | 
| 38 | 
         
            -
             
     | 
| 39 | 
         
            -
                     
     | 
| 40 | 
         
            -
             
     | 
| 41 | 
         
            -
             
     | 
| 42 | 
         
            -
             
     | 
| 43 | 
         
            -
                     
     | 
| 44 | 
         
            -
                     
     | 
| 45 | 
         
            -
             
     | 
| 46 | 
         
            -
             
     | 
| 47 | 
         
            -
             
     | 
| 48 | 
         
            -
             
     | 
| 49 | 
         
            -
                         
     | 
| 50 | 
         
            -
             
     | 
| 51 | 
         
            -
             
     | 
| 52 | 
         
            -
             
     | 
| 53 | 
         
            -
             
     | 
| 54 | 
         
            -
             
     | 
| 55 | 
         
            -
             
     | 
| 56 | 
         
            -
             
     | 
| 57 | 
         
            -
             
     | 
| 58 | 
         
            -
             
     | 
| 59 | 
         
            -
             
     | 
| 60 | 
         
            -
             
     | 
| 61 | 
         
            -
             
     | 
| 62 | 
         
            -
             
     | 
| 63 | 
         
            -
             
     | 
| 64 | 
         
            -
             
     | 
| 65 | 
         
            -
             
     | 
| 66 | 
         
            -
                                             
     | 
| 67 | 
         
            -
             
     | 
| 68 | 
         
            -
             
     | 
| 69 | 
         
            -
                                         
     | 
| 70 | 
         
            -
                                         
     | 
| 71 | 
         
            -
             
     | 
| 72 | 
         
            -
             
     | 
| 73 | 
         
            -
             
     | 
| 74 | 
         
            -
             
     | 
| 75 | 
         
            -
             
     | 
| 76 | 
         
            -
             
     | 
| 77 | 
         
            -
             
     | 
| 78 | 
         
            -
             
     | 
| 79 | 
         
            -
             
     | 
| 80 | 
         
            -
             
     | 
| 81 | 
         
            -
             
     | 
| 82 | 
         
            -
             
     | 
| 83 | 
         
            -
             
     | 
| 84 | 
         
            -
             
     | 
| 85 | 
         
            -
             
     | 
| 86 | 
         
            -
                         
     | 
| 87 | 
         
            -
                         
     | 
| 88 | 
         
            -
             
     | 
| 89 | 
         
            -
             
     | 
| 90 | 
         
            -
             
     | 
| 91 | 
         
            -
             
     | 
| 92 | 
         
            -
             
     | 
| 93 | 
         
            -
             
     | 
| 94 | 
         
            -
             
     | 
| 95 | 
         
            -
                     
     | 
| 96 | 
         
            -
             
     | 
| 97 | 
         
            -
             
     | 
| 98 | 
         
            -
                     
     | 
| 99 | 
         
            -
             
     | 
| 100 | 
         
            -
             
     | 
| 101 | 
         
            -
             
     | 
| 102 | 
         
            -
             
     | 
| 103 | 
         
            -
             
     | 
| 104 | 
         
            -
                             
     | 
| 105 | 
         
            -
             
     | 
| 106 | 
         
            -
             
     | 
| 107 | 
         
            -
             
     | 
| 108 | 
         
            -
             
     | 
| 109 | 
         
            -
             
     | 
| 110 | 
         
            -
             
     | 
| 111 | 
         
            -
                             
     | 
| 112 | 
         
            -
             
     | 
| 113 | 
         
            -
                             
     | 
| 114 | 
         
            -
             
     | 
| 115 | 
         
            -
             
     | 
| 116 | 
         
            -
             
     | 
| 117 | 
         
            -
             
     | 
| 118 | 
         
            -
             
     | 
| 119 | 
         
            -
             
     | 
| 120 | 
         
            -
             
     | 
| 121 | 
         
            -
             
     | 
| 122 | 
         
            -
             
     | 
| 123 | 
         
            -
             
     | 
| 124 | 
         
            -
                                                 
     | 
| 125 | 
         
            -
             
     | 
| 126 | 
         
            -
             
     | 
| 127 | 
         
            -
                                             
     | 
| 128 | 
         
            -
                                             
     | 
| 129 | 
         
            -
             
     | 
| 130 | 
         
            -
             
     | 
| 131 | 
         
            -
             
     | 
| 132 | 
         
            -
             
     | 
| 133 | 
         
            -
             
     | 
| 134 | 
         
            -
             
     | 
| 135 | 
         
            -
             
     | 
| 136 | 
         
            -
             
     | 
| 137 | 
         
            -
             
     | 
| 138 | 
         
            -
                                     
     | 
| 139 | 
         
            -
                                     
     | 
| 140 | 
         
            -
                                    result.get(' 
     | 
| 141 | 
         
            -
                                     
     | 
| 142 | 
         
            -
                                    result.get(' 
     | 
| 143 | 
         
            -
             
     | 
| 144 | 
         
            -
             
     | 
| 145 | 
         
            -
             
     | 
| 146 | 
         
            -
             
     | 
| 147 | 
         
            -
             
     | 
| 148 | 
         
            -
             
     | 
| 149 | 
         
            -
             
     | 
| 150 | 
         
            -
             
     | 
| 151 | 
         
            -
             
     | 
| 152 | 
         
            -
             
     | 
| 153 | 
         
            -
             
     | 
| 154 | 
         
            -
             
     | 
| 155 | 
         
            -
             
     | 
| 156 | 
         
            -
             
     | 
| 157 | 
         
            -
             
     | 
| 158 | 
         
            -
             
     | 
| 159 | 
         
            -
             
     | 
| 160 | 
         
            -
             
     | 
| 161 | 
         
            -
             
     | 
| 162 | 
         
            -
             
     | 
| 163 | 
         
            -
             
     | 
| 164 | 
         
            -
             
     | 
| 165 | 
         
            -
             
     | 
| 166 | 
         
            -
                 
     | 
| 167 | 
         
            -
             
     | 
| 168 | 
         
            -
             
     | 
| 169 | 
         
            -
                     
     | 
| 170 | 
         
            -
             
     | 
| 171 | 
         
            -
             
     | 
| 172 | 
         
            -
                 
     | 
| 173 | 
         
            -
             
     | 
| 174 | 
         
            -
             
     | 
| 175 | 
         
            -
             
     | 
| 176 | 
         
            -
                     
     | 
| 177 | 
         
            -
             
     | 
| 178 | 
         
            -
             
     | 
| 179 | 
         
            -
                         
     | 
| 180 | 
         
            -
             
     | 
| 181 | 
         
            -
                        st. 
     | 
| 182 | 
         
            -
             
     | 
| 183 | 
         
            -
             
     | 
| 184 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            #modules/morphosyntax/morphosyntax_interface.py
         
     | 
| 2 | 
         
            +
            import streamlit as st
         
     | 
| 3 | 
         
            +
            from streamlit_float import *
         
     | 
| 4 | 
         
            +
            from streamlit_antd_components import *
         
     | 
| 5 | 
         
            +
            from streamlit.components.v1 import html
         
     | 
| 6 | 
         
            +
            import base64
         
     | 
| 7 | 
         
            +
            from .morphosyntax_process import process_morphosyntactic_input
         
     | 
| 8 | 
         
            +
            from ..chatbot.chatbot import initialize_chatbot
         
     | 
| 9 | 
         
            +
            from ..utils.widget_utils import generate_unique_key
         
     | 
| 10 | 
         
            +
            from ..database.morphosintax_mongo_db import store_student_morphosyntax_result
         
     | 
| 11 | 
         
            +
            from ..database.chat_db import store_chat_history
         
     | 
| 12 | 
         
            +
            from ..database.morphosintaxis_export import export_user_interactions
         
     | 
| 13 | 
         
            +
             
     | 
| 14 | 
         
            +
            import logging
         
     | 
| 15 | 
         
            +
            logger = logging.getLogger(__name__)
         
     | 
| 16 | 
         
            +
             
     | 
| 17 | 
         
            +
            def display_morphosyntax_interface(lang_code, nlp_models, t):
         
     | 
| 18 | 
         
            +
                st.title("Análisis Morfosintáctico")
         
     | 
| 19 | 
         
            +
             
     | 
| 20 | 
         
            +
                # Contenedor para el historial del chat
         
     | 
| 21 | 
         
            +
                chat_container = st.container()
         
     | 
| 22 | 
         
            +
             
     | 
| 23 | 
         
            +
                # Input del usuario (siempre visible en la parte inferior)
         
     | 
| 24 | 
         
            +
                user_input = st.chat_input(t['morpho_input_label'])
         
     | 
| 25 | 
         
            +
             
     | 
| 26 | 
         
            +
                # Inicializar el historial del chat si no existe
         
     | 
| 27 | 
         
            +
                if 'morphosyntax_chat_history' not in st.session_state:
         
     | 
| 28 | 
         
            +
                    st.session_state.morphosyntax_chat_history = []
         
     | 
| 29 | 
         
            +
             
     | 
| 30 | 
         
            +
                # Procesar el input del usuario
         
     | 
| 31 | 
         
            +
                if user_input:
         
     | 
| 32 | 
         
            +
                    # Añadir el mensaje del usuario al historial
         
     | 
| 33 | 
         
            +
                    st.session_state.morphosyntax_chat_history.append({"role": "user", "content": user_input})
         
     | 
| 34 | 
         
            +
                    store_chat_history(st.session_state.username, [{"role": "user", "content": user_input}], "morphosyntax")
         
     | 
| 35 | 
         
            +
             
     | 
| 36 | 
         
            +
                    response, visualizations, result = process_morphosyntactic_input(user_input, lang_code, nlp_models, t)
         
     | 
| 37 | 
         
            +
             
     | 
| 38 | 
         
            +
                    # Añadir la respuesta al historial
         
     | 
| 39 | 
         
            +
                    assistant_message = {
         
     | 
| 40 | 
         
            +
                        "role": "assistant",
         
     | 
| 41 | 
         
            +
                        "content": response,
         
     | 
| 42 | 
         
            +
                        "visualizations": visualizations if visualizations else []
         
     | 
| 43 | 
         
            +
                    }
         
     | 
| 44 | 
         
            +
                    st.session_state.morphosyntax_chat_history.append(assistant_message)
         
     | 
| 45 | 
         
            +
                    store_chat_history(st.session_state.username, [assistant_message], "morphosyntax")
         
     | 
| 46 | 
         
            +
             
     | 
| 47 | 
         
            +
                    # Si es un análisis, guardarlo en la base de datos
         
     | 
| 48 | 
         
            +
                    if user_input.startswith('/analisis_morfosintactico') and result:
         
     | 
| 49 | 
         
            +
                        store_student_morphosyntax_result(
         
     | 
| 50 | 
         
            +
                            st.session_state.username,
         
     | 
| 51 | 
         
            +
                            user_input.split('[', 1)[1].rsplit(']', 1)[0],  # texto analizado
         
     | 
| 52 | 
         
            +
                            visualizations
         
     | 
| 53 | 
         
            +
                        )
         
     | 
| 54 | 
         
            +
             
     | 
| 55 | 
         
            +
                # Mostrar el historial del chat
         
     | 
| 56 | 
         
            +
                with chat_container:
         
     | 
| 57 | 
         
            +
                    for message in st.session_state.morphosyntax_chat_history:
         
     | 
| 58 | 
         
            +
                        with st.chat_message(message["role"]):
         
     | 
| 59 | 
         
            +
                            st.write(message["content"])
         
     | 
| 60 | 
         
            +
                            if "visualizations" in message and message["visualizations"]:
         
     | 
| 61 | 
         
            +
                                for i, viz in enumerate(message["visualizations"]):
         
     | 
| 62 | 
         
            +
                                    st.markdown(f"**Oración {i+1} del párrafo analizado**")
         
     | 
| 63 | 
         
            +
                                    st.components.v1.html(
         
     | 
| 64 | 
         
            +
                                        f"""
         
     | 
| 65 | 
         
            +
                                        <div style="width: 100%; overflow-x: auto; white-space: nowrap;">
         
     | 
| 66 | 
         
            +
                                            <div style="min-width: 1200px;">
         
     | 
| 67 | 
         
            +
                                                {viz}
         
     | 
| 68 | 
         
            +
                                            </div>
         
     | 
| 69 | 
         
            +
                                        </div>
         
     | 
| 70 | 
         
            +
                                        """,
         
     | 
| 71 | 
         
            +
                                        height=370,
         
     | 
| 72 | 
         
            +
                                        scrolling=True
         
     | 
| 73 | 
         
            +
                                    )
         
     | 
| 74 | 
         
            +
                                    if i < len(message["visualizations"]) - 1:
         
     | 
| 75 | 
         
            +
                                        st.markdown("---")  # Separador entre diagramas
         
     | 
| 76 | 
         
            +
             
     | 
| 77 | 
         
            +
                # Botón para limpiar el historial del chat
         
     | 
| 78 | 
         
            +
                if st.button(t['clear_chat'], key=generate_unique_key('morphosyntax', 'clear_chat')):
         
     | 
| 79 | 
         
            +
                    st.session_state.morphosyntax_chat_history = []
         
     | 
| 80 | 
         
            +
                    st.rerun()
         
     | 
| 81 | 
         
            +
             
     | 
| 82 | 
         
            +
                # Botón de exportación
         
     | 
| 83 | 
         
            +
                if st.button("Exportar Interacciones"):
         
     | 
| 84 | 
         
            +
                    pdf_buffer = export_user_interactions(st.session_state.username, 'morphosyntax')
         
     | 
| 85 | 
         
            +
                    st.download_button(
         
     | 
| 86 | 
         
            +
                        label="Descargar PDF",
         
     | 
| 87 | 
         
            +
                        data=pdf_buffer,
         
     | 
| 88 | 
         
            +
                        file_name="interacciones_morfosintaxis.pdf",
         
     | 
| 89 | 
         
            +
                        mime="application/pdf"
         
     | 
| 90 | 
         
            +
                    )
         
     | 
| 91 | 
         
            +
             
     | 
| 92 | 
         
            +
            '''
         
     | 
| 93 | 
         
            +
                if user_input:
         
     | 
| 94 | 
         
            +
                    # Añadir el mensaje del usuario al historial
         
     | 
| 95 | 
         
            +
                    st.session_state.morphosyntax_chat_history.append({"role": "user", "content": user_input})
         
     | 
| 96 | 
         
            +
             
     | 
| 97 | 
         
            +
                    # Procesar el input del usuario nuevo al 26-9-2024
         
     | 
| 98 | 
         
            +
                    response, visualizations, result = process_morphosyntactic_input(user_input, lang_code, nlp_models, t)
         
     | 
| 99 | 
         
            +
             
     | 
| 100 | 
         
            +
                    # Mostrar indicador de carga
         
     | 
| 101 | 
         
            +
                    with st.spinner(t.get('processing', 'Processing...')):
         
     | 
| 102 | 
         
            +
                        try:
         
     | 
| 103 | 
         
            +
                            # Procesar el input del usuario
         
     | 
| 104 | 
         
            +
                            response, visualizations, result = process_morphosyntactic_input(user_input, lang_code, nlp_models, t)
         
     | 
| 105 | 
         
            +
             
     | 
| 106 | 
         
            +
                            # Añadir la respuesta al historial
         
     | 
| 107 | 
         
            +
                            message = {
         
     | 
| 108 | 
         
            +
                                "role": "assistant",
         
     | 
| 109 | 
         
            +
                                "content": response
         
     | 
| 110 | 
         
            +
                            }
         
     | 
| 111 | 
         
            +
                            if visualizations:
         
     | 
| 112 | 
         
            +
                                message["visualizations"] = visualizations
         
     | 
| 113 | 
         
            +
                            st.session_state.morphosyntax_chat_history.append(message)
         
     | 
| 114 | 
         
            +
             
     | 
| 115 | 
         
            +
                            # Mostrar la respuesta más reciente
         
     | 
| 116 | 
         
            +
                            with st.chat_message("assistant"):
         
     | 
| 117 | 
         
            +
                                st.write(response)
         
     | 
| 118 | 
         
            +
                                if visualizations:
         
     | 
| 119 | 
         
            +
                                    for i, viz in enumerate(visualizations):
         
     | 
| 120 | 
         
            +
                                        st.markdown(f"**Oración {i+1} del párrafo analizado**")
         
     | 
| 121 | 
         
            +
                                        st.components.v1.html(
         
     | 
| 122 | 
         
            +
                                            f"""
         
     | 
| 123 | 
         
            +
                                            <div style="width: 100%; overflow-x: auto; white-space: nowrap;">
         
     | 
| 124 | 
         
            +
                                                <div style="min-width: 1200px;">
         
     | 
| 125 | 
         
            +
                                                    {viz}
         
     | 
| 126 | 
         
            +
                                                </div>
         
     | 
| 127 | 
         
            +
                                            </div>
         
     | 
| 128 | 
         
            +
                                            """,
         
     | 
| 129 | 
         
            +
                                            height=350,
         
     | 
| 130 | 
         
            +
                                            scrolling=True
         
     | 
| 131 | 
         
            +
                                        )
         
     | 
| 132 | 
         
            +
                                        if i < len(visualizations) - 1:
         
     | 
| 133 | 
         
            +
                                            st.markdown("---")  # Separador entre diagramas
         
     | 
| 134 | 
         
            +
             
     | 
| 135 | 
         
            +
                            # Si es un análisis, guardarlo en la base de datos
         
     | 
| 136 | 
         
            +
                            if user_input.startswith('/analisis_morfosintactico') and result:
         
     | 
| 137 | 
         
            +
                                store_morphosyntax_result(
         
     | 
| 138 | 
         
            +
                                    st.session_state.username,
         
     | 
| 139 | 
         
            +
                                    user_input.split('[', 1)[1].rsplit(']', 1)[0],  # texto analizado
         
     | 
| 140 | 
         
            +
                                    result.get('repeated_words', {}),
         
     | 
| 141 | 
         
            +
                                    visualizations,
         
     | 
| 142 | 
         
            +
                                    result.get('pos_analysis', []),
         
     | 
| 143 | 
         
            +
                                    result.get('morphological_analysis', []),
         
     | 
| 144 | 
         
            +
                                    result.get('sentence_structure', [])
         
     | 
| 145 | 
         
            +
                                )
         
     | 
| 146 | 
         
            +
             
     | 
| 147 | 
         
            +
             
     | 
| 148 | 
         
            +
                        except Exception as e:
         
     | 
| 149 | 
         
            +
                            st.error(f"{t['error_processing']}: {str(e)}")
         
     | 
| 150 | 
         
            +
             
     | 
| 151 | 
         
            +
             
     | 
| 152 | 
         
            +
             
     | 
| 153 | 
         
            +
                # Forzar la actualización de la interfaz
         
     | 
| 154 | 
         
            +
                    st.rerun()
         
     | 
| 155 | 
         
            +
             
     | 
| 156 | 
         
            +
                # Botón para limpiar el historial del chat
         
     | 
| 157 | 
         
            +
                if st.button(t['clear_chat'], key=generate_unique_key('morphosyntax', 'clear_chat')):
         
     | 
| 158 | 
         
            +
                    st.session_state.morphosyntax_chat_history = []
         
     | 
| 159 | 
         
            +
                    st.rerun()
         
     | 
| 160 | 
         
            +
            '''
         
     | 
| 161 | 
         
            +
             
     | 
| 162 | 
         
            +
             
     | 
| 163 | 
         
            +
            '''
         
     | 
| 164 | 
         
            +
            ############ MODULO PARA DEPURACIÓN Y PRUEBAS #####################################################
         
     | 
| 165 | 
         
            +
            def display_morphosyntax_interface(lang_code, nlp_models, t):
         
     | 
| 166 | 
         
            +
                st.subheader(t['morpho_title'])
         
     | 
| 167 | 
         
            +
             
     | 
| 168 | 
         
            +
                text_input = st.text_area(
         
     | 
| 169 | 
         
            +
                    t['warning_message'],
         
     | 
| 170 | 
         
            +
                    height=150,
         
     | 
| 171 | 
         
            +
                    key=generate_unique_key("morphosyntax", "text_area")
         
     | 
| 172 | 
         
            +
                )
         
     | 
| 173 | 
         
            +
             
     | 
| 174 | 
         
            +
                if st.button(
         
     | 
| 175 | 
         
            +
                    t['results_title'],
         
     | 
| 176 | 
         
            +
                    key=generate_unique_key("morphosyntax", "analyze_button")
         
     | 
| 177 | 
         
            +
                ):
         
     | 
| 178 | 
         
            +
                    if text_input:
         
     | 
| 179 | 
         
            +
                        # Aquí iría tu lógica de análisis morfosintáctico
         
     | 
| 180 | 
         
            +
                        # Por ahora, solo mostraremos un mensaje de placeholder
         
     | 
| 181 | 
         
            +
                        st.info(t['analysis_placeholder'])
         
     | 
| 182 | 
         
            +
                    else:
         
     | 
| 183 | 
         
            +
                        st.warning(t['no_text_warning'])
         
     | 
| 184 | 
         
            +
            ###
         
     | 
| 185 | 
         
            +
            #################################################
         
     | 
| 186 | 
         
            +
            '''
         
     |