File size: 29,698 Bytes
44d2f4e
7d2b8ec
43b44fb
7d2b8ec
b66aab1
7d2b8ec
43b44fb
 
e19483a
2198c15
43b44fb
fa8fbcb
 
7d2b8ec
03dc7a7
1ccbb10
 
 
 
44d2f4e
 
 
1e95929
2384502
 
 
9e382a0
2384502
 
 
 
 
 
 
44d2f4e
46645fc
 
 
cec31c3
9f78896
90e40b9
2f0c295
cec31c3
7d2b8ec
034cfa2
 
 
 
 
 
 
 
 
 
 
 
 
 
bd89d29
 
 
034cfa2
 
 
bd89d29
 
034cfa2
 
bd89d29
 
034cfa2
bd89d29
034cfa2
26486d0
 
 
3ece79a
034cfa2
3ece79a
 
 
 
 
 
 
 
 
 
 
034cfa2
2de63a1
 
 
7d2b8ec
034cfa2
 
7d2b8ec
034cfa2
e5d0ce8
 
 
 
 
2de63a1
229756d
034cfa2
 
7d2b8ec
e5d0ce8
229756d
26486d0
8d6379a
 
229756d
7d2b8ec
229756d
034cfa2
 
 
229756d
 
7d2b8ec
229756d
8d6379a
034cfa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d6379a
 
fa8fbcb
 
 
 
 
0e1ed6b
fa8fbcb
 
 
0e1ed6b
 
 
fa8fbcb
0e1ed6b
fa8fbcb
 
0e1ed6b
fa8fbcb
 
0e1ed6b
fa8fbcb
 
 
3bbbe02
fa8fbcb
 
3bbbe02
fa8fbcb
0e1ed6b
fa8fbcb
 
 
28ccd7a
 
eeadede
 
 
 
 
 
 
 
 
 
30bae7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66aab1
 
 
 
 
 
 
 
 
 
 
 
 
30bae7d
 
 
 
 
 
 
b66aab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30bae7d
 
 
 
 
 
 
 
 
 
 
 
 
eeadede
 
 
 
 
28ccd7a
034cfa2
6d01086
7d2b8ec
 
 
 
 
 
 
 
 
2384502
 
 
 
7d2b8ec
 
 
 
 
 
 
 
 
2384502
 
 
 
7d2b8ec
 
 
 
 
 
 
 
 
2384502
 
 
 
7d2b8ec
 
 
 
 
5f1328a
 
ee5cd97
 
7d2b8ec
 
 
 
 
ee5cd97
 
2384502
7d2b8ec
 
 
ee5cd97
 
 
4a52be9
 
 
7d2b8ec
 
 
4a52be9
7d2b8ec
 
 
ee5cd97
 
7d2b8ec
 
4a52be9
7d2b8ec
 
 
 
 
 
 
 
 
 
 
4a52be9
a972c69
7d2b8ec
ee5cd97
7d2b8ec
 
 
2384502
7d2b8ec
2384502
4f8d08e
2384502
7d2b8ec
c810a25
 
 
 
 
8b900da
c810a25
9addec4
2384502
 
1681510
c810a25
 
 
8b900da
c810a25
9addec4
2384502
 
1681510
c810a25
 
 
8b900da
c810a25
9addec4
2384502
 
1681510
c810a25
 
1681510
c810a25
8b900da
c810a25
2384502
8b900da
 
2384502
 
 
1681510
 
 
 
 
 
 
 
 
 
 
 
 
2384502
 
 
 
31d258b
 
 
 
 
 
 
e442b89
2384502
 
4cbcf7f
31d258b
 
 
 
 
 
e442b89
2384502
 
4cbcf7f
31d258b
 
 
 
 
 
e442b89
2384502
 
4cbcf7f
31d258b
 
 
 
 
 
 
 
 
 
 
 
 
2384502
 
4cbcf7f
2384502
 
 
4cbcf7f
 
2384502
4cbcf7f
 
 
 
 
 
 
2384502
4cbcf7f
c66842b
 
4cbcf7f
 
 
2384502
4cbcf7f
31d258b
26486d0
c66842b
 
 
 
 
 
 
 
 
7e9e1b6
c66842b
cec31c3
 
 
e19483a
cec31c3
e19483a
cec31c3
 
e19483a
cec31c3
e19483a
cec31c3
 
