Spaces:
Paused
Paused
Upload 3 files
Browse files- app.py +103 -30
- requirements.txt +7 -9
app.py
CHANGED
|
@@ -22,44 +22,113 @@ import sys
|
|
| 22 |
import subprocess
|
| 23 |
|
| 24 |
# ============================================================================
|
| 25 |
-
# INSTALL MMCV/MMDET/MMENGINE
|
| 26 |
# ============================================================================
|
| 27 |
-
# mmcv needs
|
| 28 |
-
#
|
| 29 |
# ============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def install_mm_packages():
|
| 31 |
-
"""Install mmcv, mmdet, mmengine
|
| 32 |
-
packages = [
|
| 33 |
-
("mmengine", "0.10.7"),
|
| 34 |
-
("mmcv", "2.1.0"),
|
| 35 |
-
("mmdet", "3.3.0"),
|
| 36 |
-
]
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Run installation before other imports
|
| 60 |
-
print("π§
|
| 61 |
install_mm_packages()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# ============================================================================
|
| 64 |
# STANDARD IMPORTS (after MM packages are installed)
|
| 65 |
# ============================================================================
|
|
@@ -625,4 +694,8 @@ def create_demo():
|
|
| 625 |
# ============================================================================
|
| 626 |
if __name__ == "__main__":
|
| 627 |
demo = create_demo()
|
| 628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
import subprocess
|
| 23 |
|
| 24 |
# ============================================================================
|
| 25 |
+
# INSTALL MMCV/MMDET/MMENGINE WITH CUDA EXTENSIONS
|
| 26 |
# ============================================================================
|
| 27 |
+
# mmcv needs pre-built CUDA extensions. We must install from OpenMMLab's
|
| 28 |
+
# wheel index with the correct CUDA and PyTorch version.
|
| 29 |
# ============================================================================
|
| 30 |
+
|
| 31 |
+
def get_torch_cuda_version():
|
| 32 |
+
"""Detect PyTorch and CUDA versions for wheel selection."""
|
| 33 |
+
import torch
|
| 34 |
+
|
| 35 |
+
torch_version = torch.__version__.split('+')[0] # e.g., "2.1.0"
|
| 36 |
+
torch_major_minor = '.'.join(torch_version.split('.')[:2]) # e.g., "2.1"
|
| 37 |
+
|
| 38 |
+
if torch.cuda.is_available():
|
| 39 |
+
cuda_version = torch.version.cuda # e.g., "12.1"
|
| 40 |
+
cuda_tag = 'cu' + cuda_version.replace('.', '')[:3] # e.g., "cu121"
|
| 41 |
+
else:
|
| 42 |
+
cuda_tag = 'cpu'
|
| 43 |
+
|
| 44 |
+
return torch_major_minor, cuda_tag
|
| 45 |
+
|
| 46 |
+
|
| 47 |
def install_mm_packages():
|
| 48 |
+
"""Install mmcv, mmdet, mmengine with proper CUDA extensions."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# First install mmengine (no CUDA extensions needed)
|
| 51 |
+
try:
|
| 52 |
+
import mmengine
|
| 53 |
+
print(f"β
mmengine already installed: {mmengine.__version__}")
|
| 54 |
+
except ImportError:
|
| 55 |
+
print("π¦ Installing mmengine...")
|
| 56 |
+
subprocess.run(
|
| 57 |
+
[sys.executable, "-m", "pip", "install", "mmengine==0.10.7"],
|
| 58 |
+
capture_output=True, text=True
|
| 59 |
+
)
|
| 60 |
+
print("β
mmengine installed")
|
| 61 |
+
|
| 62 |
+
# Install mmcv with CUDA extensions from OpenMMLab wheel index
|
| 63 |
+
try:
|
| 64 |
+
import mmcv
|
| 65 |
+
from mmcv.ops import roi_align # Test if extensions work
|
| 66 |
+
print(f"β
mmcv already installed with extensions: {mmcv.__version__}")
|
| 67 |
+
except (ImportError, ModuleNotFoundError) as e:
|
| 68 |
+
print(f"π¦ Installing mmcv with CUDA extensions... (reason: {e})")
|
| 69 |
+
|
| 70 |
+
# Get versions for wheel selection
|
| 71 |
+
torch_version, cuda_tag = get_torch_cuda_version()
|
| 72 |
+
print(f" Detected: PyTorch {torch_version}, CUDA tag: {cuda_tag}")
|
| 73 |
+
|
| 74 |
+
# OpenMMLab wheel index URL
|
| 75 |
+
wheel_index = f"https://download.openmmlab.com/mmcv/dist/{cuda_tag}/torch{torch_version}/index.html"
|
| 76 |
+
print(f" Wheel index: {wheel_index}")
|
| 77 |
+
|
| 78 |
+
# Uninstall any existing broken mmcv
|
| 79 |
+
subprocess.run(
|
| 80 |
+
[sys.executable, "-m", "pip", "uninstall", "mmcv", "-y"],
|
| 81 |
+
capture_output=True, text=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Install from OpenMMLab wheel index
|
| 85 |
+
result = subprocess.run(
|
| 86 |
+
[sys.executable, "-m", "pip", "install", "mmcv==2.1.0", "-f", wheel_index],
|
| 87 |
+
capture_output=True, text=True
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if result.returncode != 0:
|
| 91 |
+
print(f"β οΈ First attempt failed: {result.stderr}")
|
| 92 |
+
# Try alternative CUDA versions
|
| 93 |
+
for alt_cuda in ["cu121", "cu118", "cu117"]:
|
| 94 |
+
if alt_cuda == cuda_tag:
|
| 95 |
+
continue
|
| 96 |
+
alt_wheel_index = f"https://download.openmmlab.com/mmcv/dist/{alt_cuda}/torch{torch_version}/index.html"
|
| 97 |
+
print(f" Trying alternative: {alt_wheel_index}")
|
| 98 |
+
result = subprocess.run(
|
| 99 |
+
[sys.executable, "-m", "pip", "install", "mmcv==2.1.0", "-f", alt_wheel_index],
|
| 100 |
+
capture_output=True, text=True
|
| 101 |
)
|
| 102 |
+
if result.returncode == 0:
|
| 103 |
+
break
|
| 104 |
+
|
| 105 |
+
print("β
mmcv installed")
|
| 106 |
+
|
| 107 |
+
# Install mmdet
|
| 108 |
+
try:
|
| 109 |
+
import mmdet
|
| 110 |
+
print(f"β
mmdet already installed: {mmdet.__version__}")
|
| 111 |
+
except ImportError:
|
| 112 |
+
print("π¦ Installing mmdet...")
|
| 113 |
+
subprocess.run(
|
| 114 |
+
[sys.executable, "-m", "pip", "install", "mmdet==3.3.0"],
|
| 115 |
+
capture_output=True, text=True
|
| 116 |
+
)
|
| 117 |
+
print("β
mmdet installed")
|
| 118 |
+
|
| 119 |
|
| 120 |
# Run installation before other imports
|
| 121 |
+
print("π§ Setting up MM packages with CUDA extensions...")
|
| 122 |
install_mm_packages()
|
| 123 |
|
| 124 |
+
# Verify installation
|
| 125 |
+
print("π Verifying mmcv extensions...")
|
| 126 |
+
try:
|
| 127 |
+
from mmcv.ops import roi_align
|
| 128 |
+
print("β
mmcv._ext loaded successfully!")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"β οΈ Warning: mmcv extensions may not be fully loaded: {e}")
|
| 131 |
+
|
| 132 |
# ============================================================================
|
| 133 |
# STANDARD IMPORTS (after MM packages are installed)
|
| 134 |
# ============================================================================
|
|
|
|
| 694 |
# ============================================================================
|
| 695 |
if __name__ == "__main__":
|
| 696 |
demo = create_demo()
|
| 697 |
+
# Pass theme and css to launch() for Gradio 5.50+/6.0 compatibility
|
| 698 |
+
demo.launch(
|
| 699 |
+
theme=gr.themes.Soft(),
|
| 700 |
+
css=CUSTOM_CSS
|
| 701 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
# =============================================================================
|
| 2 |
# WeDetect HuggingFace Spaces - Requirements
|
| 3 |
# =============================================================================
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
# installed via `mim` to get proper CUDA extensions. They are installed
|
| 8 |
-
# at runtime in app.py using subprocess.
|
| 9 |
# =============================================================================
|
| 10 |
|
| 11 |
-
# Core ML frameworks
|
| 12 |
torch>=2.0.0
|
| 13 |
torchvision>=0.15.0
|
| 14 |
|
|
@@ -21,9 +19,6 @@ spaces
|
|
| 21 |
Pillow>=9.0.0
|
| 22 |
numpy>=1.21.0
|
| 23 |
|
| 24 |
-
# OpenMIM - used to install mmcv/mmdet/mmengine at runtime
|
| 25 |
-
openmim
|
| 26 |
-
|
| 27 |
# Additional dependencies for WeDetect
|
| 28 |
pycocotools
|
| 29 |
supervision>=0.19.0
|
|
@@ -32,3 +27,6 @@ accelerate>=1.0.0
|
|
| 32 |
terminaltables
|
| 33 |
jsonlines
|
| 34 |
tabulate
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# =============================================================================
|
| 2 |
# WeDetect HuggingFace Spaces - Requirements
|
| 3 |
# =============================================================================
|
| 4 |
+
# IMPORTANT: mmcv, mmdet, mmengine are NOT listed here.
|
| 5 |
+
# They are installed at runtime in app.py from OpenMMLab's wheel index
|
| 6 |
+
# to ensure we get the version with compiled CUDA extensions.
|
|
|
|
|
|
|
| 7 |
# =============================================================================
|
| 8 |
|
| 9 |
+
# Core ML frameworks (must be installed BEFORE app.py runs)
|
| 10 |
torch>=2.0.0
|
| 11 |
torchvision>=0.15.0
|
| 12 |
|
|
|
|
| 19 |
Pillow>=9.0.0
|
| 20 |
numpy>=1.21.0
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
# Additional dependencies for WeDetect
|
| 23 |
pycocotools
|
| 24 |
supervision>=0.19.0
|
|
|
|
| 27 |
terminaltables
|
| 28 |
jsonlines
|
| 29 |
tabulate
|
| 30 |
+
|
| 31 |
+
# Note: mmcv, mmdet, mmengine are installed at runtime via:
|
| 32 |
+
# pip install mmcv==2.1.0 -f https://download.openmmlab.com/mmcv/dist/{cuda}/torch{version}/index.html
|