在Notebook里怎么把训练好的模型通过ssh放到一台机器上?使用sshpass,先安装这个包,然后上传即可
# 安装 sshpass 以便在脚本中处理密码
!apt-get update -qq && apt-get install -y -qq sshpass
import os
# 设置服务器信息
host = "colab.xxxx.com"
user = "colab"
password = "colab"
local_file = "/content/model_gguf_gguf/qwen2.5-0.5b-instruct.Q8_0.gguf"
remote_path = "~/" # 你可以修改为服务器上的目标路径
if os.path.exists(local_file):
print(f"正在将文件传输至 {host}...")
# 使用 sshpass 和 scp 进行传输 (添加 -o StrictHostKeyChecking=no 避免交互式确认)
!sshpass -p "{password}" scp -o StrictHostKeyChecking=no "{local_file}" {user}@{host}:{remote_path}
print("传输尝试完成!请检查你的服务器。")
else:
print("未找到本地模型文件,请检查路径。")
将模型保存到云盘
from google.colab import drive
import shutil
import os
# 1. 挂载云盘 - 加入 force_remount=True 以解决挂载失败问题
try:
drive.mount('/content/drive', force_remount=True)
# 2. 定义源文件和目标路径
source_file = "/content/model_gguf_gguf/qwen2.5-0.5b-instruct.Q8_0.gguf"
destination_dir = "/content/drive/MyDrive/Colab_Models/"
# 创建目标文件夹(如果不存在)
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
if os.path.exists(source_file):
print(f"正在复制模型到云盘: {destination_dir}...")
shutil.copy(source_file, destination_dir)
print("复制成功!你现在可以在你的 Google Drive 中找到该模型了。")
else:
print("未找到本地模型文件,请检查路径。")
except Exception as e:
print(f"挂载或复制过程中出错: {e}")
print("请确保在弹出的对话框中点击了 '连接到 Google 云盘' 并完成了授权。")

371

被折叠的 条评论
为什么被折叠?



