kagglee / files_cells_python_en_launch_en.py
duhking's picture
Upload 4 files
cb7de7b verified
raw
history blame
4.31 kB
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
##~ LAUNCH CODE | BY: ANXETY ~##
import os
import re
import time
import json
import requests
from datetime import timedelta
# ================= DETECT ENV =================
def detect_environment():
environments = {
'COLAB_GPU': ('Google Colab', "/content"),
'KAGGLE_URL_BASE': ('Kaggle', "/kaggle/working/content"),
'SAGEMAKER_INTERNAL_IMAGE_URI': ('SageMaker Studio Lab', "/home/studio-lab-user/content")
}
for env_var, (environment, path) in environments.items():
if env_var in os.environ:
return environment, path
env, root_path = detect_environment()
webui_path = f"{root_path}/sdw"
# ----------------------------------------------
def load_settings():
SETTINGS_FILE = f'{root_path}/settings.json'
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, 'r') as f:
settings = json.load(f)
return settings
settings = load_settings()
ngrok_token = settings['ngrok_token']
commandline_arguments = settings['commandline_arguments']
# ======================== TUNNEL ========================
if env != "SageMaker Studio Lab":
import cloudpickle as pickle
password = "x1101"
def get_public_ip(version='ipv4'):
try:
url = f'https://api64.ipify.org?format=json&{version}=true'
response = requests.get(url)
data = response.json()
public_ip = data['ip']
return public_ip
except Exception as e:
print(f"Error getting public {version} address:", e)
public_ipv4 = get_public_ip(version='ipv4')
tunnel_class = pickle.load(open(f"{root_path}/new_tunnel", "rb"), encoding="utf-8")
tunnel_port= 1101
tunnel = tunnel_class(tunnel_port)
tunnel.add_tunnel(command="cl tunnel --url localhost:{port}", name="cl", pattern=re.compile(r"[\w-]+\.trycloudflare\.com"))
tunnel.add_tunnel(command="lt --port {port}", name="lt", pattern=re.compile(r"[\w-]+\.loca\.lt"), note="Password : " + "\033[32m" + public_ipv4 + "\033[0m" + " rerun cell if 404 error.")
# ======================== TUNNEL ========================
# automatic fixing path V2
get_ipython().system('sed -i \'s#"tagger_hf_cache_dir": ".*models/interrogators"#"tagger_hf_cache_dir": "{root_path}/sdw/models/interrogators"#\' {webui_path}/config.json')
get_ipython().system('sed -i \'s#"additional_networks_extra_lora_path": ".*models/Lora/"#"additional_networks_extra_lora_path": "{root_path}/sdw/models/Lora/"#\' {webui_path}/config.json')
# ---
get_ipython().system('sed -i \'s/"sd_checkpoint_hash": ".*"/"sd_checkpoint_hash": ""/g; s/"sd_model_checkpoint": ".*"/"sd_model_checkpoint": ""/g; s/"sd_vae": ".*"/"sd_vae": "None"/g\' {webui_path}/config.json')
if env != "SageMaker Studio Lab":
with tunnel:
get_ipython().system('#python -m http.server 1101')
get_ipython().run_line_magic('cd', '{webui_path}')
if ngrok_token:
commandline_arguments += ' --ngrok ' + ngrok_token
commandline_arguments += f" --port=1101 --encrypt-pass=1769" # --encrypt-pass={password}
get_ipython().system('COMMANDLINE_ARGS="{commandline_arguments}" python launch.py')
start_colab = float(open(f'{webui_path}/static/colabTimer.txt', 'r').read())
time_since_start = str(timedelta(seconds=time.time()-start_colab)).split('.')[0]
print(f"\n⌚️ \033[0mYou have been conducting this session for - \033[33m{time_since_start}\033[0m\n\n")
else:
if ngrok_token:
get_ipython().run_line_magic('cd', '{webui_path}')
commandline_arguments += ' --ngrok ' + ngrok_token
get_ipython().system('COMMANDLINE_ARGS="{commandline_arguments}" python launch.py')
start_colab = float(open(f'{webui_path}/static/colabTimer.txt', 'r').read())
time_since_start = str(timedelta(seconds=time.time()-start_colab)).split('.')[0]
print(f"\n⌚️ \033[0mYou have been conducting this session for - \033[33m{time_since_start}\033[0m\n\n")
else:
print("Oops... I think you forgot to insert the token `ngrok`..... go back to widgets and insert it to start webui... no way without it :/\nYou can get the token here:\n\nhttps://dashboard.ngrok.com/get-started/your-authtoken")