Spaces:
Build error
Build error
Download server.ts from discussion-bot/webhook: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/discussion-bot/webhook/resolve/main/server.ts
- Command line
-
hf download hf://spaces/discussion-bot/webhook/server.ts
-
curl -L -o server.ts https://huggingface.co/spaces/discussion-bot/webhook/resolve/main/server.ts
2.08 kB
| import * as express from "express"; | |||
| const PORT = 7860; | |||
| const BOT_USERNAME = "@discussion-bot"; | |||
| const INFERENCE_URL = | |||
| "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2Fbigscience%2Fbloom%26quot%3B%3C%2Fspan%3E%3B%3C!----%3E%3C%2Ftd%3E%3C%2Ftr%3E%3Ctr id="L7"> | 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}`); | |||
| }); | |||