AIdeaText commited on
Commit
46f94ad
·
verified ·
1 Parent(s): 3b679f7

Update modules/semantic/semantic_interface.py

Browse files
modules/semantic/semantic_interface.py CHANGED
@@ -25,49 +25,39 @@ 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
-
29
  def display_semantic_interface(lang_code, nlp_models, semantic_t):
30
  """
31
  Interfaz para el análisis semántico
 
 
 
 
32
  """
33
  try:
34
- # Mantener página en semántico
35
- st.session_state.page = 'semantic'
 
36
 
37
- # Inicializar estados específicos para semántico
38
- if 'semantic_state' not in st.session_state:
39
- st.session_state.semantic_state = {
40
- 'counter': 0,
41
- 'has_file': False,
42
- 'file_content': None,
43
- 'result': None
44
- }
45
 
46
- # Contenedor principal
47
- with st.container():
48
- # Área de carga de archivo
49
- uploaded_file = st.file_uploader(
50
- semantic_t.get('file_uploader', 'Upload a text file for analysis'),
51
- type=['txt'],
52
- key=f"semantic_uploader_{st.session_state.semantic_state['counter']}"
53
  )
54
 
55
- # Botones en una fila
56
- col1, col2, col3 = st.columns([2,1,2])
57
- with col2:
58
- analyze_button = st.button(
59
- semantic_t.get('analyze_button', 'Analyze text'),
60
- key=f"semantic_analyze_{st.session_state.semantic_state['counter']}",
61
- use_container_width=True,
62
- disabled=not uploaded_file
63
- )
64
-
65
- # Procesamiento
66
  if analyze_button and uploaded_file is not None:
67
  try:
68
  with st.spinner(semantic_t.get('processing', 'Processing...')):
69
  text_content = uploaded_file.getvalue().decode('utf-8')
70
- st.session_state.semantic_state['file_content'] = text_content
71
 
72
  analysis_result = process_semantic_input(
73
  text_content,
@@ -77,15 +67,17 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
77
  )
78
 
79
  if analysis_result['success']:
80
- st.session_state.semantic_state['result'] = analysis_result
81
- st.session_state.semantic_state['counter'] += 1
82
 
 
83
  if store_student_semantic_result(
84
  st.session_state.username,
85
  text_content,
86
  analysis_result['analysis']
87
  ):
88
  st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
 
89
  display_semantic_results(
90
  analysis_result,
91
  lang_code,
@@ -102,9 +94,9 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
102
  st.warning(semantic_t.get('warning_message', 'Please upload a file first'))
103
 
104
  # Mostrar resultados previos
105
- elif st.session_state.semantic_state['result'] is not None:
106
  display_semantic_results(
107
- st.session_state.semantic_state['result'],
108
  lang_code,
109
  semantic_t
110
  )
@@ -114,7 +106,6 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
114
  except Exception as e:
115
  logger.error(f"Error general en interfaz semántica: {str(e)}")
116
  st.error("Se produjo un error. Por favor, intente de nuevo.")
117
-
118
 
119
  def display_semantic_results(result, lang_code, semantic_t):
120
  """
 
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
31
+ Args:
32
+ lang_code: Código del idioma actual
33
+ nlp_models: Modelos de spaCy cargados
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,
 
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,
 
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
  )
 
106
  except Exception as e:
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
  """