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

在GCP Cloud Run中使用HuggingFace datasets读取Arrow文件失败求助

解决GCP Cloud Run部署时Hugging Face datasets load_from_disk的ArrowInvalid错误

我在GCP Cloud Run部署容器化Python应用时遇到pyarrow.lib.ArrowInvalid: Expected to read 1936876918 metadata bytes, but only read 127错误,该错误由Hugging Face datasets的load_from_disk()调用pyarrow时触发。本地构建运行容器完全正常,但云端部署失败。

相关代码

加载数据集的核心代码:

from datasets import load_from_disk

class TextEmbedder(ABC):
    def __init__(self, model_name, paragraphs_path, device, load_existing_index=False):
        self.dataset = load_from_disk(paragraphs_path)
    (...)

class SentenceTransformersTextEmbedder(TextEmbedder):
    (...)

text_embedder = SentenceTransformersTextEmbedder(...)

预先生成数据集的代码:

from datasets import Dataset
(...)
paragraphs_dataset = Dataset.from_pandas(paragraphs_dataframe)
paragraphs_dataset.save_to_disk(args.data_folder)

Dockerfile内容:

FROM python:3.10
ARG GRADIO_SERVER_NAME="0.0.0.0"
ENV GRADIO_SERVER_NAME=${GRADIO_SERVER_NAME}
WORKDIR /app
COPY . /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["python", "-u", "./app.py"]

问题原因

  • 数据集文件不完整或损坏:save_to_disk()生成的是包含多个Arrow文件的目录,容器构建时COPY . /app可能因文件系统特性(如符号链接、隐藏文件)导致部分文件未被正确复制,或复制过程中文件损坏。
  • pyarrow版本不兼容:生成数据集时使用的pyarrow版本与容器内安装的版本不一致,不同版本的Arrow元数据格式存在差异,导致读取失败。
  • 路径配置错误:容器内load_from_disk()使用的路径不正确,指向了非数据集目录或残缺的文件。

解决方案

1. 确保数据集目录完整复制

修改Dockerfile,显式复制数据集目录,避免依赖递归复制可能遗漏文件:

# 假设本地数据集目录为./datasets/paragraphs
COPY ./datasets/paragraphs /app/datasets/paragraphs

构建容器时,确认数据集目录下的所有文件(如dataset_info.json、data/子目录等)都被正确复制到容器内。

2. 统一pyarrow版本

在requirements.txt中指定与生成数据集时完全一致的pyarrow版本,示例:

pyarrow==12.0.0
datasets==2.12.0
# 其他依赖...

确保生成数据集和容器内使用的pyarrow、datasets版本完全匹配,避免序列化/反序列化的兼容性问题。

3. 验证容器内的数据集路径

在容器内打印paragraphs_path的绝对路径,确认指向正确的数据集目录:

import os
print("Dataset path:", os.path.abspath(paragraphs_path))

确保路径是容器内的有效路径,比如/app/datasets/paragraphs,而非本地路径或相对路径导致的错误指向。

4. 重新生成数据集(可选)

如果版本不兼容问题无法通过指定版本解决,使用容器内相同的依赖环境重新生成数据集:

  1. 基于当前Dockerfile构建临时容器
  2. 在容器内运行数据集生成代码
  3. 将生成的数据集目录复制回本地,再重新构建部署容器

5. 清理构建缓存

重新构建容器时使用--no-cache参数,避免旧的依赖或文件缓存导致问题:

docker build --no-cache -t your-image-name .

内容的提问来源于stack exchange,提问作者VictorND

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 20:50:14