|
|
|
import streamlit as st |
|
import spacy_streamlit |
|
from streamlit_float import * |
|
from streamlit_antd_components import * |
|
from streamlit.components.v1 import html |
|
import base64 |
|
|
|
from .morphosyntax_process import process_morphosyntactic_input, format_analysis_results |
|
|
|
from ..utils.widget_utils import generate_unique_key |
|
from ..database.morphosintax_mongo_db import store_student_morphosyntax_result |
|
from ..database.chat_db import store_chat_history |
|
from ..database.morphosintaxis_export import export_user_interactions |
|
|
|
import logging |
|
logger = logging.getLogger(__name__) |
|
|
|
def display_morphosyntax_interface(lang_code, nlp_models, t): |
|
""" |
|
Interfaz para el an谩lisis morfosint谩ctico |
|
""" |
|
morpho_t = t.get('MORPHOSYNTACTIC', {}) |
|
|
|
|
|
input_key = f"morphosyntax_input_{lang_code}" |
|
|
|
if input_key not in st.session_state: |
|
st.session_state[input_key] = "" |
|
|
|
sentence_input = st.text_area( |
|
t['input_label'], |
|
height=150, |
|
placeholder=t['input_placeholder'], |
|
value=st.session_state[input_key], |
|
key=f"text_area_{lang_code}", |
|
on_change=lambda: setattr(st.session_state, input_key, st.session_state[f"text_area_{lang_code}"]) |
|
) |
|
|
|
if st.button(t['analyze_button'], key=f"analyze_button_{lang_code}"): |
|
current_input = st.session_state[input_key] |
|
if current_input: |
|
doc = nlp_models[lang_code](current_input) |
|
|
|
|
|
advanced_analysis = perform_advanced_morphosyntactic_analysis(current_input, nlp_models[lang_code]) |
|
|
|
|
|
st.session_state.morphosyntax_result = { |
|
'doc': doc, |
|
'advanced_analysis': advanced_analysis |
|
} |
|
|
|
|
|
display_morphosyntax_results(st.session_state.morphosyntax_result, lang_code, t) |
|
|
|
|
|
if store_morphosyntax_result( |
|
st.session_state.username, |
|
current_input, |
|
get_repeated_words_colors(doc), |
|
advanced_analysis['arc_diagram'], |
|
advanced_analysis['pos_analysis'], |
|
advanced_analysis['morphological_analysis'], |
|
advanced_analysis['sentence_structure'] |
|
): |
|
st.success(t['success_message']) |
|
else: |
|
st.error(t['error_message']) |
|
else: |
|
st.warning(t['warning_message']) |
|
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None: |
|
|
|
|
|
display_morphosyntax_results(st.session_state.morphosyntax_result, lang_code, t) |
|
else: |
|
st.info(t['initial_message']) |
|
|
|
|
|
|
|
if st.button(morpho_t.get('export_button', 'Export Analysis')): |
|
pdf_buffer = export_user_interactions(st.session_state.username, 'morphosyntax') |
|
st.download_button( |
|
label=morpho_t.get('download_pdf', 'Download PDF'), |
|
data=pdf_buffer, |
|
file_name="morphosyntax_analysis.pdf", |
|
mime="application/pdf" |
|
) |
|
|
|
''' |
|
if user_input: |
|
# A帽adir el mensaje del usuario al historial |
|
st.session_state.morphosyntax_chat_history.append({"role": "user", "content": user_input}) |
|
|
|
# Procesar el input del usuario nuevo al 26-9-2024 |
|
response, visualizations, result = process_morphosyntactic_input(user_input, lang_code, nlp_models, t) |
|
|
|
# Mostrar indicador de carga |
|
with st.spinner(t.get('processing', 'Processing...')): |
|
try: |
|
# Procesar el input del usuario |
|
response, visualizations, result = process_morphosyntactic_input(user_input, lang_code, nlp_models, t) |
|
|
|
# A帽adir la respuesta al historial |
|
message = { |
|
"role": "assistant", |
|
"content": response |
|
} |
|
if visualizations: |
|
message["visualizations"] = visualizations |
|
st.session_state.morphosyntax_chat_history.append(message) |
|
|
|
# Mostrar la respuesta m谩s reciente |
|
with st.chat_message("assistant"): |
|
st.write(response) |
|
if visualizations: |
|
for i, viz in enumerate(visualizations): |
|
st.markdown(f"**Oraci贸n {i+1} del p谩rrafo analizado**") |
|
st.components.v1.html( |
|
f""" |
|
<div style="width: 100%; overflow-x: auto; white-space: nowrap;"> |
|
<div style="min-width: 1200px;"> |
|
{viz} |
|
</div> |
|
</div> |
|
""", |
|
height=350, |
|
scrolling=True |
|
) |
|
if i < len(visualizations) - 1: |
|
st.markdown("---") # Separador entre diagramas |
|
|
|
# Si es un an谩lisis, guardarlo en la base de datos |
|
if user_input.startswith('/analisis_morfosintactico') and result: |
|
store_morphosyntax_result( |
|
st.session_state.username, |
|
user_input.split('[', 1)[1].rsplit(']', 1)[0], # texto analizado |
|
result.get('repeated_words', {}), |
|
visualizations, |
|
result.get('pos_analysis', []), |
|
result.get('morphological_analysis', []), |
|
result.get('sentence_structure', []) |
|
) |
|
|
|
|
|
except Exception as e: |
|
st.error(f"{t['error_processing']}: {str(e)}") |
|
|
|
|
|
|
|
# Forzar la actualizaci贸n de la interfaz |
|
st.rerun() |
|
|
|
# Bot贸n para limpiar el historial del chat |
|
if st.button(t['clear_chat'], key=generate_unique_key('morphosyntax', 'clear_chat')): |
|
st.session_state.morphosyntax_chat_history = [] |
|
st.rerun() |
|
''' |
|
|
|
|
|
''' |
|
############ MODULO PARA DEPURACI脫N Y PRUEBAS ##################################################### |
|
def display_morphosyntax_interface(lang_code, nlp_models, t): |
|
st.subheader(t['morpho_title']) |
|
|
|
text_input = st.text_area( |
|
t['warning_message'], |
|
height=150, |
|
key=generate_unique_key("morphosyntax", "text_area") |
|
) |
|
|
|
if st.button( |
|
t['results_title'], |
|
key=generate_unique_key("morphosyntax", "analyze_button") |
|
): |
|
if text_input: |
|
# Aqu铆 ir铆a tu l贸gica de an谩lisis morfosint谩ctico |
|
# Por ahora, solo mostraremos un mensaje de placeholder |
|
st.info(t['analysis_placeholder']) |
|
else: |
|
st.warning(t['no_text_warning']) |
|
### |
|
################################################# |
|
''' |
|
|