Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# 处理 API 请求的函数
|
| 6 |
def api_call(input_text):
|
|
@@ -12,8 +14,8 @@ def api_call(input_text):
|
|
| 12 |
if input_text.startswith("cmd_run "):
|
| 13 |
command = input_text[len("cmd_run "):]
|
| 14 |
try:
|
| 15 |
-
# 使用 sudo 执行命令
|
| 16 |
-
result = subprocess.run(
|
| 17 |
return result.stdout or result.stderr
|
| 18 |
except subprocess.CalledProcessError as e:
|
| 19 |
return f"执行命令时出错: {e}"
|
|
@@ -51,19 +53,15 @@ def start_gradio():
|
|
| 51 |
|
| 52 |
demo.launch(share=False)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
def clone_repository():
|
| 56 |
destination_path = "/root/modelscope" # 更改目标路径为 /root/modelscope
|
| 57 |
repo_url = "https://hf-mirror.com/HOPStudio/modelscope"
|
| 58 |
|
| 59 |
# 检查目标路径是否已经存在
|
| 60 |
if os.path.exists(destination_path):
|
| 61 |
-
print(f"目标目录 {destination_path} 已经存在,
|
| 62 |
-
|
| 63 |
-
# 删除已存在的目录
|
| 64 |
-
subprocess.run(f"sudo rm -rf {destination_path}", shell=True, check=True)
|
| 65 |
-
except subprocess.CalledProcessError as e:
|
| 66 |
-
return f"删除目录时出错: {e}"
|
| 67 |
|
| 68 |
# 执行 git clone 命令
|
| 69 |
clone_command = f"git clone {repo_url} {destination_path}"
|
|
@@ -74,11 +72,98 @@ def clone_repository():
|
|
| 74 |
except subprocess.CalledProcessError as e:
|
| 75 |
return f"克隆仓库时出错: {e}"
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# 主函数
|
| 78 |
def main():
|
|
|
|
| 79 |
clone_message = clone_repository()
|
| 80 |
print(clone_message)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
start_gradio()
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
# 处理 API 请求的函数
|
| 8 |
def api_call(input_text):
|
|
|
|
| 14 |
if input_text.startswith("cmd_run "):
|
| 15 |
command = input_text[len("cmd_run "):]
|
| 16 |
try:
|
| 17 |
+
# 不使用 sudo 执行命令
|
| 18 |
+
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 19 |
return result.stdout or result.stderr
|
| 20 |
except subprocess.CalledProcessError as e:
|
| 21 |
return f"执行命令时出错: {e}"
|
|
|
|
| 53 |
|
| 54 |
demo.launch(share=False)
|
| 55 |
|
| 56 |
+
# 从仓库中克隆文件
|
| 57 |
def clone_repository():
|
| 58 |
destination_path = "/root/modelscope" # 更改目标路径为 /root/modelscope
|
| 59 |
repo_url = "https://hf-mirror.com/HOPStudio/modelscope"
|
| 60 |
|
| 61 |
# 检查目标路径是否已经存在
|
| 62 |
if os.path.exists(destination_path):
|
| 63 |
+
print(f"目标目录 {destination_path} 已经存在,跳过克隆操作。")
|
| 64 |
+
return f"仓库已经存在:{destination_path}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# 执行 git clone 命令
|
| 67 |
clone_command = f"git clone {repo_url} {destination_path}"
|
|
|
|
| 72 |
except subprocess.CalledProcessError as e:
|
| 73 |
return f"克隆仓库时出错: {e}"
|
| 74 |
|
| 75 |
+
# 从仓库安装 frp
|
| 76 |
+
def install_frp():
|
| 77 |
+
frp_repo_path = "/root/modelscope" # 仓库路径
|
| 78 |
+
frpc_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64") # 假设 `frpc` 二进制文件存在该路径
|
| 79 |
+
frpc_tar_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64.tar.gz") # `frp` 压缩包路径
|
| 80 |
+
|
| 81 |
+
# 确保 frpc 可执行文件存在
|
| 82 |
+
if not os.path.exists(frp_repo_path):
|
| 83 |
+
return f"仓库目录 {frp_repo_path} 不存在,请检查路径。"
|
| 84 |
+
|
| 85 |
+
# 解压 frp 压缩包
|
| 86 |
+
if os.path.exists(frpc_tar_path) and not os.path.exists(frpc_path):
|
| 87 |
+
try:
|
| 88 |
+
subprocess.run(["tar", "-xvzf", frpc_tar_path, "-C", frp_repo_path], check=True)
|
| 89 |
+
return "frp 安装成功!"
|
| 90 |
+
except subprocess.CalledProcessError as e:
|
| 91 |
+
return f"解压 frp 文件时出错: {e}"
|
| 92 |
+
|
| 93 |
+
# 如果已安装 `frpc`,直接返回
|
| 94 |
+
if os.path.exists(frpc_path):
|
| 95 |
+
return f"frp 已经安装在 {frpc_path}。"
|
| 96 |
+
|
| 97 |
+
return "frp 安装失败,无法找到所需的文件。"
|
| 98 |
+
|
| 99 |
+
# 创建 frpc.toml 配置文件
|
| 100 |
+
def create_frpc_toml():
|
| 101 |
+
frpc_toml_path = "/root/modelscope/frpc.toml" # frpc.toml 路径
|
| 102 |
+
|
| 103 |
+
# frpc 配置内容
|
| 104 |
+
toml_content = """
|
| 105 |
+
[common]
|
| 106 |
+
server_addr = 182.43.67.222
|
| 107 |
+
server_port = 7000
|
| 108 |
+
|
| 109 |
+
[jl]
|
| 110 |
+
type = tcp
|
| 111 |
+
local_ip = 127.0.0.1
|
| 112 |
+
local_port = 8888
|
| 113 |
+
remote_port = 8888
|
| 114 |
+
|
| 115 |
+
[comfyui]
|
| 116 |
+
type = tcp
|
| 117 |
+
local_ip = 127.0.0.1
|
| 118 |
+
local_port = 8188
|
| 119 |
+
remote_port = 8188
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
# 写入配置文件
|
| 123 |
+
try:
|
| 124 |
+
with open(frpc_toml_path, 'w') as file:
|
| 125 |
+
file.write(toml_content)
|
| 126 |
+
print(f"frpc.toml 配置文件已创建:{frpc_toml_path}")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"创建 frpc.toml 配置文件时出错: {e}")
|
| 129 |
+
|
| 130 |
+
# 启动 frp
|
| 131 |
+
def start_frp():
|
| 132 |
+
frpc_path = "/root/modelscope/frp_0.61.0_linux_amd64/frpc" # frpc 二进制文件路径
|
| 133 |
+
frpc_config_path = "/root/modelscope/frpc.toml" # 配置文件路径
|
| 134 |
+
|
| 135 |
+
# 确保 frpc 可执行文件存在
|
| 136 |
+
if not os.path.exists(frpc_path):
|
| 137 |
+
return "frpc 可执行文件不存在,请检查路径。"
|
| 138 |
+
|
| 139 |
+
if not os.path.exists(frpc_config_path):
|
| 140 |
+
return "frpc 配置文件不存在,请检查路径。"
|
| 141 |
+
|
| 142 |
+
# 启动 frp
|
| 143 |
+
try:
|
| 144 |
+
subprocess.run([frpc_path, "-c", frpc_config_path], check=True)
|
| 145 |
+
return "frp 启动成功。"
|
| 146 |
+
except subprocess.CalledProcessError as e:
|
| 147 |
+
return f"启动 frp 时出错: {e}"
|
| 148 |
+
|
| 149 |
# 主函数
|
| 150 |
def main():
|
| 151 |
+
# 克隆仓库
|
| 152 |
clone_message = clone_repository()
|
| 153 |
print(clone_message)
|
| 154 |
|
| 155 |
+
# 安装 frp
|
| 156 |
+
install_message = install_frp()
|
| 157 |
+
print(install_message)
|
| 158 |
+
|
| 159 |
+
# 创建配置文件
|
| 160 |
+
create_frpc_toml()
|
| 161 |
+
|
| 162 |
+
# 启动 frp
|
| 163 |
+
frp_message = start_frp()
|
| 164 |
+
print(frp_message)
|
| 165 |
+
|
| 166 |
+
# 启动 Gradio 界面
|
| 167 |
start_gradio()
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|