Update main.py
Browse files
main.py
CHANGED
|
@@ -172,15 +172,11 @@ remotePort = 8188
|
|
| 172 |
|
| 173 |
def install_frp():
|
| 174 |
FRP_VERSION = "0.69.0"
|
| 175 |
-
|
| 176 |
frp_tar_name = f"frp_{FRP_VERSION}_linux_amd64.tar.gz"
|
|
|
|
| 177 |
|
| 178 |
frp_tar_path = os.path.join(REPO_PATH, frp_tar_name)
|
| 179 |
-
|
| 180 |
-
frp_install_path = os.path.join(
|
| 181 |
-
REPO_PATH,
|
| 182 |
-
f"frp_{FRP_VERSION}_linux_amd64"
|
| 183 |
-
)
|
| 184 |
|
| 185 |
frp_download_urls = [
|
| 186 |
f"https://github.com/fatedier/frp/releases/download/v{FRP_VERSION}/{frp_tar_name}",
|
|
@@ -201,29 +197,19 @@ def install_frp():
|
|
| 201 |
["tar", "-tzf", path],
|
| 202 |
stdout=subprocess.PIPE,
|
| 203 |
stderr=subprocess.PIPE,
|
| 204 |
-
text=True
|
|
|
|
| 205 |
)
|
| 206 |
-
|
| 207 |
return result.returncode == 0
|
| 208 |
-
|
| 209 |
except Exception:
|
| 210 |
return False
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
if os.path.exists(frp_tar_path):
|
| 216 |
-
if not is_valid_tar_gz(frp_tar_path):
|
| 217 |
-
print("检测到损坏的 frp 压缩包,正在删除...")
|
| 218 |
-
os.remove(frp_tar_path)
|
| 219 |
-
|
| 220 |
-
# =========================
|
| 221 |
-
# 下载 frp
|
| 222 |
-
# =========================
|
| 223 |
-
if not os.path.exists(frp_tar_path):
|
| 224 |
-
print("开始下载 frp...")
|
| 225 |
|
| 226 |
-
|
|
|
|
| 227 |
|
| 228 |
for url in frp_download_urls:
|
| 229 |
try:
|
|
@@ -232,6 +218,8 @@ def install_frp():
|
|
| 232 |
result = subprocess.run(
|
| 233 |
[
|
| 234 |
"wget",
|
|
|
|
|
|
|
| 235 |
"--no-check-certificate",
|
| 236 |
"-O",
|
| 237 |
frp_tar_path,
|
|
@@ -239,258 +227,62 @@ def install_frp():
|
|
| 239 |
],
|
| 240 |
stdout=subprocess.PIPE,
|
| 241 |
stderr=subprocess.PIPE,
|
| 242 |
-
text=True
|
|
|
|
| 243 |
)
|
| 244 |
|
| 245 |
if result.returncode == 0 and is_valid_tar_gz(frp_tar_path):
|
| 246 |
print("frp 下载成功。")
|
| 247 |
-
download_success = True
|
| 248 |
break
|
| 249 |
|
| 250 |
-
print("下载失败或文件
|
| 251 |
|
| 252 |
if os.path.exists(frp_tar_path):
|
| 253 |
os.remove(frp_tar_path)
|
| 254 |
|
| 255 |
-
except
|
| 256 |
-
print(
|
| 257 |
-
|
| 258 |
-
if not download_success:
|
| 259 |
-
print("frp 下载失败。")
|
| 260 |
-
return False
|
| 261 |
-
|
| 262 |
-
# =========================
|
| 263 |
-
# 解压 frp
|
| 264 |
-
# =========================
|
| 265 |
-
try:
|
| 266 |
-
print(f"正在解压 frp 到 {REPO_PATH}...")
|
| 267 |
-
|
| 268 |
-
subprocess.run(
|
| 269 |
-
[
|
| 270 |
-
"tar",
|
| 271 |
-
"-xzf",
|
| 272 |
-
frp_tar_path,
|
| 273 |
-
"-C",
|
| 274 |
-
REPO_PATH
|
| 275 |
-
],
|
| 276 |
-
check=True
|
| 277 |
-
)
|
| 278 |
-
|
| 279 |
-
frpc_path = os.path.join(
|
| 280 |
-
frp_install_path,
|
| 281 |
-
"frpc"
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
-
frps_path = os.path.join(
|
| 285 |
-
frp_install_path,
|
| 286 |
-
"frps"
|
| 287 |
-
)
|
| 288 |
-
|
| 289 |
-
subprocess.run(
|
| 290 |
-
["chmod", "+x", frpc_path],
|
| 291 |
-
check=False
|
| 292 |
-
)
|
| 293 |
-
|
| 294 |
-
subprocess.run(
|
| 295 |
-
["chmod", "+x", frps_path],
|
| 296 |
-
check=False
|
| 297 |
-
)
|
| 298 |
-
|
| 299 |
-
print("frp 安装成功!")
|
| 300 |
-
return True
|
| 301 |
-
|
| 302 |
-
except subprocess.CalledProcessError as e:
|
| 303 |
-
print(f"解压 frp 时出错: {e}")
|
| 304 |
-
return False
|
| 305 |
-
|
| 306 |
-
def is_valid_tar_gz(path):
|
| 307 |
-
if not os.path.exists(path):
|
| 308 |
-
return False
|
| 309 |
-
|
| 310 |
-
try:
|
| 311 |
-
result = subprocess.run(
|
| 312 |
-
["tar", "-tzf", path],
|
| 313 |
-
stdout=subprocess.PIPE,
|
| 314 |
-
stderr=subprocess.PIPE,
|
| 315 |
-
text=True
|
| 316 |
-
)
|
| 317 |
-
return result.returncode == 0
|
| 318 |
-
except Exception:
|
| 319 |
-
return False
|
| 320 |
-
|
| 321 |
-
if not is_valid_tar_gz(frp_tar_path):
|
| 322 |
-
print("frp 压缩包不存在或不是有效 gzip 文件,正在重新下载...")
|
| 323 |
-
|
| 324 |
-
if os.path.exists(frp_tar_path):
|
| 325 |
-
os.remove(frp_tar_path)
|
| 326 |
-
|
| 327 |
-
download_success = False
|
| 328 |
-
|
| 329 |
-
for url in frp_download_urls:
|
| 330 |
-
try:
|
| 331 |
-
print(f"尝试下载: {url}")
|
| 332 |
-
|
| 333 |
-
result = subprocess.run(
|
| 334 |
-
["wget", "-O", frp_tar_path, url],
|
| 335 |
-
stdout=subprocess.PIPE,
|
| 336 |
-
stderr=subprocess.PIPE,
|
| 337 |
-
text=True
|
| 338 |
-
)
|
| 339 |
-
|
| 340 |
-
if result.returncode == 0 and is_valid_tar_gz(frp_tar_path):
|
| 341 |
-
print("frp 下载成功。")
|
| 342 |
-
download_success = True
|
| 343 |
-
break
|
| 344 |
|
| 345 |
-
|
|
|
|
| 346 |
|
| 347 |
except Exception as e:
|
| 348 |
-
print(f"下载
|
| 349 |
|
| 350 |
-
|
| 351 |
-
|
|
|
|
|
|
|
| 352 |
return False
|
| 353 |
|
| 354 |
try:
|
| 355 |
-
print(f"正在解压 frp
|
| 356 |
|
| 357 |
subprocess.run(
|
| 358 |
-
["tar", "-
|
| 359 |
-
check=True
|
|
|
|
| 360 |
)
|
| 361 |
|
| 362 |
frpc_path = os.path.join(frp_install_path, "frpc")
|
|
|
|
| 363 |
|
| 364 |
if os.path.exists(frpc_path):
|
| 365 |
subprocess.run(["chmod", "+x", frpc_path], check=False)
|
| 366 |
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
| 368 |
return True
|
| 369 |
|
| 370 |
-
except subprocess.
|
| 371 |
-
print(
|
| 372 |
return False
|
| 373 |
-
|
| 374 |
-
def start_jupyterlab_process():
|
| 375 |
-
jupyter_script = os.path.join(REPO_PATH, "start_jupyterlab.py")
|
| 376 |
-
|
| 377 |
-
if not os.path.exists(jupyter_script):
|
| 378 |
-
print(f"无法找到 {jupyter_script}")
|
| 379 |
-
return
|
| 380 |
-
|
| 381 |
-
try:
|
| 382 |
-
subprocess.Popen(
|
| 383 |
-
["python3", jupyter_script],
|
| 384 |
-
stdout=subprocess.PIPE,
|
| 385 |
-
stderr=subprocess.PIPE,
|
| 386 |
-
text=True
|
| 387 |
-
)
|
| 388 |
-
|
| 389 |
-
print("JupyterLab 启动进程已开始。")
|
| 390 |
-
|
| 391 |
-
except Exception as e:
|
| 392 |
-
print(f"启动 JupyterLab 进程时出错: {str(e)}")
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
def start_frp():
|
| 396 |
-
frpc_path = os.path.join(
|
| 397 |
-
REPO_PATH,
|
| 398 |
-
"frp_0.61.0_linux_amd64",
|
| 399 |
-
"frpc"
|
| 400 |
-
)
|
| 401 |
-
|
| 402 |
-
frpc_config_path = os.path.join(REPO_PATH, "frpc.toml")
|
| 403 |
-
|
| 404 |
-
if not os.path.exists(frpc_path):
|
| 405 |
-
print("frpc 可执行文件不存在,请检查路径。")
|
| 406 |
-
return
|
| 407 |
-
|
| 408 |
-
if not os.path.exists(frpc_config_path):
|
| 409 |
-
create_frpc_toml()
|
| 410 |
-
|
| 411 |
-
try:
|
| 412 |
-
subprocess.Popen(
|
| 413 |
-
[frpc_path, "-c", frpc_config_path],
|
| 414 |
-
stdout=subprocess.PIPE,
|
| 415 |
-
stderr=subprocess.PIPE,
|
| 416 |
-
text=True
|
| 417 |
-
)
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
print(f"启动 frp 时出错: {str(e)}")
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
def start_gradio():
|
| 426 |
-
with gr.Blocks() as demo:
|
| 427 |
-
gr.Markdown("# InternLM 控制台")
|
| 428 |
-
|
| 429 |
-
with gr.Row():
|
| 430 |
-
input_box = gr.Textbox(
|
| 431 |
-
label="输入",
|
| 432 |
-
placeholder="输入对话内容,或使用 cmd_run 执行命令"
|
| 433 |
-
)
|
| 434 |
-
|
| 435 |
-
with gr.Row():
|
| 436 |
-
model_box = gr.Textbox(
|
| 437 |
-
value="internlm2.5-latest",
|
| 438 |
-
label="模型"
|
| 439 |
-
)
|
| 440 |
-
|
| 441 |
-
with gr.Row():
|
| 442 |
-
temperature_slider = gr.Slider(
|
| 443 |
-
minimum=0,
|
| 444 |
-
maximum=1,
|
| 445 |
-
value=0.8,
|
| 446 |
-
step=0.01,
|
| 447 |
-
label="Temperature"
|
| 448 |
-
)
|
| 449 |
-
|
| 450 |
-
top_p_slider = gr.Slider(
|
| 451 |
-
minimum=0,
|
| 452 |
-
maximum=1,
|
| 453 |
-
value=0.9,
|
| 454 |
-
step=0.01,
|
| 455 |
-
label="Top-p"
|
| 456 |
-
)
|
| 457 |
-
|
| 458 |
-
with gr.Row():
|
| 459 |
-
n_slider = gr.Slider(
|
| 460 |
-
minimum=1,
|
| 461 |
-
maximum=10,
|
| 462 |
-
value=1,
|
| 463 |
-
step=1,
|
| 464 |
-
label="生成回复数量"
|
| 465 |
-
)
|
| 466 |
-
|
| 467 |
-
with gr.Row():
|
| 468 |
-
send_button = gr.Button("发送")
|
| 469 |
-
|
| 470 |
-
with gr.Row():
|
| 471 |
-
output_box = gr.Textbox(
|
| 472 |
-
label="回复",
|
| 473 |
-
lines=20,
|
| 474 |
-
interactive=False
|
| 475 |
-
)
|
| 476 |
-
|
| 477 |
-
send_button.click(
|
| 478 |
-
api_call,
|
| 479 |
-
inputs=[
|
| 480 |
-
input_box,
|
| 481 |
-
model_box,
|
| 482 |
-
temperature_slider,
|
| 483 |
-
top_p_slider,
|
| 484 |
-
n_slider
|
| 485 |
-
],
|
| 486 |
-
outputs=output_box
|
| 487 |
-
)
|
| 488 |
-
|
| 489 |
-
demo.launch(
|
| 490 |
-
server_name="0.0.0.0",
|
| 491 |
-
server_port=7860,
|
| 492 |
-
share=False
|
| 493 |
-
)
|
| 494 |
|
| 495 |
|
| 496 |
def main():
|
|
@@ -504,19 +296,25 @@ def main():
|
|
| 504 |
install_frp()
|
| 505 |
create_frpc_toml()
|
| 506 |
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
)
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
|
|
|
|
| 520 |
start_gradio()
|
| 521 |
|
| 522 |
|
|
|
|
| 172 |
|
| 173 |
def install_frp():
|
| 174 |
FRP_VERSION = "0.69.0"
|
|
|
|
| 175 |
frp_tar_name = f"frp_{FRP_VERSION}_linux_amd64.tar.gz"
|
| 176 |
+
frp_dir_name = f"frp_{FRP_VERSION}_linux_amd64"
|
| 177 |
|
| 178 |
frp_tar_path = os.path.join(REPO_PATH, frp_tar_name)
|
| 179 |
+
frp_install_path = os.path.join(REPO_PATH, frp_dir_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
frp_download_urls = [
|
| 182 |
f"https://github.com/fatedier/frp/releases/download/v{FRP_VERSION}/{frp_tar_name}",
|
|
|
|
| 197 |
["tar", "-tzf", path],
|
| 198 |
stdout=subprocess.PIPE,
|
| 199 |
stderr=subprocess.PIPE,
|
| 200 |
+
text=True,
|
| 201 |
+
timeout=20
|
| 202 |
)
|
|
|
|
| 203 |
return result.returncode == 0
|
|
|
|
| 204 |
except Exception:
|
| 205 |
return False
|
| 206 |
|
| 207 |
+
if os.path.exists(frp_tar_path) and not is_valid_tar_gz(frp_tar_path):
|
| 208 |
+
print("检测到损坏的 frp 压缩包,删除重下。")
|
| 209 |
+
os.remove(frp_tar_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
+
if not os.path.exists(frp_tar_path):
|
| 212 |
+
print("开始下载 frp。")
|
| 213 |
|
| 214 |
for url in frp_download_urls:
|
| 215 |
try:
|
|
|
|
| 218 |
result = subprocess.run(
|
| 219 |
[
|
| 220 |
"wget",
|
| 221 |
+
"--timeout=30",
|
| 222 |
+
"--tries=1",
|
| 223 |
"--no-check-certificate",
|
| 224 |
"-O",
|
| 225 |
frp_tar_path,
|
|
|
|
| 227 |
],
|
| 228 |
stdout=subprocess.PIPE,
|
| 229 |
stderr=subprocess.PIPE,
|
| 230 |
+
text=True,
|
| 231 |
+
timeout=60
|
| 232 |
)
|
| 233 |
|
| 234 |
if result.returncode == 0 and is_valid_tar_gz(frp_tar_path):
|
| 235 |
print("frp 下载成功。")
|
|
|
|
| 236 |
break
|
| 237 |
|
| 238 |
+
print("下载失败或文件无效。")
|
| 239 |
|
| 240 |
if os.path.exists(frp_tar_path):
|
| 241 |
os.remove(frp_tar_path)
|
| 242 |
|
| 243 |
+
except subprocess.TimeoutExpired:
|
| 244 |
+
print("frp 下载超时,尝试下一个源。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
if os.path.exists(frp_tar_path):
|
| 247 |
+
os.remove(frp_tar_path)
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
+
print(f"frp 下载异常: {e}")
|
| 251 |
|
| 252 |
+
if os.path.exists(frp_tar_path):
|
| 253 |
+
os.remove(frp_tar_path)
|
| 254 |
+
else:
|
| 255 |
+
print("frp 下载失败,跳过 frp,不影响 Gradio 启动。")
|
| 256 |
return False
|
| 257 |
|
| 258 |
try:
|
| 259 |
+
print(f"正在解压 frp 到 {REPO_PATH}...")
|
| 260 |
|
| 261 |
subprocess.run(
|
| 262 |
+
["tar", "-xzf", frp_tar_path, "-C", REPO_PATH],
|
| 263 |
+
check=True,
|
| 264 |
+
timeout=30
|
| 265 |
)
|
| 266 |
|
| 267 |
frpc_path = os.path.join(frp_install_path, "frpc")
|
| 268 |
+
frps_path = os.path.join(frp_install_path, "frps")
|
| 269 |
|
| 270 |
if os.path.exists(frpc_path):
|
| 271 |
subprocess.run(["chmod", "+x", frpc_path], check=False)
|
| 272 |
|
| 273 |
+
if os.path.exists(frps_path):
|
| 274 |
+
subprocess.run(["chmod", "+x", frps_path], check=False)
|
| 275 |
+
|
| 276 |
+
print("frp 安装成功。")
|
| 277 |
return True
|
| 278 |
|
| 279 |
+
except subprocess.TimeoutExpired:
|
| 280 |
+
print("frp 解压超时,跳过 frp。")
|
| 281 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
+
except subprocess.CalledProcessError as e:
|
| 284 |
+
print(f"frp 解压失败: {e}")
|
| 285 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
|
| 288 |
def main():
|
|
|
|
| 296 |
install_frp()
|
| 297 |
create_frpc_toml()
|
| 298 |
|
| 299 |
+
try:
|
| 300 |
+
frp_thread = threading.Thread(
|
| 301 |
+
target=start_frp,
|
| 302 |
+
daemon=True
|
| 303 |
+
)
|
| 304 |
+
frp_thread.start()
|
| 305 |
+
except Exception as e:
|
| 306 |
+
print(f"frp 线程启动失败: {e}")
|
|
|
|
| 307 |
|
| 308 |
+
try:
|
| 309 |
+
jupyter_thread = threading.Thread(
|
| 310 |
+
target=start_jupyterlab_process,
|
| 311 |
+
daemon=True
|
| 312 |
+
)
|
| 313 |
+
jupyter_thread.start()
|
| 314 |
+
except Exception as e:
|
| 315 |
+
print(f"JupyterLab 线程启动失败: {e}")
|
| 316 |
|
| 317 |
+
print("准备启动 Gradio...")
|
| 318 |
start_gradio()
|
| 319 |
|
| 320 |
|