Opera8 commited on
Commit
6687486
·
verified ·
1 Parent(s): 49b6fc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -282
app.py CHANGED
@@ -236,103 +236,14 @@ def infer_example(input_image, prompt, lora_adapter):
236
  res, s, status = infer(input_image, prompt, lora_adapter, 0, True, 1.0, 4, "خودکار (پیش‌فرض)", 1024, 1024)
237
  return res, s, status
238
 
239
- # --- جاوااسکریپت (اصلاح شده و قدرتمند) ---
240
- js_code = """
241
- <script>
242
- // تابع فورس کردن تم روشن
243
- function forceLightMode() {
244
- const body = document.querySelector('body');
245
- if (body) {
246
- body.classList.remove('dark');
247
- body.style.backgroundColor = '#f5f7fa';
248
- body.style.color = '#333333';
249
- }
250
- document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark'));
251
- }
252
-
253
- // تابع بستن مودال
254
- function closeQuotaModal() {
255
- const modal = document.getElementById('custom-quota-modal');
256
- if (modal) modal.style.display = 'none';
257
-
258
- // حذف پیام‌های خطا از صفحه تا دوباره نمایش داده نشوند
259
- document.querySelectorAll('.toast-wrap').forEach(el => {
260
- if (el.innerText.includes('GPU quota')) el.remove();
261
- });
262
- }
263
-
264
- // آبزرور قدرتمند برای شکار و جایگزینی خطا
265
- const observer = new MutationObserver((mutations) => {
266
- let quotaErrorFound = false;
267
-
268
- // جستجو در تمام المان‌های اضافه شده
269
- mutations.forEach((mutation) => {
270
- mutation.addedNodes.forEach((node) => {
271
- if (node.nodeType === 1) { // Element node
272
- const text = node.innerText || node.textContent;
273
- // بررسی متن‌های مربوط به Quota
274
- if (text && (text.includes('exceeded your GPU quota') || text.includes('GPU quota'))) {
275
- // مخفی کردن المان اصلی
276
- node.style.display = 'none';
277
- node.style.visibility = 'hidden';
278
- node.style.opacity = '0';
279
- quotaErrorFound = true;
280
- }
281
- }
282
- });
283
- });
284
-
285
- // بررسی المان‌های موجود (برای اطمینان)
286
- document.querySelectorAll('.toast-wrap, .error').forEach(el => {
287
- if (el.innerText.includes('GPU quota')) {
288
- el.style.display = 'none';
289
- quotaErrorFound = true;
290
- }
291
- });
292
-
293
- if (quotaErrorFound) {
294
- const modal = document.getElementById('custom-quota-modal');
295
- if (modal && modal.style.display !== 'flex') {
296
- modal.style.display = 'flex';
297
- }
298
- }
299
- });
300
-
301
- document.addEventListener('DOMContentLoaded', () => {
302
- forceLightMode();
303
- setInterval(forceLightMode, 1000);
304
-
305
- // شروع مانیتور کردن
306
- observer.observe(document.body, {
307
- childList: true,
308
- subtree: true,
309
- characterData: true
310
- });
311
- });
312
- </script>
313
- """
314
-
315
- # جاوااسکریپت دانلود
316
- js_dl = """
317
- async (image) => {
318
- if (!image) { alert("لطفاً ابتدا تصویر را تولید کنید."); return; }
319
- let fileUrl = image.url;
320
- if (fileUrl && !fileUrl.startsWith('http')) {
321
- fileUrl = window.location.origin + fileUrl;
322
- } else if (!fileUrl && image.path) {
323
- fileUrl = window.location.origin + "/file=" + image.path;
324
- }
325
- console.log("Sending download request for:", fileUrl);
326
- window.parent.postMessage({ type: 'DOWNLOAD_REQUEST', url: fileUrl }, '*');
327
- }
328
- """
329
 
