File size: 1,995 Bytes
6d49714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
const express = require("express");
const path = require("path");
const app = express();

// Middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Utility function to fetch data from HuggingFace
async function query(url, data) {
  console.log(data);
  try {
    const response = await fetch(url, {
      headers: {
        "Content-Type": "application/json",
      },
      method: "POST",
      body: JSON.stringify(data),
    });

    if (!response.ok) {
      throw new Error(
        `Server responded with ${response.status}: ${response.statusText}`
      );
    }

    const result = await response.json();
    if (
      result &&
      Array.isArray(result) &&
      result[0] &&
      result[0].generated_text
    ) {
      return result[0].generated_text;
    } else {
      throw new Error("Unexpected response format");
    }
  } catch (error) {
    console.error(`Error querying model at ${url}:`, error.message);
    return null;
  }
}

// API Routes
app.post("/api/model/mistralai", async (req, res) => {
  const result = await query(
    "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1",
    req.body
  );

  if (result) {
    res.json({ generated_text: result });
  } else {
    res.status(500).json({ error: "Failed to get response from model" });
  }
});

app.post("/api/model/zephyr", async (req, res) => {
  const result = await query(
    "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-alpha",
    req.body
  );

  if (result) {
    res.json({ generated_text: result });
  } else {
    res.status(500).json({ error: "Failed to get response from model" });
  }
});

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "index.html"));
});

// Catch-all route for all non-API URLs to redirect to root
app.get("*", (req, res) => {
  res.redirect("/");
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server started on port ${PORT}`);
});