Pyboxs commited on
Commit
ec13836
1 Parent(s): b96b843
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -3,27 +3,34 @@
3
 
4
  import os
5
  import subprocess
6
- import psutil
7
 
8
  # Definir el archivo de log antes de instalar las bibliotecas
9
  log_file = "/home/user/app/app.log"
10
 
11
  # Instalar las bibliotecas necesarias
12
  def install_packages():
13
- # Instalar los paquetes necesarios si aún no están instalados
14
  with open(log_file, "a") as f:
15
  f.write("===== Installing Packages =====\n")
16
- subprocess.run(['pip3', 'install', 'jupyterlab', 'flask', 'gradio'], check=True)
17
 
18
  install_packages()
19
 
20
  # Ahora importar las bibliotecas necesarias
21
- from flask import Flask, redirect
22
  import gradio as gr
23
- from werkzeug.serving import WSGIRequestHandler
 
24
 
25
  app = Flask(__name__)
26
 
 
 
 
 
 
 
 
27
  def configure_jupyter():
28
  # Generar la configuración de Jupyter
29
  jupyter_config_dir = os.path.expanduser('~/.jupyter')
@@ -44,13 +51,6 @@ c.ServerApp.terminado_settings = {{'shell_command': ['bash']}}
44
  c.ServerApp.allow_root = True
45
  """)
46
 
47
- def is_jupyter_running():
48
- # Verificar si JupyterLab ya está en ejecución
49
- for process in psutil.process_iter(['pid', 'name']):
50
- if 'jupyter-lab' in process.info['name']:
51
- return True
52
- return False
53
-
54
  def start_jupyter():
55
  # Iniciar JupyterLab en el puerto 8898 con autoreload si no está en ejecución
56
  if not is_jupyter_running():
@@ -68,8 +68,22 @@ def home():
68
  return demo.launch(inline=True)
69
 
70
  @app.route('/jupy')
71
- def lab():
72
- return redirect("http://localhost:8898", code=302)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  if __name__ == "__main__":
75
  with open(log_file, "a") as f:
@@ -77,5 +91,5 @@ if __name__ == "__main__":
77
 
78
  configure_jupyter()
79
  start_jupyter()
80
-
81
  app.run(host='0.0.0.0', port=7860, debug=False)
 
3
 
4
  import os
5
  import subprocess
 
6
 
7
  # Definir el archivo de log antes de instalar las bibliotecas
8
  log_file = "/home/user/app/app.log"
9
 
10
  # Instalar las bibliotecas necesarias
11
  def install_packages():
12
+ # Instalar los paquetes necesarios
13
  with open(log_file, "a") as f:
14
  f.write("===== Installing Packages =====\n")
15
+ subprocess.run(['pip3', 'install', 'jupyterlab', 'flask', 'gradio', 'psutil', 'requests'], check=True, stdout=f, stderr=f)
16
 
17
  install_packages()
18
 
19
  # Ahora importar las bibliotecas necesarias
20
+ from flask import Flask, redirect, request, Response
21
  import gradio as gr
22
+ import psutil
23
+ import requests
24
 
25
  app = Flask(__name__)
26
 
27
+ def is_jupyter_running():
28
+ # Verificar si JupyterLab ya está en ejecución
29
+ for process in psutil.process_iter(['pid', 'name']):
30
+ if 'jupyter-lab' in process.info['name']:
31
+ return True
32
+ return False
33
+
34
  def configure_jupyter():
35
  # Generar la configuración de Jupyter
36
  jupyter_config_dir = os.path.expanduser('~/.jupyter')
 
51
  c.ServerApp.allow_root = True
52
  """)
53
 
 
 
 
 
 
 
 
54
  def start_jupyter():
55
  # Iniciar JupyterLab en el puerto 8898 con autoreload si no está en ejecución
56
  if not is_jupyter_running():
 
68
  return demo.launch(inline=True)
69
 
70
  @app.route('/jupy')
71
+ def jupy():
72
+ # Proxy inverso para JupyterLab
73
+ jupyter_url = "http://localhost:8898"
74
+ resp = requests.request(
75
+ method=request.method,
76
+ url=jupyter_url + request.path,
77
+ headers={key: value for key, value in request.headers if key != 'Host'},
78
+ data=request.get_data(),
79
+ cookies=request.cookies,
80
+ allow_redirects=False)
81
+
82
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
83
+ headers = [(name, value) for name, value in resp.raw.headers.items() if name.lower() not in excluded_headers]
84
+
85
+ response = Response(resp.content, resp.status_code, headers)
86
+ return response
87
 
88
  if __name__ == "__main__":
89
  with open(log_file, "a") as f:
 
91
 
92
  configure_jupyter()
93
  start_jupyter()
94
+
95
  app.run(host='0.0.0.0', port=7860, debug=False)