AIdeaText commited on
Commit
7c29197
·
verified ·
1 Parent(s): 90e1fae

Update modules/semantic/semantic_interface.py

Browse files
Files changed (1) hide show
  1. modules/semantic/semantic_interface.py +105 -83
modules/semantic/semantic_interface.py CHANGED
@@ -27,97 +27,119 @@ 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 con controles alineados horizontalmente
 
 
 
 
31
  """
32
- # Forzar la página a semántico
33
  st.session_state.page = 'semantic'
34
-
35
- # Inicializar estados básicos
36
- if 'semantic_content' not in st.session_state:
37
- st.session_state.semantic_content = None
38
- if 'semantic_analyzed' not in st.session_state:
39
- st.session_state.semantic_analyzed = False
40
 
41
- # Contenedor principal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  with st.container():
43
- # Una sola fila para todos los controles
44
- cols = st.columns([4, 2, 2, 2])
45
 
46
- # Columna 1: Carga de archivo
47
- with cols[0]:
48
  uploaded_file = st.file_uploader(
49
- "Upload text file", # Simplificamos el mensaje
50
- type=['txt'],
51
- key="semantic_file_upload"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  )
53
 
54
- # Columna 2: Botón de análisis
55
- with cols[1]:
56
- can_analyze = uploaded_file is not None and not st.session_state.semantic_analyzed
57
- if st.button('Analyze',
58
- disabled=not can_analyze,
59
- key="semantic_analyze"):
60
- if uploaded_file:
61
- text_content = uploaded_file.getvalue().decode('utf-8')
62
- # Realizar el análisis
63
- with st.spinner("Analyzing..."):
64
- analysis_result = process_semantic_input(
65
- text_content,
66
- lang_code,
67
- nlp_models,
68
- semantic_t
69
- )
70
- if analysis_result['success']:
71
- st.session_state.semantic_result = analysis_result
72
- st.session_state.semantic_analyzed = True
73
- st.success("Analysis completed!")
74
-
75
- # Mostrar resultados
76
- display_semantic_results(
77
- analysis_result,
78
- lang_code,
79
- semantic_t
80
- )
81
-
82
- # Columna 3: Botón de exportación
83
- with cols[2]:
84
- if st.button('Export',
85
- disabled=not st.session_state.semantic_analyzed,
86
- key="semantic_export"):
87
- if st.session_state.semantic_analyzed:
88
- try:
89
- pdf_buffer = export_user_interactions(
90
- st.session_state.username,
91
- 'semantic'
92
- )
93
- st.download_button(
94
- "Download PDF",
95
- data=pdf_buffer,
96
- file_name="semantic_analysis.pdf",
97
- mime="application/pdf"
98
- )
99
- except Exception as e:
100
- st.error(f"Error exporting: {str(e)}")
101
-
102
- # Columna 4: Botón de nuevo análisis
103
- with cols[3]:
104
- if st.button('New Analysis',
105
- disabled=not st.session_state.semantic_analyzed,
106
- key="semantic_new"):
107
- st.session_state.semantic_content = None
108
- st.session_state.semantic_analyzed = False
109
- st.session_state.semantic_result = None
110
- st.rerun()
111
-
112
- # Mostrar resultados si existen
113
- if st.session_state.semantic_analyzed and 'semantic_result' in st.session_state:
114
- display_semantic_results(
115
- st.session_state.semantic_result,
116
- lang_code,
117
- semantic_t
118
- )
119
- elif not uploaded_file:
120
- st.info("Please upload a text file to begin analysis")
121
 
122
  def display_semantic_results(result, lang_code, semantic_t):
123
  """
 
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
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)
52
+
53
+ # Contenedor principal para controles
54
  with st.container():
55
+ # Área de carga de archivo y botones
56
+ col_upload, col_analyze, col_export, col_new = st.columns([4,2,2,2])
57
 
58
+ with col_upload:
 
59
  uploaded_file = st.file_uploader(
60
+ semantic_t.get('file_uploader', 'Upload text file'),
61
+ type=['txt']
62
+ )
63
+
64
+ with col_analyze:
65
+ analyze_button = st.button(
66
+ semantic_t.get('analyze_button', 'Analyze'),
67
+ disabled=(uploaded_file is None)
68
+ )
69
+
70
+ with col_export:
71
+ export_button = st.button(
72
+ semantic_t.get('export_button', 'Export'),
73
+ disabled=not ('semantic_result' in st.session_state)
74
+ )
75
+
76
+ with col_new:
77
+ new_button = st.button(
78
+ semantic_t.get('new_analysis', 'New'),
79
+ disabled=not ('semantic_result' in st.session_state)
80
  )
81
 
82
+ # Línea separadora
83
+ st.markdown("---")
84
+
85
+ # Lógica de análisis
86
+ if uploaded_file is not None and analyze_button:
87
+ try:
88
+ # Procesar el archivo
89
+ text_content = uploaded_file.getvalue().decode('utf-8')
90
+
91
+ with st.spinner(semantic_t.get('processing', 'Processing...')):
92
+ # Realizar análisis
93
+ analysis_result = perform_semantic_analysis(
94
+ text_content,
95
+ nlp_models[lang_code],
96
+ lang_code
97
+ )
98
+
99
+ # Guardar resultado
100
+ st.session_state.semantic_result = analysis_result
101
+
102
+ # Guardar en base de datos
103
+ if store_student_semantic_result(
104
+ st.session_state.username,
105
+ text_content,
106
+ analysis_result
107
+ ):
108
+ st.success(semantic_t.get('success_message', 'Analysis saved successfully'))
109
+ else:
110
+ st.error(semantic_t.get('error_message', 'Error saving analysis'))
111
+
112
+ # Mostrar resultados
113
+ display_semantic_results(analysis_result, lang_code, semantic_t)
114
+
115
+ except Exception as e:
116
+ st.error(f"Error: {str(e)}")
117
+
118
+ # Manejo de exportación
119
+ if export_button and 'semantic_result' in st.session_state:
120
+ try:
121
+ pdf_buffer = export_user_interactions(st.session_state.username, 'semantic')
122
+ st.download_button(
123
+ label=semantic_t.get('download_pdf', 'Download PDF'),
124
+ data=pdf_buffer,
125
+ file_name="semantic_analysis.pdf",
126
+ mime="application/pdf"
127
+ )
128
+ except Exception as e:
129
+ st.error(f"Error exporting: {str(e)}")
130
+
131
+ # Nuevo análisis
132
+ if new_button:
133
+ if 'semantic_result' in st.session_state:
134
+ del st.session_state.semantic_result
135
+ st.rerun()
136
+
137
+ # Mostrar resultados previos o mensaje inicial
138
+ if 'semantic_result' in st.session_state:
139
+ display_semantic_results(st.session_state.semantic_result, lang_code, semantic_t)
140
+ elif uploaded_file is None:
141
+ st.info(semantic_t.get('initial_message', 'Upload a file to begin analysis'))
142
+
 
 
 
 
 
 
143
 
144
  def display_semantic_results(result, lang_code, semantic_t):
145
  """