Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,6 @@ import datetime
|
|
| 9 |
import subprocess
|
| 10 |
import spaces # Ensure this import is correct and the module is available
|
| 11 |
|
| 12 |
-
CUSTOM_CSS = """
|
| 13 |
-
#output_box textarea {
|
| 14 |
-
font-family: IBM Plex Mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
| 15 |
-
}
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
# Ensure the model file is in the correct location
|
| 19 |
model_path = "yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
| 20 |
if not os.path.exists(model_path):
|
|
@@ -44,44 +38,6 @@ def process_image(image):
|
|
| 44 |
|
| 45 |
return annotated_img, detected_areas_labels
|
| 46 |
|
| 47 |
-
zero = torch.Tensor([0]).cuda()
|
| 48 |
-
print(zero.device) # <-- 'cuda:0' if GPU is available, otherwise 'cpu'
|
| 49 |
-
|
| 50 |
-
@spaces.GPU
|
| 51 |
-
def run_gpu() -> str:
|
| 52 |
-
print(zero.device) # <-- 'cuda:0' 🤗
|
| 53 |
-
output: str = ""
|
| 54 |
-
try:
|
| 55 |
-
output = subprocess.check_output(["nvidia-smi"], text=True)
|
| 56 |
-
except FileNotFoundError:
|
| 57 |
-
output = "nvidia-smi failed"
|
| 58 |
-
comment = (
|
| 59 |
-
datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
|
| 60 |
-
)
|
| 61 |
-
return f"# {comment}\n\n{output}"
|
| 62 |
-
|
| 63 |
-
def run(check: bool) -> str:
|
| 64 |
-
if check:
|
| 65 |
-
return run_gpu()
|
| 66 |
-
else:
|
| 67 |
-
comment = (
|
| 68 |
-
datetime.datetime.now().replace(microsecond=0).isoformat().replace("T", " ")
|
| 69 |
-
)
|
| 70 |
-
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"
|
| 71 |
-
|
| 72 |
-
output = gr.Textbox(
|
| 73 |
-
label="Command Output", max_lines=32, elem_id="output_box", value=run(False)
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
with gr.Blocks(css=CUSTOM_CSS) as demo:
|
| 77 |
-
gr.Markdown("#### `zero-gpu`: how to run on serverless GPU for free on Spaces 🔥")
|
| 78 |
-
|
| 79 |
-
output.render()
|
| 80 |
-
|
| 81 |
-
check = gr.Checkbox(label="Run on GPU")
|
| 82 |
-
|
| 83 |
-
check.change(run, inputs=[check], outputs=output, every=1)
|
| 84 |
-
|
| 85 |
# Define the Gradio interface
|
| 86 |
with gr.Blocks() as interface:
|
| 87 |
gr.Markdown("### Document Segmentation using YOLOv8")
|
|
@@ -95,9 +51,7 @@ with gr.Blocks() as interface:
|
|
| 95 |
outputs=[output_image, output_text]
|
| 96 |
)
|
| 97 |
|
| 98 |
-
demo.queue().launch(show_api=False)
|
| 99 |
interface.launch()
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
demo.launch()
|
| 103 |
interface.launch()
|
|
|
|
| 9 |
import subprocess
|
| 10 |
import spaces # Ensure this import is correct and the module is available
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Ensure the model file is in the correct location
|
| 13 |
model_path = "yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
| 14 |
if not os.path.exists(model_path):
|
|
|
|
| 38 |
|
| 39 |
return annotated_img, detected_areas_labels
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Define the Gradio interface
|
| 42 |
with gr.Blocks() as interface:
|
| 43 |
gr.Markdown("### Document Segmentation using YOLOv8")
|
|
|
|
| 51 |
outputs=[output_image, output_text]
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
interface.launch()
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
|
|
|
| 57 |
interface.launch()
|