330
- # --- تنظیمات HTML و CSS ---
331
- html_code = """
332
  <style>
333
  @import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700&display=swap');
334
 
335
- /* تنظیمات کلی */
336
  :root, .dark, body, .gradio-container {
337
  --body-background-fill: #f5f7fa !important;
338
  --body-text-color: #1f2937 !important;
@@ -353,94 +264,59 @@ body {
353
  padding: 10px;
354
  }
355
 
356
- /* استایل مودال سفارشی */
357
  #custom-quota-modal {
 
358
  position: fixed;
359
- top: 0;
360
- left: 0;
361
- width: 100vw;
362
- height: 100vh;
363
- background-color: rgba(0, 0, 0, 0.85); /* تیره‌تر برای تمرکز بیشتر */
364
- backdrop-filter: blur(8px);
365
  z-index: 2147483647; /* بالاترین لایه ممکن */
366
- display: none;
 
 
 
 
 
 
367
  justify-content: center;
368
  align-items: center;
369
  direction: rtl;
370
  }
371
 
372
  .quota-modal-content {
373
- background-color: white;
374
- padding: 40px;
375
- border-radius: 24px;
376
- width: 90%;
377
- max-width: 450px;
 
 
378
  text-align: center;
379
- box-shadow: 0 20px 60px rgba(0,0,0,0.4);
380
- animation: slideIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
381
- border: 1px solid rgba(255,255,255,0.2);
382
  }
383
 
384
- .quota-icon {
385
- font-size: 72px;
386
- margin-bottom: 25px;
387
- display: block;
388
- animation: float 3s ease-in-out infinite;
389
- }
390
-
391
- .quota-title {
392
- font-size: 1.6em;
393
- font-weight: 900;
394
- color: #e11d48;
395
- margin-bottom: 15px;
396
- }
397
-
398
- .quota-text {
399
- font-size: 1.15em;
400
- color: #374151;
401
- line-height: 1.8;
402
- margin-bottom: 30px;
403
  }
404
 
 
 
 
405
  .quota-btn {
406
- background: linear-gradient(135deg, #e11d48 0%, #be123c 100%);
407
  color: white;
 
408
  border: none;
409
- padding: 14px 35px;
410
- border-radius: 14px;
411
- font-weight: bold;
412
- font-family: 'Vazirmatn', sans-serif;
413
- font-size: 1.1em;
414
  cursor: pointer;
415
- box-shadow: 0 4px 15px rgba(225, 29, 72, 0.4);
416
- transition: transform 0.2s;
417
  width: 100%;
418
  }
 
419
 
420
- .quota-btn:hover {
421
- transform: scale(1.02);
422
- }
423
-
424
- @keyframes slideIn {
425
- from { opacity: 0; transform: translateY(30px) scale(0.9); }
426
- to { opacity: 1; transform: translateY(0) scale(1); }
427
- }
428
-
429
- @keyframes float {
430
- 0% { transform: translateY(0px); }
431
- 50% { transform: translateY(-10px); }
432
- 100% { transform: translateY(0px); }
433
- }
434
-
435
- /* مخفی کردن پیام‌های خطای پیش‌فرض Gradio */
436
- .toast-wrap, .toast-error, .error {
437
- display: none !important;
438
- opacity: 0 !important;
439
- visibility: hidden !important;
440
- pointer-events: none !important;
441
- }
442
-
443
- /* سایر استایل‌ها */
444
  #col-container {
445
  margin: 0 auto;
446
  max-width: 980px;
@@ -453,53 +329,7 @@ body {
453
  border: 1px solid rgba(255,255,255,0.8);
454
  }
455
 
456
- #main-title h1 {
457
- font-size: 2.4em !important;
458
- text-align: center;
459
- color: #1a202c !important;
460
- margin-bottom: 15px;
461
- font-weight: 800;
462
- background: -webkit-linear-gradient(45deg, #2563eb, #1e40af);
463
- -webkit-background-clip: text;
464
- -webkit-text-fill-color: transparent;
465
- }
466
-
467
- #main-description {
468
- text-align: center;
469
- font-size: 1.15em;
470
- color: #4b5563 !important;
471
- margin-bottom: 40px;
472
- line-height: 1.6;
473
- }
474
-
475
- .gr-input-label, span.label-wrap, label span {
476
- font-weight: 700 !important;
477
- color: #374151 !important;
478
- font-size: 0.95em !important;
479
- margin-bottom: 8px !important;
480
- }
481
-
482
- textarea, input[type="text"] {
483
- border: 2px solid #e2e8f0 !important;
484
- border-radius: 12px !important;
485
- background-color: #ffffff !important;
486
- color: #111827 !important;
487
- padding: 12px !important;
488
- font-family: 'Vazirmatn', sans-serif !important;
489
- }
490
-
491
- textarea:focus, input[type="text"]:focus {
492
- border-color: #3b82f6 !important;
493
- box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1) !important;
494
- outline: none;
495
- }
496
-
497
- .gr-dropdown {
498
- background: #ffffff !important;
499
- border-radius: 12px !important;
500
- }
501
-
502
- .primary-btn, button.primary {
503
  background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
504
  border: none !important;
505
  color: white !important;
@@ -507,91 +337,101 @@ textarea:focus, input[type="text"]:focus {
507
  font-size: 1.1em !important;
508
  padding: 14px 28px !important;
509
  border-radius: 14px !important;
510
- box-shadow: 0 4px 15px rgba(16, 185, 129, 0.3) !important;
511
- transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
512
- cursor: pointer !important;
513
- width: 100%;
514
- margin-top: 15px;
515
- }
516
-
517
- .primary-btn:hover, button.primary:hover {
518
- transform: translateY(-2px);
519
- box-shadow: 0 8px 25px rgba(16, 185, 129, 0.45) !important;
520
- }
521
-
522
- #download-btn {
523
- background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%) !important;
524
- box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3) !important;
525
- }
526
- #download-btn:hover {
527
- box-shadow: 0 8px 25px rgba(59, 130, 246, 0.45) !important;
528
- }
529
-
530
- .gradio-container .prose table,
531
- .gradio-container table {
532
- background-color: #ffffff !important;
533
- color: #111827 !important;
534
- border: 1px solid #e5e7eb !important;
535
- border-radius: 12px !important;
536
- overflow: hidden !important;
537
- width: 100% !important;
538
- margin-top: 20px !important;
539
  }
540
 
541
- .gradio-container thead th {
542
- background-color: #f3f4f6 !important;
543
- color: #374151 !important;
544
- font-weight: 700 !important;
545
- border-bottom: 2px solid #e5e7eb !important;
546
- padding: 12px !important;
547
- text-align: right !important;
548
- }
549
-
550
- .gradio-container tbody tr {
551
- background-color: #ffffff !important;
552
- border-bottom: 1px solid #f3f4f6 !important;
553
- }
554
-
555
- .gradio-container tbody tr:hover {
556
- background-color: #f9fafb !important;
557
- }
558
-
559
- .gradio-container tbody td {
560
- background-color: #ffffff !important;
561
- color: #374151 !important;
562
- padding: 10px !important;
563
- }
564
-
565
- footer { display: none !important; }
566
- .flagging { display: none !important; }
567
-
568
- @media (prefers-color-scheme: dark) {
569
- body, .gradio-container, .prose, table, tr, td, th {
570
- background-color: #ffffff !important;
571
- color: #333333 !important;
572
- }
573
  }
574
  </style>
575
- """
576
 
