Spaces:
Paused
Paused
# Filename: /home/user/app/app.py | |
# Version: 1691068773 (Unix timestamp at creation moment) | |
import os | |
import subprocess | |
from flask import Flask, redirect, request | |
import gradio as gr | |
app = Flask(__name__) | |
def install_packages(): | |
# Install necessary packages | |
subprocess.run(['pip3', 'install', 'jupyterlab', 'flask', 'gradio'], check=True) | |
def configure_jupyter(): | |
# Generate Jupyter configuration | |
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 = 8888 | |
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(): | |
# Start JupyterLab on port 8888 with autoreload | |
subprocess.run(['jupyter-lab', '--port', '8888', '--autoreload']) | |
def home(): | |
def greet(name): | |
return f"Hello {name}!" | |
iface = gr.Interface(fn=greet, inputs="text", outputs="text") | |
return iface.launch(inline=True) | |
def lab(): | |
return redirect("http://localhost:8888", code=302) | |
if __name__ == "__main__": | |
install_packages() | |
configure_jupyter() | |
subprocess.Popen(start_jupyter) | |
app.run(host='0.0.0.0', port=7860) |