akhaliq HF Staff commited on
Commit
187c581
·
verified ·
1 Parent(s): d1846d3

Import client from own origin first; CDNs only as fallback

Browse files
Files changed (1) hide show
  1. index.html +29 -11
index.html CHANGED
@@ -210,19 +210,42 @@
210
  handle.addEventListener("pointerup", () => { dragging = false; });
211
  compare.addEventListener("click", (e) => { if (e.target !== handle) setSlider(pctFromEvent(e)); });
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  async function processFile(file) {
214
  if (!file) return;
215
  const isVideo = file.type.startsWith("video/");
216
  const sourceUrl = URL.createObjectURL(file);
217
  setStatus(`Estimating depth${isVideo ? " (first 60 frames)" : ""}\u2026`);
218
  try {
219
- if (!clientPromise) {
220
- clientPromise = import("https://cdn.jsdelivr.net/npm/@gradio/client@6.32.0/dist/index.min.js")
221
- .then((m) => m.Client.connect(window.location.origin));
222
- }
223
- const client = await clientPromise;
224
  const result = await client.predict(isVideo ? "/predict_depth_video" : "/predict_depth", {
225
- [isVideo ? "video" : "image"]: m_handle_file(file),
226
  });
227
  const depthUrl = result.data[0].url;
228
  show(isVideo ? "video" : "image", sourceUrl, depthUrl);
@@ -233,11 +256,6 @@
233
  }
234
  }
235
 
236
- let m_handle_file = null;
237
- // handle_file comes from the same module; wrap it once the import lands.
238
- import("https://cdn.jsdelivr.net/npm/@gradio/client@6.32.0/dist/index.min.js")
239
- .then((m) => { m_handle_file = m.handle_file; });
240
-
241
  $("browse").addEventListener("click", () => fileInput.click());
242
  fileInput.addEventListener("change", () => {
243
  if (fileInput.files.length) processFile(fileInput.files[0]);
 
210
  handle.addEventListener("pointerup", () => { dragging = false; });
211
  compare.addEventListener("click", (e) => { if (e.target !== handle) setSlider(pctFromEvent(e)); });
212
 
213
+ const CLIENT_SOURCES = [
214
+ "/gradio-client.js",
215
+ "https://cdn.jsdelivr.net/npm/@gradio/client@6.32.0/dist/index.min.js",
216
+ "https://unpkg.com/@gradio/client@6.32.0/dist/index.min.js",
217
+ ];
218
+ async function loadClientModule() {
219
+ for (const src of CLIENT_SOURCES) {
220
+ try {
221
+ return await import(src);
222
+ } catch (err) {
223
+ console.error("Client source failed:", src, err);
224
+ }
225
+ }
226
+ throw new Error("Could not load the Gradio client from this Space or any CDN.");
227
+ }
228
+
229
+ let handleFile = null;
230
+ async function getClient() {
231
+ if (!clientPromise) {
232
+ clientPromise = loadClientModule().then((m) => {
233
+ handleFile = m.handle_file;
234
+ return m.Client.connect(window.location.origin);
235
+ });
236
+ }
237
+ return clientPromise;
238
+ }
239
+
240
  async function processFile(file) {
241
  if (!file) return;
242
  const isVideo = file.type.startsWith("video/");
243
  const sourceUrl = URL.createObjectURL(file);
244
  setStatus(`Estimating depth${isVideo ? " (first 60 frames)" : ""}\u2026`);
245
  try {
246
+ const client = await getClient();
 
 
 
 
247
  const result = await client.predict(isVideo ? "/predict_depth_video" : "/predict_depth", {
248
+ [isVideo ? "video" : "image"]: handleFile(file),
249
  });
250
  const depthUrl = result.data[0].url;
251
  show(isVideo ? "video" : "image", sourceUrl, depthUrl);
 
256
  }
257
  }
258
 
 
 
 
 
 
259
  $("browse").addEventListener("click", () => fileInput.click());
260
  fileInput.addEventListener("change", () => {
261
  if (fileInput.files.length) processFile(fileInput.files[0]);