Spaces:
Sleeping
Sleeping
Update server.ts
Browse files
server.ts
CHANGED
|
@@ -2,20 +2,18 @@ import * as express from "express";
|
|
| 2 |
|
| 3 |
const PORT = 7860;
|
| 4 |
const BOT_USERNAME = "@smolSWE";
|
| 5 |
-
const INFERENCE_URL =
|
| 6 |
-
"https://api-inference.huggingface.co/models/bigscience/bloom";
|
| 7 |
-
const PROMPT = `Pretend that you are a bot that replies to discussions about machine learning, and reply to the following comment:\n`;
|
| 8 |
|
| 9 |
if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
|
| 10 |
console.error(
|
| 11 |
-
"This app needs
|
| 12 |
"WEBHOOK_SECRET, HF_TOKEN"
|
| 13 |
);
|
| 14 |
process.exit();
|
| 15 |
}
|
| 16 |
|
| 17 |
const app = express();
|
| 18 |
-
|
|
|
|
| 19 |
app.use(express.json());
|
| 20 |
|
| 21 |
app.get("/", (req, res) => {
|
|
@@ -24,33 +22,27 @@ app.get("/", (req, res) => {
|
|
| 24 |
|
| 25 |
app.post("/", async (req, res) => {
|
| 26 |
if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
|
| 27 |
-
console.error("
|
| 28 |
-
return res.status(400).json({ error: "
|
| 29 |
}
|
|
|
|
| 30 |
console.log(req.body);
|
|
|
|
| 31 |
const event = req.body.event;
|
|
|
|
| 32 |
if (
|
| 33 |
event.action === "create" &&
|
| 34 |
event.scope === "discussion.comment" &&
|
| 35 |
req.body.comment.content.includes(BOT_USERNAME)
|
| 36 |
) {
|
| 37 |
-
//
|
| 38 |
-
|
| 39 |
-
// body: JSON.stringify({ inputs: PROMPT + req.body.comment.content }),
|
| 40 |
-
// });
|
| 41 |
-
// if (response.ok) {
|
| 42 |
-
// const output = await response.json();
|
| 43 |
-
// const continuationText = output[0].generated_text.replace(
|
| 44 |
-
// PROMPT + req.body.comment.content,
|
| 45 |
-
// ""
|
| 46 |
-
// );
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
console.log(continuationText);
|
| 51 |
-
/// Finally, let's post it as a comment in the same discussion
|
| 52 |
-
const commentUrl = req.body.discussion.url.api + "/comment";
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
const commentApiResponse = await fetch(commentUrl, {
|
| 55 |
method: "POST",
|
| 56 |
headers: {
|
|
@@ -61,15 +53,15 @@ app.post("/", async (req, res) => {
|
|
| 61 |
});
|
| 62 |
|
| 63 |
const apiOutput = await commentApiResponse.json();
|
| 64 |
-
|
| 65 |
console.log(apiOutput);
|
| 66 |
-
}
|
| 67 |
-
console.error(
|
| 68 |
}
|
| 69 |
}
|
|
|
|
| 70 |
res.json({ success: true });
|
| 71 |
});
|
| 72 |
|
| 73 |
app.listen(PORT, () => {
|
| 74 |
-
console.debug(`
|
| 75 |
});
|
|
|
|
| 2 |
|
| 3 |
const PORT = 7860;
|
| 4 |
const BOT_USERNAME = "@smolSWE";
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
|
| 7 |
console.error(
|
| 8 |
+
"This app needs the following env variables to be defined:",
|
| 9 |
"WEBHOOK_SECRET, HF_TOKEN"
|
| 10 |
);
|
| 11 |
process.exit();
|
| 12 |
}
|
| 13 |
|
| 14 |
const app = express();
|
| 15 |
+
|
| 16 |
+
// Parse HTTP request bodies as JSON
|
| 17 |
app.use(express.json());
|
| 18 |
|
| 19 |
app.get("/", (req, res) => {
|
|
|
|
| 22 |
|
| 23 |
app.post("/", async (req, res) => {
|
| 24 |
if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
|
| 25 |
+
console.error("Incorrect secret");
|
| 26 |
+
return res.status(400).json({ error: "Incorrect secret" });
|
| 27 |
}
|
| 28 |
+
|
| 29 |
console.log(req.body);
|
| 30 |
+
|
| 31 |
const event = req.body.event;
|
| 32 |
+
|
| 33 |
if (
|
| 34 |
event.action === "create" &&
|
| 35 |
event.scope === "discussion.comment" &&
|
| 36 |
req.body.comment.content.includes(BOT_USERNAME)
|
| 37 |
) {
|
| 38 |
+
// Simple static response instead of AI-generated text
|
| 39 |
+
const continuationText = `You mentioned ${BOT_USERNAME}. Here's a helpful response! ${req.body.comment.content}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
console.log(continuationText);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
const commentUrl = req.body.discussion.url.api + "/comment";
|
| 44 |
+
|
| 45 |
+
try {
|
| 46 |
const commentApiResponse = await fetch(commentUrl, {
|
| 47 |
method: "POST",
|
| 48 |
headers: {
|
|
|
|
| 53 |
});
|
| 54 |
|
| 55 |
const apiOutput = await commentApiResponse.json();
|
|
|
|
| 56 |
console.log(apiOutput);
|
| 57 |
+
} catch (error) {
|
| 58 |
+
console.error("Failed to post comment:", error);
|
| 59 |
}
|
| 60 |
}
|
| 61 |
+
|
| 62 |
res.json({ success: true });
|
| 63 |
});
|
| 64 |
|
| 65 |
app.listen(PORT, () => {
|
| 66 |
+
console.debug(`Server started at http://localhost:${PORT}`);
|
| 67 |
});
|