File size: 1,672 Bytes
d0e82ca
ae43e08
 
 
 
 
 
 
 
 
 
 
 
20649e6
ae43e08
 
d0e82ca
ae43e08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02a2176
ae43e08
 
20649e6
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import spaces
import datetime
import os
import subprocess
import gradio as gr


CUSTOM_CSS = """
#output_box textarea {
    font-family: IBM Plex Mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
"""




@spaces.GPU
def run():
    output: str = ""
    try:
        output = subprocess.check_output(["nvidia-smi"], text=True)
    except FileNotFoundError:
        output = subprocess.check_output(["ls", "-alh"], text=True)
    comment = (
        datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
    )
    return f"# {comment}\n\n{output}"


def run_custom_command(custom_command: str, secret: str):
    if secret != os.environ.get("SECRET"):
        return "You can't access this"

    print("custom_command", custom_command)
    try:
        return subprocess.check_output(custom_command.split(), text=True)
    except Exception as e:
        return f"{e}"


output = gr.Textbox(
    label="Command Output", max_lines=32, elem_id="output_box", value=run()
)

with gr.Blocks(css=CUSTOM_CSS) as demo:
    gr.Markdown("#### `nvidia-smi`: How is my GPU Space running right now 🔥")

    with gr.Accordion(label="Power user mode", open=False):
        custom_command = gr.Textbox(label="Input command", value="pwd")
        secret = gr.Textbox(
            label="Secret",
        )
        custom_command_btn = gr.Button("Run")
        custom_command_btn.click(
            fn=run_custom_command,
            inputs=[custom_command, secret],
            outputs=output,
        )

    output.render()

    demo.load(fn=run, inputs=None, outputs=output, every=1)


demo.queue().launch()