e19483a
cec31c3
e19483a
cec31c3
 
 
e19483a
098b351
cec31c3
 
 
16dcc0e
098b351
c4ed35f
e19483a
098b351
c4ed35f
8a2688a
3fdaa70
 
c4ed35f
8a2688a
c4ed35f
3fdaa70
098b351
e68d122
3fdaa70
e68d122
b47b329
c4ed35f
 
 
 
3fdaa70
c4ed35f
 
bd565ef
c4ed35f
bd565ef
c4ed35f
bd565ef
 
 
c4ed35f
3fdaa70
c4ed35f
b47b329
e68d122
b47b329
 
 
 
 
 
3fdaa70
 
8d6379a
5a27028
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#Importaciones generales
import streamlit as st
import re
import io
from io import BytesIO
import base64
import matplotlib.pyplot as plt
import pandas as pd
import time
from datetime import datetime
from streamlit_player import st_player  # Necesitarás instalar esta librería: pip install streamlit-player
from modules.database import store_application_request
from modules.email import send_email_notification
from spacy import displacy
import logging

# Configuración del logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

#Importaciones locales
#Importaciones locales de autenticación y base de datos 
from .auth import authenticate_user, register_user

from .database import (
    get_student_data, 
    store_application_request,
    store_morphosyntax_result, 
    store_semantic_result, 
    store_discourse_analysis_result,
    store_chat_history, 
    create_admin_user, 
    create_student_user
)

#Importaciones locales de uiadmin
from .admin_ui import admin_page

#Importaciones locales funciones de análisis
from .morpho_analysis import generate_arc_diagram, get_repeated_words_colors, highlight_repeated_words, POS_COLORS, POS_TRANSLATIONS
from .semantic_analysis import visualize_semantic_relations, perform_semantic_analysis
from .discourse_analysis import compare_semantic_analysis, perform_discourse_analysis
from .chatbot import initialize_chatbot, get_chatbot_response

##################################################################################################
def initialize_session_state():
    if 'initialized' not in st.session_state:
        st.session_state.clear()
        st.session_state.initialized = True
        st.session_state.logged_in = False
        st.session_state.page = 'login'
        st.session_state.username = None
        st.session_state.role = None

##################################################################################################
def main():
    initialize_session_state()
    
    print(f"Página actual: {st.session_state.page}")
    print(f"Rol del usuario: {st.session_state.role}")
    
    if st.session_state.page == 'login':
        login_register_page()
    elif st.session_state.page == 'admin':
        print("Intentando mostrar página de admin")
        admin_page()
    elif st.session_state.page == 'user':
        user_page()
    else:
        print(f"Página no reconocida: {st.session_state.page}")
    
    print(f"Estado final de la sesión: {st.session_state}")

##################################################################################################
def login_register_page():
    st.title("AIdeaText")

    left_column, right_column = st.columns([1, 3])

    with left_column:
        tab1, tab2 = st.tabs(["Iniciar Sesión", "Registrarse"])
        
        with tab1:
            login_form()
        
        with tab2:
            register_form()

    with right_column:
        display_videos_and_info()

##################################################################################################

def login_form():
    username = st.text_input("Correo electrónico", key="login_username")
    password = st.text_input("Contraseña", type="password", key="login_password")
    
    if st.button("Iniciar Sesión", key="login_button"):
        success, role = authenticate_user(username, password)
        if success:
            st.session_state.logged_in = True
            st.session_state.username = username
            st.session_state.role = role
            st.session_state.page = 'admin' if role == 'Administrador' else 'user'
            print(f"Inicio de sesión exitoso. Usuario: {username}, Rol: {role}")
            print(f"Estado de sesión después de login: {st.session_state}")
            st.rerun()
        else:
            st.error("Credenciales incorrectas")

##################################################################################################
def admin_page():
    st.title("Panel de Administración")
    st.write(f"Bienvenido, {st.session_state.username}")
    
    st.header("Crear Nuevo Usuario Estudiante")
    new_username = st.text_input("Correo electrónico del nuevo usuario", key="admin_new_username")
    new_password = st.text_input("Contraseña", type="password", key="admin_new_password")
    if st.button("Crear Usuario", key="admin_create_user"):
        if create_student_user(new_username, new_password):
            st.success(f"Usuario estudiante {new_username} creado exitosamente")
        else:
            st.error("Error al crear el usuario estudiante")

    # Aquí puedes añadir más funcionalidades para el panel de administración

