ACCA225 commited on
Commit
4c6fd94
1 Parent(s): 4679d32

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +141 -3
app1.py CHANGED
@@ -1,4 +1,20 @@
1
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  # import wandb
3
  os.system("pip install nvidia-ml-py3")
4
  os.chdir(f"/home/xlab-app-center")
@@ -68,7 +84,117 @@ import time
68
  import time
69
  import nvidia_smi
70
  import wandb
71
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  # 初始化 nvidia_smi
73
  nvidia_smi.nvmlInit()
74
 
@@ -93,8 +219,20 @@ def wandb():
93
 
94
 
95
  def start():
96
- os.system(f"python launch.py --api --xformers --enable-insecure-extension-access --ui-settings-file /home/xlab-app-center/config.json --ui-config-file /home/xlab-app-center/ui-config.json --gradio-queue --disable-safe-unpickle")
97
-
 
 
 
 
 
 
 
 
 
 
 
 
98
  # Create threads for each function
99
  wandb_thread = threading.Thread(target=wandb)
100
  start_thread = threading.Thread(target=start)
 
1
  import os
2
+ import threading
3
+ #使用的库
4
+ from pathlib import Path
5
+ import subprocess
6
+ import pandas as pd
7
+ import shutil
8
+ import os
9
+ import time
10
+ import re
11
+ import gc
12
+ import requests
13
+ import zipfile
14
+ import threading
15
+ import time
16
+ import socket
17
+ from concurrent.futures import ProcessPoolExecutor
18
  # import wandb
19
  os.system("pip install nvidia-ml-py3")
20
  os.chdir(f"/home/xlab-app-center")
 
84
  import time
85
  import nvidia_smi
86
  import wandb
