julien-c HF staff commited on
Commit
f636bb5
1 Parent(s): abe70aa

first version

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +47 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env/
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import datetime
3
+ import os
4
+ import subprocess
5
+ import gradio as gr
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
+ """
12
+
13
+ @spaces.GPU
14
+ def run_gpu() -> str:
15
+ output: str = ""
16
+ try:
17
+ output = subprocess.check_output(["nvidia-smi"], text=True)
18
+ except FileNotFoundError:
19
+ output = "nvidia-smi failed"
20
+ comment = (
21
+ datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
22
+ )
23
+ return f"# {comment}\n\n{output}"
24
+
25
+ def run(check: bool) -> str:
26
+ if check:
27
+ return run_gpu()
28
+ else:
29
+ comment = (
30
+ datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
31
+ )
32
+ return f"# {comment}\n\nThis is running on CPU\n\nClick on 'Run on GPU' to move to GPU instantly"
33
+
34
+ output = gr.Textbox(
35
+ label="Command Output", max_lines=32, elem_id="output_box", value=run(False)
36
+ )
37
+
38
+ with gr.Blocks(css=CUSTOM_CSS) as demo:
39
+ gr.Markdown("#### `zero-gpu`: how to run on serverless GPU for free on Spaces 🔥")
40
+
41
+ output.render()
42
+
43
+ check = gr.Checkbox(label="Run on GPU", value=True)
44
+
45
+ demo.load(fn=run, inputs=[check], outputs=output, every=1)
46
+
47
+ demo.launch(show_api=False)