ausername-12345 commited on
Commit
67dcad9
·
1 Parent(s): 981e67c

Add health check endpoint, fetch timeouts, switch to zephyr-7b-beta

Browse files
Files changed (3) hide show
  1. .DS_Store +0 -0
  2. server.js +29 -20
  3. src/rememberit.jsx +1 -1
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
server.js CHANGED
@@ -9,14 +9,21 @@ const PORT = process.env.PORT || 7860;
9
 
10
  app.use(express.json({ limit: "1mb" }));
11
 
 
 
 
 
 
 
 
 
 
 
12
  async function searchDuckDuckGo(query) {
13
  try {
14
  const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
15
- const res = await fetch(url, {
16
- headers: {
17
- "User-Agent":
18
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
19
- },
20
  });
21
  const html = await res.text();
22
  const $ = cheerio.load(html);
@@ -28,7 +35,8 @@ async function searchDuckDuckGo(query) {
28
  if (title) results.push({ title, snippet, link });
29
  });
30
  return results.slice(0, 6);
31
- } catch {
 
32
  return [];
33
  }
34
  }
@@ -49,9 +57,7 @@ app.post("/api/hf", async (req, res) => {
49
  const searchContext =
50
  searchResults.length > 0
51
  ? "\n\nWeb search results:\n" +
52
- searchResults
53
- .map((r, i) => `${i + 1}. "${r.title}"\n ${r.snippet}\n URL: ${r.link}`)
54
- .join("\n\n")
55
  : "";
56
 
57
  const body = {
@@ -65,16 +71,14 @@ app.post("/api/hf", async (req, res) => {
65
  ],
66
  };
67
 
68
- const hfRes = await fetch(
69
  "https://api-inference.huggingface.co/v1/chat/completions",
70
  {
71
  method: "POST",
72
- headers: {
73
- "Content-Type": "application/json",
74
- Authorization: `Bearer ${token}`,
75
- },
76
  body: JSON.stringify(body),
77
  },
 
78
  );
79
 
80
  let data;
@@ -82,15 +86,11 @@ app.post("/api/hf", async (req, res) => {
82
  data = await hfRes.json();
83
  } catch {
84
  const text = await hfRes.text();
85
- return res.status(502).json({
86
- error: `HF API returned non-JSON (${hfRes.status}): ${text.slice(0, 500)}`,
87
- });
88
  }
89
 
90
  if (!hfRes.ok) {
91
- return res.status(hfRes.status).json({
92
- error: data.error?.message || data.error || JSON.stringify(data).slice(0, 300),
93
- });
94
  }
95
 
96
  res.json(data);
@@ -99,6 +99,15 @@ app.post("/api/hf", async (req, res) => {
99
  }
100
  });
101
 
 
 
 
 
 
 
 
 
 
102
  const distPath = join(__dirname, "dist");
103
  app.use(express.static(distPath));
104
  app.get("*", (_, res) => res.sendFile(join(distPath, "index.html")));
 
9
 
10
  app.use(express.json({ limit: "1mb" }));
11
 
12
+ async function fetchWithTimeout(url, opts, ms = 10000) {
13
+ const ctrl = new AbortController();
14
+ const id = setTimeout(() => ctrl.abort(), ms);
15
+ try {
16
+ return await fetch(url, { ...opts, signal: ctrl.signal });
17
+ } finally {
18
+ clearTimeout(id);
19
+ }
20
+ }
21
+
22
  async function searchDuckDuckGo(query) {
23
  try {
24
  const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
25
+ const res = await fetchWithTimeout(url, {
26
+ headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" },
 
 
 
27
  });
28
  const html = await res.text();
29
  const $ = cheerio.load(html);
 
35
  if (title) results.push({ title, snippet, link });
36
  });
37
  return results.slice(0, 6);
38
+ } catch (e) {
39
+ console.error("Search failed:", e.message);
40
  return [];
41
  }
42
  }
 
57
  const searchContext =
58
  searchResults.length > 0
59
  ? "\n\nWeb search results:\n" +
60
+ searchResults.map((r, i) => `${i + 1}. "${r.title}"\n ${r.snippet}\n URL: ${r.link}`).join("\n\n")
 
 
61
  : "";
62
 
63
  const body = {
 
71
  ],
72
  };
73
 
74
+ const hfRes = await fetchWithTimeout(
75
  "https://api-inference.huggingface.co/v1/chat/completions",
76
  {
77
  method: "POST",
78
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
 
 
 
79
  body: JSON.stringify(body),
80
  },
81
+ 30000,
82
  );
83
 
84
  let data;
 
86
  data = await hfRes.json();
87
  } catch {
88
  const text = await hfRes.text();
89
+ return res.status(502).json({ error: `HF returned non-JSON (${hfRes.status}): ${text.slice(0, 500)}` });
 
 
90
  }
91
 
92
  if (!hfRes.ok) {
93
+ return res.status(hfRes.status).json({ error: data.error?.message || data.error || JSON.stringify(data).slice(0, 300) });
 
 
94
  }
95
 
96
  res.json(data);
 
99
  }
100
  });
101
 
102
+ app.get("/api/check", (_, res) => {
103
+ res.json({ ok: true, token: !!process.env.HF_TOKEN });
104
+ });
105
+
106
+ app.use((err, _req, res, _next) => {
107
+ console.error("Unhandled:", err);
108
+ res.status(500).json({ error: err.message || "Internal error" });
109
+ });
110
+
111
  const distPath = join(__dirname, "dist");
112
  app.use(express.static(distPath));
113
  app.get("*", (_, res) => res.sendFile(join(distPath, "index.html")));
src/rememberit.jsx CHANGED
@@ -113,7 +113,7 @@ export default function RememberIt() {
113
  method: "POST",
114
  headers: { "Content-Type": "application/json" },
115
  body: JSON.stringify({
116
- model: "microsoft/Phi-3-mini-4k-instruct",
117
  max_tokens: 1000,
118
  messages: [
119
  { role: "system", content: SYSTEM_PROMPT },
 
113
  method: "POST",
114
  headers: { "Content-Type": "application/json" },
115
  body: JSON.stringify({
116
+ model: "HuggingFaceH4/zephyr-7b-beta",
117
  max_tokens: 1000,
118
  messages: [
119
  { role: "system", content: SYSTEM_PROMPT },