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

R调用Hugging Face Transformers多字符串输入的等效语法求解

解决R中reticulate调用Hugging Face零样本图像分类模型的多输入问题

核心问题是你误用了参数名:Hugging Face的zero-shot-image-classification pipeline接收多标签输入的参数为candidate_labels,而非text。之前的报错是因为模型无法获取有效候选标签,导致内部处理时触发NoneType相关错误。

以下是正确的R代码实现:

library(reticulate)
transformers <- reticulate::import("transformers")

# 初始化零样本图像分类pipeline
image_classification_zero_shot <- transformers$pipeline(
  task = "zero-shot-image-classification", 
  model = "openai/clip-vit-base-patch32"
)

image_url <- "http://images.cocodataset.org/val2017/000000039769.jpg"

# 正确传入多标签:使用candidate_labels参数,R向量或列表均可
result <- image_classification_zero_shot(
  images = image_url,
  candidate_labels = c("a photo of a cat", "a photo of a dog")
)

# 输出结果
print(result)

关键说明

  • 在R中,reticulate会自动将R字符向量(c()创建)或列表(list()创建)转换为Python列表,无需额外手动转换。
  • 必须使用candidate_labels参数传递分类候选标签,这是zero-shot-image-classification pipeline的标准参数要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 23:31:03