使用 BigQuery DataFrame 在 Python 中分析多模态数据

本教程将向您展示如何使用 BigQuery DataFrames 类和方法在 Python 笔记本中分析多模态数据

本教程使用的是公开的 Cymbal 宠物商店数据集中的产品目录。

如需上传已填充本教程中介绍的任务的笔记本,请参阅 BigFrames 多模态 DataFrame

目标

  • 创建多模态 DataFrame。
  • 在 DataFrame 中组合结构化数据和非结构化数据。
  • 转换图片。
  • 根据图片数据生成文本和嵌入。
  • 将 PDF 分块以进行进一步分析。

费用

在本文档中,您将使用 Google Cloud的以下收费组件:

  • BigQuery: you incur costs for the data that you process in BigQuery.
  • BigQuery Python UDFs: you incur costs for using BigQuery DataFrames image transformation and chunk PDF methods.
  • Cloud Storage: you incur costs for the objects stored in Cloud Storage.
  • Vertex AI: you incur costs for calls to Vertex AI models.

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

如需了解详情,请参阅以下价格页面:

准备工作

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Google Cloud project.

  3. Enable the BigQuery, BigQuery Connection, Cloud Storage, and Vertex AI APIs.

    Enable the APIs

所需的角色

如需获得完成本教程所需的权限,请让您的管理员为您授予以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

设置

在本部分中,您将创建本教程中使用的 Cloud Storage 存储桶、连接和记事本。

创建存储桶

创建一个 Cloud Storage 存储桶来存储转换后的对象:

  1. 在 Google Cloud 控制台中,前往存储桶页面。

    进入“存储桶”

  2. 点击 创建

  3. 创建存储桶页面的开始使用部分中,输入符合存储桶名称要求的全局唯一名称。

  4. 点击创建

创建连接

创建 Cloud 资源连接并获取连接的服务账号。 BigQuery 使用该连接来访问 Cloud Storage 中的对象。

  1. 转到 BigQuery 页面。

    转到 BigQuery

  2. 浏览器窗格中,点击 添加数据

    系统随即会打开添加数据对话框。

  3. 过滤条件窗格中的数据源类型部分,选择商务应用

    或者,在搜索数据源字段中,输入 Vertex AI

  4. 精选数据源部分中,点击 Vertex AI

  5. 点击 Vertex AI 模型:BigQuery 联合解决方案卡片。

  6. 连接类型列表中,选择 Vertex AI 远程模型、远程函数和 BigLake(Cloud 资源)

  7. 连接 ID 字段中,输入 bigframes-default-connection

  8. 点击创建连接

  9. 点击转到连接

  10. 连接信息窗格中,复制服务账号 ID 以在后续步骤中使用。

向连接的服务账号授予权限

向关联的服务账号授予其访问 Cloud Storage 和 Vertex AI 所需的角色。您必须在您在准备工作部分创建或选择的项目中授予这些角色。

如需授予该角色,请按以下步骤操作:

  1. 前往 IAM 和管理页面。

    转到“IAM 和管理”

  2. 点击 授予访问权限

  3. 新的主账号字段中,输入您之前复制的服务账号 ID。

  4. 选择角色字段中,选择 Cloud Storage,然后选择 Storage Object User

  5. 点击添加其他角色

  6. 选择角色字段中,选择 Vertex AI,然后选择 Vertex AI User

  7. 点击保存

创建笔记本

创建一个可运行 Python 代码的笔记本:

  1. 转到 BigQuery 页面。

    转到 BigQuery

  2. 在编辑器窗格的标签页栏中,点击 SQL 查询旁边的 下拉箭头,然后点击记事本

  3. 从模板开始窗格中,点击关闭

  4. 依次点击连接 > 连接到运行时

  5. 如果您已有现有运行时,请接受默认设置,然后点击连接。如果您没有现有运行时,请选择创建新运行时,然后点击连接

    运行时可能需要几分钟才能设置完毕。

创建多模态 DataFrame

使用 Session 类的 from_glob_path 方法创建一个多模态 DataFrame,以集成结构化数据和非结构化数据:

  1. 在笔记本中,创建一个代码单元,并将以下代码复制到其中:
    import bigframes
    
    # Flags to control preview image/video preview size
    bigframes.options.display.blob_display_width = 300
    
    import bigframes.pandas as bpd
    
    # Create blob columns from wildcard path.
    df_image = bpd.from_glob_path(
        "gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/images/*", name="image"
    )
    # Other ways are: from string uri column
    # df = bpd.DataFrame({"uri": ["gs://<my_bucket>/<my_file_0>", "gs://<my_bucket>/<my_file_1>"]})
    # df["blob_col"] = df["uri"].str.to_blob()
    
    # From an existing object table
    # df = bpd.read_gbq_object_table("<my_object_table>", name="blob_col")
    
    # Take only the 5 images to deal with. Preview the content of the Mutimodal DataFrame
    df_image = df_image.head(5)
    df_image
  2. 点击 运行

    df_image 的最后一次调用会返回已添加到 DataFrame 中的图片。或者,您也可以调用 .display 方法。

