ValueError: 该模型不支持pipeline中的`text-generation`任务。支持的任务有:['feature-extraction', 'fill-mask', 'sentence-similarity', 'text-classification']。
ValueError: The task `text-generation` is not supported by the pipeline for this model. Supported tasks are: ['feature-extraction', 'fill-mask', 'sentence-similarity', 'text-classification'].
ID: huggingface/pipeline-task-unavailable
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| transformers 4.36.0 | active | — | — | — |
| huggingface-hub 0.20.0 | active | — | — | — |
| Python 3.11 | active | — | — | — |
根因分析
pipeline配置的任务是模型架构无法执行的,通常因为模型是仅编码器类型(如BERT),但任务是仅解码器类型(如文本生成)。
English
The pipeline is configured with a task that the model's architecture cannot perform, often because the model is an encoder-only type (e.g., BERT) but the task is decoder-only (e.g., text generation).
官方文档
https://huggingface.co/docs/transformers/main_classes/pipelines解决方案
-
使用支持所需任务的模型。对于文本生成,使用仅解码器模型如GPT-2:`pipeline('text-generation', model='gpt2')`。在Hugging Face Hub上查看模型卡片以了解支持的任务。 -
如果必须使用当前模型,选择支持的任务。例如,对于BERT:`pipeline('text-classification', model='bert-base-uncased')`。
无效尝试
常见但无效的做法:
-
95% 失败
BERT is an encoder-only model; it lacks a language modeling head for generation, so forcing the task will cause runtime errors or produce gibberish.
-
70% 失败
The error is about model-task compatibility, not library version; a newer version won't make BERT generate text.