##################################################################################################
def user_page():
    st.title("Bienvenido a AIdeaText")
    st.write(f"Hola, {st.session_state.username}")

    # Aquí puedes añadir las funcionalidades para el usuario estudiante
    # Por ejemplo:
    tabs = st.tabs(["Análisis Morfosintáctico", "Análisis Semántico", "Análisis del Discurso", "Chat", "Mi Progreso"])
    
    with tabs[0]:
        display_morphosyntax_analysis_interface(nlp_models, 'es')  # Asumiendo que 'es' es el idioma por defecto
    with tabs[1]:
        display_semantic_analysis_interface(nlp_models, 'es')
    with tabs[2]:
        display_discourse_analysis_interface(nlp_models, 'es')
    with tabs[3]:
        display_chatbot_interface('es')
    with tabs[4]:
        display_student_progress(st.session_state.username, 'es')

##################################################################################################
def display_videos_and_info():
    st.header("Videos: pitch, demos, entrevistas, otros")
    
    videos = {
        "Intro AideaText": "https://www.youtube.com/watch?v=UA-md1VxaRc",
        "Pitch IFE Explora": "https://www.youtube.com/watch?v=Fqi4Di_Rj_s",
        "Entrevista Dr. Guillermo Ruíz": "https://www.youtube.com/watch?v=_ch8cRja3oc",
        "Demo versión desktop": "https://www.youtube.com/watch?v=nP6eXbog-ZY"
    }
    
    selected_title = st.selectbox("Selecciona un video tutorial:", list(videos.keys()))
    
    if selected_title in videos:
        try:
            st_player(videos[selected_title])
        except Exception as e:
            st.error(f"Error al cargar el video: {str(e)}")
    
    st.markdown("""
    ## Novedades de la versión actual
    - Nueva función de análisis semántico
    - Soporte para múltiples idiomas
    - Interfaz mejorada para una mejor experiencia de usuario
    """)

##################################################################################################
def register_form():
    st.header("Solicitar prueba de la aplicación")
    
    name = st.text_input("Nombre completo")
    email = st.text_input("Correo electrónico institucional")
    institution = st.text_input("Institución")
    role = st.selectbox("Rol", ["Estudiante", "Profesor", "Investigador", "Otro"])
    reason = st.text_area("¿Por qué estás interesado en probar AIdeaText?")
    
    if st.button("Enviar solicitud"):
        logger.info(f"Attempting to submit application for {email}")
        logger.debug(f"Form data: name={name}, email={email}, institution={institution}, role={role}, reason={reason}")
        
        if not name or not email or not institution or not reason:
            logger.warning("Incomplete form submission")
            st.error("Por favor, completa todos los campos.")
        elif not is_institutional_email(email):
            logger.warning(f"Non-institutional email used: {email}")
            st.error("Por favor, utiliza un correo electrónico institucional.")
        else:
            logger.info(f"Attempting to store application for {email}")
            success = store_application_request(name, email, institution, role, reason)
            if success:
                st.success("Tu solicitud ha sido enviada. Te contactaremos pronto.")
                logger.info(f"Application request stored successfully for {email}")
            else:
                st.error("Hubo un problema al enviar tu solicitud. Por favor, intenta de nuevo más tarde.")
                logger.error(f"Failed to store application request for {email}")


def is_institutional_email(email):
    forbidden_domains = ['gmail.com', 'hotmail.com', 'yahoo.com', 'outlook.com']
    return not any(domain in email.lower() for domain in forbidden_domains)
################################################################################

