chat-vs-chat / server.js
victor's picture
victor HF Staff
:)
6d49714
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}`);
});