File size: 1,917 Bytes
4f83637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""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