lastwarning commited on
Commit
b8efcd0
Β·
verified Β·
1 Parent(s): f8d9d44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -51
app.py CHANGED
@@ -619,26 +619,27 @@ CSS = """
619
  .primary-button { min-height: 48px; }
620
  """
621
 
 
622
  SIDEBAR_HTML = """
623
- <button id="hamburger-btn" onclick="toggleSidebar()" title="API Settings">☰</button>
624
 
625
- <div id="sidebar-overlay" onclick="closeSidebar()"></div>
626
 
627
  <div id="sidebar-panel">
628
- <button class="close-btn" onclick="closeSidebar()">βœ•</button>
629
  <h2>βš™ API Access</h2>
630
 
631
  <div class="sidebar-label">Base URL</div>
632
  <div class="sidebar-row">
633
  <span id="base-url-text" style="flex:1">Loading...</span>
634
- <button class="copy-btn" onclick="copyText('base-url-text')">Copy</button>
635
  </div>
636
 
637
  <div class="sidebar-label">API Key</div>
638
  <div id="key-display">No key generated yet.</div>
639
- <button class="copy-btn" style="margin-top:8px; width:100%" onclick="copyText('key-display')">Copy Key</button>
640
 
641
- <button class="gen-key-btn" onclick="generateKey()">πŸ”‘ Generate New API Key</button>
642
 
643
  <div style="margin-top:24px; padding-top:16px; border-top:1px solid #45475a">
644
  <div class="sidebar-label" style="margin-bottom:8px">Available Endpoints</div>
@@ -651,60 +652,78 @@ SIDEBAR_HTML = """
651
  </div>
652
  </div>
653
  </div>
 
654
 
655
- <script>
656
- function toggleSidebar() {
657
- const overlay = document.getElementById('sidebar-overlay');
658
- const panel = document.getElementById('sidebar-panel');
659
- const isOpen = panel.style.display === 'block';
660
- overlay.style.display = isOpen ? 'none' : 'block';
661
- panel.style.display = isOpen ? 'none' : 'block';
662
- if (!isOpen) {
663
- document.getElementById('base-url-text').textContent = window.location.origin;
664
- }
665
- }
 
 
 
 
 
666
 
667
- function closeSidebar() {
668
- document.getElementById('sidebar-overlay').style.display = 'none';
669
- document.getElementById('sidebar-panel').style.display = 'none';
670
- }
 
671
 
672
- function copyText(elemId) {
673
- const el = document.getElementById(elemId);
674
- const text = el.textContent.trim();
675
- navigator.clipboard.writeText(text).then(() => {
676
- const btn = el.nextElementSibling || el.previousElementSibling;
677
- if (btn && btn.classList.contains('copy-btn')) {
678
- const orig = btn.textContent;
679
- btn.textContent = 'βœ“ Copied';
680
- setTimeout(() => { btn.textContent = orig; }, 1500);
 
 
 
 
681
  }
682
- });
683
- }
684
 
685
- async function generateKey() {
686
- const btn = document.querySelector('.gen-key-btn');
687
- btn.textContent = '⏳ Generating...';
688
- btn.disabled = true;
689
- try {
690
- const resp = await fetch('/generate_key', { method: 'POST' });
691
- const data = await resp.json();
692
- document.getElementById('key-display').textContent = data.key;
693
- btn.textContent = 'βœ… Key Generated!';
694
- setTimeout(() => {
695
- btn.textContent = 'πŸ”‘ Generate New API Key';
696
- btn.disabled = false;
697
- }, 2000);
698
- } catch(e) {
699
- btn.textContent = '❌ Error β€” retry';
700
- btn.disabled = false;
 
 
 
 
 
 
 
 
701
  }
 
 
702
  }
703
- </script>
704
  """
705
 
706
 
707
- with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title=APP_NAME) as demo:
708
 
709
  gr.HTML(SIDEBAR_HTML)
710
 
@@ -815,7 +834,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title=APP_NAME) as demo:
815
  with gr.Row():
816
  gen_key_btn = gr.Button("πŸ”‘ Generate New API Key", variant="primary", scale=1)
817
 
818
- key_output = gr.Textbox(label="Generated Key", interactive=False, show_copy_button=True)
819
  key_status = gr.Markdown()
820
 
821
  gen_key_btn.click(
@@ -853,6 +872,11 @@ async def rest_generate_key():
853
 
854
  from gradio.routes import mount_gradio_app
855
 
 
 
 
 
 
856
  app = mount_gradio_app(api_app, demo, path="/")
857
 
858
 
 
619
  .primary-button { min-height: 48px; }
620
  """
621
 
