File size: 1,432 Bytes
8332d88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""
Pre-installation setup for Hugging Face Spaces
Run this before main app to ensure detectron2 is properly installed
"""

import subprocess
import sys
import os

# Downgrade setuptools first
subprocess.run([sys.executable, "-m", "pip", "install", "setuptools==69.5.1"], check=True)

# Set environment variables
os.environ['CUDA_VISIBLE_DEVICES'] = ''
os.environ['FORCE_CUDA'] = '0'

# Install torch first
subprocess.run([
    sys.executable, "-m", "pip", "install", 
    "torch==2.0.1+cpu", "torchvision==0.15.2+cpu",
    "--index-url", "https://download.pytorch.org/whl/cpu"
], check=True)

# Install detectron2 dependencies
subprocess.run([
    sys.executable, "-m", "pip", "install",
    "numpy==1.24.3", "pillow==10.0.0", "pycocotools", "opencv-python"
], check=True)

# Clone and install detectron2
import tempfile
import shutil

with tempfile.TemporaryDirectory() as tmpdir:
    # Clone repo
    subprocess.run([
        "git", "clone", "https://github.com/facebookresearch/detectron2.git",
        os.path.join(tmpdir, "detectron2")
    ], check=True)
    
    # Checkout stable version
    os.chdir(os.path.join(tmpdir, "detectron2"))
    subprocess.run(["git", "checkout", "v0.6"], check=True)
    
    # Install without build isolation
    subprocess.run([
        sys.executable, "-m", "pip", "install",
        "--no-build-isolation", "."
    ], check=True)

print("Pre-installation complete!")