OmarAlasqa commited on
Commit
9411d61
Β·
1 Parent(s): 3b12998

Fix: Async startup to bypass platform init errors and upload missing config files

Browse files
Files changed (2) hide show
  1. app.py +32 -13
  2. packages.txt +13 -0
app.py CHANGED
@@ -9,19 +9,38 @@ Tabs:
9
  import sys, os, json, urllib.request
10
 
11
  import subprocess
 
12
  import torch
13
  import torch.nn.functional as F
14
 
15
- # ── HF Space Pre-run ──────────────────────────────────────────────────────────
16
- # Hugging Face doesn't run pre_run.sh automatically for Gradio SDK. We do it here.
17
- _MM_INIT = "/usr/local/lib/python3.10/site-packages/mmcv/__init__.py"
18
- if not os.path.exists(_MM_INIT):
19
- print("πŸš€ Initializing HF Space environment (running pre_run.sh)...")
20
- try:
21
- # We use subprocess to ensure output is captured in the logs
22
- subprocess.run(["sh", "pre_run.sh"], check=True)
23
- except Exception as e:
24
- print(f"⚠️ Pre-run setup failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  from torchvision import transforms
27
  from PIL import Image, ImageDraw
@@ -382,10 +401,10 @@ with gr.Blocks(title="GeoViG Demo", theme=_THEME, css="footer { display: none !i
382
  info="M: 40.7 Box AP Β· B: 42.5 Box AP")
383
  det_thr = gr.Slider(0.1, 0.9, value=0.7, step=0.05, label="Score threshold")
384
  det_btn = gr.Button("πŸ” Detect & Segment", variant="primary")
385
- det_output_txt = gr.Textbox(label="Status", lines=2)
386
  with gr.Column(scale=2):
387
  with gr.Row():
388
- det_output_img = gr.Image(label="Detection (Boxes)", type="pil")
389
  seg_output_img = gr.Image(label="Instance Segmentation (Masks)", type="pil")
390
 
391
  det_btn.click(fn=detect, inputs=[det_image, det_size, det_thr],
@@ -426,7 +445,7 @@ with gr.Blocks(title="GeoViG Demo", theme=_THEME, css="footer { display: none !i
426
  med_btn = gr.Button("πŸ”¬ Segment", variant="primary")
427
  with gr.Column(scale=1):
428
  med_output_img = gr.Image(label="Segmentation Result", type="pil")
429
- med_output_txt = gr.Textbox(label="Status", lines=3)
430
 
431
  med_btn.click(fn=segment_medical, inputs=[med_image, med_dataset, med_thr],
432
  outputs=[med_output_img, med_output_txt])
 
9
  import sys, os, json, urllib.request
10
 
11
  import subprocess
12
+ import threading
13
  import torch
14
  import torch.nn.functional as F
15
 
16
+ # ── Global Setup State ───────────────────────────────────────────────────────
17
+ # Background thread to install MMCV/MMDET without blocking the Gradio UI
18
+ MMDET_OK = False
19
+ SETUP_LOG = "πŸš€ Initializing detection environment (this takes ~3 mins)..."
20
+
21
+ def run_setup():
22
+ global MMDET_OK, SETUP_LOG
23
+ _MM_INIT = "/usr/local/lib/python3.10/site-packages/mmcv/__init__.py"
24
+ if not os.path.exists(_MM_INIT):
25
+ try:
26
+ # Explicitly stream to logs
27
+ process = subprocess.Popen(["sh", "pre_run.sh"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
28
+ for line in process.stdout:
29
+ print(f"[SETUP] {line.strip()}")
30
+ process.wait()
31
+ if process.returncode == 0:
32
+ MMDET_OK = True
33
+ SETUP_LOG = "βœ… Detection environment ready."
34
+ else:
35
+ SETUP_LOG = f"⚠️ Setup failed with exit code {process.returncode}."
36
+ except Exception as e:
37
+ SETUP_LOG = f"⚠️ Pre-run setup failed: {e}"
38
+ else:
39
+ MMDET_OK = True
40
+ SETUP_LOG = "βœ… Detection environment ready (cached)."
41
+
42
+ # Start setup immediately
43
+ threading.Thread(target=run_setup, daemon=True).start()
44
 
45
  from torchvision import transforms
46
  from PIL import Image, ImageDraw
 
401
  info="M: 40.7 Box AP Β· B: 42.5 Box AP")
402
  det_thr = gr.Slider(0.1, 0.9, value=0.7, step=0.05, label="Score threshold")
403
  det_btn = gr.Button("πŸ” Detect & Segment", variant="primary")
404
+ det_output_txt = gr.Textbox(label="Status", lines=2, value=SETUP_LOG)
405
  with gr.Column(scale=2):
406
  with gr.Row():
407
+ det_output_img = gr.Image(label="Detection Results", type="pil")
408
  seg_output_img = gr.Image(label="Instance Segmentation (Masks)", type="pil")
409
 
410
  det_btn.click(fn=detect, inputs=[det_image, det_size, det_thr],
 
445
  med_btn = gr.Button("πŸ”¬ Segment", variant="primary")
446
  with gr.Column(scale=1):
447
  med_output_img = gr.Image(label="Segmentation Result", type="pil")
448
+ med_output_txt = gr.Textbox(label="Status", lines=3, value=SETUP_LOG)
449
 
450
  med_btn.click(fn=segment_medical, inputs=[med_image, med_dataset, med_thr],
451
  outputs=[med_output_img, med_output_txt])
packages.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ cmake
2
+ ffmpeg
3
+ git
4
+ git-lfs
5
+ libgl1
6
+ libglib2.0-0
7
+ libsm6
8
+ libxext6
9
+ libxrender1
10
+ rsync
11
+ ninja-build
12
+ build-essential
13
+ libgl1-mesa-glx