Public
Script
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
const HUGGING_FACE_API_URL = "https://api-inference.huggingface.co/models";
const HUGGING_FACE_API_KEY = Deno.env.get("HUGGING_FACE_API_KEY");
const defaultModels = {
"feature-extraction": "sentence-transformers/all-MiniLM-L6-v2",
"text-classification": "distilbert-base-uncased-finetuned-sst-2-english",
"token-classification": "dbmdz/bert-large-cased-finetuned-conll03-english",
"question-answering": "deepset/roberta-base-squad2",
"summarization": "sshleifer/distilbart-cnn-12-6",
"translation": "google-t5/t5-small",
"text-generation": "gpt2",
"sentence-similarity": "sentence-transformers/all-MiniLM-L6-v2"
};
class Pipeline {
constructor(task, model = defaultModels[task]) {
this.task = task;
this.model = model;
}
async run(input, options = {}) {
let result;
const formattedInput = this.formatInput(input);
if (this.task === "feature-extraction") {
result = await this.featureExtraction(formattedInput, options);
} else if (this.task === "text-classification") {
result = await this.textClassification(formattedInput, options);
} else if (this.task === "token-classification") {
result = await this.tokenClassification(formattedInput, options);
} else if (this.task === "question-answering") {
result = await this.questionAnswering(formattedInput, options);
} else if (this.task === "summarization") {
result = await this.summarization(formattedInput, options);
} else if (this.task === "translation") {
result = await this.translation(formattedInput, options);
} else if (this.task === "text-generation") {
result = await this.textGeneration(formattedInput, options);
} else if (this.task === "sentence-similarity") {
result = await this.sentenceSimilarity(formattedInput, options);
} else {
result = { error: "Unknown task" };
}
return result;
}
formatInput(input) {
if (this.task === "sentence-similarity") {
return {
inputs: {
source_sentence: input.source_sentence,
sentences: input.sentences
}
};
} else if (this.task === "feature-extraction") {
return { inputs: input };
} else {
return { inputs: input.source_sentence || input };
}
}
async featureExtraction(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async textClassification(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async tokenClassification(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async questionAnswering(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async summarization(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async translation(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async textGeneration(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async sentenceSimilarity(input, options) {
return this.callHuggingFaceAPI(input, options);
}
async callHuggingFaceAPI(payload, options) {
try {
const response = await fetch(`${HUGGING_FACE_API_URL}/${this.model}`, {
method: "POST",
headers: {
"Authorization": `Bearer ${HUGGING_FACE_API_KEY}`,
"Content-Type": "application/json"
},
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 19, 2024