导入
文本分类
学习如何使用 Hugging Face 推理 API 执行文本分类任务。
...
文本分类模型
| 模型 | 描述 |
|---|---|
| distilbert-base-uncased-finetuned-sst-2-english | 由 Hugging Face 团队开发用于文本分类。基础模型为 distilbert-base-uncased,已在 sst2 数据集上微调。适用于英语语言。 |
| cardiffnlp/twitter-roberta-base-sentiment | 在约 5800 万条推文上训练,用于情感分析。标签预测结果为 "Negative"、"Positive" 和 "Neutral"。适用于英语语言。 |
| ProsusAI/finbert | 专用于金融文本分类。模型输出 "positive"、"neutral" 和 "negative" 标签。 |
| cardiffnlp/twitter-roberta-base-emotion | 在约 5800 万条推文上训练,用于情感分析,并进一步微调以识别情绪文本。输出标签包括 "anger"、"joy"、"sadness" 等。 |
| finiteautomata/bertweet-base-sentiment-analysis | 基于约 4 万条推文训练,用于情感分析。模型输出 "POS"、"NEG" 和 "NEU" 标签。 |
代码示例
1https://api-inference.huggingface.co/models/{model}
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| inputs | 字符串 | 必需 | 需要分类的字符串或字符串数组 |
| options.use_cache | 布尔值 | 可选 | Hugging Face 推理 API 实现了缓存机制以加速请求。建议对确定性模型使用此功能。默认值为 true。 |
| options.wait_for_model | 布尔值 | 可选 | Hugging Face 推理 API 模型需要时间启动并处理请求。若设置为 true,则等待模型准备就绪,而非返回错误。默认值为 false。 |
1// 接口地址 2const endpointUrl = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"; 3 4const headerParameters = { 5 "Authorization": "Bearer {{ACCESS_TOKEN}}" 6}; 7// 待分类的输入文本 8const data = JSON.stringify({ 9 inputs: "Call me Ishmael. Some years ago—never \ 10 mind how long precisely—having little or no money in my purse, \ 11 and nothing particular to interest me on shore, I thought I would \ 12 sail about a little and see the watery part of the world. It is a \ 13 way I have of driving off the spleen, and regulating the circulation.", 14 options: { 15 wait_for_model: true 16 } 17}); 18 19const options = { 20 method: "POST", 21 headers: headerParameters, 22 body: data 23}; 24 25async function textClassification() { 26 try { 27 const response = await fetch(endpointUrl, options); 28 printResponse(response); 29 } catch (error) { 30 printError(error); 31 } 32} 33 34textClassification();
| 参数 | 类型 | 描述 |
|---|---|---|
| label | 字符串 | 指定文本的预测标签 |
| score | 浮点数 | 指定文本的置信度得分 |
1inputs: "Dickens is one of those writers who are \ 2 well worth stealing. Even the burial of his body in Westminster \ 3 Abbey was a species of theft, if you come to think of it"
1inputs: ["To be, or not to be", 2 "With mirth and laughter let old wrinkles come"]
零样本分类模型
| 模型 | 描述 |
|---|---|
| facebook/bart-large-mnli | 基于 bart-large 模型,在 mnli 数据集上训练。利用预训练的自然语言推理(NLI)模型实现零样本分类。 |
| joeddav/xlm-roberta-large-xnli | 在包含 15 种语言的数据集上训练,基于 xlm-roberta-large 模型。基础模型在 100 种语言上训练,因此对其他语言也有一定泛化能力。适用于零样本分类。 |
| valhalla/distilbart-mnli-12-1 | 通过取 bart-large-mnli 模型的交替层,在相同类型数据集上进行训练。 |
| typeform/distilbert-base-uncased-mnli | 基于 distilbert-base-uncased 模型,并在 mnli 数据集上微调。适用于英语语言。 |
代码示例
1https://api-inference.huggingface.co/models/{model}
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| inputs | 字符串 | 必需 | 需要分类的字符串或字符串数组 |
| parameters.candidate_labels | 字符串数组 | 必需 | 输入文本的候选标签列表。最多允许 10 个候选标签。 |
| parameters.multi_label | 布尔值 | 可选 | 默认值为 false。若需要超过 10 个候选标签,可设为 true。 |
| options.use_cache | 布尔值 | 可选 | Hugging Face 推理 API 实现了缓存机制以加速请求。建议对确定性模型使用此功能。默认值为 true。 |
| options.wait_for_model | 布尔值 | 可选 | Hugging Face 推理 API 模型需要时间启动并处理请求。若设置为 true,则等待模型准备就绪,而非返回错误。默认值为 false。 |
1// 接口地址 2const endpointUrl = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"; 3 4const headerParameters = { 5 "Authorization": "Bearer {{ACCESS_TOKEN}}" 6}; 7// 待分类的输入文本 8const data = JSON.stringify({ 9 inputs: "Machine Learning skills are some of the most sought-after in the modern \ 10 job market. Modern ML Engineers make dozens of thousands of dollars more per year \ 11 than other developers. ", 12 parameters: { 13 candidate_labels: ["Software Engineer", "Machine Learning Engineer", "Electrical Engineer"] 14 }, 15 options: { 16 wait_for_model: true 17 } 18}); 19 20const options = { 21 method: "POST", 22 headers: headerParameters, 23 body: data 24}; 25 26async function textClassification() { 27 try { 28 const response = await fetch(endpointUrl, options); 29 printResponse(response); 30 } catch (error) { 31 printError(error); 32 } 33} 34 35textClassification();
| 参数 | 类型 | 描述 |
|---|---|---|
| sequence | 字符串 | 指定待预测标签的输入文本 |
| labels | 字符串数组 | 我们为零样本分类提供的标签数组 |
| scores | 浮点数数组 | 与标签数组对应的置信度得分数组 |