def display_student_progress(username, lang_code='es'):
    student_data = get_student_data(username)
    
    if student_data is None or len(student_data['entries']) == 0:
        st.warning("No se encontraron datos para este estudiante.")
        st.info("Intenta realizar algunos análisis de texto primero.")
        return

    st.title(f"Progreso de {username}")

    with st.expander("Resumen de Actividades y Progreso", expanded=True):
        # Resumen de actividades
        total_entries = len(student_data['entries'])
        st.write(f"Total de análisis realizados: {total_entries}")

        # Gráfico de tipos de análisis
        analysis_types = [entry['analysis_type'] for entry in student_data['entries']]
        analysis_counts = pd.Series(analysis_types).value_counts()
        
        fig, ax = plt.subplots()
        analysis_counts.plot(kind='bar', ax=ax)
        ax.set_title("Tipos de análisis realizados")
        ax.set_xlabel("Tipo de análisis")
        ax.set_ylabel("Cantidad")
        st.pyplot(fig)

        # Progreso a lo largo del tiempo
        dates = [datetime.fromisoformat(entry['timestamp']) for entry in student_data['entries']]
        analysis_counts = pd.Series(dates).value_counts().sort_index()
        
        fig, ax = plt.subplots()
        analysis_counts.plot(kind='line', ax=ax)
        ax.set_title("Análisis realizados a lo largo del tiempo")
        ax.set_xlabel("Fecha")
        ax.set_ylabel("Cantidad de análisis")
        st.pyplot(fig)

    with st.expander("Histórico de Análisis Morfosintácticos"):
        morphosyntax_entries = [entry for entry in student_data['entries'] if entry['analysis_type'] == 'morphosyntax']
        for entry in morphosyntax_entries:
            st.subheader(f"Análisis del {entry['timestamp']}")
            if entry['arc_diagrams']:
                st.write(entry['arc_diagrams'][0], unsafe_allow_html=True)

    with st.expander("Histórico de Análisis Semánticos"):
            semantic_entries = [entry for entry in student_data['entries'] if entry['analysis_type'] == 'semantic']
            for entry in semantic_entries:
                st.subheader(f"Análisis del {entry['timestamp']}")
                st.write(f"Archivo analizado: {entry.get('filename', 'Nombre no disponible')}")
                if 'network_diagram' in entry:
                    try:
                        # Intentar decodificar la imagen si está en formato base64
                        image_bytes = base64.b64decode(entry['network_diagram'])
                        st.image(image_bytes)
                    except Exception as e:
                        st.error(f"No se pudo mostrar la imagen: {str(e)}")
                        st.write("Datos de la imagen (para depuración):")
                        st.write(entry['network_diagram'][:100] + "...")  # 

    with st.expander("Histórico de Análisis Discursivos"):
        discourse_entries = [entry for entry in student_data['entries'] if entry['analysis_type'] == 'discourse']
        for entry in discourse_entries:
            st.subheader(f"Análisis del {entry['timestamp']}")
            st.write(f"Archivo patrón: {entry.get('filename1', 'Nombre no disponible')}")
            st.write(f"Archivo comparado: {entry.get('filename2', 'Nombre no disponible')}")
            
            try:
                # Intentar obtener y combinar las dos imágenes
                if 'graph1' in entry and 'graph2' in entry:
                    img1 = Image.open(BytesIO(base64.b64decode(entry['graph1'])))
                    img2 = Image.open(BytesIO(base64.b64decode(entry['graph2'])))
                    
                    # Crear una nueva imagen combinada
                    total_width = img1.width + img2.width
                    max_height = max(img1.height, img2.height)
                    combined_img = Image.new('RGB', (total_width, max_height))
                    
                    # Pegar las dos imágenes lado a lado
                    combined_img.paste(img1, (0, 0))
                    combined_img.paste(img2, (img1.width, 0))
                    
                    # Convertir la imagen combinada a bytes
                    buffered = BytesIO()
                    combined_img.save(buffered, format="PNG")
                    img_str = base64.b64encode(buffered.getvalue()).decode()
                    
                    # Mostrar la imagen combinada
                    st.image(f"data:image/png;base64,{img_str}")
                elif 'combined_graph' in entry:
                    # Si ya existe una imagen combinada, mostrarla directamente
                    img_bytes = base64.b64decode(entry['combined_graph'])
                    st.image(img_bytes)
                else:
                    st.write("No se encontraron gráficos para este análisis.")
            except Exception as e:
                st.error(f"No se pudieron mostrar los gráficos: {str(e)}")
                st.write("Datos de los gráficos (para depuración):")
                if 'graph1' in entry:
                    st.write("Graph 1:", entry['graph1'][:100] + "...")
                if 'graph2' in entry:
                    st.write("Graph 2:", entry['graph2'][:100] + "...")
                if 'combined_graph' in entry:
                    st.write("Combined Graph:", entry['combined_graph'][:100] + "...")

    with st.expander("Histórico de Conversaciones con el ChatBot"):
        if 'chat_history' in student_data:
            for i, chat in enumerate(student_data['chat_history']):
                st.subheader(f"Conversación {i+1} - {chat['timestamp']}")
                for message in chat['messages']:
                    if message['role'] == 'user':
                        st.write("Usuario: " + message['content'])
                    else:
                        st.write("Asistente: " + message['content'])
                st.write("---")
        else:
            st.write("No se encontraron conversaciones con el ChatBot.")

    # Añadir logs para depuración
    if st.checkbox("Mostrar datos de depuración"):
        st.write("Datos del estudiante (para depuración):")
        st.json(student_data)

