Opera8 commited on
Commit
3e616f3
·
verified ·
1 Parent(s): 3c32107

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -103
app.py CHANGED
@@ -265,7 +265,7 @@ async (image) => {
265
  }
266
  """
267
 
268
- # --- جاوااسکریپت سراسری (استراتژی حذف کامل برای رفع مشکل رفرش) ---
269
  js_global_content = """
270
  <script>
271
  document.addEventListener('DOMContentLoaded', () => {
@@ -282,44 +282,46 @@ document.addEventListener('DOMContentLoaded', () => {
282
  forceLight();
283
  setInterval(forceLight, 1000);
284
 
285
- // 2. HELPER: Completely destroy the error element
286
- // This is the key fix. By removing the element, Gradio is forced
287
- // to render a completely new one next time, which our scanner will catch.
288
- window.destroyErrorToast = function(btn) {
289
- // Try to find the container
290
- const toast = btn.closest('.toast-wrap') || btn.closest('.toast-body') || btn.closest('.error');
291
-
292
  // Find the main run button and click it to retry
293
  const runBtn = document.getElementById('run-btn');
294
- if(runBtn) runBtn.click();
295
-
296
- // Remove the error UI immediately
297
- if (toast) {
298
- toast.remove();
299
  }
300
-
301
- // Aggressive cleanup: remove ANY other error toasts visible
 
 
302
  document.querySelectorAll('.toast-body, .toast-wrap, .error').forEach(el => {
303
- if(el.innerText.includes("قدم تا ساخت") || el.innerText.includes("quota")) {
304
- el.remove();
305
- }
 
 
 
 
 
 
306
  });
307
  };
308
 
309
- // Just remove toast (for Back button)
310
  window.closeErrorToast = function(btn) {
311
  const toast = btn.closest('.toast-wrap') || btn.closest('.toast-body') || btn.closest('.error');
312
  if(toast) toast.remove();
313
  };
314
 
315
- // 3. LOGIC: Replace content logic
316
  const replaceErrorContent = (node) => {
317
  // Safety check: if already has our custom container, do nothing
318
  if (node.querySelector('.ip-reset-guide-container')) return;
319
 
320
  const text = node.innerText || "";
321
- // Check triggers
322
- if (text.toLowerCase().includes('quota') || text.toLowerCase().includes('exceeded') || text.includes('requested vs')) {
 
 
323
 
324
  const prettyHtml = `
325
  <div class="ip-reset-guide-container">
@@ -368,7 +370,7 @@ document.addEventListener('DOMContentLoaded', () => {
368
  <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>
369
  <span>بازگشت</span>
370
  </button>
371
- <button class="action-button retry-button" onclick="window.destroyErrorToast(this)">
372
  <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>
373
  <span>تلاش مجدد</span>
374
  </button>
@@ -379,29 +381,31 @@ document.addEventListener('DOMContentLoaded', () => {
379
  // Inject the HTML
380
  node.innerHTML = prettyHtml;
381
 
382
- // Clean up styles to prevent double boxes
383
  node.style.padding = '0';
384
  node.style.border = 'none';
385
  node.style.backgroundColor = 'transparent';
386
  node.style.boxShadow = 'none';
 
387
  }
388
  };
389
 
390
- // 4. AGGRESSIVE SCANNER: Check frequently for errors
 
391
  setInterval(() => {
392
- // Find all potential error containers
393
- const potentialErrors = document.querySelectorAll('.toast-body, .error, .toast-wrap');
394
 
395
  potentialErrors.forEach(el => {
396
- // Only process if it has text and doesn't have our card yet
397
- // AND specifically checks for quota text
398
- if (el.innerText && (el.innerText.includes('quota') || el.innerText.includes('exceeded'))) {
399
  if (!el.querySelector('.ip-reset-guide-container')) {
400
  replaceErrorContent(el);
401
  }
402
  }
403
  });
404
- }, 150); // Fast check
405
  });
406
  </script>
407
  """
@@ -779,75 +783,4 @@ with gr.Blocks() as demo:
779
  value="تبدیل عکس به انیمه"
780
  )
781
 
782
- with gr.Accordion("تنظیمات پیشرفته", open=False, visible=True):
783
- aspect_ratio_selection = gr.Dropdown(
784
- label="ابعاد تصویر خروجی",
785
- choices=ASPECT_RATIOS_LIST,
786
- value="خودکار (پیش‌فرض)",
787
- interactive=True
788
- )
789
-
790
- with gr.Row(visible=False) as custom_dims_row:
791
- custom_width = gr.Slider(
792
- label="عرض دلخواه (Width)",
793
- minimum=256, maximum=2048, step=8, value=1024
794
- )
795
- custom_height = gr.Slider(
796
- label="ارتفاع دلخواه (Height)",
797
- minimum=256, maximum=2048, step=8, value=1024
798
- )
799
-
800
- seed = gr.Slider(label="دانه تصادفی (Seed)", minimum=0, maximum=MAX_SEED, step=1, value=0)
801
- randomize_seed = gr.Checkbox(label="استفاده از Seed تصادفی", value=True)
802
- guidance_scale = gr.Slider(label="میزان وفاداری به متن (Guidance Scale)", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
803
- steps = gr.Slider(label="تعداد مراحل پردازش (Steps)", minimum=1, maximum=50, step=1, value=4)
804
-
805
- # اصلاح تابع نمایش ردیف اسلایدرها
806
- def toggle_row(choice):
807
- if choice == "شخصی‌سازی (Custom)":
808
- return gr.update(visible=True)
809
- return gr.update(visible=False)
810
-
811
- aspect_ratio_selection.change(
812
- fn=toggle_row,
813
- inputs=aspect_ratio_selection,
814
- outputs=custom_dims_row
815
- )
816
-
817
- gr.Examples(
818
- examples=[
819
- ["examples/1.jpg", "تبدیل به انیمه کن.", "تبدیل عکس به انیمه"],
820
- ["examples/5.jpg", "سایه‌ها را حذف کن و نورپردازی نرم به تصویر بده.", "اصلاح نور و سایه"],
821
- ["examples/4.jpg", "از فیلتر ساعت طلایی با پخش نور ملایم استفاده کن.", "نورپردازی مجدد (Relight)"],
822
- ["examples/2.jpeg", "دوربین را ۴۵ درجه به سمت چپ بچرخان.", "تغییر زاویه دید"],
823
- ["examples/7.jpg", "منبع نور را از سمت راست عقب قرار بده.", "نورپردازی چند زاویه‌ای"],
824
- ["examples/10.jpeg", "کیفیت تصویر را افزایش بده (Upscale).", "افزایش کیفیت (Upscale)"],
825
- ["examples/7.jpg", "منبع نور را از پایین بتابان.", "نورپردازی چند زاویه‌ای"],
826
- ["examples/2.jpeg", "زاویه دوربین را به نمای بالا گوشه راست تغییر بده.", "تغییر زاویه دید"],
827
- ["examples/9.jpg", "دوربین کمی به جلو حرکت می‌کند در حالی که نور خورشید از میان ابرها می‌تابد و درخششی نرم اطراف شبح شخصیت در مه ایجاد می‌کند. سبک سینمایی واقعی.", "صحنه بعدی (سینمایی)"],
828
- ["examples/8.jpg", "جزئیات پوست سوژه را برج��ته‌تر و طبیعی‌تر کن.", "روتوش پوست"],
829
- ["examples/6.jpg", "دوربین را به نمای پایین به بالا تغییر بده.", "تغییر زاویه دید"],
830
- ],
831
- inputs=[input_image, prompt, lora_adapter],
832
- outputs=[output_image, seed, status_box],
833
- fn=infer_example,
834
- cache_examples=False,
835
- label="نمونه‌ها (برای تست کلیک کنید)"
836
- )
837
-
838
- run_button.click(
839
- fn=infer,
840
- inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps, aspect_ratio_selection, custom_width, custom_height],
841
- outputs=[output_image, seed, status_box],
842
- api_name="predict"
843
- )
844
-
845
- download_button.click(
846
- fn=None,
847
- inputs=[output_image],
848
- outputs=None,
849
- js=js_download_func
850
- )
851
-
852
- if __name__ == "__main__":
853
- demo.queue(max_size=30).launch(show_error=True)
 
265
  }
266
  """
267
 
268
+ # --- جاوااسکریپت سراسری (Aggressive Scanner & Cleaner) ---
269
  js_global_content = """
270
  <script>
271
  document.addEventListener('DOMContentLoaded', () => {
 
282
  forceLight();
283
  setInterval(forceLight, 1000);
284
 
285
+ // 2. RETRY FUNCTIONALITY
286
+ window.retryGeneration = function(btn) {
 
 
 
 
 
287
  // Find the main run button and click it to retry
288
  const runBtn = document.getElementById('run-btn');
289
+ if(runBtn) {
290
+ runBtn.click();
 
 
 
291
  }
292
+
293
+ // IMPORTANT: Forcefully remove ALL error containers immediately.
294
+ // This ensures that when Gradio renders the next error, it creates a new element
295
+ // or updates the DOM in a way our scanner will definitely catch.
296
  document.querySelectorAll('.toast-body, .toast-wrap, .error').forEach(el => {
297
+ el.remove();
298
+ });
299
+
300
+ // Also find any parent containers that might be holding the error
301
+ const parents = document.querySelectorAll('.gradio-container .prose');
302
+ parents.forEach(p => {
303
+ if (p.innerText.includes("quota") || p.innerText.includes("Error")) {
304
+ p.innerHTML = "";
305
+ }
306
  });
307
  };
308
 
309
+ // Close function for Back button
310
  window.closeErrorToast = function(btn) {
311
  const toast = btn.closest('.toast-wrap') || btn.closest('.toast-body') || btn.closest('.error');
312
  if(toast) toast.remove();
313
  };
314
 
315
+ // 3. REPLACEMENT LOGIC
316
  const replaceErrorContent = (node) => {
317
  // Safety check: if already has our custom container, do nothing
318
  if (node.querySelector('.ip-reset-guide-container')) return;
319
 
320
  const text = node.innerText || "";
321
+
322
+ // Check triggers: "quota", "exceeded", or even just "Error" if it's in the status bar area
323
+ // We are aggressive here to catch the "red bar" shown in your video
324
+ if (text.toLowerCase().includes('quota') || text.toLowerCase().includes('exceeded') || (text.includes('Error') && node.classList.contains('error'))) {
325
 
326
  const prettyHtml = `
327
  <div class="ip-reset-guide-container">
 
370
  <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>
371
  <span>بازگشت</span>
372
  </button>
373
+ <button class="action-button retry-button" onclick="window.retryGeneration(this)">
374
  <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>
375
  <span>تلاش مجدد</span>
376
  </button>
 
381
  // Inject the HTML
382
  node.innerHTML = prettyHtml;
383
 
384
+ // Clean up styles to prevent double boxes and red backgrounds
385
  node.style.padding = '0';
386
  node.style.border = 'none';
387
  node.style.backgroundColor = 'transparent';
388
  node.style.boxShadow = 'none';
389
+ node.classList.remove('error'); // Remove error class to stop red styling
390
  }
391
  };
392
 
393
+ // 4. SCANNER: Check continuously
394
+ // We scan very fast (50ms) to catch the error element the moment it appears
395
  setInterval(() => {
396
+ // We look for both toasts AND the inline error status bar (which was likely the red bar in your video)
397
+ const potentialErrors = document.querySelectorAll('.toast-body, .error, .toast-wrap, .eta-bar.error');
398
 
399
  potentialErrors.forEach(el => {
400
+ // If it has our text triggers and hasn't been replaced yet, replace it
401
+ if (el.innerText && (el.innerText.includes('quota') || el.innerText.includes('exceeded') || el.innerText.includes('Error'))) {
402
+ // Check if we already injected our card
403
  if (!el.querySelector('.ip-reset-guide-container')) {
404
  replaceErrorContent(el);
405
  }
406
  }
407
  });
408
+ }, 50);
409
  });
410
  </script>
411
  """
 
783
  value="تبدیل عکس به انیمه"
784
  )
785
 
786
+ with gr.Accordion("تنظیمات پیشرفته", open=Fa