577
- modal_html = """
578
  <div id="custom-quota-modal">
579
- <div class="quota-modal-content">
580
- <span class="quota-icon">✈️</span>
581
- <div class="quota-title">محدودیت استفاده</div>
582
- <div class="quota-text">
583
- سهمیه استفاده از گرافیک پر شده است.
584
- <br><br>
585
- برای ادامه، لطفاً <b>حالت هواپیما (Airplane Mode)</b> گوشی خود را یکبار روشن و خاموش کنید تا IP شما تغییر کند و سپس مجدد تلاش کنید.
586
- </div>
587
- <button class="quota-btn" onclick="closeQuotaModal()">متوجه شدم</button>
588
  </div>
 
 
589
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
590
  """
591
 
592
  with gr.Blocks() as demo:
593
- # تزریق کدها به ترتیب
594
- gr.HTML(html_code + modal_html + js_code)
595
 
596
  with gr.Column(elem_id="col-container"):
597
  gr.Markdown("# **ویرایشگر هوشمند آلفا**", elem_id="main-title")
@@ -694,7 +534,7 @@ with gr.Blocks() as demo:
694
  fn=None,
695
  inputs=[output_image],
696
  outputs=None,
697
- js=js_dl
698
  )
699
 
700
  if __name__ == "__main__":
 
