Update app.py
Browse files
app.py
CHANGED
|
@@ -29,12 +29,12 @@ from modules.syntax_analysis import visualize_syntax
|
|
| 29 |
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
| 30 |
cosmos_key = os.environ.get("COSMOS_KEY")
|
| 31 |
|
| 32 |
-
if not cosmos_endpoint or not cosmos_key:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
# SQL API database for user management
|
| 40 |
user_database = cosmos_client.get_database_client("user_database")
|
|
@@ -48,6 +48,10 @@ except Exception as e:
|
|
| 48 |
# MongoDB API configuration for text analysis results
|
| 49 |
#mongo_connection_string = os.environ.get("MONGODB_CONNECTION_STRING")
|
| 50 |
cosmos_mongodb_connection_string = os.getenv("MONGODB_CONNECTION_STRING")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Variable global para el cliente de MongoDB
|
| 53 |
mongo_client = None
|
|
@@ -55,6 +59,9 @@ db = None
|
|
| 55 |
analysis_collection = None
|
| 56 |
|
| 57 |
def initialize_mongodb_connection():
|
|
|
|
|
|
|
|
|
|
| 58 |
global mongo_client, db, analysis_collection
|
| 59 |
try:
|
| 60 |
# Crear el cliente de MongoDB con configuración TLS
|
|
@@ -127,8 +134,23 @@ def load_spacy_models():
|
|
| 127 |
'en': spacy.load("en_core_web_lg"),
|
| 128 |
'fr': spacy.load("fr_core_news_lg")
|
| 129 |
}
|
| 130 |
-
|
| 131 |
def store_analysis_result(username, text, repeated_words, arc_diagrams, network_diagram):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
try:
|
| 133 |
# Convertir el gráfico de matplotlib a base64
|
| 134 |
buffer = io.BytesIO()
|
|
@@ -160,7 +182,8 @@ def store_analysis_result(username, text, repeated_words, arc_diagrams, network_
|
|
| 160 |
except Exception as e:
|
| 161 |
logging.error(f"Error al guardar el análisis para el usuario {username}: {str(e)}")
|
| 162 |
return False
|
| 163 |
-
|
|
|
|
| 164 |
def login_page():
|
| 165 |
st.title("Iniciar Sesión")
|
| 166 |
username = st.text_input("Usuario")
|
|
@@ -175,6 +198,7 @@ def login_page():
|
|
| 175 |
else:
|
| 176 |
st.error("Usuario o contraseña incorrectos")
|
| 177 |
|
|
|
|
| 178 |
def register_page():
|
| 179 |
st.title("Registrarse")
|
| 180 |
new_username = st.text_input("Nuevo Usuario")
|
|
@@ -193,6 +217,7 @@ def register_page():
|
|
| 193 |
else:
|
| 194 |
st.error("El usuario ya existe o ocurrió un error durante el registro")
|
| 195 |
|
|
|
|
| 196 |
def main_app():
|
| 197 |
# Load spaCy models
|
| 198 |
nlp_models = load_spacy_models()
|
|
@@ -346,7 +371,11 @@ def main_app():
|
|
| 346 |
st.write("Bienvenido, profesor. Aquí podrás ver el progreso de tus estudiantes.")
|
| 347 |
# Add logic to display student progress
|
| 348 |
|
|
|
|
| 349 |
def main():
|
|
|
|
|
|
|
|
|
|
| 350 |
if 'logged_in' not in st.session_state:
|
| 351 |
st.session_state.logged_in = False
|
| 352 |
|
|
|
|
| 29 |
cosmos_endpoint = os.environ.get("COSMOS_ENDPOINT")
|
| 30 |
cosmos_key = os.environ.get("COSMOS_KEY")
|
| 31 |
|
| 32 |
+
if not cosmos_endpoint or not cosmos_key:
|
| 33 |
+
raise ValueError("Las variables de entorno COSMOS_ENDPOINT y COSMOS_KEY deben estar configuradas")
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
cosmos_key = clean_and_validate_key(cosmos_key)
|
| 37 |
+
cosmos_client = CosmosClient(cosmos_endpoint, cosmos_key)
|
| 38 |
|
| 39 |
# SQL API database for user management
|
| 40 |
user_database = cosmos_client.get_database_client("user_database")
|
|
|
|
| 48 |
# MongoDB API configuration for text analysis results
|
| 49 |
#mongo_connection_string = os.environ.get("MONGODB_CONNECTION_STRING")
|
| 50 |
cosmos_mongodb_connection_string = os.getenv("MONGODB_CONNECTION_STRING")
|
| 51 |
+
if not cosmos_mongodb_connection_string:
|
| 52 |
+
logger.error("La variable de entorno MONGODB_CONNECTION_STRING no está configurada")
|
| 53 |
+
else:
|
| 54 |
+
logger.info("La variable de entorno MONGODB_CONNECTION_STRING está configurada")
|
| 55 |
|
| 56 |
# Variable global para el cliente de MongoDB
|
| 57 |
mongo_client = None
|
|
|
|
| 59 |
analysis_collection = None
|
| 60 |
|
| 61 |
def initialize_mongodb_connection():
|
| 62 |
+
if not initialize_mongodb_connection():
|
| 63 |
+
st.error("No se pudo establecer la conexión con MongoDB. Algunas funcionalidades pueden no estar disponibles.")
|
| 64 |
+
|
| 65 |
global mongo_client, db, analysis_collection
|
| 66 |
try:
|
| 67 |
# Crear el cliente de MongoDB con configuración TLS
|
|
|
|
| 134 |
'en': spacy.load("en_core_web_lg"),
|
| 135 |
'fr': spacy.load("fr_core_news_lg")
|
| 136 |
}
|
| 137 |
+
#########################################################################################################
|
| 138 |
def store_analysis_result(username, text, repeated_words, arc_diagrams, network_diagram):
|
| 139 |
+
#if analysis_collection is None:
|
| 140 |
+
# logging.error("La conexión a MongoDB no está inicializada")
|
| 141 |
+
# return False
|
| 142 |
+
if store_analysis_result(
|
| 143 |
+
st.session_state.username,
|
| 144 |
+
sentence_input,
|
| 145 |
+
word_colors,
|
| 146 |
+
arc_diagrams,
|
| 147 |
+
fig
|
| 148 |
+
):
|
| 149 |
+
st.success("Análisis guardado correctamente.")
|
| 150 |
+
else:
|
| 151 |
+
st.error("Hubo un problema al guardar el análisis. Por favor, inténtelo de nuevo.")
|
| 152 |
+
logger.error("Falló el guardado del análisis. Username: %s", st.session_state.username)
|
| 153 |
+
|
| 154 |
try:
|
| 155 |
# Convertir el gráfico de matplotlib a base64
|
| 156 |
buffer = io.BytesIO()
|
|
|
|
| 182 |
except Exception as e:
|
| 183 |
logging.error(f"Error al guardar el análisis para el usuario {username}: {str(e)}")
|
| 184 |
return False
|
| 185 |
+
|
| 186 |
+
#############################################################################################33
|
| 187 |
def login_page():
|
| 188 |
st.title("Iniciar Sesión")
|
| 189 |
username = st.text_input("Usuario")
|
|
|
|
| 198 |
else:
|
| 199 |
st.error("Usuario o contraseña incorrectos")
|
| 200 |
|
| 201 |
+
#####################################################################################################3
|
| 202 |
def register_page():
|
| 203 |
st.title("Registrarse")
|
| 204 |
new_username = st.text_input("Nuevo Usuario")
|
|
|
|
| 217 |
else:
|
| 218 |
st.error("El usuario ya existe o ocurrió un error durante el registro")
|
| 219 |
|
| 220 |
+
############################################################################################
|
| 221 |
def main_app():
|
| 222 |
# Load spaCy models
|
| 223 |
nlp_models = load_spacy_models()
|
|
|
|
| 371 |
st.write("Bienvenido, profesor. Aquí podrás ver el progreso de tus estudiantes.")
|
| 372 |
# Add logic to display student progress
|
| 373 |
|
| 374 |
+
#####################################################################################################
|
| 375 |
def main():
|
| 376 |
+
if not initialize_mongodb_connection():
|
| 377 |
+
st.warning("La conexión a la base de datos MongoDB no está disponible. Algunas funciones pueden no estar operativas.")
|
| 378 |
+
|
| 379 |
if 'logged_in' not in st.session_state:
|
| 380 |
st.session_state.logged_in = False
|
| 381 |
|