AIdeaText commited on
Commit
f75bca8
·
verified ·
1 Parent(s): 99dc60b

Update modules/semantic/semantic_interface.py

Browse files
modules/semantic/semantic_interface.py CHANGED
@@ -25,6 +25,8 @@ from ..utils.widget_utils import generate_unique_key
25
  from ..database.semantic_mongo_db import store_student_semantic_result
26
  from ..database.semantic_export import export_user_interactions
27
 
 
 
28
  def display_semantic_interface(lang_code, nlp_models, semantic_t):
29
  """
30
  Interfaz para el análisis semántico
@@ -34,31 +36,42 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
34
  semantic_t: Diccionario de traducciones semánticas
35
  """
36
  try:
37
- # Inicializar el estado si no existe
38
  if 'semantic_analysis_counter' not in st.session_state:
39
  st.session_state.semantic_analysis_counter = 0
 
 
 
 
40
 
41
- # Opción para cargar archivo con key única
42
  uploaded_file = st.file_uploader(
43
- semantic_t.get('file_uploader', 'Upload a text file for analysis'),
44
  type=['txt'],
45
  key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}"
46
  )
47
 
48
- # Botón de análisis con key única
49
  col1, col2, col3 = st.columns([2,1,2])
50
- with col2:
51
  analyze_button = st.button(
52
- semantic_t.get('analyze_button', 'Analyze text'),
53
  key=f"semantic_analyze_button_{st.session_state.semantic_analysis_counter}",
54
  use_container_width=True
55
  )
56
 
57
- if analyze_button and uploaded_file is not None:
 
 
 
 
 
58
  try:
59
  with st.spinner(semantic_t.get('processing', 'Processing...')):
 
60
  text_content = uploaded_file.getvalue().decode('utf-8')
61
 
 
62
  analysis_result = process_semantic_input(
63
  text_content,
64
  lang_code,
@@ -66,40 +79,46 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
66
  semantic_t
67
  )
68
 
69
- if analysis_result['success']:
70
- st.session_state.semantic_result = analysis_result
71
- st.session_state.semantic_analysis_counter += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- # Guardar en la base de datos
74
- if store_student_semantic_result(
75
- st.session_state.username,
76
- text_content,
77
- analysis_result['analysis']
78
- ):
79
- st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
80
- # Mostrar resultados
81
- display_semantic_results(
82
- analysis_result,
83
- lang_code,
84
- semantic_t
85
- )
86
- else:
87
- st.error(semantic_t.get('error_message', 'Error saving analysis'))
88
  else:
89
- st.error(analysis_result['message'])
 
90
  except Exception as e:
91
  logger.error(f"Error en análisis semántico: {str(e)}")
92
  st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
93
- elif analyze_button:
94
- st.warning(semantic_t.get('warning_message', 'Please upload a file first'))
95
-
96
- # Mostrar resultados previos
97
  elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
98
  display_semantic_results(
99
  st.session_state.semantic_result,
100
  lang_code,
101
  semantic_t
102
  )
 
103
  else:
104
  st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
105
 
@@ -107,6 +126,7 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
107
  logger.error(f"Error general en interfaz semántica: {str(e)}")
108
  st.error("Se produjo un error. Por favor, intente de nuevo.")
109
 
 
110
  def display_semantic_results(result, lang_code, semantic_t):
111
  """
112
  Muestra los resultados del análisis semántico en tabs
 
25
  from ..database.semantic_mongo_db import store_student_semantic_result
26
  from ..database.semantic_export import export_user_interactions
27
 
28
+
29
+ ###############################
30
  def display_semantic_interface(lang_code, nlp_models, semantic_t):
31
  """
32
  Interfaz para el análisis semántico
 
36
  semantic_t: Diccionario de traducciones semánticas
37
  """
38
  try:
39
+ # 1. Inicializar estados de sesión
40
  if 'semantic_analysis_counter' not in st.session_state:
41
  st.session_state.semantic_analysis_counter = 0
42
+
43
+ input_key = f"semantic_input_{lang_code}"
44
+ if input_key not in st.session_state:
45
+ st.session_state[input_key] = ""
46
 
47
+ # 2. Configurar área de entrada (file uploader)
48
  uploaded_file = st.file_uploader(
49
+ semantic_t.get('semantic_file_uploader', 'Upload a text file for semantic analysis'),
50
  type=['txt'],
51
  key=f"semantic_file_uploader_{st.session_state.semantic_analysis_counter}"
52
  )
53
 
54
+ # 3. Configurar botones de control
55
  col1, col2, col3 = st.columns([2,1,2])
56
+ with col1:
57
  analyze_button = st.button(
58
+ semantic_t.get('semantic_analyze_button', 'Analyze Semantic'),
59
  key=f"semantic_analyze_button_{st.session_state.semantic_analysis_counter}",
60
  use_container_width=True
61
  )
62
 
63
+ # 4. Procesar análisis cuando se activa
64
+ if analyze_button:
65
+ if uploaded_file is None:
66
+ st.warning(semantic_t.get('warning_message', 'Please upload a file first'))
67
+ return
68
+
69
  try:
70
  with st.spinner(semantic_t.get('processing', 'Processing...')):
71
+ # 4.1 Leer contenido del archivo
72
  text_content = uploaded_file.getvalue().decode('utf-8')
73
 
74
+ # 4.2 Realizar análisis semántico
75
  analysis_result = process_semantic_input(
76
  text_content,
77
  lang_code,
 
79
  semantic_t
80
  )
81
 
82
+ if not analysis_result['success']:
83
+ st.error(analysis_result['message'])
84
+ return
85
+
86
+ # 4.3 Guardar resultado en el estado de la sesión
87
+ st.session_state.semantic_result = analysis_result
88
+
89
+ # 4.4 Incrementar el contador de análisis
90
+ st.session_state.semantic_analysis_counter += 1
91
+
92
+ # 4.5 Guardar en la base de datos
93
+ if store_student_semantic_result(
94
+ st.session_state.username,
95
+ text_content,
96
+ analysis_result['analysis']
97
+ ):
98
+ st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
99
 
100
+ # 4.6 Mostrar resultados
101
+ display_semantic_results(
102
+ st.session_state.semantic_result,
103
+ analysis_result,
104
+ lang_code,
105
+ semantic_t
106
+ )
 
 
 
 
 
 
 
 
107
  else:
108
+ st.error(semantic_t.get('error_message', 'Error saving analysis'))
109
+
110
  except Exception as e:
111
  logger.error(f"Error en análisis semántico: {str(e)}")
112
  st.error(semantic_t.get('error_processing', f'Error processing text: {str(e)}'))
113
+
114
+ # 5. Mostrar resultados previos si existen
 
 
115
  elif 'semantic_result' in st.session_state and st.session_state.semantic_result is not None:
116
  display_semantic_results(
117
  st.session_state.semantic_result,
118
  lang_code,
119
  semantic_t
120
  )
121
+ # 6. Mostrar mensaje inicial
122
  else:
123
  st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
124
 
 
126
  logger.error(f"Error general en interfaz semántica: {str(e)}")
127
  st.error("Se produjo un error. Por favor, intente de nuevo.")
128
 
129
+ #######################################
130
  def display_semantic_results(result, lang_code, semantic_t):
131
  """
132
  Muestra los resultados del análisis semántico en tabs