Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,43 +1,18 @@
|
|
| 1 |
-
import express from "express";
|
| 2 |
-
import path from "path";
|
| 3 |
-
import { fileURLToPath } from "url";
|
| 4 |
-
import fetch from "node-fetch"; // needed for API calls
|
| 5 |
-
|
| 6 |
-
const __filename = fileURLToPath(import.meta.url);
|
| 7 |
-
const __dirname = path.dirname(__filename);
|
| 8 |
-
|
| 9 |
-
const app = express();
|
| 10 |
-
app.use(express.json());
|
| 11 |
-
app.use(express.static(path.join(__dirname, "public")));
|
| 12 |
-
|
| 13 |
-
const PORT = process.env.PORT || 7860;
|
| 14 |
-
|
| 15 |
-
// 🔹 Updated Hugging Face Space FastAPI backend URL
|
| 16 |
const HF_API_URL = "https://aag111-fianl-chatbot.hf.space/chat";
|
| 17 |
|
| 18 |
-
// 🧠 Chat endpoint for frontend to talk to
|
| 19 |
app.post("/chat", async (req, res) => {
|
| 20 |
const userMessage = req.body.message;
|
| 21 |
-
if (!userMessage) {
|
| 22 |
-
return res.status(400).json({ reply: "Please send a valid message." });
|
| 23 |
-
}
|
| 24 |
|
| 25 |
try {
|
| 26 |
-
// 🔹 Send query to the FastAPI backend
|
| 27 |
const response = await fetch(HF_API_URL, {
|
| 28 |
method: "POST",
|
| 29 |
headers: { "Content-Type": "application/json" },
|
| 30 |
-
body: JSON.stringify({ query: userMessage, k: 3 }),
|
| 31 |
});
|
| 32 |
|
| 33 |
-
if (!response.ok) {
|
| 34 |
-
const text = await response.text();
|
| 35 |
-
console.error("Backend error:", text);
|
| 36 |
-
return res.status(500).json({ reply: "Backend returned an error." });
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
const data = await response.json();
|
| 40 |
-
const botReply = data.answer || "Sorry, I couldn’t
|
| 41 |
|
| 42 |
res.json({ reply: botReply });
|
| 43 |
} catch (error) {
|
|
@@ -45,5 +20,3 @@ app.post("/chat", async (req, res) => {
|
|
| 45 |
res.status(500).json({ reply: "Error connecting to chatbot API." });
|
| 46 |
}
|
| 47 |
});
|
| 48 |
-
|
| 49 |
-
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const HF_API_URL = "https://aag111-fianl-chatbot.hf.space/chat";
|
| 2 |
|
|
|
|
| 3 |
app.post("/chat", async (req, res) => {
|
| 4 |
const userMessage = req.body.message;
|
| 5 |
+
if (!userMessage) return res.status(400).json({ reply: "Please send a valid message." });
|
|
|
|
|
|
|
| 6 |
|
| 7 |
try {
|
|
|
|
| 8 |
const response = await fetch(HF_API_URL, {
|
| 9 |
method: "POST",
|
| 10 |
headers: { "Content-Type": "application/json" },
|
| 11 |
+
body: JSON.stringify({ query: userMessage, k: 3 }),
|
| 12 |
});
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
const data = await response.json();
|
| 15 |
+
const botReply = data.answer || "Sorry, I couldn’t find an answer.";
|
| 16 |
|
| 17 |
res.json({ reply: botReply });
|
| 18 |
} catch (error) {
|
|
|
|
| 20 |
res.status(500).json({ reply: "Error connecting to chatbot API." });
|
| 21 |
}
|
| 22 |
});
|
|
|
|
|
|