##################################################################################################
def display_morphosyntax_analysis_interface(nlp_models, lang_code):
    translations = {
        'es': {
            'title': "AIdeaText - Análisis morfológico y sintáctico",
            'input_label': "Ingrese un texto para analizar (máx. 5,000 palabras):",
            'input_placeholder': "El objetivo de esta aplicación es que mejore sus habilidades de redacción...",  
            'analyze_button': "Analizar texto",
            'repeated_words': "Palabras repetidas",
            'legend': "Leyenda: Categorías gramaticales",
            'arc_diagram': "Análisis sintáctico: Diagrama de arco",
            'sentence': "Oración",
            'success_message': "Análisis guardado correctamente.",
            'error_message': "Hubo un problema al guardar el análisis. Por favor, inténtelo de nuevo.",
            'warning_message': "Por favor, ingrese un texto para analizar."
        },
        'en': {
            'title': "AIdeaText - Morphological and Syntactic Analysis",
            'input_label': "Enter a text to analyze (max 5,000 words):",
            'input_placeholder': "The goal of this app is for you to improve your writing skills...",
            'analyze_button': "Analyze text",
            'repeated_words': "Repeated words",
            'legend': "Legend: Grammatical categories",
            'arc_diagram': "Syntactic analysis: Arc diagram",
            'sentence': "Sentence",
            'success_message': "Analysis saved successfully.",
            'error_message': "There was a problem saving the analysis. Please try again.",
            'warning_message': "Please enter a text to analyze."
        },
        'fr': {
            'title': "AIdeaText - Analyse morphologique et syntaxique",
            'input_label': "Entrez un texte à analyser (max 5 000 mots) :",
            'input_placeholder': "Le but de cette application est d'améliorer vos compétences en rédaction...",
            'analyze_button': "Analyser le texte",
            'repeated_words': "Mots répétés",
            'legend': "Légende : Catégories grammaticales",
            'arc_diagram': "Analyse syntaxique : Diagramme en arc",
            'sentence': "Phrase",
            'success_message': "Analyse enregistrée avec succès.",
            'error_message': "Un problème est survenu lors de l'enregistrement de l'analyse. Veuillez réessayer.",
            'warning_message': "Veuillez entrer un texte à analyser."
        }
    }

    t = translations[lang_code]

    input_key = f"morphosyntax_input_{lang_code}"

    if input_key not in st.session_state:
        st.session_state[input_key] = ""

    sentence_input = st.text_area(
        t['input_label'],
        height=150,
        placeholder=t['input_placeholder'],
        value=st.session_state[input_key],
        key=f"text_area_{lang_code}",
        on_change=lambda: setattr(st.session_state, input_key, st.session_state[f"text_area_{lang_code}"])
    )

    if st.button(t['analyze_button'], key=f"analyze_button_{lang_code}"):
        current_input = st.session_state[input_key]
        if current_input:
            doc = nlp_models[lang_code](current_input)
            
            word_colors = get_repeated_words_colors(doc)
            
            with st.expander(t['repeated_words'], expanded=True):
                highlighted_text = highlight_repeated_words(doc, word_colors)
                st.markdown(highlighted_text, unsafe_allow_html=True)
            
            st.markdown(f"##### {t['legend']}")
            legend_html = "<div style='display: flex; flex-wrap: wrap;'>"
            for pos, color in POS_COLORS.items():
                if pos in POS_TRANSLATIONS[lang_code]:
                    legend_html += f"<div style='margin-right: 10px;'><span style='background-color: {color}; padding: 2px 5px;'>{POS_TRANSLATIONS[lang_code][pos]}</span></div>"
            legend_html += "</div>"
            st.markdown(legend_html, unsafe_allow_html=True)
            
            with st.expander(t['arc_diagram'], expanded=True):
                sentences = list(doc.sents)
                arc_diagrams = []
                for i, sent in enumerate(sentences):
                    st.subheader(f"{t['sentence']} {i+1}")
                    html = displacy.render(sent, style="dep", options={"distance": 100})
                    html = html.replace('height="375"', 'height="200"')
                    html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
                    html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
                    st.write(html, unsafe_allow_html=True)
                    arc_diagrams.append(html)
            
            if store_morphosyntax_result(
                st.session_state.username,
                current_input,
                word_colors,
                arc_diagrams,
            ):
                st.success(t['success_message'])
            else:
                st.error(t['error_message'])
        else:
            st.warning(t['warning_message'])

