@huggingface/inference 1.4.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +527 -0
- package/dist/index.js +188 -0
- package/dist/index.mjs +162 -0
- package/package.json +4 -4
- package/src/HuggingFace.ts +687 -0
- package/src/index.ts +3 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var src_exports = {};
|
|
21
|
+
__export(src_exports, {
|
|
22
|
+
HuggingFace: () => HuggingFace,
|
|
23
|
+
default: () => src_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/HuggingFace.ts
|
|
28
|
+
var HuggingFace = class {
|
|
29
|
+
apiKey;
|
|
30
|
+
defaultOptions;
|
|
31
|
+
constructor(apiKey, defaultOptions = {}) {
|
|
32
|
+
this.apiKey = apiKey;
|
|
33
|
+
this.defaultOptions = defaultOptions;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
|
|
37
|
+
*/
|
|
38
|
+
async fillMask(args, options) {
|
|
39
|
+
return this.request(args, options);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* This task is well known to summarize longer text into shorter text. Be careful, some models have a maximum length of input. That means that the summary cannot handle full books for instance. Be careful when choosing your model.
|
|
43
|
+
*/
|
|
44
|
+
async summarization(args, options) {
|
|
45
|
+
return (await this.request(args, options))?.[0];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Want to have a nice know-it-all bot that can answer any question?. Recommended model: deepset/roberta-base-squad2
|
|
49
|
+
*/
|
|
50
|
+
async questionAnswer(args, options) {
|
|
51
|
+
return await this.request(args, options);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Don’t know SQL? Don’t want to dive into a large spreadsheet? Ask questions in plain english! Recommended model: google/tapas-base-finetuned-wtq.
|
|
55
|
+
*/
|
|
56
|
+
async tableQuestionAnswer(args, options) {
|
|
57
|
+
return await this.request(args, options);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Usually used for sentiment-analysis this will output the likelihood of classes of an input. Recommended model: distilbert-base-uncased-finetuned-sst-2-english
|
|
61
|
+
*/
|
|
62
|
+
async textClassification(args, options) {
|
|
63
|
+
return (await this.request(args, options))?.[0];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with).
|
|
67
|
+
*/
|
|
68
|
+
async textGeneration(args, options) {
|
|
69
|
+
return (await this.request(args, options))?.[0];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Usually used for sentence parsing, either grammatical, or Named Entity Recognition (NER) to understand keywords contained within text. Recommended model: dbmdz/bert-large-cased-finetuned-conll03-english
|
|
73
|
+
*/
|
|
74
|
+
async tokenClassification(args, options) {
|
|
75
|
+
return HuggingFace.toArray(await this.request(args, options));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
|
|
79
|
+
*/
|
|
80
|
+
async translation(args, options) {
|
|
81
|
+
return (await this.request(args, options))?.[0];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* This task is super useful to try out classification with zero code, you simply pass a sentence/paragraph and the possible labels for that sentence, and you get a result. Recommended model: facebook/bart-large-mnli.
|
|
85
|
+
*/
|
|
86
|
+
async zeroShotClassification(args, options) {
|
|
87
|
+
return HuggingFace.toArray(await this.request(args, options));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* This task corresponds to any chatbot like structure. Models tend to have shorter max_length, so please check with caution when using a given model if you need long range dependency or not. Recommended model: microsoft/DialoGPT-large.
|
|
91
|
+
*
|
|
92
|
+
*/
|
|
93
|
+
async conversational(args, options) {
|
|
94
|
+
return await this.request(args, options);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* This task reads some text and outputs raw float values, that are usually consumed as part of a semantic database/semantic search.
|
|
98
|
+
*/
|
|
99
|
+
async featureExtraction(args, options) {
|
|
100
|
+
return await this.request(args, options);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* This task reads some audio input and outputs the said words within the audio files.
|
|
104
|
+
* Recommended model (english language): facebook/wav2vec2-large-960h-lv60-self
|
|
105
|
+
*/
|
|
106
|
+
async automaticSpeechRecognition(args, options) {
|
|
107
|
+
return await this.request(args, {
|
|
108
|
+
...options,
|
|
109
|
+
binary: true
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* This task reads some audio input and outputs the likelihood of classes.
|
|
114
|
+
* Recommended model: superb/hubert-large-superb-er
|
|
115
|
+
*/
|
|
116
|
+
async audioClassification(args, options) {
|
|
117
|
+
return await this.request(args, {
|
|
118
|
+
...options,
|
|
119
|
+
binary: true
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* This task reads some image input and outputs the likelihood of classes.
|
|
124
|
+
* Recommended model: google/vit-base-patch16-224
|
|
125
|
+
*/
|
|
126
|
+
async imageClassification(args, options) {
|
|
127
|
+
return await this.request(args, {
|
|
128
|
+
...options,
|
|
129
|
+
binary: true
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
|
|
134
|
+
* Recommended model: facebook/detr-resnet-50
|
|
135
|
+
*/
|
|
136
|
+
async objectDetection(args, options) {
|
|
137
|
+
return await this.request(args, {
|
|
138
|
+
...options,
|
|
139
|
+
binary: true
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
|
|
144
|
+
* Recommended model: facebook/detr-resnet-50-panoptic
|
|
145
|
+
*/
|
|
146
|
+
async imageSegmentation(args, options) {
|
|
147
|
+
return await this.request(args, {
|
|
148
|
+
...options,
|
|
149
|
+
binary: true
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
async request(args, options) {
|
|
153
|
+
const mergedOptions = { ...this.defaultOptions, ...options };
|
|
154
|
+
const { model, ...otherArgs } = args;
|
|
155
|
+
const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
|
|
156
|
+
headers: { Authorization: `Bearer ${this.apiKey}` },
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: options?.binary ? args.data : JSON.stringify({
|
|
159
|
+
...otherArgs,
|
|
160
|
+
options: mergedOptions
|
|
161
|
+
})
|
|
162
|
+
});
|
|
163
|
+
if (mergedOptions.retry_on_error !== false && response.status === 503 && !mergedOptions.wait_for_model) {
|
|
164
|
+
return this.request(args, {
|
|
165
|
+
...mergedOptions,
|
|
166
|
+
wait_for_model: true
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const res = await response.json();
|
|
170
|
+
if (res.error) {
|
|
171
|
+
throw new Error(res.error);
|
|
172
|
+
}
|
|
173
|
+
return res;
|
|
174
|
+
}
|
|
175
|
+
static toArray(obj) {
|
|
176
|
+
if (Array.isArray(obj)) {
|
|
177
|
+
return obj;
|
|
178
|
+
}
|
|
179
|
+
return [obj];
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// src/index.ts
|
|
184
|
+
var src_default = HuggingFace;
|
|
185
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
186
|
+
0 && (module.exports = {
|
|
187
|
+
HuggingFace
|
|
188
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// src/HuggingFace.ts
|
|
2
|
+
var HuggingFace = class {
|
|
3
|
+
apiKey;
|
|
4
|
+
defaultOptions;
|
|
5
|
+
constructor(apiKey, defaultOptions = {}) {
|
|
6
|
+
this.apiKey = apiKey;
|
|
7
|
+
this.defaultOptions = defaultOptions;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
|
|
11
|
+
*/
|
|
12
|
+
async fillMask(args, options) {
|
|
13
|
+
return this.request(args, options);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* This task is well known to summarize longer text into shorter text. Be careful, some models have a maximum length of input. That means that the summary cannot handle full books for instance. Be careful when choosing your model.
|
|
17
|
+
*/
|
|
18
|
+
async summarization(args, options) {
|
|
19
|
+
return (await this.request(args, options))?.[0];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Want to have a nice know-it-all bot that can answer any question?. Recommended model: deepset/roberta-base-squad2
|
|
23
|
+
*/
|
|
24
|
+
async questionAnswer(args, options) {
|
|
25
|
+
return await this.request(args, options);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Don’t know SQL? Don’t want to dive into a large spreadsheet? Ask questions in plain english! Recommended model: google/tapas-base-finetuned-wtq.
|
|
29
|
+
*/
|
|
30
|
+
async tableQuestionAnswer(args, options) {
|
|
31
|
+
return await this.request(args, options);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Usually used for sentiment-analysis this will output the likelihood of classes of an input. Recommended model: distilbert-base-uncased-finetuned-sst-2-english
|
|
35
|
+
*/
|
|
36
|
+
async textClassification(args, options) {
|
|
37
|
+
return (await this.request(args, options))?.[0];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with).
|
|
41
|
+
*/
|
|
42
|
+
async textGeneration(args, options) {
|
|
43
|
+
return (await this.request(args, options))?.[0];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Usually used for sentence parsing, either grammatical, or Named Entity Recognition (NER) to understand keywords contained within text. Recommended model: dbmdz/bert-large-cased-finetuned-conll03-english
|
|
47
|
+
*/
|
|
48
|
+
async tokenClassification(args, options) {
|
|
49
|
+
return HuggingFace.toArray(await this.request(args, options));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
|
|
53
|
+
*/
|
|
54
|
+
async translation(args, options) {
|
|
55
|
+
return (await this.request(args, options))?.[0];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* This task is super useful to try out classification with zero code, you simply pass a sentence/paragraph and the possible labels for that sentence, and you get a result. Recommended model: facebook/bart-large-mnli.
|
|
59
|
+
*/
|
|
60
|
+
async zeroShotClassification(args, options) {
|
|
61
|
+
return HuggingFace.toArray(await this.request(args, options));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* This task corresponds to any chatbot like structure. Models tend to have shorter max_length, so please check with caution when using a given model if you need long range dependency or not. Recommended model: microsoft/DialoGPT-large.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
async conversational(args, options) {
|
|
68
|
+
return await this.request(args, options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* This task reads some text and outputs raw float values, that are usually consumed as part of a semantic database/semantic search.
|
|
72
|
+
*/
|
|
73
|
+
async featureExtraction(args, options) {
|
|
74
|
+
return await this.request(args, options);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* This task reads some audio input and outputs the said words within the audio files.
|
|
78
|
+
* Recommended model (english language): facebook/wav2vec2-large-960h-lv60-self
|
|
79
|
+
*/
|
|
80
|
+
async automaticSpeechRecognition(args, options) {
|
|
81
|
+
return await this.request(args, {
|
|
82
|
+
...options,
|
|
83
|
+
binary: true
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* This task reads some audio input and outputs the likelihood of classes.
|
|
88
|
+
* Recommended model: superb/hubert-large-superb-er
|
|
89
|
+
*/
|
|
90
|
+
async audioClassification(args, options) {
|
|
91
|
+
return await this.request(args, {
|
|
92
|
+
...options,
|
|
93
|
+
binary: true
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* This task reads some image input and outputs the likelihood of classes.
|
|
98
|
+
* Recommended model: google/vit-base-patch16-224
|
|
99
|
+
*/
|
|
100
|
+
async imageClassification(args, options) {
|
|
101
|
+
return await this.request(args, {
|
|
102
|
+
...options,
|
|
103
|
+
binary: true
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
|
|
108
|
+
* Recommended model: facebook/detr-resnet-50
|
|
109
|
+
*/
|
|
110
|
+
async objectDetection(args, options) {
|
|
111
|
+
return await this.request(args, {
|
|
112
|
+
...options,
|
|
113
|
+
binary: true
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
|
|
118
|
+
* Recommended model: facebook/detr-resnet-50-panoptic
|
|
119
|
+
*/
|
|
120
|
+
async imageSegmentation(args, options) {
|
|
121
|
+
return await this.request(args, {
|
|
122
|
+
...options,
|
|
123
|
+
binary: true
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
async request(args, options) {
|
|
127
|
+
const mergedOptions = { ...this.defaultOptions, ...options };
|
|
128
|
+
const { model, ...otherArgs } = args;
|
|
129
|
+
const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
|
|
130
|
+
headers: { Authorization: `Bearer ${this.apiKey}` },
|
|
131
|
+
method: "POST",
|
|
132
|
+
body: options?.binary ? args.data : JSON.stringify({
|
|
133
|
+
...otherArgs,
|
|
134
|
+
options: mergedOptions
|
|
135
|
+
})
|
|
136
|
+
});
|
|
137
|
+
if (mergedOptions.retry_on_error !== false && response.status === 503 && !mergedOptions.wait_for_model) {
|
|
138
|
+
return this.request(args, {
|
|
139
|
+
...mergedOptions,
|
|
140
|
+
wait_for_model: true
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const res = await response.json();
|
|
144
|
+
if (res.error) {
|
|
145
|
+
throw new Error(res.error);
|
|
146
|
+
}
|
|
147
|
+
return res;
|
|
148
|
+
}
|
|
149
|
+
static toArray(obj) {
|
|
150
|
+
if (Array.isArray(obj)) {
|
|
151
|
+
return obj;
|
|
152
|
+
}
|
|
153
|
+
return [obj];
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/index.ts
|
|
158
|
+
var src_default = HuggingFace;
|
|
159
|
+
export {
|
|
160
|
+
HuggingFace,
|
|
161
|
+
src_default as default
|
|
162
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huggingface/inference",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
|
|
6
6
|
"description": "Typescript wrapper for the Hugging Face Inference API",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"ai"
|
|
22
22
|
],
|
|
23
23
|
"engines": {
|
|
24
|
-
"node": ">=
|
|
24
|
+
"node": ">=18"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"dist",
|
|
28
|
+
"src"
|
|
29
29
|
],
|
|
30
30
|
"source": "src/index.ts",
|
|
31
31
|
"types": "dist/index.d.ts",
|