在Notebook里怎么把训练好的模型通过ssh放到一台机器上

在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 云盘' 并完成了授权。")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值