###############################################################################################################
def display_semantic_analysis_interface(nlp_models, lang_code):
    translations = {
        'es': {
            'title': "AIdeaText - Análisis semántico",
            'file_uploader': "Cargar archivo de texto",
            'analyze_button': "Analizar texto",
            'semantic_relations': "Relaciones Semánticas Relevantes",
            'success_message': "Análisis semántico guardado correctamente.",
            'error_message': "Hubo un problema al guardar el análisis semántico. Por favor, inténtelo de nuevo.",
            'warning_message': "Por favor, cargue un archivo para analizar."
        },
        'en': {
            'title': "AIdeaText - Semantic Analysis",
            'file_uploader': "Upload text file",
            'analyze_button': "Analyze text",
            'semantic_relations': "Relevant Semantic Relations",
            'success_message': "Semantic analysis saved successfully.",
            'error_message': "There was a problem saving the semantic analysis. Please try again.",
            'warning_message': "Please upload a file to analyze."
        },
        'fr': {
            'title': "AIdeaText - Analyse sémantique",
            'file_uploader': "Télécharger le fichier texte",
            'analyze_button': "Analyser le texte",
            'semantic_relations': "Relations Sémantiques Pertinentes",
            'success_message': "Analyse sémantique enregistrée avec succès.",
            'error_message': "Un problème est survenu lors de l'enregistrement de l'analyse sémantique. Veuillez réessayer.",
            'warning_message': "Veuillez télécharger un fichier à analyser."
        }
    }
    
    t = translations[lang_code]
    st.header(t['title'])

    # Opción para cargar archivo
    uploaded_file = st.file_uploader(t['file_uploader'], type=['txt'])

    if st.button(t['analyze_button']):
        if uploaded_file is not None:
            text_content = uploaded_file.getvalue().decode('utf-8')
            
            # Realizar el análisis
            relations_graph = perform_semantic_analysis(text_content, nlp_models[lang_code], lang_code)
            
            # Mostrar el gráfico de relaciones semánticas
            with st.expander(t['semantic_relations'], expanded=True):
                st.pyplot(relations_graph)
            
            # Guardar el resultado del análisis
            if store_semantic_result(st.session_state.username, text_content, relations_graph):
                st.success(t['success_message'])
            else:
                st.error(t['error_message'])
        else:
            st.warning(t['warning_message'])

##################################################################################################
def display_discourse_analysis_interface(nlp_models, lang_code):
    translations = {
        'es': {
            'title': "AIdeaText - Análisis del discurso",
            'file_uploader1': "Cargar archivo de texto 1 (Patrón)",
            'file_uploader2': "Cargar archivo de texto 2 (Comparación)",
            'analyze_button': "Analizar textos",
            'comparison': "Comparación de Relaciones Semánticas",
            'success_message': "Análisis del discurso guardado correctamente.",
            'error_message': "Hubo un problema al guardar el análisis del discurso. Por favor, inténtelo de nuevo.",
            'warning_message': "Por favor, cargue ambos archivos para analizar."
        },
        'en': {
            'title': "AIdeaText - Discourse Analysis",
            'file_uploader1': "Upload text file 1 (Pattern)",
            'file_uploader2': "Upload text file 2 (Comparison)",
            'analyze_button': "Analyze texts",
            'comparison': "Comparison of Semantic Relations",
            'success_message': "Discourse analysis saved successfully.",
            'error_message': "There was a problem saving the discourse analysis. Please try again.",
            'warning_message': "Please upload both files to analyze."
        },
        'fr': {
            'title': "AIdeaText - Analyse du discours",
            'file_uploader1': "Télécharger le fichier texte 1 (Modèle)",
            'file_uploader2': "Télécharger le fichier texte 2 (Comparaison)",
            'analyze_button': "Analyser les textes",
            'comparison': "Comparaison des Relations Sémantiques",
            'success_message': "Analyse du discours enregistrée avec succès.",
            'error_message': "Un problème est survenu lors de l'enregistrement de l'analyse du discours. Veuillez réessayer.",
            'warning_message': "Veuillez télécharger les deux fichiers à analyser."
        }
    }

    t = translations[lang_code]
    st.header(t['title'])

    col1, col2 = st.columns(2)

    with col1:
        uploaded_file1 = st.file_uploader(t['file_uploader1'], type=['txt'])

    with col2:
        uploaded_file2 = st.file_uploader(t['file_uploader2'], type=['txt'])

    if st.button(t['analyze_button']):
        if uploaded_file1 is not None and uploaded_file2 is not None:
            text_content1 = uploaded_file1.getvalue().decode('utf-8')
            text_content2 = uploaded_file2.getvalue().decode('utf-8')

            # Realizar el análisis
            graph1, graph2 = perform_discourse_analysis(text_content1, text_content2, nlp_models[lang_code], lang_code)

            # Mostrar los gráficos de comparación
            st.subheader(t['comparison'])
            col1, col2 = st.columns(2)
            with col1:
                st.pyplot(graph1)
            with col2:
                st.pyplot(graph2)

            # Guardar el resultado del análisis
            #if store_discourse_analysis_result(st.session_state.username, text_content1 + "\n\n" + text_content2, graph1, graph2):
            if store_discourse_analysis_result(st.session_state.username, text_content1, text_content2, graph1, graph2):    
                st.success(t['success_message'])
            else:
                st.error(t['error_message'])
        else:
            st.warning(t['warning_message'])

