AIdeaText commited on
Commit
dc7ec9e
·
verified ·
1 Parent(s): 404e1f8

Update modules/morphosyntax/morphosyntax_interface.py

Browse files
modules/morphosyntax/morphosyntax_interface.py CHANGED
@@ -28,7 +28,7 @@ logger = logging.getLogger(__name__)
28
 
29
  def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
30
  try:
31
- # CSS para mejorar la estabilidad y prevenir saltos
32
  st.markdown("""
33
  <style>
34
  .stTextArea textarea {
@@ -60,90 +60,81 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
60
  # Inicializar el estado
61
  if 'morphosyntax_state' not in st.session_state:
62
  st.session_state.morphosyntax_state = {
63
- 'input_text': "",
64
  'analysis_count': 0,
65
  'last_analysis': None,
66
  }
67
 
68
- # Contenedor principal con manejo de estado mejorado
69
- with st.container():
70
- # Campo de entrada de texto con manejo de estado explícito
71
- input_key = f"morpho_input_{st.session_state.morphosyntax_state['analysis_count']}"
72
-
73
- # Función para manejar cambios en el texto
74
- def on_text_change():
75
- text = st.session_state[input_key]
76
- st.session_state.morphosyntax_state['input_text'] = text
77
-
78
- # Campo de texto con callback
79
- sentence_input = st.text_area(
80
- morpho_t.get('morpho_input_label', 'Enter text to analyze'),
81
- height=150,
82
- key=input_key,
83
- placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...'),
84
- on_change=on_text_change
 
 
 
 
85
  )
86
 
87
- # Botón de análisis
88
- col1, col2, col3 = st.columns([2,1,2])
89
- with col1:
90
- analyze_button = st.button(
91
- morpho_t.get('morpho_analyze_button', 'Analyze Morphosyntax'),
92
- key=f"morpho_button_{st.session_state.morphosyntax_state['analysis_count']}",
93
- type="primary",
94
- icon="🔍",
95
- disabled=not bool(st.session_state.morphosyntax_state['input_text'].strip()),
96
- use_container_width=True
97
- )
98
-
99
- # Procesar análisis
100
- if analyze_button and st.session_state.morphosyntax_state['input_text'].strip():
101
- try:
102
- with st.spinner(morpho_t.get('processing', 'Processing...')):
103
- # Procesar el texto
104
- doc = nlp_models[lang_code](st.session_state.morphosyntax_state['input_text'])
105
-
106
- # Realizar análisis
107
- advanced_analysis = perform_advanced_morphosyntactic_analysis(
108
- st.session_state.morphosyntax_state['input_text'],
109
- nlp_models[lang_code]
 
 
 
 
 
 
 
 
 
 
110
  )
111
-
112
- # Guardar resultado en el estado
113
- st.session_state.morphosyntax_result = {
114
- 'doc': doc,
115
- 'advanced_analysis': advanced_analysis
116
- }
117
-
118
- # Incrementar contador
119
- st.session_state.morphosyntax_state['analysis_count'] += 1
120
-
121
- # Guardar en base de datos
122
- if store_student_morphosyntax_result(
123
- username=st.session_state.username,
124
- text=st.session_state.morphosyntax_state['input_text'],
125
- arc_diagrams=advanced_analysis['arc_diagrams']
126
- ):
127
- st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
128
- display_morphosyntax_results(
129
- st.session_state.morphosyntax_result,
130
- lang_code,
131
- morpho_t
132
- )
133
- else:
134
- st.error(morpho_t.get('error_message', 'Error saving analysis'))
135
-
136
- except Exception as e:
137
- logger.error(f"Error en análisis morfosintáctico: {str(e)}")
138
- st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
139
-
140
- # Mostrar resultados previos
141
- elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result:
142
- display_morphosyntax_results(
143
- st.session_state.morphosyntax_result,
144
- lang_code,
145
- morpho_t
146
- )
147
 
148
  except Exception as e:
149
  logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
 
28
 
29
  def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
30
  try:
31
+ # CSS para mejorar la estabilidad
32
  st.markdown("""
33
  <style>
34
  .stTextArea textarea {
 
60
  # Inicializar el estado
61
  if 'morphosyntax_state' not in st.session_state:
62
  st.session_state.morphosyntax_state = {
 
63
  'analysis_count': 0,
64
  'last_analysis': None,
65
  }
66
 
67
+ # Generar key única para el input
68
+ input_key = f"morpho_input_{st.session_state.morphosyntax_state['analysis_count']}"
69
+
70
+ # Campo de entrada de texto
71
+ sentence_input = st.text_area(
72
+ morpho_t.get('morpho_input_label', 'Enter text to analyze'),
73
+ height=150,
74
+ key=input_key,
75
+ placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...')
76
+ )
77
+
78
+ # Botón de análisis
79
+ col1, col2, col3 = st.columns([2,1,2])
80
+ with col1:
81
+ analyze_button = st.button(
82
+ morpho_t.get('morpho_analyze_button', 'Analyze Morphosyntax'),
83
+ key=f"morpho_button_{st.session_state.morphosyntax_state['analysis_count']}",
84
+ type="primary",
85
+ icon="🔍",
86
+ disabled=not bool(sentence_input.strip()),
87
+ use_container_width=True
88
  )
89
 
90
+ # Procesar análisis
91
+ if analyze_button and sentence_input.strip():
92
+ try:
93
+ with st.spinner(morpho_t.get('processing', 'Processing...')):
94
+ # Procesar el texto
95
+ doc = nlp_models[lang_code](sentence_input)
96
+
97
+ # Realizar análisis
98
+ advanced_analysis = perform_advanced_morphosyntactic_analysis(
99
+ sentence_input,
100
+ nlp_models[lang_code]
101
+ )
102
+
103
+ # Guardar resultado en el estado
104
+ st.session_state.morphosyntax_result = {
105
+ 'doc': doc,
106
+ 'advanced_analysis': advanced_analysis
107
+ }
108
+
109
+ # Incrementar contador
110
+ st.session_state.morphosyntax_state['analysis_count'] += 1
111
+
112
+ # Guardar en base de datos
113
+ if store_student_morphosyntax_result(
114
+ username=st.session_state.username,
115
+ text=sentence_input,
116
+ arc_diagrams=advanced_analysis['arc_diagrams']
117
+ ):
118
+ st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
119
+ display_morphosyntax_results(
120
+ st.session_state.morphosyntax_result,
121
+ lang_code,
122
+ morpho_t
123
  )
124
+ else:
125
+ st.error(morpho_t.get('error_message', 'Error saving analysis'))
126
+
127
+ except Exception as e:
128
+ logger.error(f"Error en análisis morfosintáctico: {str(e)}")
129
+ st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
130
+
131
+ # Mostrar resultados previos
132
+ elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result:
133
+ display_morphosyntax_results(
134
+ st.session_state.morphosyntax_result,
135
+ lang_code,
136
+ morpho_t
137
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
  except Exception as e:
140
  logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")