AIdeaText commited on
Commit
525418e
·
verified ·
1 Parent(s): 489c034

Update modules/semantic/semantic_interface.py

Browse files
modules/semantic/semantic_interface.py CHANGED
@@ -28,24 +28,22 @@ from ..database.semantic_export import export_user_interactions
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
35
  """
36
  # Mantener la página actual
37
  st.session_state.page = 'semantic'
38
 
39
- # Estilos para los botones y controles
 
 
 
 
 
 
40
  st.markdown("""
41
  <style>
42
  .stButton button {
43
  width: 100%;
44
- padding: 0.5rem;
45
- }
46
- .upload-container {
47
- display: flex;
48
- align-items: center;
49
  }
50
  </style>
51
  """, unsafe_allow_html=True)
@@ -54,96 +52,111 @@ def display_semantic_interface(lang_code, nlp_models, semantic_t):
54
  with st.container():
55
  col_upload, col_analyze, col_export, col_new = st.columns([4,2,2,2])
56
 
 
57
  with col_upload:
58
  uploaded_file = st.file_uploader(
59
  semantic_t.get('file_uploader', 'Upload text file'),
60
  type=['txt'],
61
- key="semantic_file_uploader" # Key única para semántico
62
  )
63
-
 
 
 
 
 
 
 
 
 
64
  with col_analyze:
 
65
  analyze_button = st.button(
66
- semantic_t.get('semantic_analyze_button', 'Analyze Semantics'), # Nombre específico
67
- key="semantic_analysis_button", # Key única para semántico
68
- disabled=(uploaded_file is None),
69
  use_container_width=True
70
  )
71
 
 
72
  with col_export:
 
73
  export_button = st.button(
74
- semantic_t.get('semantic_export_button', 'Export Semantic'), # Nombre específico
75
- key="semantic_export_button", # Key única para semántico
76
- disabled=not ('semantic_result' in st.session_state),
77
  use_container_width=True
78
  )
79
 
 
80
  with col_new:
 
81
  new_button = st.button(
82
- semantic_t.get('semantic_new_button', 'New Semantic'), # Nombre específico
83
- key="semantic_new_button", # Key única para semántico
84
- disabled=not ('semantic_result' in st.session_state),
85
  use_container_width=True
86
  )
87
-
88
- # Línea separadora
89
  st.markdown("---")
90
 
91
- # Lógica de análisis
92
- if uploaded_file is not None and analyze_button:
93
  try:
94
- # Procesar el archivo
95
  text_content = uploaded_file.getvalue().decode('utf-8')
96
 
97
  with st.spinner(semantic_t.get('processing', 'Processing...')):
98
- # Realizar análisis
99
  analysis_result = perform_semantic_analysis(
100
  text_content,
101
  nlp_models[lang_code],
102
  lang_code
103
  )
104
 
105
- # Guardar resultado
106
- st.session_state.semantic_result = analysis_result
107
-
108
- # Guardar en base de datos
109
- if store_student_semantic_result(
110
- st.session_state.username,
111
- text_content,
112
- analysis_result
113
- ):
114
- st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
 
 
 
 
115
  else:
116
- st.error(semantic_t.get('error_message', 'Error saving analysis'))
117
-
118
- # Mostrar resultados
119
- display_semantic_results(analysis_result, lang_code, semantic_t)
120
-
121
  except Exception as e:
122
  st.error(f"Error: {str(e)}")
123
 
124
- # Manejo de exportación
125
- if export_button and 'semantic_result' in st.session_state:
126
  try:
127
  pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
128
  st.download_button(
129
  label=semantic_t.get('download_pdf', 'Download PDF'),
130
  data=pdf_buffer,
131
  file_name="semantic_analysis.pdf",
132
- mime="application/pdf"
 
133
  )
134
  except Exception as e:
135
  st.error(f"Error exporting: {str(e)}")
136
 
137
- # Nuevo análisis
138
  if new_button:
139
- if 'semantic_result' in st.session_state:
140
- del st.session_state.semantic_result
 
141
  st.rerun()
142
 
143
- # Mostrar resultados previos o mensaje inicial
144
- if 'semantic_result' in st.session_state:
145
  display_semantic_results(st.session_state.semantic_result, lang_code, semantic_t)
146
- elif uploaded_file is None:
147
  st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
148
 
149
 
 
28
  def display_semantic_interface(lang_code, nlp_models, semantic_t):
29
  """
30
  Interfaz para el análisis semántico
 
 
 
 
