lang03383 commited on
Commit
caccb37
1 Parent(s): ed07009

Create launch_en.py

Browse files
Files changed (1) hide show
  1. sd_yun/launch_en.py +125 -0
sd_yun/launch_en.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ##~ LAUNCH CODE | BY: ANXETY ~##
2
+
3
+ import os
4
+ import re
5
+ import time
6
+ import json
7
+ import requests
8
+ import subprocess
9
+ import cloudpickle as pickle
10
+ from datetime import timedelta
11
+ from IPython.display import clear_output
12
+
13
+
14
+ # Setup Env
15
+ env = os.getenv('ENV_NAME')
16
+ root_path = os.getenv('ROOT_PATH')
17
+ webui_path = os.getenv('WEBUI_PATH')
18
+ free_plan = os.getenv('FREE_PLAN')
19
+
20
+
21
+ def load_settings():
22
+ SETTINGS_FILE = f'{root_path}/settings.json'
23
+ if os.path.exists(SETTINGS_FILE):
24
+ with open(SETTINGS_FILE, 'r') as f:
25
+ return json.load(f)
26
+ return {}
27
+
28
+ settings = load_settings()
29
+ ngrok_token = settings.get('ngrok_token', "")
30
+ zrok_token = settings.get('zrok_token', "")
31
+ commandline_arguments = settings.get('commandline_arguments', "")
32
+ change_webui = settings.get('change_webui', "")
33
+
34
+
35
+ # ========================== OTHER ==========================
36
+ def is_package_installed(package_name):
37
+ try:
38
+ subprocess.run(["npm", "ls", "-g", package_name], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
39
+ return True
40
+ except subprocess.CalledProcessError:
41
+ return False
42
+
43
+ lt_tunnel = is_package_installed('localtunnel')
44
+
45
+
46
+ # ======================== TUNNEL V2 ========================
47
+ print('Please Wait...')
48
+
49
+ def get_public_ip(version='ipv4'):
50
+ try:
51
+ url = f'https://api64.ipify.org?format=json&{version}=true'
52
+ response = requests.get(url)
53
+ return response.json().get('ip', 'N/A')
54
+ except Exception as e:
55
+ print(f"Error getting public {version} address:", e)
56
+
57
+ # Check if public IP is already saved, if not then get it
58
+ public_ip_file = f"{root_path}/public_ip.txt"
59
+ if os.path.exists(public_ip_file):
60
+ with open(public_ip_file, 'r') as file:
61
+ public_ipv4 = file.read().strip()
62
+ else:
63
+ public_ipv4 = get_public_ip(version='ipv4')
64
+ with open(public_ip_file, 'w') as file:
65
+ file.write(public_ipv4)
66
+
67
+ tunnel_class = pickle.load(open(f"{root_path}/new_tunnel", "rb"), encoding="utf-8")
68
+ tunnel_port = 1834
69
+ tunnel = tunnel_class(tunnel_port)
70
+ tunnel.add_tunnel(command="cl tunnel --url localhost:{port}", name="cl", pattern=re.compile(r"[\w-]+\.trycloudflare\.com"))
71
+
72
+ if lt_tunnel:
73
+ 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.")
74
+
75
+ if zrok_token:
76
+ get_ipython().system('zrok enable {zrok_token} &> /dev/null')
77
+ tunnel.add_tunnel(command="zrok share public http://localhost:{port}/ --headless", name="zrok", pattern=re.compile(r"[\w-]+\.share\.zrok\.io"))
78
+
79
+ clear_output()
80
+
81
+
82
+ # ================= Automatic Fixing Path V3 ================
83
+ paths_to_check = {
84
+ "tagger_hf_cache_dir": f"{webui_path}/models/interrogators/",
85
+ "ad_extra_models_dir": f"{webui_path}/models/adetailer/",
86
+ "sd_checkpoint_hash": "",
87
+ "sd_model_checkpoint": "",
88
+ "sd_vae": "None"
89
+ }
90
+
91
+ config_path = f'{webui_path}/config.json'
92
+
93
+ if os.path.exists(config_path):
94
+ with open(config_path, 'r') as file:
95
+ config_data = json.load(file)
96
+
97
+ for key, value in paths_to_check.items():
98
+ if key in config_data and config_data[key] != value:
99
+ sed_command = f"sed -i 's|\"{key}\": \".*\"|\"{key}\": \"{value}\"|' {config_path}"
100
+ os.system(sed_command)
101
+
102
+
103
+ with tunnel:
104
+ get_ipython().run_line_magic('cd', '{webui_path}')
105
+
106
+ commandline_arguments += f' --port={tunnel_port}'
107
+ if ngrok_token:
108
+ commandline_arguments += f' --ngrok {ngrok_token}'
109
+ if env != "Google Colab":
110
+ commandline_arguments += f' --encrypt-pass={tunnel_port} --api'
111
+
112
+ if change_webui == 'A1111':
113
+ commandline_arguments += ' --xformers'
114
+ else:
115
+ commandline_arguments += ' --disable-xformers --opt-sdp-attention --cuda-stream --pin-shared-memory'
116
+ get_ipython().system('COMMANDLINE_ARGS="{commandline_arguments}" python upload.py &> /dev/null &')
117
+ get_ipython().system('COMMANDLINE_ARGS="{commandline_arguments}" python launch.py')
118
+
119
+ start_colab = float(open(f'{webui_path}/static/colabTimer.txt', 'r').read())
120
+ time_since_start = str(timedelta(seconds=time.time()-start_colab)).split('.')[0]
121
+ print(f"\n⌚️ \033[0mYou have been conducting this session for - \033[33m{time_since_start}\033[0m\n\n")
122
+
123
+ if zrok_token:
124
+ get_ipython().system('zrok disable &> /dev/null')
125
+