julien-c HF staff commited on
Commit
ae43e08
1 Parent(s): e85fe8f

initial import

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ import os
3
+ import subprocess
4
+ import gradio as gr
5
+
6
+
7
+ CUSTOM_CSS = """
8
+ #output_box textarea {
9
+ font-family: IBM Plex Mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
10
+ }
11
+ #run_button {
12
+ visibility: hidden;
13
+ }
14
+ """
15
+
16
+ CUSTOM_JS_ON_LOAD = """
17
+ async () => {
18
+ console.log("page.load");
19
+ window.itv = setInterval(() => {
20
+ const btn = document.querySelector('body > gradio-app').shadowRoot.querySelector("#run_button");
21
+ btn.click();
22
+ }, 1000);
23
+ // otherwise JS error
24
+ return [];
25
+ }
26
+ """
27
+
28
+
29
+ def run():
30
+ output: str = ""
31
+ try:
32
+ output = subprocess.check_output(["nvidia-smi"], text=True)
33
+ except FileNotFoundError:
34
+ output = subprocess.check_output(["ls", "-alh"], text=True)
35
+ comment = (
36
+ datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
37
+ )
38
+ return f"# {comment}\n\n{output}"
39
+
40
+
41
+ def run_custom_command(custom_command: str, secret: str):
42
+ if secret != os.environ.get("SECRET"):
43
+ return "You can't access this"
44
+
45
+ print("custom_command", custom_command)
46
+ try:
47
+ return subprocess.check_output(custom_command.split(), text=True)
48
+ except Exception as e:
49
+ return f"{e}"
50
+
51
+
52
+ output = gr.Textbox(
53
+ label="Command Output", max_lines=32, elem_id="output_box", value=run()
54
+ )
55
+
56
+ with gr.Blocks(css=CUSTOM_CSS) as demo:
57
+ gr.Markdown("#### `nvidia-smi`: How is my GPU Space running right now 🔥")
58
+
59
+ with gr.Accordion(label="Power user mode", open=False):
60
+ custom_command = gr.Textbox(label="Input command", value="pwd")
61
+ secret = gr.Textbox(
62
+ label="Secret",
63
+ )
64
+ custom_command_btn = gr.Button("Run")
65
+ custom_command_btn.click(
66
+ fn=run_custom_command,
67
+ inputs=[custom_command, secret],
68
+ outputs=output,
69
+ )
70
+ custom_command_btn.click(
71
+ fn=None,
72
+ inputs=[],
73
+ outputs=[],
74
+ _js="(...args) => { console.log('itv', window.itv, args); clearInterval(window.itv); return args; }",
75
+ )
76
+
77
+ output.render()
78
+
79
+ run_btn = gr.Button("Run", elem_id="run_button")
80
+ run_btn.click(fn=run, inputs=[], outputs=output)
81
+ demo.load(_js=CUSTOM_JS_ON_LOAD)
82
+
83
+
84
+ demo.launch()