Spaces:
Running
Running
Update modules/semantic/semantic_interface.py
Browse files
modules/semantic/semantic_interface.py
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importaciones generales
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import re
|
| 4 |
+
import io
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import base64
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
import time
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
from streamlit_player import st_player # Necesitarás instalar esta librería: pip install streamlit-player
|
| 14 |
+
from spacy import displacy
|
| 15 |
+
import logging
|
| 16 |
+
import random
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
from ..utils.widget_utils import generate_unique_key
|
| 20 |
+
|
| 21 |
+
from ..database.morphosintax_mongo_db import store_student_morphosyntax_result
|
| 22 |
+
from ..database.chat_db import store_chat_history
|
| 23 |
+
from ..database.morphosintaxis_export import export_user_interactions
|
| 24 |
+
|
| 25 |
+
import logging
|
| 26 |
+
logger = logging.getLogger(__name__)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def display_semantic_analysis_interface(nlp_models, lang_code):
|
| 30 |
+
|
| 31 |
+
t = translations[lang_code]
|
| 32 |
+
|
| 33 |
+
st.header(t['title'])
|
| 34 |
+
|
| 35 |
+
# Opción para introducir texto
|
| 36 |
+
text_input = st.text_area(
|
| 37 |
+
t['text_input_label'],
|
| 38 |
+
height=150,
|
| 39 |
+
placeholder=t['text_input_placeholder'],
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Opción para cargar archivo
|
| 43 |
+
uploaded_file = st.file_uploader(t['file_uploader'], type=['txt'])
|
| 44 |
+
|
| 45 |
+
if st.button(t['analyze_button']):
|
| 46 |
+
if text_input or uploaded_file is not None:
|
| 47 |
+
if uploaded_file:
|
| 48 |
+
text_content = uploaded_file.getvalue().decode('utf-8')
|
| 49 |
+
else:
|
| 50 |
+
text_content = text_input
|
| 51 |
+
|
| 52 |
+
# Realizar el análisis
|
| 53 |
+
analysis_result = perform_semantic_analysis(text_content, nlp_models[lang_code], lang_code)
|
| 54 |
+
|
| 55 |
+
# Guardar el resultado en el estado de la sesión
|
| 56 |
+
st.session_state.semantic_result = analysis_result
|
| 57 |
+
|
| 58 |
+
# Mostrar resultados
|
| 59 |
+
display_semantic_results(st.session_state.semantic_result, lang_code, t)
|
| 60 |
+
|
| 61 |
+
# Guardar el resultado del análisis
|
| 62 |
+
if store_semantic_result(st.session_state.username, text_content, analysis_result):
|
| 63 |
+
st.success(t['success_message'])
|
| 64 |
+
else:
|
| 65 |
+
st.error(t['error_message'])
|
| 66 |
+
else:
|
| 67 |
+
st.warning(t['warning_message'])
|
| 68 |
+
|
| 69 |
+
elif 'semantic_result' in st.session_state:
|
| 70 |
+
|
| 71 |
+
# Si hay un resultado guardado, mostrarlo
|
| 72 |
+
display_semantic_results(st.session_state.semantic_result, lang_code, t)
|
| 73 |
+
|
| 74 |
+
else:
|
| 75 |
+
st.info(t['initial_message']) # Asegúrate de que 'initial_message' esté en tus traducciones
|
| 76 |
+
|
| 77 |
+
def display_semantic_results(result, lang_code, t):
|
| 78 |
+
if result is None:
|
| 79 |
+
st.warning(t['no_results']) # Asegúrate de que 'no_results' esté en tus traducciones
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
# Mostrar conceptos clave
|
| 83 |
+
with st.expander(t['key_concepts'], expanded=True):
|
| 84 |
+
concept_text = " | ".join([f"{concept} ({frequency:.2f})" for concept, frequency in result['key_concepts']])
|
| 85 |
+
st.write(concept_text)
|
| 86 |
+
|
| 87 |
+
# Mostrar el gráfico de relaciones conceptuales
|
| 88 |
+
with st.expander(t['conceptual_relations'], expanded=True):
|
| 89 |
+
st.pyplot(result['relations_graph'])
|