File size: 7,869 Bytes
3b297bb
 
 
 
9130197
3b297bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9130197
3b297bb
 
 
 
9130197
 
 
 
 
 
 
 
 
 
 
 
3b297bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9130197
3b297bb
 
 
 
 
 
 
 
 
 
9130197
3b297bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9130197
3b297bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9130197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b297bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9130197
3b297bb
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "81db1252",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.5.0\n",
      "1.9.0\n"
     ]
    }
   ],
   "source": [
    "import transformers\n",
    "import huggingface_hub\n",
    "\n",
    "print(transformers.__version__)\n",
    "print(huggingface_hub.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "26843392",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3950ca2f944412ba421836867810a02",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading weights:   0%|          | 0/104 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'label': 'POSITIVE', 'score': 0.9998855590820312}]\n"
     ]
    }
   ],
   "source": [
    "# 从transformers库导入pipeline模块\n",
    "from transformers import pipeline\n",
    "\n",
    "# 修改镜像站\n",
    "import os\n",
    "os.environ[\"HF_ENDPOINT\"] = \"https://hf-mirror.com\"\n",
    "\n",
    "\n",
    "# 创建情感分析分类器\n",
    "classifier = pipeline(model=\"distilbert/distilbert-base-uncased-finetuned-sst-2-english\")\n",
    "\n",
    "# 对文本进行情感分析\n",
    "result = classifier(\"I love this product!\")\n",
    "\n",
    "# 打印分析结果\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "abdfb345",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有支持的任务列表(共 24 个):\n",
      "- audio-classification\n",
      "- automatic-speech-recognition\n",
      "- text-to-audio\n",
      "- feature-extraction\n",
      "- text-classification\n",
      "- token-classification\n",
      "- table-question-answering\n",
      "- document-question-answering\n",
      "- fill-mask\n",
      "- text-generation\n",
      "- zero-shot-classification\n",
      "- zero-shot-image-classification\n",
      "- zero-shot-audio-classification\n",
      "- image-classification\n",
      "- image-feature-extraction\n",
      "- image-segmentation\n",
      "- image-text-to-text\n",
      "- object-detection\n",
      "- zero-shot-object-detection\n",
      "- depth-estimation\n",
      "- video-classification\n",
      "- mask-generation\n",
      "- keypoint-matching\n",
      "- any-to-any\n"
     ]
    }
   ],
   "source": [
    "from transformers.pipelines import SUPPORTED_TASKS\n",
    "\n",
    "# 打印所有任务的名称,统计总个数\n",
    "total_tasks = len(SUPPORTED_TASKS)\n",
    "print(f\"所有支持的任务列表(共 {total_tasks} 个):\")\n",
    "for task_name in SUPPORTED_TASKS.keys():\n",
    "    print(f\"- {task_name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "57f0cee3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务名称                           | 模型类型            | Pipeline实现类\n",
      "----------------------------------------------------------------------\n",
      "audio-classification           | audio           | AudioClassificationPipeline\n",
      "automatic-speech-recognition   | multimodal      | AutomaticSpeechRecognitionPipeline\n",
      "text-to-audio                  | text            | TextToAudioPipeline\n",
      "feature-extraction             | text            | FeatureExtractionPipeline\n",
      "text-classification            | text            | TextClassificationPipeline\n",
      "token-classification           | text            | TokenClassificationPipeline\n",
      "table-question-answering       | text            | TableQuestionAnsweringPipeline\n",
      "document-question-answering    | multimodal      | DocumentQuestionAnsweringPipeline\n",
      "fill-mask                      | text            | FillMaskPipeline\n",
      "text-generation                | text            | TextGenerationPipeline\n",
      "zero-shot-classification       | text            | ZeroShotClassificationPipeline\n",
      "zero-shot-image-classification | multimodal      | ZeroShotImageClassificationPipeline\n",
      "zero-shot-audio-classification | multimodal      | ZeroShotAudioClassificationPipeline\n",
      "image-classification           | image           | ImageClassificationPipeline\n",
      "image-feature-extraction       | image           | ImageFeatureExtractionPipeline\n",
      "image-segmentation             | multimodal      | ImageSegmentationPipeline\n",
      "image-text-to-text             | multimodal      | ImageTextToTextPipeline\n",
      "object-detection               | multimodal      | ObjectDetectionPipeline\n",
      "zero-shot-object-detection     | multimodal      | ZeroShotObjectDetectionPipeline\n",
      "depth-estimation               | image           | DepthEstimationPipeline\n",
      "video-classification           | video           | VideoClassificationPipeline\n",
      "mask-generation                | multimodal      | MaskGenerationPipeline\n",
      "keypoint-matching              | image           | KeypointMatchingPipeline\n",
      "any-to-any                     | multimodal      | AnyToAnyPipeline\n"
     ]
    }
   ],
   "source": [
    "from transformers.pipelines import SUPPORTED_TASKS\n",
    "\n",
    "print(f\"{'任务名称':<30} | {'模型类型':<15} | {'Pipeline实现类'}\")\n",
    "print(\"-\" * 70)\n",
    "for task_name, task_info in SUPPORTED_TASKS.items():\n",
    "    task_type = task_info['type']\n",
    "    impl_class = task_info['impl'].__name__\n",
    "    print(f\"{task_name:<30} | {task_type:<15} | {impl_class}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b5ffa08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id                   | distilbert/distilbert-base-uncased-finetuned-sst-2-english\n",
      "pipeline_tag         | text-classification\n",
      "tags                 | ['transformers', 'pytorch', 'tf', 'rust', 'onnx', 'safetensors', 'distilbert', 'text-classification', 'en', 'dataset:sst2', 'dataset:glue', 'arxiv:1910.01108', 'doi:10.57967/hf/0181', 'license:apache-2.0', 'model-index', 'endpoints_compatible', 'deploy:azure', 'region:us']\n",
      "library_name         | transformers\n",
      "model_type           | N/A\n",
      "downloads            | 2966288\n",
      "likes                | 887\n",
      "created_at           | 2022-03-02 23:29:04+00:00\n",
      "last_modified        | 2023-12-19 16:29:37+00:00\n"
     ]
    }
   ],
   "source": [
    "from huggingface_hub import model_info\n",
    "\n",
    "info = model_info(\"distilbert/distilbert-base-uncased-finetuned-sst-2-english\")\n",
    "\n",
    "fields = [\"id\", \"pipeline_tag\", \"tags\", \"library_name\", \"model_type\",\n",
    "          \"downloads\", \"likes\", \"created_at\", \"last_modified\"]\n",
    "\n",
    "for field in fields:\n",
    "    value = getattr(info, field, \"N/A\")\n",
    "    print(f\"{field:<20} | {value}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "hgface",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}