在 DataFrame 中组合结构化数据和非结构化数据

在多模态 DataFrame 中组合文本和图片数据:

  1. 在笔记本中,创建一个代码单元,并将以下代码复制到其中:
    # Combine unstructured data with structured data
    df_image["author"] = ["alice", "bob", "bob", "alice", "bob"]  # type: ignore
    df_image["content_type"] = df_image["image"].blob.content_type()
    df_image["size"] = df_image["image"].blob.size()
    df_image["updated"] = df_image["image"].blob.updated()
    df_image
  2. 点击运行

    该代码会返回 DataFrame 数据。

  3. 在笔记本中,创建一个代码单元,然后将以下代码复制到其中:

    # Filter images and display, you can also display audio and video types. Use width/height parameters to constrain window sizes.
    df_image[df_image["author"] == "alice"]["image"].blob.display()
  4. 点击运行

    该代码会从 author 列值为 alice 的 DataFrame 中返回图片。

执行图片转换

使用 Series.BlobAccessor的以下方法转换图片数据:

转换后的图片会写入 Cloud Storage。

转换图片:

  1. 在笔记本中,创建一个代码单元,并将以下代码复制到其中:
    df_image["blurred"] = df_image["image"].blob.image_blur(
        (20, 20), dst=f"{dst_bucket}/image_blur_transformed/"
    )
    df_image["resized"] = df_image["image"].blob.image_resize(
        (300, 200), dst=f"{dst_bucket}/image_resize_transformed/"
    )
    df_image["normalized"] = df_image["image"].blob.image_normalize(
        alpha=50.0,
        beta=150.0,
        norm_type="minmax",
        dst=f"{dst_bucket}/image_normalize_transformed/",
    )
    
    # You can also chain functions together
    df_image["blur_resized"] = df_image["blurred"].blob.image_resize(
        (300, 200), dst=f"{dst_bucket}/image_blur_resize_transformed/"
    )
    df_image
  2. 将对 {dst_bucket} 的所有引用更新为引用您创建的存储桶,格式为 gs://mybucket
  3. 点击运行

    该代码会返回原始图片及其所有转换。

生成文本

使用 GeminiTextGenerator 类的 predict 方法根据多模态数据生成文本:

  1. 在笔记本中,创建一个代码单元,并将以下代码复制到其中:
    from bigframes.ml import llm
    
    gemini = llm.GeminiTextGenerator(model_name="gemini-2.0-flash-001")
    
    # Deal with first 2 images as example
    df_image = df_image.head(2)
    
    # Ask the same question on the images
    df_image = df_image.head(2)
    answer = gemini.predict(df_image, prompt=["what item is it?", df_image["image"]])
    answer[["ml_generate_text_llm_result", "image"]]
  2. 点击运行

    该代码会返回 df_image 中的前两张图片,以及针对这两张图片生成的回答问题 what item is it? 的文本。

  3. 在笔记本中,创建一个代码单元,然后将以下代码复制到其中:

    # Ask different questions
    df_image["question"] = [  # type: ignore
        "what item is it?",
        "what color is the picture?",
    ]
    answer_alt = gemini.predict(
        df_image, prompt=[df_image["question"], df_image["image"]]
    )
    answer_alt[["ml_generate_text_llm_result", "image"]]
  4. 点击运行

    该代码会在 df_image 中返回前两个图片,其中第一个图片的文字是针对问题 what item is it? 生成的,第二个图片的文字是针对问题 what color is the picture? 生成的。

生成嵌入

使用 MultimodalEmbeddingGeneratorpredict 方法为多模态数据生成嵌入:

  1. 在笔记本中,创建一个代码单元,并将以下代码复制到其中:
    # Generate embeddings on images
    embed_model = llm.MultimodalEmbeddingGenerator()
    embeddings = embed_model.predict(df_image["image"])
    embeddings
  2. 点击运行

    该代码会返回调用嵌入模型生成的嵌入。

分块处理 PDF

使用 Series.BlobAccessorpdf_chunk 方法对 PDF 对象进行分块:

  1. 在笔记本中,创建一个代码单元,并将以下代码复制到其中:
    # PDF chunking
    df_pdf = bpd.from_glob_path(
        "gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/documents/*", name="pdf"
    )
    df_pdf["chunked"] = df_pdf["pdf"].blob.pdf_chunk()
    chunked = df_pdf["chunked"].explode()
    chunked
  2. 点击运行

    该代码会返回分块的 PDF 数据。

清理

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.