Spaces:
Running
Running
File size: 2,075 Bytes
5064319 |
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 |
import * as express from "express";
const PORT = 7860;
const BOT_USERNAME = "@discussion-bot";
const INFERENCE_URL =
"https://api-inference.huggingface.co/models/bigscience/bloom";
const PROMPT = `Pretend that you are a bot that replies to discussions about machine learning, and reply to the following comment:\n`;
if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
console.error(
"This app needs those env variables to be defined",
"WEBHOOK_SECRET, HF_TOKEN"
);
process.exit();
}
const app = express();
// parse HTTP request bodies as json
app.use(express.json());
app.get("/", (req, res) => {
res.json({ hello: "world" });
});
app.post("/", async (req, res) => {
if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
console.error("incorrect secret");
return res.status(400).json({ error: "incorrect secret" });
}
console.log(req.body);
const event = req.body.event;
if (
event.action === "create" &&
event.scope === "discussion.comment" &&
req.body.comment.content.includes(BOT_USERNAME)
) {
const response = await fetch(INFERENCE_URL, {
method: "POST",
body: JSON.stringify({ inputs: PROMPT + req.body.comment.content }),
});
if (response.ok) {
const output = await response.json();
const continuationText = output[0].generated_text.replace(
PROMPT + req.body.comment.content,
""
);
console.log(continuationText);
/// Finally, let's post it as a comment in the same discussion
const commentUrl = req.body.discussion.url.api + "/comment";
const commentApiResponse = await fetch(commentUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.HF_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ comment: continuationText }),
});
const apiOutput = await commentApiResponse.json();
console.log(apiOutput);
} else {
console.error(`API Error`, await response.json());
}
}
res.json({ success: true });
});
app.listen(PORT, () => {
console.debug(`server started at http://localhost:${PORT}`);
});
|