87
+ show_shell_info = False
88
+ def run(command, cwd=None, desc=None, errdesc=None, custom_env=None,try_error:bool=True) -> str:
89
+ global show_shell_info
90
+ if desc is not None:
91
+ print(desc)
92
+
93
+ run_kwargs = {
94
+ "args": command,
95
+ "shell": True,
96
+ "cwd": cwd,
97
+ "env": os.environ if custom_env is None else custom_env,
98
+ "encoding": 'utf8',
99
+ "errors": 'ignore',
100
+ }
101
+
102
+ if not show_shell_info:
103
+ run_kwargs["stdout"] = run_kwargs["stderr"] = subprocess.PIPE
104
+
105
+ result = subprocess.run(**run_kwargs)
106
+
107
+ if result.returncode != 0:
108
+ error_bits = [
109
+ f"{errdesc or 'Error running command'}.",
110
+ f"Command: {command}",
111
+ f"Error code: {result.returncode}",
112
+ ]
113
+ if result.stdout:
114
+ error_bits.append(f"stdout: {result.stdout}")
115
+ if result.stderr:
116
+ error_bits.append(f"stderr: {result.stderr}")
117
+ if try_error:
118
+ print((RuntimeError("\n".join(error_bits))))
119
+ else:
120
+ raise RuntimeError("\n".join(error_bits))
121
+
122
+ if show_shell_info:
123
+ print((result.stdout or ""))
124
+ return (result.stdout or "")
125
+
126
+ def mkdirs(path, exist_ok=True):
127
+ if path and not Path(path).exists():
128
+ os.makedirs(path,exist_ok=exist_ok)
129
+ proxy_path={
130
+ '/sd2/':'http://127.0.0.1:7862/',
131
+ '/sd3/':'http://127.0.0.1:7863/'
132
+ } # 增加一个comfyui的代理
133
+ server_port=7860 # webui 默认端口
134
+ _server_port = locals().get('server_port') or globals().get('server_port') or 7860
135
+
136
+
137
+ _proxy_path = locals().get('proxy_path') or globals().get('proxy_path') or {}
138
+ # nginx 反向代理配置文件
139
+ def echoToFile(content:str,path:str):
140
+ if path.find('/') >= 0:
141
+ _path = '/'.join(path.split('/')[:-1])
142
+ run(f'''mkdir -p {_path}''')
143
+ with open(path,'w') as sh:
144
+ sh.write(content)
145
+ # 检查网络
146
+ def check_service(host, port):
147
+ try:
148
+ socket.create_connection((host, port), timeout=5)
149
+ return True
150
+ except socket.error:
151
+ return False
152
+ def localProxy():
153
+ os.system('sudo apt install nginx -y')
154
+
155
+ _proxy_path['/'] = f'http://127.0.0.1:{_server_port+1}/'
156
+ _proxy_path['/1/'] = f'http://127.0.0.1:{_server_port+2}/'
157
+
158
+ def getProxyLocation(subPath:str, localServer:str):
159
+ return '''
160
+ location '''+ subPath +'''
161
+ {
162
+ proxy_pass '''+ localServer +''';
163
+ proxy_set_header Host $host;
164
+ proxy_set_header X-Real-IP $remote_addr;
165
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
166
+ proxy_set_header REMOTE-HOST $remote_addr;
167
+ proxy_set_header Upgrade $http_upgrade;
168
+ proxy_set_header Connection upgrade;
169
+ proxy_http_version 1.1;
170
+ proxy_connect_timeout 10m;
171
+ proxy_read_timeout 10m;
172
+ }
173
+
174
+ '''
175
+
176
+ conf = '''
177
+ server
178
+ {
179
+ listen '''+str(_server_port)+''';
180
+ listen [::]:'''+str(_server_port)+''';
181
+ server_name 127.0.0.1 localhost 0.0.0.0 "";
182
+
183
+ if ($request_method = OPTIONS) {
184
+ return 200;
185
+ }
186
+ fastcgi_send_timeout 10m;
187
+ fastcgi_read_timeout 10m;
188
+ fastcgi_connect_timeout 10m;
189
+
190
+ '''+ ''.join([getProxyLocation(key,_proxy_path[key]) for key in _proxy_path.keys()]) +'''
191
+ }
192
+ '''
193
+ echoToFile(conf,'/home/xlab-app-center/etc/nginx/conf.d/proxy_nginx.conf')
194
+ if not check_service('localhost',_server_port):
195
+ run(f'''nginx -c /home/xlab-app-center/etc/nginx/nginx.conf''')
196
+ run(f'''nginx -s reload''')
197
+
198
  # 初始化 nvidia_smi
199
  nvidia_smi.nvmlInit()
200
 
 
219
 
220
 
221
  def start():
222
+ try:
223
+ print('启动proxy')
224
+ threading.Thread(target = localProxy,daemon=True).start()
225
+ except Exception as e:
226
+ # 在这里处理异常的代码
227
+ print(f"proxy An error occurred: {e}")
228
+ try:
229
+ #安装环境
230
+ os.system(f"python launch.py --api --xformers --exit --enable-insecure-extension-access --gradio-queue --disable-safe-unpickle")
231
+ time.sleep(5)
232
+ os.system(f"python launch.py --ngrok=2YypH9d2VXVL78HxF7g0lintH17_2GbtkFNFFngUkTZcyQ4SD --api --xformers --enable-insecure-extension-access --ui-settings-file /home/xlab-app-center/config.json --ui-config-file /home/xlab-app-center/ui-config.json --gradio-queue --disable-safe-unpickle & python launch.py --api --xformers --enable-insecure-extension-access --ui-settings-file /home/xlab-app-center/config.json --ui-config-file /home/xlab-app-center/ui-config.json --gradio-queue --disable-safe-unpickle --port=7862")
233
+ except Exception as e:
234
+ # 在这里处理异常的代码
235
+ print(f"启动SD发生错误: {e}")
236
  # Create threads for each function
237
  wandb_thread = threading.Thread(target=wandb)
238
  start_thread = threading.Thread(target=start)