622
+ # HTML only β€” no <script> tags (Gradio 6 blocks innerHTML script execution)
623
  SIDEBAR_HTML = """
624
+ <button id="hamburger-btn" title="API Settings">☰</button>
625
 
626
+ <div id="sidebar-overlay"></div>
627
 
628
  <div id="sidebar-panel">
629
+ <button class="close-btn" id="sidebar-close-btn">βœ•</button>
630
  <h2>βš™ API Access</h2>
631
 
632
  <div class="sidebar-label">Base URL</div>
633
  <div class="sidebar-row">
634
  <span id="base-url-text" style="flex:1">Loading...</span>
635
+ <button class="copy-btn" id="copy-base-url-btn">Copy</button>
636
  </div>
637
 
638
  <div class="sidebar-label">API Key</div>
639
  <div id="key-display">No key generated yet.</div>
640
+ <button class="copy-btn" id="copy-key-btn" style="margin-top:8px; width:100%">Copy Key</button>
641
 
642
+ <button class="gen-key-btn" id="gen-key-sidebar-btn">πŸ”‘ Generate New API Key</button>
643
 
644
  <div style="margin-top:24px; padding-top:16px; border-top:1px solid #45475a">
645
  <div class="sidebar-label" style="margin-bottom:8px">Available Endpoints</div>
 
652
  </div>
653
  </div>
654
  </div>
655
+ """
656
 
657
+ # JS injected via js_on_load β€” executes properly in Gradio 6
658
+ SIDEBAR_JS = """
659
+ () => {
660
+ function attachSidebarEvents() {
661
+ const hamburger = document.getElementById('hamburger-btn');
662
+ const overlay = document.getElementById('sidebar-overlay');
663
+ const panel = document.getElementById('sidebar-panel');
664
+ const closeBtn = document.getElementById('sidebar-close-btn');
665
+ const copyBaseBtn = document.getElementById('copy-base-url-btn');
666
+ const copyKeyBtn = document.getElementById('copy-key-btn');
667
+ const genKeyBtn = document.getElementById('gen-key-sidebar-btn');
668
+
669
+ if (!hamburger) {
670
+ setTimeout(attachSidebarEvents, 300);
671
+ return;
672
+ }
673
 
674
+ function openSidebar() {
675
+ overlay.style.display = 'block';
676
+ panel.style.display = 'block';
677
+ document.getElementById('base-url-text').textContent = window.location.origin;
678
+ }
679
 
680
+ function closeSidebar() {
681
+ overlay.style.display = 'none';
682
+ panel.style.display = 'none';
683
+ }
684
+
685
+ function copyText(elemId, btn) {
686
+ const el = document.getElementById(elemId);
687
+ const text = el.textContent.trim();
688
+ navigator.clipboard.writeText(text).then(() => {
689
+ const orig = btn.textContent;
690
+ btn.textContent = 'βœ“ Copied';
691
+ setTimeout(() => { btn.textContent = orig; }, 1500);
692
+ });
693
  }
 
 
694
 
695
+ hamburger.addEventListener('click', openSidebar);
696
+ overlay.addEventListener('click', closeSidebar);
697
+ closeBtn.addEventListener('click', closeSidebar);
698
+
699
+ copyBaseBtn.addEventListener('click', () => copyText('base-url-text', copyBaseBtn));
700
+ copyKeyBtn.addEventListener('click', () => copyText('key-display', copyKeyBtn));
701
+
702
+ genKeyBtn.addEventListener('click', async () => {
703
+ genKeyBtn.textContent = '⏳ Generating...';
704
+ genKeyBtn.disabled = true;
705
+ try {
706
+ const resp = await fetch('/generate_key', { method: 'POST' });
707
+ const data = await resp.json();
708
+ document.getElementById('key-display').textContent = data.key;
709
+ genKeyBtn.textContent = 'βœ… Key Generated!';
710
+ setTimeout(() => {
711
+ genKeyBtn.textContent = 'πŸ”‘ Generate New API Key';
712
+ genKeyBtn.disabled = false;
713
+ }, 2000);
714
+ } catch(e) {
715
+ genKeyBtn.textContent = '❌ Error β€” retry';
716
+ genKeyBtn.disabled = false;
717
+ }
718
+ });
719
  }
720
+
721
+ attachSidebarEvents();
722
  }
 
723
  """
724
 
725
 
726
+ with gr.Blocks(title=APP_NAME, js=SIDEBAR_JS) as demo:
727
 
728
  gr.HTML(SIDEBAR_HTML)
729
 
 
834
  with gr.Row():
835
  gen_key_btn = gr.Button("πŸ”‘ Generate New API Key", variant="primary", scale=1)
836
 
837
+ key_output = gr.Textbox(label="Generated Key", interactive=False)
838
  key_status = gr.Markdown()
839
 
840
  gen_key_btn.click(
 
872
 
873
  from gradio.routes import mount_gradio_app
874
 
875
+ # In Gradio 6, theme and css belong in launch(); when mounting into FastAPI
876
+ # we pass them here via app_kwargs workaround β€” or set on the Blocks object directly.
877
+ demo.theme = gr.themes.Soft()
878
+ demo.css = CSS
879
+
880
  app = mount_gradio_app(api_app, demo, path="/")
881
 
882