fdsgsfjsfg commited on
Commit
3aeaeb2
·
verified ·
1 Parent(s): f0b85a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -26
app.py CHANGED
@@ -441,8 +441,47 @@ def infer(
441
  # =========================================================
442
  # UI
443
  # =========================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
  with gr.Blocks(
445
  theme=theme,
 
446
  css="""
447
  .gradio-container {
448
  max-width: 1400px !important;
@@ -463,13 +502,19 @@ with gr.Blocks(
463
  color: #666;
464
  margin-bottom: 0;
465
  }
466
- /* 画笔工具栏移到画布外部,不再遮挡图像 */
467
- .src-editor .toolbar-wrap,
468
- .src-editor .image-editor-toolbar,
469
- .src-editor [class*="toolbar"] {
470
- position: relative !important;
471
- bottom: auto !important;
472
- top: auto !important;
 
 
 
 
 
 
473
  }
474
  """,
475
  ) as demo:
@@ -495,11 +540,9 @@ with gr.Blocks(
495
  color_mode="defaults",
496
  ),
497
  )
498
- with gr.Accordion("画笔颜色说明(点击展开/折叠)", open=False):
499
- gr.Markdown(
500
- "🔴 红 · 🟢 绿 · 🔵 蓝 · 🟡 黄 · 🟣 紫 · ⬜ 白\n\n"
501
- "画好后提示词写:*去掉红色标注的区域* 等"
502
- )
503
  with gr.Row():
504
  ref_input = gr.Image(
505
  label="Reference Image(参考图,可选)",
@@ -572,17 +615,3 @@ with gr.Blocks(
572
  type="numpy",
573
  sources=["upload", "clipboard"],
574
  image_mode="RGBA",
575
- )
576
- apply_button = gr.Button("Apply", variant="primary")
577
-
578
- with gr.Column(scale=1):
579
- placement_output = gr.Image(label="Result", type="pil")
580
-
581
- apply_button.click(
582
- fn=paste_png_into_mask,
583
- inputs=[mask_editor, png_input],
584
- outputs=[placement_output],
585
- )
586
-
587
- if __name__ == "__main__":
588
- demo.launch()
 
441
  # =========================================================
442
  # UI
443
  # =========================================================
444
+ # JS:等 ImageEditor 渲染完后,把绝对定位的工具栏改为相对定位,
445
+ # 使其不再悬浮覆盖画布(CSS 选择器会被 Svelte 作用域哈希阻挡,
446
+ # 所以用 JS 通过 getComputedStyle 精确检测并强制修改)
447
+ _FIX_TOOLBAR_JS = """
448
+ () => {
449
+ const setup = (ed) => {
450
+ if (ed.dataset.toggleReady) return;
451
+ // 找工具栏元素(Gradio/Svelte 会给 class 加哈希,用 includes 匹配)
452
+ const toolbar = Array.from(ed.querySelectorAll('*')).find(el => {
453
+ const cls = el.getAttribute('class') || '';
454
+ return cls.includes('toolbar') || cls.includes('tool-bar');
455
+ });
456
+ if (!toolbar) return;
457
+ ed.dataset.toggleReady = '1';
458
+
459
+ // 插入切换按钮,放在 toolbar 的父容器第一位
460
+ const btn = document.createElement('button');
461
+ btn.className = 'toolbar-toggle-btn';
462
+ btn.textContent = '🎨 隐藏画笔工具栏';
463
+ let hidden = false;
464
+ btn.onclick = () => {
465
+ hidden = !hidden;
466
+ // 用 visibility 而非 display,避免画布区域跳动
467
+ toolbar.style.visibility = hidden ? 'hidden' : '';
468
+ toolbar.style.pointerEvents = hidden ? 'none' : '';
469
+ btn.textContent = hidden ? '🎨 显示画笔工具栏' : '🎨 隐藏画笔工具栏';
470
+ };
471
+ toolbar.parentNode.insertBefore(btn, toolbar);
472
+ };
473
+
474
+ const mo = new MutationObserver(() => {
475
+ document.querySelectorAll('.src-editor').forEach(setup);
476
+ });
477
+ mo.observe(document.body, { childList: true, subtree: true });
478
+ setTimeout(() => document.querySelectorAll('.src-editor').forEach(setup), 1000);
479
+ }
480
+ """
481
+
482
  with gr.Blocks(
483
  theme=theme,
484
+ js=_FIX_TOOLBAR_JS,
485
  css="""
486
  .gradio-container {
487
  max-width: 1400px !important;
 
502
  color: #666;
503
  margin-bottom: 0;
504
  }
505
+ /* 工具栏隐藏时,隐藏按钮仍可点击 */
506
+ .toolbar-toggle-btn {
507
+ display: block;
508
+ width: 100%;
509
+ padding: 4px 10px;
510
+ margin-bottom: 2px;
511
+ background: #f0f0f0;
512
+ border: 1px solid #ddd;
513
+ border-radius: 4px;
514
+ font-size: 12px;
515
+ cursor: pointer;
516
+ text-align: left;
517
+ color: #555;
518
  }
519
  """,
520
  ) as demo:
 
540
  color_mode="defaults",
541
  ),
542
  )
543
+ gr.Markdown(
544
+ "<small>🔴红 🟢绿 🔵蓝 🟡黄 🟣紫 ⬜白 — 画好后提示词写:*去掉红色标注的区域* 等</small>"
545
+ )
 
 
546
  with gr.Row():
547
  ref_input = gr.Image(
548
  label="Reference Image(参考图,可选)",
 
615
  type="numpy",
616
  sources=["upload", "clipboard"],
617
  image_mode="RGBA",