automatic-drawing / gpu_note.py
jejunepixels's picture
Remove in-app sign-in; explain the GPU allowance on the intro page
4f83637 verified
Raw History Blame Contribute Delete
1.92 kB
"""GPU-allowance line for the ZeroGPU Spaces in this folder.
Canonical copy: shared-ui/gpu_note.py. Edit it there, then run
`python shared-ui/sync.py` to copy it into each project.
ZeroGPU charges GPU time to the visitor's own daily allowance when they open the
Space on huggingface.co while signed in there: the Space page passes their
identity to the embedded app. Everyone else uses a short guest allowance. The
apps need no sign-in of their own, so the intro page explains this instead.
"""
import html
import gradio as gr
EMBEDDED = ("No account needed. {action} uses a little GPU time from a short daily guest allowance; "
"if you’re signed in to huggingface.co, your account’s larger allowance is used instead.")
DIRECT = ("No account needed. Here GPU time uses the short daily guest allowance; "
"{link} to use your account’s allowance.")
# Show the Space-page wording inside the huggingface.co frame, the own-address wording otherwise.
SHOW_FOR_CONTEXT = """() => {
const direct = window.self === window.top;
document.querySelectorAll('.gpu-note-embedded').forEach(el => { el.hidden = direct; });
document.querySelectorAll('.gpu-note-direct').forEach(el => { el.hidden = !direct; });
}"""
def gpu_note(demo, action, name, space_id):
"""Add the line inside `demo`'s Blocks; `action` is e.g. "Each reply", `space_id` e.g. "owner/name"."""
url = f"https://huggingface.co/spaces/{space_id}"
link = f'<a href="{html.escape(url)}" target="_blank" rel="noopener"><strong>open {html.escape(name)} on Hugging Face</strong></a>'
note = gr.HTML(
f'<p class="signin-note gpu-note-embedded">{html.escape(EMBEDDED.format(action=action))}</p>'
f'<p class="signin-note gpu-note-direct" hidden>{DIRECT.format(link=link)}</p>',
padding=False, elem_classes=["gpu-note"])
demo.load(None, None, None, js=SHOW_FOR_CONTEXT)
return note