julien-c HF staff commited on
Commit
cf5db11
1 Parent(s): b865405

more feature-complete app

Browse files
Files changed (3) hide show
  1. .prettierrc +3 -0
  2. .vscode/settings.json +2 -1
  3. server.ts +29 -4
.prettierrc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "useTabs": true
3
+ }
.vscode/settings.json CHANGED
@@ -1,3 +1,4 @@
1
  {
2
- "editor.formatOnSave": true
 
3
  }
 
1
  {
2
+ "editor.formatOnSave": true,
3
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
4
  }
server.ts CHANGED
@@ -1,5 +1,11 @@
1
  import * as express from "express";
2
 
 
 
 
 
 
 
3
  const app = express();
4
  // parse HTTP request bodies as json
5
  app.use(express.json());
@@ -8,18 +14,37 @@ app.get("/", (req, res) => {
8
  res.json({ hello: "world" });
9
  });
10
 
11
- app.post("/", (req, res) => {
12
-
13
  if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
14
  return res.status(400).json({ error: "incorrect secret" });
15
  }
16
  console.log(req.body);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
 
 
 
 
 
18
  res.json({ success: true });
19
  });
20
 
21
-
22
- const PORT = 7860;
23
  app.listen(PORT, () => {
24
  console.debug(`server started at http://localhost:${PORT}`);
25
  });
 
1
  import * as express from "express";
2
 
3
+ const PORT = 7860;
4
+ const BOT_USERNAME = "@discussion-bot";
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
  const app = express();
10
  // parse HTTP request bodies as json
11
  app.use(express.json());
 
14
  res.json({ hello: "world" });
15
  });
16
 
17
+ app.post("/", async (req, res) => {
 
18
  if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
19
  return res.status(400).json({ error: "incorrect secret" });
20
  }
21
  console.log(req.body);
22
+ const event = req.body.event;
23
+ if (
24
+ event.action === "create" &&
25
+ event.scope === "discussion.comment" &&
26
+ req.body.comment.content.includes(BOT_USERNAME)
27
+ ) {
28
+ const response = await fetch(INFERENCE_URL, {
29
+ method: "POST",
30
+ body: JSON.stringify({ inputs: PROMPT + req.body.comment.content }),
31
+ });
32
+ if (response.ok) {
33
+ const output = await response.json();
34
+ const continuationText = output[0].generated_text.replace(
35
+ PROMPT + req.body.comment.content,
36
+ ""
37
+ );
38
 
39
+ console.log(continuationText);
40
+ /// Finally, let's post it as a comment in the same discussion
41
+ } else {
42
+ console.error(`API Error`, await response.json());
43
+ }
44
+ }
45
  res.json({ success: true });
46
  });
47
 
 
 
48
  app.listen(PORT, () => {
49
  console.debug(`server started at http://localhost:${PORT}`);
50
  });