|
|
|
|
|
import streamlit as st |
|
from streamlit_player import st_player |
|
import logging |
|
from datetime import datetime, timezone |
|
from dateutil.parser import parse |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
from session_state import initialize_session_state, logout |
|
|
|
from translations import get_translations |
|
|
|
from ..studentact.student_activities import display_student_progress |
|
|
|
from ..auth.auth import authenticate_user, authenticate_student, authenticate_admin |
|
|
|
from ..admin.admin_ui import admin_page |
|
|
|
|
|
|
|
from ..database.sql_db import ( |
|
store_application_request, |
|
store_student_feedback, |
|
store_application_request |
|
) |
|
|
|
from ..database.mongo_db import ( |
|
get_collection, |
|
insert_document, |
|
find_documents, |
|
update_document, |
|
delete_document |
|
) |
|
|
|
from ..database.morphosintax_mongo_db import ( |
|
store_student_morphosyntax_result, |
|
get_student_morphosyntax_analysis, |
|
update_student_morphosyntax_analysis, |
|
delete_student_morphosyntax_analysis, |
|
get_student_morphosyntax_data |
|
) |
|
|
|
from ..morphosyntax.morphosyntax_interface import ( |
|
display_morphosyntax_interface, |
|
display_morphosyntax_results |
|
) |
|
|
|
from ..semantic.semantic_interface import ( |
|
display_semantic_interface, |
|
display_semantic_results |
|
) |
|
|
|
from ..discourse.discourse_interface import ( |
|
display_discourse_interface, |
|
display_discourse_results |
|
) |
|
|
|
def main(): |
|
logger.info(f"Entrando en main() - Página actual: {st.session_state.page}") |
|
|
|
if 'nlp_models' not in st.session_state: |
|
logger.error("Los modelos NLP no están inicializados.") |
|
st.error("Los modelos NLP no están inicializados. Por favor, reinicie la aplicación.") |
|
return |
|
|
|
lang_code = st.session_state.get('lang_code', 'es') |
|
t = get_translations(lang_code) |
|
|
|
logger.info(f"Página actual antes de la lógica de enrutamiento: {st.session_state.page}") |
|
|
|
if st.session_state.get('logged_out', False): |
|
st.session_state.logged_out = False |
|
st.session_state.page = 'login' |
|
st.rerun() |
|
|
|
if not st.session_state.get('logged_in', False): |
|
logger.info("Usuario no ha iniciado sesión. Mostrando página de login/registro") |
|
login_register_page(lang_code, t) |
|
elif st.session_state.page == 'user': |
|
if st.session_state.role == 'Administrador': |
|
logger.info("Redirigiendo a la página de administrador") |
|
st.session_state.page = 'Admin' |
|
st.rerun() |
|
else: |
|
logger.info("Renderizando página de usuario") |
|
user_page(lang_code, t) |
|
elif st.session_state.page == "Admin": |
|
logger.info("Renderizando página de administrador") |
|
admin_page() |
|
|
|
elif st.session_state.page == "semantic": |
|
|
|
|
|
logger.info("Redirigiendo página semántica a página de usuario") |
|
st.session_state.page = 'user' |
|
st.session_state.selected_tab = 1 |
|
st.rerun() |
|
|
|
elif st.session_state.page == "morpho": |
|
|
|
logger.info("Redirigiendo página morfosintáctica a página de usuario") |
|
st.session_state.page = 'user' |
|
st.session_state.selected_tab = 0 |
|
st.rerun() |
|
|
|
else: |
|
logger.error(f"Página no reconocida: {st.session_state.page}") |
|
st.error(t.get('unrecognized_page', 'Página no reconocida')) |
|
|
|
st.session_state.page = 'user' |
|
st.rerun() |
|
|
|
logger.info(f"Saliendo de main() - Estado final de la sesión: {st.session_state}") |
|
|
|
def login_register_page(lang_code, t): |
|
|
|
|
|
|
|
left_column, right_column = st.columns([1, 3]) |
|
|
|
with left_column: |
|
tab1, tab2 = st.tabs([t.get("login", "Iniciar Sesión"), |
|
t.get("register", "Registrarse")]) |
|
|
|
with tab1: |
|
login_form(lang_code, t) |
|
|
|
with tab2: |
|
register_form(lang_code, t) |
|
|
|
with right_column: |
|
display_videos_and_info(lang_code, t) |
|
|
|
def login_form(lang_code, t): |
|
with st.form("login_form"): |
|
username = st.text_input(t.get("email", "Correo electrónico")) |
|
password = st.text_input(t.get("password", "Contraseña"), type="password") |
|
submit_button = st.form_submit_button(t.get("login", "Iniciar Sesión")) |
|
|
|
if submit_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 |
|
if role == 'Administrador': |
|
st.session_state.page = 'Admin' |
|
else: |
|
st.session_state.page = 'user' |
|
logger.info(f"Usuario autenticado: {username}, Rol: {role}") |
|
st.rerun() |
|
else: |
|
st.error(t.get("invalid_credentials", "Credenciales incorrectas")) |
|
|
|
def register_form(lang_code, t): |
|
st.header(t.get("request_trial", "Solicitar prueba de la aplicación")) |
|
|
|
name = st.text_input(t.get("name", "Nombre")) |
|
lastname = st.text_input(t.get("lastname", "Apellidos")) |
|
institution = st.text_input(t.get("institution", "Institución")) |
|
current_role = st.selectbox(t.get("current_role", "Rol en la institución donde labora"), |
|
[t.get("professor", "Profesor"), t.get("student", "Estudiante"), t.get("administrative", "Administrativo")]) |
|
desired_role = st.selectbox(t.get("desired_role", "Rol con el que desea registrarse en AIdeaText"), |
|
[t.get("professor", "Profesor"), t.get("student", "Estudiante")]) |
|
email = st.text_input(t.get("institutional_email", "Correo electrónico de su institución")) |
|
reason = st.text_area(t.get("interest_reason", "¿Por qué estás interesado en probar AIdeaText?")) |
|
|
|
if st.button(t.get("submit_application", "Enviar solicitud")): |
|
logger.info(f"Attempting to submit application for {email}") |
|
logger.debug(f"Form data: name={name}, lastname={lastname}, email={email}, institution={institution}, current_role={current_role}, desired_role={desired_role}, reason={reason}") |
|
|
|
if not name or not lastname or not email or not institution or not reason: |
|
logger.warning("Incomplete form submission") |
|
st.error(t.get("complete_all_fields", "Por favor, completa todos los campos.")) |
|
elif not is_institutional_email(email): |
|
logger.warning(f"Non-institutional email used: {email}") |
|
st.error(t.get("use_institutional_email", "Por favor, utiliza un correo electrónico institucional.")) |
|
else: |
|
logger.info(f"Attempting to store application for {email}") |
|
success = store_application_request(name, lastname, email, institution, current_role, desired_role, reason) |
|
if success: |
|
st.success(t.get("application_sent", "Tu solicitud ha sido enviada. Te contactaremos pronto.")) |
|
logger.info(f"Application request stored successfully for {email}") |
|
else: |
|
st.error(t.get("application_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_videos_and_info(lang_code, t): |
|
st.header("Conferencias") |
|
|
|
videos = { |
|
"Reel AIdeaText": "https://youtu.be/UA-md1VxaRc", |
|
"Presentación en SENDA, UNAM. Ciudad de México, México" : "https://www.youtube.com/watch?v=XFLvjST2cE0", |
|
"Presentación en PyCon 2024. Colombia, Medellín": "https://www.youtube.com/watch?v=Jn545-IKx5Q", |
|
"Presentación en la Fundación Ser Maaestro. Lima, Perú": "https://www.youtube.com/watch?v=imc4TI1q164", |
|
"Presentación en el programa de incubación Explora del IFE, TEC de Monterrey, Nuevo León, México": "https://www.youtube.com/watch?v=Fqi4Di_Rj_s", |
|
"Entrevista con el Dr. Guillermo Ruíz. Lima, Perú": "https://www.youtube.com/watch?v=_ch8cRja3oc", |
|
"Demo de la versión de escritorio.": "https://www.youtube.com/watch?v=nP6eXbog-ZY" |
|
} |
|
|
|
selected_title = st.selectbox("Selecciona una conferencia:", 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 |
|
- Interfaz mejorada para una mejor experiencia de usuario |
|
- Optimización del análisis morfosintáctico |
|
- Soporte para múltiples idiomas |
|
""") |
|
|
|
|
|
|
|
def user_page(lang_code, t): |
|
logger.info(f"Entrando en user_page para el estudiante: {st.session_state.username}") |
|
|
|
|
|
if 'selected_tab' not in st.session_state: |
|
st.session_state.selected_tab = 0 |
|
|
|
|
|
if 'user_data' not in st.session_state: |
|
with st.spinner(t.get('loading_data', "Cargando tus datos...")): |
|
try: |
|
st.session_state.user_data = get_student_morphosyntax_data(st.session_state.username) |
|
st.session_state.last_data_fetch = datetime.now(timezone.utc).isoformat() |
|
except Exception as e: |
|
logger.error(f"Error al obtener datos del usuario: {str(e)}") |
|
st.error(t.get('data_load_error', "Hubo un problema al cargar tus datos. Por favor, intenta recargar la página.")) |
|
return |
|
|
|
logger.info(f"Idioma actual: {st.session_state.lang_code}") |
|
logger.info(f"Modelos NLP cargados: {'nlp_models' in st.session_state}") |
|
|
|
|
|
languages = {'Español': 'es', 'English': 'en', 'Français': 'fr'} |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.stSelectbox > div > div { |
|
padding-top: 0px; |
|
} |
|
.stButton > button { |
|
padding-top: 2px; |
|
margin-top: 0px; |
|
} |
|
div[data-testid="stHorizontalBlock"] > div:nth-child(3) { |
|
display: flex; |
|
justify-content: flex-end; |
|
align-items: center; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
with st.container(): |
|
col1, col2, col3 = st.columns([2, 2, 1]) |
|
with col1: |
|
st.markdown(f"<h3 style='margin-bottom: 0; padding-top: 10px;'>{t['welcome']}, {st.session_state.username}</h3>", |
|
unsafe_allow_html=True) |
|
with col2: |
|
selected_lang = st.selectbox( |
|
t['select_language'], |
|
list(languages.keys()), |
|
index=list(languages.values()).index(st.session_state.lang_code), |
|
key=f"language_selector_{st.session_state.username}_{st.session_state.lang_code}" |
|
) |
|
new_lang_code = languages[selected_lang] |
|
if st.session_state.lang_code != new_lang_code: |
|
st.session_state.lang_code = new_lang_code |
|
st.rerun() |
|
with col3: |
|
if st.button(t['logout'], |
|
key=f"logout_button_{st.session_state.username}_{st.session_state.lang_code}"): |
|
st.session_state.clear() |
|
st.rerun() |
|
|
|
st.markdown("---") |
|
|
|
|
|
tabs = st.tabs([ |
|
t.get('morpho_tab', 'Análisis Morfosintáctico'), |
|
t.get('semantic_tab', 'Análisis Semántico'), |
|
t.get('discourse_tab', 'Análsis de discurso'), |
|
t.get('activities_tab', 'Mis Actividades'), |
|
t.get('feedback_tab', 'Formulario de Comentarios') |
|
]) |
|
|
|
|
|
for index, tab in enumerate(tabs): |
|
if tab.selected: |
|
st.session_state.selected_tab = index |
|
|
|
with tab: |
|
if index == 0: |
|
display_morphosyntax_interface( |
|
st.session_state.lang_code, |
|
st.session_state.nlp_models, |
|
t.get('TRANSLATIONS', {}) |
|
) |
|
elif index == 1: |
|
display_semantic_interface( |
|
st.session_state.lang_code, |
|
st.session_state.nlp_models, |
|
t.get('TRANSLATIONS', {}) |
|
) |
|
elif index == 2: |
|
display_discourse_interface( |
|
st.session_state.lang_code, |
|
st.session_state.nlp_models, |
|
t.get('TRANSLATIONS', {}) |
|
) |
|
elif index == 3: |
|
if st.session_state.user_data: |
|
display_student_progress( |
|
st.session_state.username, |
|
st.session_state.lang_code, |
|
t |
|
) |
|
else: |
|
st.warning(t.get('no_data_warning', 'No se encontraron datos para este estudiante.')) |
|
elif index == 3: |
|
display_feedback_form(st.session_state.lang_code, t) |
|
|
|
|
|
if st.session_state.get('debug_mode', False): |
|
with st.expander("Debug Info"): |
|
st.write(f"Página actual: {st.session_state.page}") |
|
st.write(f"Usuario: {st.session_state.get('username', 'No logueado')}") |
|
st.write(f"Rol: {st.session_state.get('role', 'No definido')}") |
|
st.write(f"Idioma: {st.session_state.lang_code}") |
|
st.write(f"Tab seleccionado: {st.session_state.selected_tab}") |
|
st.write(f"Última actualización de datos: {st.session_state.get('last_data_fetch', 'Nunca')}") |
|
|
|
|
|
def display_feedback_form(lang_code, t): |
|
logging.info(f"display_feedback_form called with lang_code: {lang_code}") |
|
|
|
st.header(t['feedback_title']) |
|
|
|
name = st.text_input(t['name']) |
|
email = st.text_input(t['email']) |
|
feedback = st.text_area(t['feedback']) |
|
|
|
if st.button(t['submit']): |
|
if name and email and feedback: |
|
if store_student_feedback(st.session_state.username, name, email, feedback): |
|
st.success(t['feedback_success']) |
|
else: |
|
st.error(t['feedback_error']) |
|
else: |
|
st.warning(t['complete_all_fields']) |
|
|
|
|
|
__all__ = ['main', 'login_register_page', 'initialize_session_state'] |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |