Opera8 commited on
Commit
b4f468a
·
verified ·
1 Parent(s): 4d3509e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -265,7 +265,7 @@ async (image) => {
265
  }
266
  """
267
 
268
- # --- جاوااسکریپت سراسری (بهینه شده برای رفع مشکل عدم نمایش بار دوم) ---
269
  js_global_content = """
270
  <script>
271
  document.addEventListener('DOMContentLoaded', () => {
@@ -282,17 +282,30 @@ document.addEventListener('DOMContentLoaded', () => {
282
  forceLight();
283
  setInterval(forceLight, 1000);
284
 
285
- // 2. NEW GPU Quota Replacer (ROBUST VERSION)
286
- // This function handles the replacement logic
 
 
 
 
 
 
 
 
 
 
287
  const replaceError = (node) => {
288
- if (!node || node.dataset.translated) return;
289
 
 
 
 
290
  const originalText = node.innerText || "";
291
  // Regex to catch Quota Exceeded error
292
  const regex = /(\d+)s requested vs. (\d+)s left/;
293
  const match = originalText.match(regex);
294
 
295
- // Also check if it's a generic error that might have been caused by quota but lost details
296
  if (match || originalText.includes("GPU quota") || originalText.includes("exceeded")) {
297
 
298
  const prettyHtml = `
@@ -338,15 +351,14 @@ document.addEventListener('DOMContentLoaded', () => {
338
  </div>
339
 
340
  <div class="guide-actions">
341
- <button class="action-button back-button" onclick="this.closest('.toast-wrap').remove()">
342
  <svg class="action-button-icon" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M19 12H5M12 19l-7-7 7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg>
343
  <span>بازگشت</span>
344
  </button>
345
  <button class="action-button retry-button" onclick="
346
  const runBtn = document.getElementById('run-btn');
347
  if(runBtn) runBtn.click();
348
- // Optional: Close current toast, though clicking run usually clears it
349
- try { this.closest('.toast-wrap').remove(); } catch(e){}
350
  ">
351
  <svg class="action-button-icon" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M23 4v6h-6M1 20v-6h6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path><path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg>
352
  <span>تلاش مجدد</span>
@@ -356,24 +368,28 @@ document.addEventListener('DOMContentLoaded', () => {
356
  `;
357
 
358
  node.innerHTML = prettyHtml;
359
- node.dataset.translated = 'true';
360
  node.style.padding = '0';
361
  node.style.backgroundColor = 'transparent';
 
 
362
  }
363
  };
364
 
365
- // Method A: Mutation Observer (Fast reaction)
366
  const observer = new MutationObserver((mutations) => {
367
  mutations.forEach((mutation) => {
368
  if (mutation.addedNodes.length) {
369
  mutation.addedNodes.forEach((node) => {
370
- if (node.nodeType === 1 && (node.classList.contains('toast-body') || node.classList.contains('error'))) {
371
- replaceError(node);
372
- }
373
- // Also check children just in case
374
- if (node.nodeType === 1 && node.querySelector) {
375
- const childError = node.querySelector('.toast-body, .error');
376
- if (childError) replaceError(childError);
 
 
377
  }
378
  });
379
  }
@@ -381,16 +397,18 @@ document.addEventListener('DOMContentLoaded', () => {
381
  });
382
  observer.observe(document.body, { childList: true, subtree: true });
383
 
384
- // Method B: Interval Scanner (Backup for missed events/re-renders)
385
- // This fixes the issue where the second error doesn't show
 
386
  setInterval(() => {
387
  const errorNodes = document.querySelectorAll('.toast-body, .error');
388
  errorNodes.forEach(node => {
389
- if (!node.dataset.translated) {
 
390
  replaceError(node);
391
  }
392
  });
393
- }, 500);
394
  });
395
  </script>
396
  """
 
265
  }
266
  """
267
 
268
+ # --- جاوااسکریپت سراسری (Fixed to ensure retry works every time) ---
269
  js_global_content = """
270
  <script>
271
  document.addEventListener('DOMContentLoaded', () => {
 
282
  forceLight();
283
  setInterval(forceLight, 1000);
284
 
285
+ // Function to completely remove the error toast
286
+ // This forces Gradio to create a brand new one next time
287
+ window.removeErrorToast = function(btn) {
288
+ const toast = btn.closest('.toast-wrap') || btn.closest('.toast-body');
289
+ if (toast) {
290
+ toast.remove();
291
+ }
292
+ // Also try to find any other lingering error messages and remove them
293
+ document.querySelectorAll('.toast-body, .toast-wrap').forEach(el => el.remove());
294
+ };
295
+
296
+ // 2. NEW GPU Quota Replacer logic
297
  const replaceError = (node) => {
298
+ if (!node) return;
299
 
300
+ // Prevent re-processing if our custom container is already inside
301
+ if (node.querySelector('.ip-reset-guide-container')) return;
302
+
303
  const originalText = node.innerText || "";
304
  // Regex to catch Quota Exceeded error
305
  const regex = /(\d+)s requested vs. (\d+)s left/;
306
  const match = originalText.match(regex);
307
 
308
+ // Check triggers
309
  if (match || originalText.includes("GPU quota") || originalText.includes("exceeded")) {
310
 
311
  const prettyHtml = `
 
351
  </div>
352
 
353
  <div class="guide-actions">
354
+ <button class="action-button back-button" onclick="window.removeErrorToast(this)">
355
  <svg class="action-button-icon" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M19 12H5M12 19l-7-7 7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg>
356
  <span>بازگشت</span>
357
  </button>
358
  <button class="action-button retry-button" onclick="
359
  const runBtn = document.getElementById('run-btn');
360
  if(runBtn) runBtn.click();
361
+ window.removeErrorToast(this);
 
362
  ">
363
  <svg class="action-button-icon" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M23 4v6h-6M1 20v-6h6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path><path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg>
364
  <span>تلاش مجدد</span>
 
368
  `;
369
 
370
  node.innerHTML = prettyHtml;
371
+ // Force removal of default styling to avoid conflicts
372
  node.style.padding = '0';
373
  node.style.backgroundColor = 'transparent';
374
+ node.style.boxShadow = 'none';
375
+ node.style.border = 'none';
376
  }
377
  };
378
 
379
+ // Mutation Observer to watch for NEW errors
380
  const observer = new MutationObserver((mutations) => {
381
  mutations.forEach((mutation) => {
382
  if (mutation.addedNodes.length) {
383
  mutation.addedNodes.forEach((node) => {
384
+ // Gradio usually adds a .toast-body inside a .toast-wrap
385
+ if (node.nodeType === 1) {
386
+ if (node.classList.contains('toast-body') || node.classList.contains('error')) {
387
+ replaceError(node);
388
+ } else if (node.querySelector) {
389
+ // Check children
390
+ const childError = node.querySelector('.toast-body, .error');
391
+ if (childError) replaceError(childError);
392
+ }
393
  }
394
  });
395
  }
 
397
  });
398
  observer.observe(document.body, { childList: true, subtree: true });
399
 
400
+ // CONTINUOUS SCANNER: This is critical for the "second attempt" issue.
401
+ // Sometimes Gradio updates the text node without adding a new element,
402
+ // or recycles the container. This interval ensures we catch it.
403
  setInterval(() => {
404
  const errorNodes = document.querySelectorAll('.toast-body, .error');
405
  errorNodes.forEach(node => {
406
+ // Only try to replace if it doesn't already have our custom UI
407
+ if (!node.querySelector('.ip-reset-guide-container')) {
408
  replaceError(node);
409
  }
410
  });
411
+ }, 200); // Check every 200ms
412
  });
413
  </script>
414
  """