|
|
|
import gradio as gr |
|
import os |
|
import logging |
|
from modules.ui.landing_ui import create_landing_interface |
|
from modules.ui.login_ui import create_login_interface |
|
from modules.auth.auth import authenticate_user |
|
from modules.database.database_init import initialize_database_connections |
|
import spaces |
|
import torch |
|
|
|
zero = torch.Tensor([0]).cuda() |
|
print(zero.device) |
|
|
|
@spaces.GPU |
|
def greet(n): |
|
print(zero.device) |
|
return f"Hello {zero + n} Tensor" |
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
COSMOS_ENDPOINT = os.getenv("COSMOS_ENDPOINT") |
|
COSMOS_KEY = os.getenv("COSMOS_KEY") |
|
|
|
if not COSMOS_ENDPOINT or not COSMOS_KEY: |
|
raise ValueError("Faltan las variables de entorno COSMOS_ENDPOINT y COSMOS_KEY.") |
|
|
|
|
|
if not initialize_database_connections(): |
|
raise ValueError("No se pudo inicializar la conexi贸n a la base de datos.") |
|
|
|
|
|
def app_main(): |
|
""" |
|
Inicializa la aplicaci贸n con Gradio. |
|
""" |
|
logger.info("Iniciando AIdeaText") |
|
|
|
|
|
state = gr.State({ |
|
"logged_in": False, |
|
"username": None, |
|
"role": None |
|
}) |
|
|
|
with gr.Blocks(css="#container { max-width: 800px; margin: auto; }") as app_interface: |
|
gr.HTML("<div id='container'>") |
|
|
|
|
|
landing_container = gr.Group(visible=True) |
|
login_container = gr.Group(visible=False) |
|
user_container = gr.Group(visible=False) |
|
|
|
|
|
def switch_view(to_view, state_update=None): |
|
updates = { |
|
"landing": [True, False, False], |
|
"login": [False, True, False], |
|
"user": [False, False, True] |
|
} |
|
visibilities = updates[to_view] |
|
if state_update: |
|
state.update(state_update) |
|
return [gr.update(visible=v) for v in visibilities] |
|
|
|
|
|
def handle_login(username, password, state): |
|
success, role = authenticate_user(username, password) |
|
if success: |
|
state.update({ |
|
"logged_in": True, |
|
"username": username, |
|
"role": role |
|
}) |
|
return switch_view("user", state) + [f"Bienvenido, {username}"] |
|
return switch_view("login") + ["Credenciales incorrectas"] |
|
|
|
|
|
with landing_container: |
|
landing_interface = create_landing_interface() |
|
landing_interface.render(lambda: switch_view("login")) |
|
|
|
with login_container: |
|
login_interface = create_login_interface() |
|
login_interface.render( |
|
lambda: switch_view("landing"), |
|
handle_login |
|
) |
|
|
|
with user_container: |
|
user_interface = create_user_interface() |
|
user_interface.render(lambda: switch_view("landing")) |
|
|
|
gr.HTML("</div>") |
|
|
|
app_interface.launch(server_name="0.0.0.0", server_port=7860) |
|
|
|
if __name__ == "__main__": |
|
app_main() |
|
|