-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.js
More file actions
52 lines (47 loc) · 1.29 KB
/
Copy pathsummarizer.js
File metadata and controls
52 lines (47 loc) · 1.29 KB
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
class HuggingFaceSummarizer {
constructor(token, model = "facebook/bart-large-cnn") {
if (HuggingFaceSummarizer.instance) {
return HuggingFaceSummarizer.instance
}
if (!token) {
throw new Error("API token is required");
}
this.token = token;
this.model = model;
HuggingFaceSummarizer.instance = this
}
async summarize(text) {
const payload = {
"inputs": `summarize this and also give the key point : ${text}`,
"parameters": { "temperature": 0.8, "max_new_tokens": 50, "seed": 42 },
}
const response = await fetch(`${HuggingFaceSummarizer.getUrl()}/${this.model}`, {
method: 'POST',
headers: {
"Authorization": `Bearer ${this.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
const data = response.json()
return res.status(200)
.json({
status: 200,
data
})
}
static getUrl() {
if (!process.env.HF_URL) {
throw new Error("Hugging Face URL is missing in env.")
}
return process.env.HF_URL;
}
static getInstance(token, model) {
return new HuggingFaceSummarizer(token, model)
}
}
// HuggingFaceSummarizer.prototype.apiKey = null;
// HuggingFaceSummarizer.prototype.model = null;
export {
HuggingFaceSummarizer
}