236
  res, s, status = infer(input_image, prompt, lora_adapter, 0, True, 1.0, 4, "خودکار (پیش‌فرض)", 1024, 1024)
237
  return res, s, status
238
 
239
+ # --- جاوااسکریپت و HTML ---
240
+ # در اینجا از setInterval برای بررسی مداوم وجود پیام خطا استفاده می‌کنیم
241
+ # و به محض پیدا شدن، آن را مخفی و مودال خودمان را نشان می‌دهیم.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
+ html_js_modal_code = """
 
244
  <style>
245
  @import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700&display=swap');
246
 
 
247
  :root, .dark, body, .gradio-container {
248
  --body-background-fill: #f5f7fa !important;
249
  --body-text-color: #1f2937 !important;
 
264
  padding: 10px;
265
  }
266
 
267
+ /* استایل مودال خطای سفارشی - طراحی شده برای دیده شدن قطعی */
268
  #custom-quota-modal {
269
+ display: none; /* پیش‌فرض مخفی */
270
  position: fixed;
 
 
 
 
 
 
271
  z-index: 2147483647; /* بالاترین لایه ممکن */
272
+ left: 0;
273
+ top: 0;
274
+ width: 100%;
275
+ height: 100%;
276
+ overflow: auto;
277
+ background-color: rgba(0,0,0,0.8);
278
+ backdrop-filter: blur(5px);
279
  justify-content: center;
280
  align-items: center;
281
  direction: rtl;
282
  }
283
 
284
  .quota-modal-content {
285
+ background-color: #fefefe;
286
+ margin: auto;
287
+ padding: 30px;
288
+ border: 1px solid #888;
289
+ width: 85%;
290
+ max-width: 400px;
291
+ border-radius: 20px;
292
  text-align: center;
293
+ box-shadow: 0 4px 20px rgba(0,0,0,0.5);
294
+ position: relative;
295
+ animation: animatetop 0.4s;
296
  }
297
 
298
+ @keyframes animatetop {
299
+ from {top: -300px; opacity: 0}
300
+ to {top: 0; opacity: 1}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  }
302
 
303
+ .quota-icon { font-size: 50px; display: block; margin-bottom: 15px; }
304
+ .quota-title { font-size: 22px; font-weight: bold; color: #e11d48; margin-bottom: 10px; }
305
+ .quota-text { font-size: 16px; color: #333; margin-bottom: 25px; line-height: 1.6; }
306
  .quota-btn {
307
+ background-color: #e11d48;
308
  color: white;
309
+ padding: 12px 24px;
310
  border: none;
311
+ border-radius: 10px;
 
 
 
 
312
  cursor: pointer;
313
+ font-size: 16px;
314
+ font-family: 'Vazirmatn', sans-serif;
315
  width: 100%;
316
  }
317
+ .quota-btn:hover { background-color: #be123c; }
318
 
319
+ /* استایل سایر اجزا */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
  #col-container {
321
  margin: 0 auto;
322
  max-width: 980px;
 
329
  border: 1px solid rgba(255,255,255,0.8);
330
  }
331
 
332
+ .primary-btn {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
334
  border: none !important;
335
  color: white !important;
 
337
  font-size: 1.1em !important;
338
  padding: 14px 28px !important;
339
  border-radius: 14px !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  }
341
 
342
+ /* مخفی کردن شدید پیام‌های خطای پیش‌فرض گرادیو */
343
+ .toast-wrap, .toast-error, .error, .toast {
344
+ display: none !important;
345
+ visibility: hidden !important;
346
+ opacity: 0 !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  }
348
  </style>
 
349
 
350
+ <!-- کد HTML مودال -->
351
  <div id="custom-quota-modal">
352
+ <div class="quota-modal-content">
353
+ <span class="quota-icon">✈️</span>
354
+ <div class="quota-title">محدودیت استفاده از سرور</div>
355
+ <div class="quota-text">
356
+ سهمیه گرافیک رایگان شما تمام شده است.
357
+ <br><br>
358
+ برای ادامه، لطفاً <b>حالت هواپیما (Airplane Mode)</b> گوشی خود را یکبار روشن و خاموش کنید تا IP تغییر کند، سپس دوباره تلاش کنید.
 
 
359
  </div>
360
+ <button class="quota-btn" onclick="document.getElementById('custom-quota-modal').style.display='none'">متوجه شدم</button>
361
+ </div>
362
  </div>
363
+
364
+ <script>
365
+ // اسکریپت جاوااسکریپت برای مانیتورینگ خطا
366
+ (function() {
367
+ // 1. Force Light Mode
368
+ function forceLightMode() {
369
+ document.body.classList.remove('dark');
370
+ document.body.style.backgroundColor = '#f5f7fa';
371
+ document.body.style.color = '#333';
372
+ document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark'));
373
+ }
374
+
375
+ // 2. Error Monitor Loop (Polling) - بسیار قدرتمندتر از Observer
376
+ setInterval(() => {
377
+ forceLightMode();
378
+
379
+ // جستجو در تمام المان‌های صفحه برای پیدا کردن متن خطا
380
+ // ما دنبال المان‌هایی می‌گردیم که حاوی متن "exceeded your GPU quota" باشند
381
+ const allElements = document.body.getElementsByTagName("*");
382
+
383
+ for (let i = 0; i < allElements.length; i++) {
384
+ const el = allElements[i];
385
+
386
+ // بررسی متن داخل المان (فقط نودهای متنی مستقیم یا کانتینرهای خطا)
387
+ if (el.shadowRoot) continue; // از شدو روت‌ها می‌گذریم
388
+
389
+ if (el.innerText && el.innerText.includes("exceeded your GPU quota")) {
390
+ // اگر خطا پیدا شد:
391
+
392
+ // 1. مخفی کردن المان والد (معمولا toast-wrap یا error-box)
393
+ // سعی می‌کنیم والد مناسب را پیدا کنیم تا کل باکس مخفی شود
394
+ let parent = el;
395
+ let foundContainer = false;
396
+ // تا 5 سطح بالا می‌رویم تا کانتینر اصلی را پیدا کنیم
397
+ for(let j=0; j<5; j++) {
398
+ if (parent.classList.contains('toast-wrap') || parent.classList.contains('error')) {
399
+ parent.style.display = 'none';
400
+ foundContainer = true;
401
+ break;
402
+ }
403
+ if(parent.parentElement) parent = parent.parentElement;
404
+ }
405
+ if (!foundContainer) el.style.display = 'none'; // اگر کانتینر پیدا نشد، خود المان را مخفی کن
406
+
407
+ // 2. نمایش مودال سفارشی
408
+ const modal = document.getElementById('custom-quota-modal');
409
+ if (modal && modal.style.display !== 'flex') {
410
+ modal.style.display = 'flex';
411
+ }
412
+ }
413
+ }
414
+
415
+ }, 500); // هر نیم ثانیه چک می‌کند
416
+ })();
417
+
418
+ // PostMessage Function
419
+ async function sendImageToParent(image) {
420
+ if (!image) { alert("لطفاً ابتدا تصویر را تولید کنید."); return; }
421
+ let fileUrl = image.url;
422
+ if (fileUrl && !fileUrl.startsWith('http')) {
423
+ fileUrl = window.location.origin + fileUrl;
424
+ } else if (!fileUrl && image.path) {
425
+ fileUrl = window.location.origin + "/file=" + image.path;
426
+ }
427
+ console.log("Sending download request for:", fileUrl);
428
+ window.parent.postMessage({ type: 'DOWNLOAD_REQUEST', url: fileUrl }, '*');
429
+ }
430
+ </script>
431
  """
432
 
433
  with gr.Blocks() as demo:
434
+ gr.HTML(html_js_modal_code)
 
435
 
436
  with gr.Column(elem_id="col-container"):
437
  gr.Markdown("# **ویرایشگر هوشمند آلفا**", elem_id="main-title")
 
534
  fn=None,
535
  inputs=[output_image],
536
  outputs=None,
537
+ js="sendImageToParent"
538
  )
539
 
540
  if __name__ == "__main__":