Abid Ali Awan Codex commited on
Commit ·
27b6c4e
1
Parent(s): db68987
Request x-ip-token from HF iframe for ZeroGPU PRO quota
Browse filesThe handshake sets window.supports_zerogpu_headers but the raw
fetch() calls never request the actual token from the HF parent
iframe. Add requestZeroGpuToken() that uses postMessage with a
MessageChannel (same as @gradio/client JS) to fetch the token
before each API call, and include it as x-ip-token header.
Co-authored-by: Codex <codex@openai.com>
- static/app.js +32 -1
static/app.js
CHANGED
|
@@ -239,10 +239,41 @@ function setRiskLabel(label) {
|
|
| 239 |
elements.risk.textContent = t(keys[label] || label);
|
| 240 |
}
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
async function callGradioApi(name, data) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
const response = await fetch(`/gradio_api/call/${name}`, {
|
| 244 |
method: "POST",
|
| 245 |
-
headers
|
| 246 |
body: JSON.stringify({ data }),
|
| 247 |
});
|
| 248 |
if (!response.ok) throw new Error(t("requestStartError"));
|
|
|
|
| 239 |
elements.risk.textContent = t(keys[label] || label);
|
| 240 |
}
|
| 241 |
|
| 242 |
+
async function requestZeroGpuToken() {
|
| 243 |
+
if (!window.supports_zerogpu_headers) return null;
|
| 244 |
+
const hostname = window.location.hostname;
|
| 245 |
+
if (!hostname.endsWith(".hf.space") && !hostname.includes(".dev.")) return null;
|
| 246 |
+
const origin = hostname.includes(".dev.")
|
| 247 |
+
? `https://moon-${hostname.split(".")[1]}.dev.spaces.huggingface.tech`
|
| 248 |
+
: "https://huggingface.co";
|
| 249 |
+
try {
|
| 250 |
+
return await new Promise((resolve) => {
|
| 251 |
+
const channel = new MessageChannel();
|
| 252 |
+
const timeout = setTimeout(() => {
|
| 253 |
+
channel.port1.close();
|
| 254 |
+
resolve(null);
|
| 255 |
+
}, 2000);
|
| 256 |
+
channel.port1.onmessage = ({ data }) => {
|
| 257 |
+
clearTimeout(timeout);
|
| 258 |
+
channel.port1.close();
|
| 259 |
+
resolve(data || null);
|
| 260 |
+
};
|
| 261 |
+
window.parent.postMessage("zerogpu-headers", origin, [channel.port2]);
|
| 262 |
+
});
|
| 263 |
+
} catch {
|
| 264 |
+
return null;
|
| 265 |
+
}
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
async function callGradioApi(name, data) {
|
| 269 |
+
const headers = { "Content-Type": "application/json" };
|
| 270 |
+
const zerogpuToken = await requestZeroGpuToken();
|
| 271 |
+
if (zerogpuToken) {
|
| 272 |
+
headers["x-ip-token"] = zerogpuToken;
|
| 273 |
+
}
|
| 274 |
const response = await fetch(`/gradio_api/call/${name}`, {
|
| 275 |
method: "POST",
|
| 276 |
+
headers,
|
| 277 |
body: JSON.stringify({ data }),
|
| 278 |
});
|
| 279 |
if (!response.ok) throw new Error(t("requestStartError"));
|