31
  """
32
  # Mantener la página actual
33
  st.session_state.page = 'semantic'
34
 
35
+ # Inicializar estados si no existen
36
+ if 'semantic_has_file' not in st.session_state:
37
+ st.session_state.semantic_has_file = False
38
+ if 'semantic_result' not in st.session_state:
39
+ st.session_state.semantic_result = None
40
+
41
+ # Estilos para los botones
42
  st.markdown("""
43
  <style>
44
  .stButton button {
45
  width: 100%;
46
+ height: 38px;
 
 
 
 
47
  }
48
  </style>
49
  """, unsafe_allow_html=True)
 
52
  with st.container():
53
  col_upload, col_analyze, col_export, col_new = st.columns([4,2,2,2])
54
 
55
+ # Columna para carga de archivo
56
  with col_upload:
57
  uploaded_file = st.file_uploader(
58
  semantic_t.get('file_uploader', 'Upload text file'),
59
  type=['txt'],
60
+ key="semantic_file_uploader"
61
  )
62
+ # Actualizar estado cuando se carga un archivo
63
+ if uploaded_file is not None:
64
+ st.session_state.semantic_has_file = True
65
+ if 'file_content' not in st.session_state or st.session_state.file_content != uploaded_file:
66
+ st.session_state.file_content = uploaded_file
67
+ st.session_state.semantic_result = None # Resetear resultado si el archivo cambia
68
+ else:
69
+ st.session_state.semantic_has_file = False
70
+
71
+ # Columna para botón de análisis
72
  with col_analyze:
73
+ analyze_disabled = not st.session_state.semantic_has_file
74
  analyze_button = st.button(
75
+ semantic_t.get('semantic_analyze_button', 'Analyze'),
76
+ disabled=analyze_disabled,
77
+ key="semantic_analysis_button",
78
  use_container_width=True
79
  )
80
 
81
+ # Columna para botón de exportación
82
  with col_export:
83
+ export_disabled = st.session_state.semantic_result is None
84
  export_button = st.button(
85
+ semantic_t.get('semantic_export_button', 'Export'),
86
+ disabled=export_disabled,
87
+ key="semantic_export_button",
88
  use_container_width=True
89
  )
90
 
91
+ # Columna para botón de nuevo análisis
92
  with col_new:
93
+ new_disabled = not st.session_state.semantic_has_file
94
  new_button = st.button(
95
+ semantic_t.get('semantic_new_button', 'New'),
96
+ disabled=new_disabled,
97
+ key="semantic_new_button",
98
  use_container_width=True
99
  )
100
+
 
101
  st.markdown("---")
102
 
103
+ # Procesar análisis
104
+ if analyze_button and uploaded_file is not None:
105
  try:
 
106
  text_content = uploaded_file.getvalue().decode('utf-8')
107
 
108
  with st.spinner(semantic_t.get('processing', 'Processing...')):
 
109
  analysis_result = perform_semantic_analysis(
110
  text_content,
111
  nlp_models[lang_code],
112
  lang_code
113
  )
114
 
115
+ if analysis_result['success']:
116
+ st.session_state.semantic_result = analysis_result
117
+
118
+ # Guardar en base de datos
119
+ if store_student_semantic_result(
120
+ st.session_state.username,
121
+ text_content,
122
+ analysis_result
123
+ ):
124
+ st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
125
+ # Mostrar resultados
126
+ display_semantic_results(analysis_result, lang_code, semantic_t)
127
+ else:
128
+ st.error(semantic_t.get('error_message', 'Error saving analysis'))
129
  else:
130
+ st.error(analysis_result['message'])
131
+
 
 
 
132
  except Exception as e:
133
  st.error(f"Error: {str(e)}")
134
 
135
+ # Manejar exportación
136
+ if export_button and st.session_state.semantic_result is not None:
137
  try:
138
  pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
139
  st.download_button(
140
  label=semantic_t.get('download_pdf', 'Download PDF'),
141
  data=pdf_buffer,
142
  file_name="semantic_analysis.pdf",
143
+ mime="application/pdf",
144
+ key="semantic_download_button"
145
  )
146
  except Exception as e:
147
  st.error(f"Error exporting: {str(e)}")
148
 
149
+ # Manejar nuevo análisis
150
  if new_button:
151
+ st.session_state.semantic_result = None
152
+ st.session_state.semantic_has_file = False
153
+ st.session_state.file_content = None
154
  st.rerun()
155
 
156
+ # Mostrar resultados o mensaje inicial
157
+ if st.session_state.semantic_result is not None:
158
  display_semantic_results(st.session_state.semantic_result, lang_code, semantic_t)
159
+ elif not st.session_state.semantic_has_file:
160
  st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
161
 
162