Skip to content

遠端資料來源

從 URL、雲端儲存、資料庫等載入標註資料。

Potato 支援從本地檔案以外的各種遠端來源載入標註資料,包括 URL、雲端儲存服務、資料庫和 Hugging Face 資料集。

概述

資料來源系統提供:

  • 多種來源類型:URL、Google Drive、Dropbox、S3、Hugging Face、Google Sheets、SQL 資料庫
  • 部分載入:大數據集分塊載入
  • 增量載入:隨標註進度自動載入更多資料
  • 快取:本地快取遠端檔案以避免重複下載
  • 安全憑證:環境變數替換敏感資訊

配置

在 config.yaml 中新增 data_sources

yaml
data_sources:
  - type: file
    path: "data/annotations.jsonl"
 
  - type: url
    url: "https://example.com/data.jsonl"

來源類型

本地檔案

yaml
data_sources:
  - type: file
    path: "data/annotations.jsonl"

HTTP/HTTPS URL

yaml
data_sources:
  - type: url
    url: "https://example.com/data.jsonl"
    headers:
      Authorization: "Bearer ${API_TOKEN}"
    max_size_mb: 100
    timeout_seconds: 30
    block_private_ips: true    # SSRF protection

Amazon S3

yaml
data_sources:
  - type: s3
    bucket: "my-annotation-data"
    key: "datasets/items.jsonl"
    region: "us-east-1"
    access_key_id: "${AWS_ACCESS_KEY_ID}"
    secret_access_key: "${AWS_SECRET_ACCESS_KEY}"

依賴:pip install boto3

Google Drive

yaml
data_sources:
  - type: google_drive
    url: "https://drive.google.com/file/d/xxx/view?usp=sharing"

對於私有檔案,使用 credentials_file 和服務賬戶。依賴:pip install google-api-python-client google-auth

Dropbox

yaml
data_sources:
  - type: dropbox
    url: "https://www.dropbox.com/s/xxx/file.jsonl?dl=0"

對於私有檔案,使用 access_token: "${DROPBOX_TOKEN}"。依賴:pip install dropbox

Hugging Face Datasets

yaml
data_sources:
  - type: huggingface
    dataset: "squad"
    split: "train"
    token: "${HF_TOKEN}"        # For private datasets
    id_field: "id"
    text_field: "context"

依賴:pip install datasets

Google Sheets

yaml
data_sources:
  - type: google_sheets
    spreadsheet_id: "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
    sheet_name: "Sheet1"
    credentials_file: "credentials/service_account.json"

依賴:pip install google-api-python-client google-auth

SQL 資料庫

yaml
data_sources:
  - type: database
    connection_string: "${DATABASE_URL}"
    query: "SELECT id, text, metadata FROM items WHERE status = 'pending'"

或使用單獨參數:

yaml
data_sources:
  - type: database
    dialect: postgresql
    host: "localhost"
    port: 5432
    database: "annotations"
    username: "${DB_USER}"
    password: "${DB_PASSWORD}"
    table: "items"

依賴:pip install sqlalchemy psycopg2-binary(PostgreSQL)或 pymysql(MySQL)

部分/增量載入

對於大數據集,啟用部分載入:

yaml
partial_loading:
  enabled: true
  initial_count: 1000
  batch_size: 500
  auto_load_threshold: 0.8     # Auto-load when 80% annotated

快取

遠端來源在本地快取:

yaml
data_cache:
  enabled: true
  cache_dir: ".potato_cache"
  ttl_seconds: 3600            # 1 hour
  max_size_mb: 500

憑證管理

對敏感值使用環境變數:

yaml
data_sources:
  - type: url
    url: "https://api.example.com/data"
    headers:
      Authorization: "Bearer ${API_TOKEN}"
 
credentials:
  env_substitution: true
  env_file: ".env"

多資料來源

組合多個來源的資料:

yaml
data_sources:
  - type: file
    path: "data/base.jsonl"
 
  - type: url
    url: "https://example.com/extra.jsonl"
 
  - type: s3
    bucket: "my-bucket"
    key: "annotations/batch1.jsonl"

向後相容

data_files 配置繼續與 data_sources 一起工作:

yaml
data_files:
  - "data/existing.jsonl"
 
data_sources:
  - type: url
    url: "https://example.com/additional.jsonl"

安全性

  • URL 來源預設阻止私有/內部 IP 地址(SSRF 防護)
  • 永遠不要將憑證提交到版本控制
  • 使用 ${VAR_NAME} 語法處理敏感資訊
  • .env 檔案儲存在倉庫之外

延伸閱讀

有關實現細節,請參閱原始碼文件