Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -619,26 +619,27 @@ CSS = """
|
|
| 619 |
.primary-button { min-height: 48px; }
|
| 620 |
"""
|
| 621 |
|
|
|
|
| 622 |
SIDEBAR_HTML = """
|
| 623 |
-
<button id="hamburger-btn"
|
| 624 |
|
| 625 |
-
<div id="sidebar-overlay"
|
| 626 |
|
| 627 |
<div id="sidebar-panel">
|
| 628 |
-
<button class="close-btn"
|
| 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"
|
| 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%"
|
| 640 |
|
| 641 |
-
<button class="gen-key-btn"
|
| 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 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
document.getElementById('base-url-
|
| 664 |
-
|
| 665 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
|
| 667 |
-
function
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
|
|
|
| 671 |
|
| 672 |
-
function
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
const
|
| 679 |
-
|
| 680 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
}
|
| 682 |
-
});
|
| 683 |
-
}
|
| 684 |
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
}
|
|
|
|
|
|
|
| 702 |
}
|
| 703 |
-
</script>
|
| 704 |
"""
|
| 705 |
|
| 706 |
|
| 707 |
-
with gr.Blocks(
|
| 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
|
| 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 |
|