##################################################################################################
#def display_saved_discourse_analysis(analysis_data):
#    img_bytes = base64.b64decode(analysis_data['combined_graph'])
#    img = plt.imread(io.BytesIO(img_bytes), format='png')
    
#    st.image(img, use_column_width=True)
#    st.write("Texto del documento patrón:")
#    st.write(analysis_data['text1'])
#    st.write("Texto del documento comparado:")
#    st.write(analysis_data['text2'])

##################################################################################################            
def display_chatbot_interface(lang_code):
    translations = {
        'es': {
            'title': "Expertos en Vacaciones",
            'input_placeholder': "Escribe tu mensaje aquí...",
            'initial_message': "¡Hola! ¿Cómo podemos ayudarte?"
        },
        'en': {
            'title': "Vacation Experts",
            'input_placeholder': "Type your message here...",
            'initial_message': "Hi! How can we help you?"
        },
        'fr': {
            'title': "Experts en Vacances",
            'input_placeholder': "Écrivez votre message ici...",
            'initial_message': "Bonjour! Comment pouvons-nous vous aider?"
        }
    }
    t = translations[lang_code]
    st.title(t['title'])

    if 'chatbot' not in st.session_state:
        st.session_state.chatbot = initialize_chatbot()
    if 'messages' not in st.session_state:
        st.session_state.messages = [{"role": "assistant", "content": t['initial_message']}]

    # Contenedor principal para el chat
    chat_container = st.container()

    # Mostrar mensajes existentes
    with chat_container:
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

    # Área de entrada del usuario
    user_input = st.chat_input(t['input_placeholder'])

    if user_input:
        # Agregar mensaje del usuario
        st.session_state.messages.append({"role": "user", "content": user_input})

        # Mostrar mensaje del usuario
        with chat_container:
            with st.chat_message("user"):
                st.markdown(user_input)

        # Generar respuesta del chatbot
        with chat_container:
            with st.chat_message("assistant"):
                message_placeholder = st.empty()
                full_response = ""
                for chunk in get_chatbot_response(st.session_state.chatbot, user_input, lang_code):
                    full_response += chunk
                    message_placeholder.markdown(full_response + "▌")
                message_placeholder.markdown(full_response)

        # Agregar respuesta del asistente a los mensajes
        st.session_state.messages.append({"role": "assistant", "content": full_response})

        # Guardar la conversación en la base de datos
        try:
            store_chat_history(st.session_state.username, st.session_state.messages)
            st.success("Conversación guardada exitosamente")
        except Exception as e:
            st.error(f"Error al guardar la conversación: {str(e)}")
            logger.error(f"Error al guardar el historial de chat para {st.session_state.username}: {str(e)}")

    # Scroll al final del chat
    st.markdown('<script>window.scrollTo(0,document.body.scrollHeight);</script>', unsafe_allow_html=True)

######################################################
if __name__ == "__main__":
    main()