Daniel-F commited on
Commit
1b944d0
·
1 Parent(s): 76b9976
Files changed (1) hide show
  1. app.py +36 -0
app.py CHANGED
@@ -1,3 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
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
38
  import cv2
39
  import numpy as np