You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将具有层级结构的项目上传至Google Colab?

将层级结构项目上传至Google Colab的方法

项目背景

项目结构

项目结构示意图

Google Colab环境

Colab环境示意图

项目代码示例

项目各目录包含Python源码文件,示例代码如下:

import numpy as np
import torch
import viet_text_tools as vtts
from preprocessing import *

class Dataset(torch.utils.data.Dataset):
    def __init__(self, df, tag_names, tokenizer, ):
        self.df = df
        self.df["sent"] = self.df["sent"].apply(vtts.normalize_diacritics)
        self.tag_names = tag_names
        self.sents, self.annos = [words.split() for words in list(df["sent"].values)], [tags.split() for tags in
                                                                                        list(df["anno"].values)]
        self.tokenizer = tokenizer
        self.max_length = 64
        self.criterion_ignored_la = -100
        self.specials = {
            "pad_token_id": self.tokenizer.pad_token_id,
            "cls_token_id": self.tokenizer.cls_token_id,
            "sep_token_id": self.tokenizer.sep_token_id,
            "pad_token_la": self.criterion_ignored_la,
            "cls_token_la": self.criterion_ignored_la,
            "sep_token_la": self.criterion_ignored_la,
        }

    def show_sent(self, idx):
        sent, anno = self.sents[idx], self.annos[idx]
        print("{:>40}: {}".format("word", "tag"))
        for i in range(len(sent)):
            print("{:>40}: {}".format(sent[i], anno[i]))

    def __len__(self):
        dataset_len = len(self.df)
        return dataset_len

    def __getitem__(self, idx):
        sent, anno = self.sents[idx], self.annos[idx]
        sent, anno = pad_and_add_special_tokens(self, *tokenize(self, sent, anno).values()).values()
        sent, anno = np.array(sent), np.array(anno)
        return sent, anno

问题

如何将这类具有层级结构的项目上传至Google Colab?

实用上传方法

方法1:上传压缩包后解压

  • 把本地项目文件夹打包成ZIP格式
  • 在Colab左侧「文件」面板点击「上传到会话存储」,选择ZIP包完成上传
  • 运行命令解压,保留原层级结构:
!unzip 你的项目压缩包名.zip -d /content/

替换命令中的压缩包名为实际文件名,解压后项目会出现在/content/目录下。

方法2:挂载Google Drive同步

  • 运行以下代码挂载Google Drive,按提示完成授权:
from google.colab import drive
drive.mount('/content/drive')
  • 将本地项目文件夹上传到Google Drive的任意路径(比如MyDrive/Projects/我的项目/)
  • 在Colab中通过完整路径访问项目,若要导入项目模块,可先添加路径:
import sys
sys.path.append('/content/drive/MyDrive/Projects/我的项目/')
from preprocessing import *

方法3:克隆Git仓库

如果项目托管在Git仓库,直接运行克隆命令:

!git clone https://你的仓库地址.git

克隆完成后,项目的层级结构会完整保留在当前目录,直接即可使用。

方法4:直接上传文件夹

  • 在Colab左侧「文件」面板右键空白处,选择「上传文件夹」
  • 选中本地项目文件夹等待上传完成,此方式直接保留原结构,但大文件夹上传速度较慢,适合小型项目。

内容的提问来源于stack exchange,提问作者Đỗ Như Vỹ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 12:40:35