# Filename: /home/user/app/app.py # Version: 1691068773 (Unix timestamp at creation moment) import os import subprocess # Definir el archivo de log antes de instalar las bibliotecas log_file = "/home/user/app/app.log" # Instalar las bibliotecas necesarias def install_packages(): # Instalar los paquetes necesarios si aún no están instalados with open(log_file, "a") as f: f.write("===== Installing Packages =====\n") subprocess.run(['pip3', 'install', '--quiet', 'jupyterlab', 'flask', 'gradio', 'spaces'], check=True) install_packages() # Ahora importar las bibliotecas necesarias from flask import Flask, redirect import gradio as gr from werkzeug.serving import WSGIRequestHandler app = Flask(__name__) def configure_jupyter(): # Generar la configuración de Jupyter jupyter_config_dir = os.path.expanduser('~/.jupyter') os.makedirs(jupyter_config_dir, exist_ok=True) config_file = os.path.join(jupyter_config_dir, 'jupyter_lab_config.py') jupyter_password = os.environ.get('JUPYPASS', '') password_config = f"c.ServerApp.password = u'{jupyter_password}'" if jupyter_password else '' with open(config_file, 'w') as f: f.write(f""" c.ServerApp.ip = '0.0.0.0' c.ServerApp.port = 8898 c.ServerApp.open_browser = False {password_config} c.ServerApp.root_dir = '/home/user/app' c.ServerApp.terminado_settings = {{'shell_command': ['bash']}} c.ServerApp.allow_root = True """) def start_jupyter(): # Iniciar JupyterLab en el puerto 8898 con autoreload with open(log_file, "a") as f: subprocess.Popen(['jupyter-lab', '--port', '8898', '--autoreload'], stdout=f, stderr=f) @app.route('/') def home(): import spaces @spaces.GPU(duration=150) def greet(name): return f"Hello {name}!" with gr.Blocks() as demo: gr.Interface(fn=greet, inputs="text", outputs="text") return demo.launch(inline=True) @app.route('/lab') def lab(): return redirect("http://localhost:8898", code=302) if __name__ == "__main__": with open(log_file, "a") as f: f.write("===== Application Startup at {0} =====\n".format(subprocess.run(['date'], capture_output=True, text=True).stdout)) configure_jupyter() start_jupyter() # Deshabilitar los logs para Flask WSGIRequestHandler.log_request = lambda self, *args, **kwargs: None app.run(host='0.0.0.0', port=7860, debug=False)