ausername-12345 commited on
Commit
f8cdebf
·
1 Parent(s): bc61845

Test Basic auth for HF inference endpoint

Browse files
Files changed (1) hide show
  1. server.js +8 -7
server.js CHANGED
@@ -116,23 +116,24 @@ app.get("/api/test-network", async (_, res) => {
116
  }
117
  }
118
  const token = process.env.HF_TOKEN;
 
119
  for (const cfg of [
120
  { url: "https://google.com" },
121
  { url: "https://huggingface.co" },
122
  { url: "https://huggingface.co/api/inference", auth: true },
123
- { url: "https://huggingface.co/api/inference/models/HuggingFaceH4/zephyr-7b-beta", auth: true, method: "POST", body: JSON.stringify({ inputs: "hello", parameters: { max_new_tokens: 10 } }) },
124
- { url: "https://huggingface.co/models/HuggingFaceH4/zephyr-7b-beta" },
125
- { url: "https://router.huggingface.co" },
126
- { url: "https://api-inference.huggingface.co" },
127
  ]) {
128
  try {
129
  const headers = { "User-Agent": "node" };
130
- if (cfg.auth && token) headers.Authorization = `Bearer ${token}`;
 
 
131
  const opts = { redirect: "manual", headers, method: cfg.method || "GET" };
132
  if (cfg.method === "POST") opts.body = cfg.body;
133
- const r = await fetchWithTimeout(cfg.url, opts, 5000);
134
  const text = await r.text();
135
- results[cfg.url] = { status: r.status, body: text.slice(0, 200) };
136
  } catch (e) {
137
  results[cfg.url] = { error: e.message || String(e), code: e.cause?.code || e.code };
138
  }
 
116
  }
117
  }
118
  const token = process.env.HF_TOKEN;
119
+ const basic = token ? Buffer.from(`user:${token}`).toString("base64") : "";
120
  for (const cfg of [
121
  { url: "https://google.com" },
122
  { url: "https://huggingface.co" },
123
  { url: "https://huggingface.co/api/inference", auth: true },
124
+ { url: "https://huggingface.co/api/inference", auth: "basic" },
125
+ { url: "https://huggingface.co/api/inference/models/HuggingFaceH4/zephyr-7b-beta", auth: "bearer", method: "POST", body: JSON.stringify({ inputs: "hello", parameters: { max_new_tokens: 10 } }) },
 
 
126
  ]) {
127
  try {
128
  const headers = { "User-Agent": "node" };
129
+ if (cfg.auth === true) headers.Authorization = `Bearer ${token}`;
130
+ if (cfg.auth === "bearer" && token) headers.Authorization = `Bearer ${token}`;
131
+ if (cfg.auth === "basic" && token) headers.Authorization = `Basic ${basic}`;
132
  const opts = { redirect: "manual", headers, method: cfg.method || "GET" };
133
  if (cfg.method === "POST") opts.body = cfg.body;
134
+ const r = await fetchWithTimeout(cfg.url, opts, 8000);
135
  const text = await r.text();
136
+ results[cfg.url] = { status: r.status, body: text.slice(0, 300) };
137
  } catch (e) {
138
  results[cfg.url] = { error: e.message || String(e), code: e.cause?.code || e.code };
139
  }