feat: 添加展示HuggingFace支持任务的脚本和测试笔记本
Browse files- hgface.py +8 -0
- test.ipynb +186 -0
hgface.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.pipelines import SUPPORTED_TASKS
|
| 2 |
+
|
| 3 |
+
print(f"{'任务名称':<30} | {'模型类型':<15} | {'Pipeline实现类'}")
|
| 4 |
+
print("-" * 70)
|
| 5 |
+
for task_name, task_info in SUPPORTED_TASKS.items():
|
| 6 |
+
task_type = task_info['type']
|
| 7 |
+
impl_class = task_info['impl'].__name__
|
| 8 |
+
print(f"{task_name:<30} | {task_type:<15} | {impl_class}")
|
test.ipynb
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"id": "81db1252",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"5.5.0\n",
|
| 14 |
+
"1.9.0\n"
|
| 15 |
+
]
|
| 16 |
+
}
|
| 17 |
+
],
|
| 18 |
+
"source": [
|
| 19 |
+
"import transformers\n",
|
| 20 |
+
"import huggingface_hub\n",
|
| 21 |
+
"\n",
|
| 22 |
+
"print(transformers.__version__)\n",
|
| 23 |
+
"print(huggingface_hub.__version__)"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"cell_type": "code",
|
| 28 |
+
"execution_count": 9,
|
| 29 |
+
"id": "26843392",
|
| 30 |
+
"metadata": {},
|
| 31 |
+
"outputs": [
|
| 32 |
+
{
|
| 33 |
+
"name": "stderr",
|
| 34 |
+
"output_type": "stream",
|
| 35 |
+
"text": [
|
| 36 |
+
"Loading weights: 100%|██████████| 104/104 [00:00<00:00, 6078.62it/s]\n"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"name": "stdout",
|
| 41 |
+
"output_type": "stream",
|
| 42 |
+
"text": [
|
| 43 |
+
"[{'label': 'POSITIVE', 'score': 0.9998855590820312}]\n"
|
| 44 |
+
]
|
| 45 |
+
}
|
| 46 |
+
],
|
| 47 |
+
"source": [
|
| 48 |
+
"# 从transformers库导入pipeline模块\n",
|
| 49 |
+
"from transformers import pipeline\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"# 修改镜像站\n",
|
| 52 |
+
"import os\n",
|
| 53 |
+
"os.environ[\"HF_ENDPOINT\"] = \"https://hf-mirror.com\"\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"\n",
|
| 56 |
+
"# 创建情感分析分类器\n",
|
| 57 |
+
"classifier = pipeline(\"text-classification\", model=\"distilbert/distilbert-base-uncased-finetuned-sst-2-english\")\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"# 对文本进行情感分析\n",
|
| 60 |
+
"result = classifier(\"I love this product!\")\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"# 打印分析结果\n",
|
| 63 |
+
"print(result)"
|
| 64 |
+
]
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"cell_type": "code",
|
| 68 |
+
"execution_count": 5,
|
| 69 |
+
"id": "abdfb345",
|
| 70 |
+
"metadata": {},
|
| 71 |
+
"outputs": [
|
| 72 |
+
{
|
| 73 |
+
"name": "stdout",
|
| 74 |
+
"output_type": "stream",
|
| 75 |
+
"text": [
|
| 76 |
+
"所有支持的任务列表(共 24 个):\n",
|
| 77 |
+
"- audio-classification\n",
|
| 78 |
+
"- automatic-speech-recognition\n",
|
| 79 |
+
"- text-to-audio\n",
|
| 80 |
+
"- feature-extraction\n",
|
| 81 |
+
"- text-classification\n",
|
| 82 |
+
"- token-classification\n",
|
| 83 |
+
"- table-question-answering\n",
|
| 84 |
+
"- document-question-answering\n",
|
| 85 |
+
"- fill-mask\n",
|
| 86 |
+
"- text-generation\n",
|
| 87 |
+
"- zero-shot-classification\n",
|
| 88 |
+
"- zero-shot-image-classification\n",
|
| 89 |
+
"- zero-shot-audio-classification\n",
|
| 90 |
+
"- image-classification\n",
|
| 91 |
+
"- image-feature-extraction\n",
|
| 92 |
+
"- image-segmentation\n",
|
| 93 |
+
"- image-text-to-text\n",
|
| 94 |
+
"- object-detection\n",
|
| 95 |
+
"- zero-shot-object-detection\n",
|
| 96 |
+
"- depth-estimation\n",
|
| 97 |
+
"- video-classification\n",
|
| 98 |
+
"- mask-generation\n",
|
| 99 |
+
"- keypoint-matching\n",
|
| 100 |
+
"- any-to-any\n"
|
| 101 |
+
]
|
| 102 |
+
}
|
| 103 |
+
],
|
| 104 |
+
"source": [
|
| 105 |
+
"from transformers.pipelines import SUPPORTED_TASKS\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"# 打印所有任务的名称,统计总个数\n",
|
| 108 |
+
"total_tasks = len(SUPPORTED_TASKS)\n",
|
| 109 |
+
"print(f\"所有支持的任务列表(共 {total_tasks} 个):\")\n",
|
| 110 |
+
"for task_name in SUPPORTED_TASKS.keys():\n",
|
| 111 |
+
" print(f\"- {task_name}\")"
|
| 112 |
+
]
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"cell_type": "code",
|
| 116 |
+
"execution_count": 4,
|
| 117 |
+
"id": "57f0cee3",
|
| 118 |
+
"metadata": {},
|
| 119 |
+
"outputs": [
|
| 120 |
+
{
|
| 121 |
+
"name": "stdout",
|
| 122 |
+
"output_type": "stream",
|
| 123 |
+
"text": [
|
| 124 |
+
"任务名称 | 模型类型 | Pipeline实现类\n",
|
| 125 |
+
"----------------------------------------------------------------------\n",
|
| 126 |
+
"audio-classification | audio | AudioClassificationPipeline\n",
|
| 127 |
+
"automatic-speech-recognition | multimodal | AutomaticSpeechRecognitionPipeline\n",
|
| 128 |
+
"text-to-audio | text | TextToAudioPipeline\n",
|
| 129 |
+
"feature-extraction | text | FeatureExtractionPipeline\n",
|
| 130 |
+
"text-classification | text | TextClassificationPipeline\n",
|
| 131 |
+
"token-classification | text | TokenClassificationPipeline\n",
|
| 132 |
+
"table-question-answering | text | TableQuestionAnsweringPipeline\n",
|
| 133 |
+
"document-question-answering | multimodal | DocumentQuestionAnsweringPipeline\n",
|
| 134 |
+
"fill-mask | text | FillMaskPipeline\n",
|
| 135 |
+
"text-generation | text | TextGenerationPipeline\n",
|
| 136 |
+
"zero-shot-classification | text | ZeroShotClassificationPipeline\n",
|
| 137 |
+
"zero-shot-image-classification | multimodal | ZeroShotImageClassificationPipeline\n",
|
| 138 |
+
"zero-shot-audio-classification | multimodal | ZeroShotAudioClassificationPipeline\n",
|
| 139 |
+
"image-classification | image | ImageClassificationPipeline\n",
|
| 140 |
+
"image-feature-extraction | image | ImageFeatureExtractionPipeline\n",
|
| 141 |
+
"image-segmentation | multimodal | ImageSegmentationPipeline\n",
|
| 142 |
+
"image-text-to-text | multimodal | ImageTextToTextPipeline\n",
|
| 143 |
+
"object-detection | multimodal | ObjectDetectionPipeline\n",
|
| 144 |
+
"zero-shot-object-detection | multimodal | ZeroShotObjectDetectionPipeline\n",
|
| 145 |
+
"depth-estimation | image | DepthEstimationPipeline\n",
|
| 146 |
+
"video-classification | video | VideoClassificationPipeline\n",
|
| 147 |
+
"mask-generation | multimodal | MaskGenerationPipeline\n",
|
| 148 |
+
"keypoint-matching | image | KeypointMatchingPipeline\n",
|
| 149 |
+
"any-to-any | multimodal | AnyToAnyPipeline\n"
|
| 150 |
+
]
|
| 151 |
+
}
|
| 152 |
+
],
|
| 153 |
+
"source": [
|
| 154 |
+
"from transformers.pipelines import SUPPORTED_TASKS\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"print(f\"{'任务名称':<30} | {'模型类型':<15} | {'Pipeline实现类'}\")\n",
|
| 157 |
+
"print(\"-\" * 70)\n",
|
| 158 |
+
"for task_name, task_info in SUPPORTED_TASKS.items():\n",
|
| 159 |
+
" task_type = task_info['type']\n",
|
| 160 |
+
" impl_class = task_info['impl'].__name__\n",
|
| 161 |
+
" print(f\"{task_name:<30} | {task_type:<15} | {impl_class}\")"
|
| 162 |
+
]
|
| 163 |
+
}
|
| 164 |
+
],
|
| 165 |
+
"metadata": {
|
| 166 |
+
"kernelspec": {
|
| 167 |
+
"display_name": "hgface",
|
| 168 |
+
"language": "python",
|
| 169 |
+
"name": "python3"
|
| 170 |
+
},
|
| 171 |
+
"language_info": {
|
| 172 |
+
"codemirror_mode": {
|
| 173 |
+
"name": "ipython",
|
| 174 |
+
"version": 3
|
| 175 |
+
},
|
| 176 |
+
"file_extension": ".py",
|
| 177 |
+
"mimetype": "text/x-python",
|
| 178 |
+
"name": "python",
|
| 179 |
+
"nbconvert_exporter": "python",
|
| 180 |
+
"pygments_lexer": "ipython3",
|
| 181 |
+
"version": "3.12.13"
|
| 182 |
+
}
|
| 183 |
+
},
|
| 184 |
+
"nbformat": 4,
|
| 185 |
+
"nbformat_minor": 5
|
| 186 |
+
}
|