Daniel-F commited on
Commit
c8757ee
·
1 Parent(s): f076cb6
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -1,22 +1,37 @@
1
  import subprocess
2
 
 
 
 
 
 
 
 
 
3
  def setup_deps():
4
- print("Setting up dependencies...")
 
 
 
 
5
  try:
6
  import torch
7
- # Already installed? Skip setup
8
- return
 
9
  except ImportError:
10
  pass
11
 
12
- def run(cmd, cwd=None):
13
- print(f"▶ {cmd}")
14
- subprocess.check_call(cmd, shell=True, cwd=cwd)
15
-
16
  run("pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu")
17
  run("pip install -e .", cwd="segment-anything-2")
18
  run("pip install --no-deps -r requirements_manual.txt")
19
 
 
 
 
 
 
20
  setup_deps()
21
 
22
  import gradio as gr
 
1
  import subprocess
2
 
3
+ import os
4
+ import sys
5
+ import subprocess
6
+
7
+ def run(cmd, cwd=None):
8
+ print(f"▶ {cmd}")
9
+ subprocess.check_call(cmd, shell=True, cwd=cwd)
10
+
11
  def setup_deps():
12
+ # Use a flag to prevent infinite restarts
13
+ if os.environ.get("HF_SPACE_BOOTSTRAPPED") == "1":
14
+ return
15
+
16
+ # Try importing something to check if it's already set up
17
  try:
18
  import torch
19
+ import sam2
20
+ print("🔧 Dependencies already installed.")
21
+ return # all good, don't reinstall
22
  except ImportError:
23
  pass
24
 
25
+ print("🔧 Installing dependencies...")
 
 
 
26
  run("pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu")
27
  run("pip install -e .", cwd="segment-anything-2")
28
  run("pip install --no-deps -r requirements_manual.txt")
29
 
30
+ # Relaunch the script with an env flag to avoid looping
31
+ print("♻️ Restarting app to apply changes...")
32
+ os.environ["HF_SPACE_BOOTSTRAPPED"] = "1"
33
+ os.execv(sys.executable, [sys.executable] + sys.argv)
34
+
35
  setup_deps()
36
 
37
  import gradio as gr