AIdeaText commited on
Commit
eb367ae
·
verified ·
1 Parent(s): a142dd0

Update modules/morphosyntax/morphosyntax_interface.py

Browse files
modules/morphosyntax/morphosyntax_interface.py CHANGED
@@ -31,82 +31,91 @@ import logging
31
  logger = logging.getLogger(__name__)
32
 
33
  def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
34
- """
35
- Interfaz para el análisis morfosintáctico
36
- Args:
37
- lang_code: Código del idioma actual
38
- nlp_models: Modelos de spaCy cargados
39
- t: Diccionario de traducciones
40
- """
41
- # Obtener el diccionario de traducciones morfosintácticas
42
- # morpho_t = t.get('MORPHOSYNTACTIC', {})
43
-
44
- # Inicializar el estado de la entrada
45
- input_key = f"morphosyntax_input_{lang_code}"
46
- if input_key not in st.session_state:
47
- st.session_state[input_key] = ""
48
 
49
- # Campo de entrada de texto
50
- sentence_input = st.text_area(
51
- morpho_t.get('morpho_input_label', 'Enter text to analyze'),
52
- height=150,
53
- placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...'),
54
- value=st.session_state[input_key],
55
- key=f"text_area_{lang_code}",
56
- on_change=lambda: setattr(st.session_state, input_key, st.session_state[f"text_area_{lang_code}"])
57
- )
 
58
 
59
- if st.button(morpho_t.get('analyze_button', 'Analyze text'), key=f"analyze_button_{lang_code}"):
60
- current_input = st.session_state[input_key]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- if current_input:
63
- try:
64
- # Procesar el texto
65
- doc = nlp_models[lang_code](current_input)
66
-
67
- # Realizar análisis morfosintáctico
68
- advanced_analysis = perform_advanced_morphosyntactic_analysis(
69
- current_input,
70
- nlp_models[lang_code]
71
- )
72
-
73
- # Guardar resultado en el estado de la sesión
74
- st.session_state.morphosyntax_result = {
75
- 'doc': doc,
76
- 'advanced_analysis': advanced_analysis
77
- }
78
-
79
- # Mostrar resultados
80
- display_morphosyntax_results(
81
- st.session_state.morphosyntax_result,
82
- lang_code,
83
- morpho_t
84
- )
 
 
 
 
 
 
 
 
85
 
86
- # Guardar el análisis en la base de datos
87
- if store_student_morphosyntax_result(
88
- username=st.session_state.username,
89
- text=current_input,
90
- arc_diagrams=advanced_analysis['arc_diagrams']
91
- ):
92
- st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
93
- else:
94
- st.error(morpho_t.get('error_message', 'Error saving analysis'))
95
-
96
- except Exception as e:
97
- st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
98
  else:
99
- st.warning(morpho_t.get('warning_message', 'Please enter a text to analyze'))
100
 
101
- # Si no se presionó el botón, verificar si hay resultados previos
102
- elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None:
103
- display_morphosyntax_results(
104
- st.session_state.morphosyntax_result,
105
- lang_code,
106
- morpho_t
107
- )
108
- else:
109
- st.info(morpho_t.get('morpho_initial_message', 'Enter text to begin analysis'))
110
 
111
  ############################################################################################################
112
  def display_morphosyntax_results(result, lang_code, morpho_t):
 
31
  logger = logging.getLogger(__name__)
32
 
33
  def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
34
+ try:
35
+ # Inicializar el estado de la entrada
36
+ input_key = f"morphosyntax_input_{lang_code}"
37
+ if input_key not in st.session_state:
38
+ st.session_state[input_key] = ""
39
+
40
+ # Inicializar contador de análisis si no existe
41
+ if 'analysis_counter' not in st.session_state:
42
+ st.session_state.analysis_counter = 0
 
 
 
 
 
43
 
44
+ # Campo de entrada de texto
45
+ sentence_input = st.text_area(
46
+ morpho_t.get('morpho_input_label', 'Enter text to analyze'),
47
+ height=150,
48
+ placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...'),
49
+ value=st.session_state[input_key],
50
+ key=f"text_area_{lang_code}_{st.session_state.analysis_counter}", # Clave única
51
+ on_change=lambda: setattr(st.session_state, input_key,
52
+ st.session_state[f"text_area_{lang_code}_{st.session_state.analysis_counter}"])
53
+ )
54
 
55
+ # Botón de análisis con clave única
56
+ if st.button(
57
+ morpho_t.get('analyze_button', 'Analyze text'),
58
+ key=f"analyze_button_{lang_code}_{st.session_state.analysis_counter}"
59
+ ):
60
+ current_input = st.session_state[input_key]
61
+
62
+ if current_input:
63
+ try:
64
+ with st.spinner(morpho_t.get('processing', 'Processing...')):
65
+ # Procesar el texto
66
+ doc = nlp_models[lang_code](current_input)
67
+
68
+ # Realizar análisis morfosintáctico
69
+ advanced_analysis = perform_advanced_morphosyntactic_analysis(
70
+ current_input,
71
+ nlp_models[lang_code]
72
+ )
73
 
74
+ # Guardar resultado en el estado de la sesión
75
+ st.session_state.morphosyntax_result = {
76
+ 'doc': doc,
77
+ 'advanced_analysis': advanced_analysis
78
+ }
79
+
80
+ # Incrementar el contador de análisis
81
+ st.session_state.analysis_counter += 1
82
+
83
+ # Mostrar resultados
84
+ display_morphosyntax_results(
85
+ st.session_state.morphosyntax_result,
86
+ lang_code,
87
+ morpho_t
88
+ )
89
+
90
+ # Guardar el análisis en la base de datos
91
+ if store_student_morphosyntax_result(
92
+ username=st.session_state.username,
93
+ text=current_input,
94
+ arc_diagrams=advanced_analysis['arc_diagrams']
95
+ ):
96
+ st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
97
+ else:
98
+ st.error(morpho_t.get('error_message', 'Error saving analysis'))
99
+
100
+ except Exception as e:
101
+ logger.error(f"Error en análisis morfosintáctico: {str(e)}")
102
+ st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
103
+ else:
104
+ st.warning(morpho_t.get('warning_message', 'Please enter a text to analyze'))
105
 
106
+ # Si no se presionó el botón, verificar si hay resultados previos
107
+ elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None:
108
+ display_morphosyntax_results(
109
+ st.session_state.morphosyntax_result,
110
+ lang_code,
111
+ morpho_t
112
+ )
 
 
 
 
 
113
  else:
114
+ st.info(morpho_t.get('morpho_initial_message', 'Enter text to begin analysis'))
115
 
116
+ except Exception as e:
117
+ logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
118
+ st.error("Se produjo un error. Por favor, intente de nuevo.")
 
 
 
 
 
 
119
 
120
  ############################################################################################################
121
  def display_morphosyntax_results(result, lang_code, morpho_t):