Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,52 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import cv2
|
| 4 |
-
import numpy as np
|
| 5 |
import os
|
| 6 |
-
import
|
| 7 |
import torch
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
annotated_img = results[0].plot()
|
| 29 |
-
annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 30 |
-
|
| 31 |
-
# Prepare detected areas and labels as text output
|
| 32 |
-
detected_areas_labels = "\n".join(
|
| 33 |
-
[f"{box.label}: {box.conf:.2f}" for box in results[0].boxes]
|
| 34 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
gr.Textbox(label="Detected Areas and Labels")]
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
interface.launch()
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import datetime
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
+
import subprocess
|
| 5 |
import torch
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
+
CUSTOM_CSS = """
|
| 9 |
+
#output_box textarea {
|
| 10 |
+
font-family: IBM Plex Mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
| 11 |
+
}
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
zero = torch.Tensor([0]).cuda()
|
| 15 |
+
print(zero.device) # <-- 'cpu' 🤔
|
| 16 |
+
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def run_gpu() -> str:
|
| 19 |
+
print(zero.device) # <-- 'cuda:0' 🤗
|
| 20 |
+
output: str = ""
|
| 21 |
+
try:
|
| 22 |
+
output = subprocess.check_output(["nvidia-smi"], text=True)
|
| 23 |
+
except FileNotFoundError:
|
| 24 |
+
output = "nvidia-smi failed"
|
| 25 |
+
comment = (
|
| 26 |
+
datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
+
return f"# {comment}\n\n{output}"
|
| 29 |
+
|
| 30 |
+
def run(check: bool) -> str:
|
| 31 |
+
if check:
|
| 32 |
+
return run_gpu()
|
| 33 |
+
else:
|
| 34 |
+
comment = (
|
| 35 |
+
datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
|
| 36 |
+
)
|
| 37 |
+
return f"# {comment}\n\nThis is running on CPU\n\nClick on 'Run on GPU' below to move to GPU instantly and run nvidia-smi"
|
| 38 |
+
|
| 39 |
+
output = gr.Textbox(
|
| 40 |
+
label="Command Output", max_lines=32, elem_id="output_box", value=run(False)
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
| 44 |
+
gr.Markdown("#### `zero-gpu`: how to run on serverless GPU for free on Spaces 🔥")
|
| 45 |
|
| 46 |
+
output.render()
|
| 47 |
+
|
| 48 |
+
check = gr.Checkbox(label="Run on GPU")
|
| 49 |
+
|
| 50 |
+
check.change(run, inputs=[check], outputs=output, every=1)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
demo.queue().launch(show_api=False)
|
|
|