v3 / modules /ui /ui.py
AIdeaText's picture
Update modules/ui/ui.py
ad9c16f verified
raw
history blame
12.6 kB
# Importaciones generales
import streamlit as st
from streamlit_player import st_player
import logging
from datetime import datetime, timezone
from dateutil.parser import parse
# Configuraci贸n del logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Importaciones locales
#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
##Importaciones desde la configuraci贸n de bases datos #######
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
)
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()
else:
logger.error(f"P谩gina no reconocida: {st.session_state.page}")
st.error(t.get('unrecognized_page', 'P谩gina no reconocida'))
logger.info(f"Saliendo de main() - Estado final de la sesi贸n: {st.session_state}")
def login_register_page(lang_code, t):
st.title("AIdeaText")
st.write(t.get("welcome_message", "Bienvenido. Por favor, inicie sesi贸n o reg铆strese."))
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("Videos: pitch, demos, entrevistas, otros")
videos = {
"Presentaci贸n en PyCon Colombia, Medell铆n, 2024": "https://www.youtube.com/watch?v=Jn545-IKx5Q",
"Presentaci贸n fundaci贸n Ser Maaestro": "https://www.youtube.com/watch?v=imc4TI1q164",
}
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
- Interfaz mejorada para una mejor experiencia de usuario
- Optimizaci贸n del an谩lisis morfosint谩ctico
- Soporte para m煤ltiples idiomas
""")
#Despu茅s de iniciar sesi贸n
def user_page(lang_code, t):
logger.info(f"Entrando en user_page para el estudiante: {st.session_state.username}")
current_time = datetime.now(timezone.utc)
last_fetch_time = st.session_state.get('last_data_fetch')
if last_fetch_time:
last_fetch_time = parse(last_fetch_time)
else:
last_fetch_time = datetime.min.replace(tzinfo=timezone.utc)
# Comprobar si necesitamos recargar los datos del usuario
if 'user_data' not in st.session_state or (current_time - last_fetch_time).total_seconds() > 60:
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 = current_time.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'}
# Estilos CSS personalizados (mantener los estilos existentes)
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}"):
logout()
st.rerun()
# Reinicializar el estado de la sesi贸n
initialize_session_state()
# Recargar la aplicaci贸n
st.rerun()
st.markdown("---")
tabs = st.tabs([
t.get('morpho_tab', 'An谩lisis Morfosint谩ctico'),
t.get('activities_tab', 'Mis Actividades'),
t.get('feedback_tab', 'Formulario de Comentarios')
])
with tabs[0]:
display_morphosyntax_interface(st.session_state.lang_code, st.session_state.nlp_models, t)
with tabs[1]:
if 'user_data' in st.session_state and 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.'))
with tabs[2]:
display_feedback_form(st.session_state.lang_code, t)
# Informaci贸n de depuraci贸n
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"脷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'])
# Definici贸n de __all__ para especificar qu茅 se exporta
__all__ = ['main', 'login_register_page', 'initialize_session_state']
# Bloque de ejecuci贸n condicional
if __name__ == "__main__":
main()